DrupalCamp Spain 2014 wrap-up

DrupalCamp Spain 2014

DrupalCamp Spain 2014

Last weekend I attended DrupalCamp Spain at the beautiful Centro Cultural Bancaja, in Valencia, Spain. Three whole days of Drupal. This was my first Drupal conference, and… wow! The Drupal community is huge and full of passionate developers eager to learn. ServerGrove was one of the bronze sponsors and consolidates its support for the Drupal community. I was there because I was invited to give a talk about the Symfony components that are being used to create the new version of Drupal (Drupal 8). I got some great feedback from the attendees and I am excited to have the opportunity to participate the Drupal community.

Symfony in Drupal 8

In my talk I pointed out some of the problems of the current version of Drupal, which is mature, robust, and highly extensible, however, it lacks some of the recommended best practices for software engineering. This makes it harder to maintain and find new contributors. I borrowed one of the examples from Anthony Ferrara‘s talk on “Development, by the numbers”, which discusses some metrics of the drupal_http_request() function in Drupal 7, such as the cyclomatic and NPath complexity, and shows that it really needs a refactor, or in that case, use an HTTP abstraction component like HttpFoundation.

Fortunately the (still work in progress) 8th version of Drupal, 50% of its external dependencies are Symfony components, and others such as Twig, Assetic of Symfony CMF come also from the Symfony world.

In the talk I explained nine components being used by Drupal 8:

  • HttpFoundation: Abstraction of the HTTP protocol (more info).
  • HttpKernel: Defines an abstract process to convert a Request object into a Response object (more info).
  • ClassLoader: Class autoload using MapClass or PSR-0 strategies.
  • Routing: Drupal 8 uses CMF/Routing, which extends from the Routing component providing the flexibility needed by a CMS (more info).
  • EventDispatcher: Alternative to the classic Drupal hooks. Implements the Mediator Pattern (more info).
  • DependencyInjection: Implements a dependency injection container to centralize the object construction.
  • Validator: Provides validation tools for values or objects (more info).
  • Serializer: Serializes PHP objects into different formats (JSON, XML…), as wel as in the other way.
  • Yaml: Loads and dumps data in YAML format. A useful format for configuration files written by humans.

Slides are only available in Spanish, but they will be translated to English too as soon as possible. If you understand Spanish, check them out: http://www.slideshare.net/raulfraile/symfony-en-drupal-8-drupalcamp-spain.

Other talks

It was a great opportunity for me to learn different approaches for common problems from a different community. I am used to attend PHP and Symfony events, but as I said before, this was my first time ever in a Drupal conference. The talks I liked most were:

  • WTF! Huge errors in projects told by their own authors: This talk was hilarious. 5 experienced Drupal developers told their unspeakable errors as beginners, such as not using a CVS and losing all the work the night before the deadline.
  • Sockets to the limit: The talk went over an interesting use-case to have a centralised server to manage Symfony translations, and using websockets to “install” them in any server.

Community

Just a photo to see how awesome this community is…

14020046267_383924b817_z

BTW, can you find Waldo? :)

Sponsors

For the second time in a row, ServerGrove proudly sponsored the most important Drupal conference in Spain. We are really happy to be a small part of this great event and hopefully we will keep collaborating in the future. If you attended the conference, remember that we have a couple of coupons for you: “DPSpain15″ for a 15% monthly discount in VPS from VPS 100 to 700, and “DPSpain20″ for a 20% yearly discount in VPS also ranging from VPS 100 to VPS 700. Take a look at our VPS plans.

Photo: Foto de Grupo Drupalcamp Spain 2014, by Pedro Lozano.

mayo 19 / 2014

Upcoming Conferences

WeCamp

PHP New Zealand

PHP Summer Camp Croatia

PHPNE

MadisonPHP

brnoPHP

SymfonyLiveLondon

ZgPHP

PHPSouthAfrica

PHPNWUK

SymfonyLiveNYC

PHPForumParis

PHPARG

PHPWorld

TechMeetupUY

SymfonyConMadrid

WordCamp Miami 2014

The weekend of May 10th & 11th we attended the 5th anniversary of WordCamp Miami of which ServerGrove was one of the sponsors and I had the honor of speaking at.

WordCamp Miami T-Shirt Art

One of the framed t-shirts of previous WordCamp Miami

The conference started early for those who arrived on Thursday and attended the networking party offered by LiveNinja at their shiny new office in WynWood. It was a great time to catch up with local developers and designers as well those that came from as far as L.A. and the U.K. It was also great meeting some of the organizers of WordCamp NYC and WordCamp Orlando.

For its 5th anniversary, WordCamp Miami had a new venue. The Student Center Complex at the University of Miami was a great venue for the 700+ attendees, speakers, sponsors, and volunteers. The UM campus is beautiful and the Student Center had great meeting and social spaces.

There were also great talks, including one on WordPress Security by Dre Armeda. He went over some great security tips for WordPress including a .htaccess directive to be placed in the uploads directory to prevent PHP scripts from running there.

We offer a similar tip done in a different way.

RemoveHandler .php .phtml .php3 php4
RemoveType .php .phtml .php3 php4

Diane Kinney also had a great talk for people looking to hire developers on how to manage themselves, the project, and the developers. It’s not an easy thing to hire and trust someone else to build one of the most important tools your business has. Diane’s talk was honest about the expectations to have when hiring a development company and how to properly manage yourself and the project.

My talk on Linux command line basics was the first talk on the Developers track. I covered some basic commands like find and grep as well as how permissions worked. I was glad to see a few people ask questions during the talk or come up after and discuss ways of how to use command line tools to manage their sites.

One thing that was a new experience for me was the WordCamp Happiness Bar, a meeting space where attendees could meet with developers and designers to ask questions about how to solve specific issues with their sites. I spent a couple of hours helping with the Happiness Bar on Sunday and it was great to see the different level of questions that attendees had.

Overall it was a great conference professionally organized by the WordCamp Miami team and it’s no surprise that it’s still going strong after 5 years.

mayo 17 / 2014
Author Adrian Cardenas
Category Conferences, PHP
Comments No Comments

Upcoming Conferences

WeCamp

PHP New Zealand

PHP Summer Camp Croatia

PHPNE

MadisonPHP

brnoPHP

SymfonyLiveLondon

ZgPHP

PHPSouthAfrica

PHPNWUK

SymfonyLiveNYC

PHPForumParis

PHPARG

PHPWorld

TechMeetupUY

SymfonyConMadrid

PHP 5.5.12 and 5.4.28 released and available on ServerGrove

This week the PHP development team released PHP 5.5.12 and 5.4.28. The release contained numerous bug fixes, including an important one for CVE-2014-0185 regarding PHP-FPM. If you run PHP with PHP-FPM we recommend you upgrade ASAP.

If you are using our repositories, upgrading is simple. Here are instructions on how to use (or upgrade) our Ubuntu/Debian or CentOS PHP packages.

Other updates

Pimple

Pimple is a small Dependency Injection Container for PHP that consists of just one file and one class (about 80 lines of code). It is used by Silex and other projects. Just a few days ago, it was released as an implementation of the library written in C as PHP extension, providing a performance boost for those that need it. We have prepared packages of the extension for CentOS 6, Ubuntu 12, and Debian 6. Ubuntu 14 and Debian 7 will follow soon.

Package names for Ubuntu & Debian:

- php53-pimple_2.1-3_amd64.deb
- php54-pimple_2.1-3_amd64.deb
- php55-pimple_2.1-3_amd64.deb

Package names forCentOS:

- php53-pimple-2.1-1.x86_64.rpm
- php54-pimple-2.1-1.x86_64.rpm
- php55-pimple-2.1-1.x86_64.rpm

We will update these packages as new versions come out. Please use this at your own risk!

HHVM / Hack

Some days ago we announced our packages for HHVM / Hack in our repositories. Give it a try!

mayo 01 / 2014
Author Pablo
Category PHP
Comments No Comments

Upcoming Conferences

WeCamp

PHP New Zealand

PHP Summer Camp Croatia

PHPNE

MadisonPHP

brnoPHP

SymfonyLiveLondon

ZgPHP

PHPSouthAfrica

PHPNWUK

SymfonyLiveNYC

PHPForumParis

PHPARG

PHPWorld

TechMeetupUY

SymfonyConMadrid

Running Composer with HHVM, not so fast!

HHVM is an open-source virtual machine developed by Facebook and designed for executing programs written in Hack and PHP. It offers increased performance for PHP, most of the time. You can already use HHVM in ServerGrove servers by using our packages.

We have read numerous blog posts and articles suggesting to use HHVM when running Composer. If you are not familiar with Composer, check out our Composer 101 article.

Since Composer needs to perform some heavy computations in order to resolve the dependencies of a project, it makes sense to use HHVM. However, the heavy computations are mainly done when running composer update, or when the composer.lock file has not yet been generated so this is where you will see most of your gains in execution time.

Here are some tests while working with a Symfony2 Standard Edition project.

Running composer update with HHVM  takes ~4 seconds:

$ time hhvm composer.phar update --prefer-dist --no-scripts
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

real 0m4.362s
user 0m3.540s
sys 0m0.168s

While running the same command with PHP takes almost 11 seconds:


$ time php composer.phar update --prefer-dist --no-scripts
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

real 0m10.768s
user 0m9.692s
sys 0m0.180s

Running composer install

So running composer update with HHVM is clearly a benefit. It will save you a lot of time, and the larger the number of dependencies, the more time you will save.

However, we found that when running composer install with HHVM takes longer than doing it with PHP. This is especially important when using Composer in deployment workflows, as usually you would run install and not update and you will have a composer.lock file.

The reasons why HHVM is slower than PHP when running composer install are still unclear. There is some time lost while doing the JIT warmup, and HHVM really shines when it can run optimized and compiled bytecode, which is not the case with command line scripts, but we believe there is more than just the JIT warmup, as tests show several seconds of difference. We gained some performance by disabling the JIT (hhvm -v Eval.Jit=0), but it is still slower than PHP.

Composer install with PHP:

real 0m2.547s
user 0m1.190s
sys 0m0.919s

Composer install with HHVM:

real 0m4.905s
user 0m3.010s
sys 0m1.337s

Composer install with HHVM and JIT disabled:

real 0m3.062s
user 0m1.604s
sys 0m1.158s

You can run the same tests by using the shell script in this gist. Please share your findings with us.

Conclusion

We have seen many posts and articles suggest that you should create an alias in your shell configuration so you always run Composer with HHVM. Given the fact that HHVM is not always faster than PHP, we don’t think this is a good idea yet. We expect the HHVM team to improve the performance and at some point this article will become obsolete. Until then, consider this before you go all the way HHVM.

Also, we would like to know what others think and we encourage to try this test with your project. Share with us what you find.

abril 17 / 2014
Author Pablo
Category PHP
Comments 3 Comments
Tags ,

Upcoming Conferences

WeCamp

PHP New Zealand

PHP Summer Camp Croatia

PHPNE

MadisonPHP

brnoPHP

SymfonyLiveLondon

ZgPHP

PHPSouthAfrica

PHPNWUK

SymfonyLiveNYC

PHPForumParis

PHPARG

PHPWorld

TechMeetupUY

SymfonyConMadrid

Symfony2 components overview: Process

This post covers the Symfony2 process component and is the 11th post from our series on Symfony2 components.

The Symfony2 Process component, allows us to execute commands in sub-processes.

171847361_546785da09_z

The Process component does all the hard low-level work for you
when dealing with sub-processes

Installation

The recommended way of installing the component is through Composer:

{
    "require": {
        "symfony/process": "2.4.*"
    }
}

If you have never used Composer before check out our Composer 101 post.

The component

The Process component provides an object-oriented abstraction on top of proc_* functions to execute independent processes from PHP.

For example, to list the files and directories in the directory where the current PHP script is located:


use Symfony\Component\Process\Process;

$process = new Process('ls -lh ' . escapeshellarg(__DIR__));
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new \RuntimeException($process->getErrorOutput());
}

echo $process->getOutput();

The example is quite self-explanatory, but let’s take a look and go step-by-step to see what happened exactly. First, we create a Process instance, which is the main class of the component. Besides passing the command we want to execute, it is also possible to pass the working directory, environment variables or a timeout.

function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())

As soon as we call the run() method, the command is executed and the PHP interpreter waits until the command finishes. In case that the execution was not successful (exit code different than 0), it throws an exception. If the execution was successful, it simply prints out the command output.

The output of the script would be similar to this:

total 64
-rw-r--r--  1 raul  raul   374B Apr 14 09:14 composer.json
-rw-r--r--  1 raul  raul    22K Apr 14 09:15 composer.lock
-rw-r--r--  1 raul  raul   321B Apr 14 11:32 ex1.php
drwxr-xr-x  8 raul  raul   272B Apr 14 09:15 vendor

Exit codes

The process class provides a few methods to deal with exit codes. We can even get the exit code as a text, as the Process class maintains a map of common exit codes:

$processA = new Process('ls -lh');
$processA->run();

// int(0) string(2) "OK"
var_dump($processA->getExitCode(), $processA->getExitCodeText());

$processB = new Process('foo');
$processB->run();

// int(127) string(17) "Command not found"
var_dump($processB->getExitCode(), $processB->getExitCodeText());

The exit code is also used to determine if a command was executed successfully, by the isSuccessful() method, which returns true if the exit code is 0 and false otherwise.

Long Running Processes

When we have to deal with long running processes things tend to get a little trickier, as we have take into account things like timeouts, incremental outputs, responsiveness, and signals. The Process class provides ways to make these problems manageable.

Timeouts

There are two available timeouts: process timeout (max runtime) and process idle timeout (max. time since last output). In the following code, as the ping command in Unix systems runs infinitely (unless we specify the “-c” option), a ProcessTimedOutException exception will be thrown after 10 seconds:

$process = new Process('ping example.com');
$process->setTimeout(10);
$process->run();

This would not be true with the idle timeout, as most of the time the ping command outputs new information in less than 10 seconds. In this case, the process will probably run until exceeds the memory_limit setting:

$process = new Process('ping example.com');
$process->setTimeout(null); // disable "normal" timeout
$process->setIdleTimeout(10);
$process->run();

Outputs

In long running processes we need some sort of “real time” output so the user perceives that the process is still running and is not dead. There are two ways to do this: outputting the command output as soon as it gets available, or printing some “loading” or “in progress” message. Let’s see an example of both approaches using again the ping command:

In the following example, we pass PHP callable to run whenever there is some output available on STDOUT (standard output) or STDERR (standard error). Each time there is output available, we print a dot so the user knows the command did not hang and is still running.

$process = new Process('ping -c 5 example.com');
echo 'Executing';
$process->run(function ($type) {
    if (Process::OUT === $type) {
        echo '.';
    }
});

It is also possible to print the command output as soon as it gets available defining a second parameter in the callable. This time, the script will print the command output as soon as it gets available:

$process = new Process('ping -c 5 example.com');
$process->run(function ($type, $buffer) {
    if (Process::ERR === $type) {
        echo $buffer;
    }
});

This can be done also with the getIncrementalOutput() and getIncrementalErrorOutput() methods, that returns only the new output since the last call.

$process = new Process('ping -c 5 example.com');
$process->start();

while ($process->isRunning()) {
    echo $process->getIncrementalOutput();
}

It is recommended to add a small delay in the while loop if we can afford it to reduce the number of calls to getIncrementalOutput().

Signals

Signals are asynchronous notifications sent to a process to notify of an event that occurred. Using signals we can for instance stop asynchronous processes using the signal() method:

$process = new Process('ping -c 50 example.com');
$process->start();
sleep(3);
$process->signal(SIGKILL);

The ping command is “killed” after 3 seconds. The SIGKILL constant is defined in PCNTL.

PIDs

A PID is a number to temporarily uniquely identify a process. We can get the PID of a running process with the getPid() method:

$process = new Process('ls -lh');
$process->start();

var_dump($process->getPid()); // int(78316)

Executing PHP Code

The component provides the class PhpProcess (which extends from Process), to execute PHP code in isolation. That means that it is run in a different process so no variables or open resources are shared between them.

use Symfony\Component\Process\PhpProcess;

$process = new PhpProcess('<!--?php echo "Hello world!";'); $process--->run();

echo $process->getOutput();

It will print out “Hello world!”.

Under the hood

If you are following the posts of this series you know that we always like to dive a little bit deeper and find out how the component is made internally. Usually, the official documentation for Symfony components is excellent so we try to give back to the community by explaining them in a different way and trying to share roughly how they work internally.

Internally, the Process class makes use of the proc_open() function to execute a command and open file pointers for input/output. The proc_open() function is not straightforward to use, as it needs a descriptor specification.

The descriptor specification is an indexed array to tell the function how we want to handle stdin, stdout and stderr. By default, in the component, pipes are used, but it can be configured to use a file for stdout instead. These are the parameters that proc_open() receives when executing “ls -lh”:

string(6) "ls -lh"
array(3) {
  [0] =>
  array(2) {
    [0] => string(4) "pipe"
    [1] => string(1) "r"
  }
  [1] =>
  array(2) {
    [0] => string(4) "pipe"
    [1] => string(1) "w"
  }
  [2] =>
  array(2) {
    [0] => string(4) "pipe"
    [1] => string(1) "w"
  }
}
array(0) {
}
string(31) "/Users/raulfraile/servergrove/process"
NULL
array(2) {
  'suppress_errors' => bool(true)
  'binary_pipes' => bool(true)
}

The option “suppress_errors” is only for Windows systems and suppresses errors generated by the proc_open() function, while “binary_pipes” forces to open pipes in binary mode, instead of using the usual stream_encoding.

The Process component is one of the oldest in the Symfony framework, it is quite interesting to view the transformation it went through over 4 years, take a look at how simple it was (254 LoC) and how much more complete it became (1446 LoC). This is a big reason why it is great to reuse well developed and tested libraries.

Finally, as a curiosity, the PhpProcess class, which is used to execute PHP code, already supports HHVM. The PhpExecutableFinder class, used to find the PHP binary, checks whether HHVM is being used by reading the HHVM_VERSION constant, only available when HHVM is the current engine.

Who’s using it?

More info

Photo: Work in progress, by Stefano Mortellaro

abril 16 / 2014

Upcoming Conferences

WeCamp

PHP New Zealand

PHP Summer Camp Croatia

PHPNE

MadisonPHP

brnoPHP

SymfonyLiveLondon

ZgPHP

PHPSouthAfrica

PHPNWUK

SymfonyLiveNYC

PHPForumParis

PHPARG

PHPWorld

TechMeetupUY

SymfonyConMadrid