Ce blog — désormais archivé — est en lecture seule. Rendez-vous sur mon nouveau site personnel.

Prendre un Café

L'espace d'expression de Nicolas Perriault

Aller au contenu | Aller au menu | Aller à la recherche

Dev

Développement PHP, actionScript2, Ruby, XHTML, CSS, javascript, etc.

Fil des billets

mardi 6 octobre 2009

Simple Continuous Integration of a Symfony Project using Hudson

I love tests, I just cannot program without them anymore, especially with Symfony. And as I write a lot of them on a daily basis, the full test suite can take a very long time to be executed ; running a symfony test:all command before every granular commit suppose you have to wait sometimes more than 5, 10 minutes or more for the whole tests to be ran: hardly acceptable. Post commit hooks can be a solution, but what if a lot of devs are working simultaneously on the same project? Added to the previously seen test execution duration, it can become a problem difficult to deal with.

Here comes continuous integration : regularly, eg. every hour, the symfony project will be built up and configured from scratch, then the full test suite will be executed and the resulting log will be aggregated. It’s a very convenient way to keep an eye on the overall code quality and integrity of the project.

There are several open source continuous integration software available on the market: Cruise Control (and phpUnderControl), Continuum, Integrity, the uncertain but awesomely looking Sismo which powers the Symfony continous integration server… But I recently had the opportunity to test Hudson, a java based one. It’s very simple to install, setup and configure, and has tons of plugins. So let’s see how it can be used to test a Symfony project.

img hudson_tests_weather

Hudson Installation

That’s really not the hardest part, because you just need a working JRE 1.5 installation on your machine, and to retrieve the latest version of the program.

Then, you can launch it directly from a shell prompt:

$  java -jar /path/to/hudson.war

If you fear to break something or just want to give it a go without the hassle of firing a term, just launch the available Java Web Start version.

On debian/ubuntu, it’s just as easy as:

$ sudo echo deb http://hudson-ci.org/debian binary/ >> /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get install hudson
$ sudo /etc/init.d/hudson start

Configuration and Symfony Project Integration

Once Hudson is installed an running, head up to http://localhost:8080/ and start playing with the web based user interface of Hudson. The navigation and configuration forms are quite obvious, and therefore efficient. Creating a project following the provided guidelines is easy and can be achieved within minutes:

First, create a new Job, choosing the Build a free-style software project option.

Then, configure the subversion repository[1] used by your project by entering its base URL in the Source Code Management field You can schedule builds, watch for SCM activity and even trigger builds from remote script using a neat XMLRPC interface[2].

img hudson_svn

img hudson_hudson_scheduling

Configuring the building process is as easy as writing down some commands, like the one you’d execute to setup your project on a new box:

img hudson_build_commands

As of Symfony 1.3, test result logs can be exported in a jUnit XML file, so Hudson will be able to parse them and provide some useful reports, metrics and charts:

huson_test_results.png

Of course, you can configure failure notifications: email, atom feed, there’s even a twitter plugin!

As a conclusion, Hudson is a very simple but efficient continous integration tool which can easily be configured to monitor the quality of your Symfony projects… at least if you write tests ;)

Notes

[1] If you’re not using Subversion, lot of plugins are available for other SCM in the huge Hudson plugins repository

[2] Useful if you wish to use post-commit build hook

mardi 15 septembre 2009

Optimize your Doctrine Workflow with Specialized Queries

I’m currently working on a big Symfony project, with a lot of Doctrine models and complex queries to write. I found a way to organize all of them in an object-oriented and cleaner way than using the traditionnal addNamedQuery() and createNamedQuery() methods workflow[1].

The idea is to create dedicated query classes for a given model ; this way, you can provide useful methods to build the business-related parts of your query.

As usual, the theory is more understandable with a concrete example. Let’s consider this simple Doctrine model[2] :

Disclaimer: The provided examples have been written in a hurry, so mistakes might have been not detected by my attentive proof-reading ;)

 yaml
BlogAuthor:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true  
    name:
      type: string(255)
  relations:
    Post:
      type: one
      class: BlogPost
      local: id
      foreign: author_id

BlogPost:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    author_id:
      type: integer(4)
      notnull: true
    title:
      type: string(255)
    content:
      type: string(65535)
  relations:
    Author:
      type: one
      class: BlogAuthor
      local: author_id
      foreign: id
    Comments:
      type: many
      class: BlogComment
      local: id
      foreign: post_id

BlogComment:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    post_id:
      type: integer(4)
      notnull: true
    author:
      type: string(255)
    content:
      type: string(5000)
  relations:
    Post:
      type: one
      class: BlogPost
      local: post_id
      foreign: id

Now let’s imagine a Query class dedicated to query the BlogPost table:

 php
<?php 
class BlogPostQuery extends Doctrine_Query
{
  static public function create($conn = null, $class = null)
  {
    return parent::create($conn, 'BlogPostQuery')
      ->from('BlogPost p');
  }
  
  public function addPosts($fields = 'p.*')
  {
    return $this->addSelect('p.*');
  }
  
  public function addComments($fields = 'c.*')
  {
    return $this
      ->addSelect($fields)
      ->leftJoin('p.Comments c')
      ->addGroupBy('c.id');
  }
  
  public function addAuthors($fields = 'a.*')
  {
    return $this
      ->addSelect($fields)
      ->leftJoin('p.Author a')
      ->addGroupBy('a.id');
  }
  
  public function addCommentsCount($alias = 'nb_comments')
  {
    return $this
      ->addSelect(sprintf('COUNT(c.id) as %s', $alias))
      ->addGroupBy('c.id');
  }
  
  public function filterByAuthorName($authorName)
  {
    return $this
      ->andWhere('a.name = ?', $authorName);
  }
}

So how can we use this query object? Here are some sample uses:

 php
// Retrieve all posts
$posts = BlogPostQuery::create()
  ->addPosts()
  ->fetchArray();

// Retrieve all posts with comments
$posts = BlogPostQuery::create()
  ->addPosts()
  ->addComments()
  ->fetchArray();

// Retrieve all posts with comments and their count per post
$posts = BlogPostQuery::create()
  ->addPosts()
  ->addComments()
  ->addCommentsCount('yataa')
  ->fetchArray();

// Retrieve all post with chuck as its author and related comments
$posts = BlogPostQuery::create()
  ->addAuthors()
  ->addPosts()
  ->addComments()
  ->filterByAuthorName('chuck')
  ->fetchArray();

// and so on...

Of course, this example of use is not really relevant as our model is really simple, but when you’re dealing with dozens of internationalized objects, it can help cleaning your model classes, controllers and improving the organization of your work.

Update and important precisions

Some people are having negative feedback regarding this technique, claiming it will encourage people using the custom query object directly in the controllers; that’s absolutely not the case as the queries are to be used only within the model layer, for example in the BlogPostTable class:

 php
<?php
class BlogPostTable extends Doctrine_Table
{
  static public function getPostsWithCommentsByAuthor($authorName)
  {
    return BlogPostQuery::create()
      ->addPosts()
      ->addComments()
      ->filterByAuthorName($authorName)
      ->fetchArray()
    ;
  }
}

And in a controller:

 php
class blogActions extends sfActions
{
  public function executeListByAuthor(sfWebRequest $request)
  {
    $this->posts = BlogPostTable::getPostsWithCommentsByAuthor($request->getParameter('author'));
  }
}

Notes

[1] … or raw queries written directly within controllers, but you may know that this is really bad ;)

[2] I’m using Doctrine 1.2 beta (bundled with upcoming symfony 1.3) in the provided example.

vendredi 4 septembre 2009

30 Symfony Best Practices, the slides from my talk at SymfonyDay Cologne '09

Hey there, it’s been a while, huh?

Today I gave a talk about Symfony best practices at the Symfony Day event in Cologne, Germany; you can get the slides on slideshare or directly browse them below:

The Symfony Day event has been purely awesomely incredibly well organized, many kudos and thanks to Interlutions and to all the attendees. You’re all great people. Thanks.

Symfony Day '09 Cologne

dimanche 26 juillet 2009

YAML Brush for the SyntaxHighlighter Javascript Library

During a trip back to Paris by train, I quickly coded for a project of mine a new brush for SyntaxHighlighter, a client-side syntax coloring library powered by Javascript, in order to highlight YAML code snippets.

YAML syntax highlighting brush

You can get the code here and provide feedback by sending me an email.

Edit: Oops, didn’t find this already existing brush (I hadn’t wifi access in the train). I think my version support more specific aspects of the syntax, though.

jeudi 16 juillet 2009

Little Symfony Forms Tricks

Hey, it’s been a long time I didn’t blog something clever here on Symfony, let me try to remedy this.

I’ve just stumbled upon this blog post about the use of sfValidatorCallback, which is quite cool because by using this particular validator you can virtually employ any kind of php callable to validate something. But as the author warned, it can be problematic to tie the symfony form validation framework to your model classes.

Personnaly, I rather prefer to declare a method in the form class itself to validate some value without boring myself writing each time a new sfValidatorBase derived class[1] :

 php
<?php
class myForm extends sfForm
{
  public function configure()
  {
    $this->setWidgets(array(
      'name' => new sfWidgetFormInput(),
    ));
    
    $this->setValidators(array(
      'name' => new sfValidatorCallback(array('callback' => array($this, 'validateChuckNorris'))),
    ));
  }
  
  public function validateChuckNorris(sfValidatorBase $validator, $value)
  {
    // you can't validate chuck norris, but chuck norris can invalidate you
    if ('Chuck Norris' === $value)
    {
      throw new sfValidatorError($validator, 'invalid');
    }
  }
}

Of course, if you got the exact same need in another form, you should create a dedicated validator class. If you want custom error messages and options, you’ll have to create a dedicated class as well. But for simple and casual needs, this is just enough.

The neat thing with the sfValidatorCallback validator is you can even validate form schema values (say, an array containing all the values bound to the form) the same way, eg. using a post validator. Let’s see an example reusing the form shown previously:

 php
<?php
class myForm extends sfForm
{
  static protected $choices = array(
    'none' => 'No emails', 
    'commercials' => 'Commercial emails', 
    'news' => 'Annoucement emails', 
    'alerts' => 'Alert emails',
  );

  public function configure()
  {
    $this->setWidgets(array(
      //...
      'email' => new sfWidgetFormInput(),
      'opt_in' => new sfWidgetFormSelectRadio(array('choices' => self::$choices)),
    ));
    
    $this->setValidators(array(
      //...
      'email' => new sfValidatorEmail(array('required' => false)),
      'opt_in' => new sfValidatorChoice(array('choices' => array_keys(self::$choices))),
    ));
    
    $this->validatorSchema->setPostValidator(new sfValidatorCallback(array(
      'callback' => array($this, 'validateSchema'),
    )));
  }
  
  // We want the user to provide his email if he choosed to receive stuff by email
  public function validateSchema(sfValidatorBase $validator, array $values)
  {
    if ($values['opt_in'] !== 'none' && !$values['email'])
    {
      throw new sfValidatorErrorSchema($validator, array(
        'email' => new sfValidatorError($validator, 'required'),
      ));
    }
  }
}

As a cool side effect, it will also make testing your form validation very easy, because you just have to test the callable method of your form.

Here’s another trick I stole to Kris on a project we worked together on lately, using a formatter callback to alter the presentation of a widget. Here’s an example showing how to get rid of the unordered list displaying a collection of checkboxes by default, by inlining them instead:

 php
<?php
class myForm extends sfForm
{
  static protected $choices = array(
    'none' => 'No emails', 
    'commercials' => 'Commercial emails', 
    'news' => 'Annoucement emails', 
    'alerts' => 'Alert emails',
  );

  public function configure()
  {
    $this->setWidgets(array(
      //...
      'opt_in' => new sfWidgetFormSelectRadio(array(
        'choices' => self::$choices,
        'formatter' => array($this, 'formatInline'),
      )),
    ));
    
    $this->setValidators(array(
      //...
      'opt_in' => new sfValidatorChoice(array('choices' => array_keys(self::$choices))),
    ));
  }
  
  public function formatInline($widget, $inputs)
  {
    $formatted = array();
    
    foreach ($inputs as $input)
    {
      $formatted[] = $input['input'].' '.$input['label'];
    }

    return join(' ', $formatted);
  }
}

There are tons of little tricks like these which make the life of a developer using the forms framework easier, I’ll try to share them with you progressively.

Notes

[1] All my examples are using latest Symfony 1.2.

mardi 2 juin 2009

Utiliser les fonctions GeoIP de PHP sous Ubuntu

Mon Dieu, les billets techniques refleuriraient-ils au printemps ? Si vous désirez récupérer des informations géographiques à partir de l’adresse IP (ou du hostname) d’un utilisateur, vous pouvez utiliser les fonctions fournies par l’extension PECL GeoIP.

Voici la procédure d’installation sur une Ubuntu 8.04:

$ sudo -s
# apt-get install build-essential php5-dev php5-cli libgeoip-dev libgeoip1 php-pear
# pecl install geoip

Si toiut s’est bien passé :

# echo "extension=geoip.so" >> /etc/php5/cli/php.ini

Si vous utilisez Apache comme serveur :

# echo "extension=geoip.so" >> /etc/php5/apache2/php.ini

Il faut également installer la base GeoIPCity de Maxmind :

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
# gunzip GeoLiteCity.dat.gz
# mv GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

Vous pouvez maintenant tester l’extension avec une ligne du genre[1] :

$ echo "<?php var_dump(geoip_record_by_name('209.202.168.**'));"|php

Ça donne ici :

array(11) {  ["continent_code"]=>
  string(2) "NA"
  ["country_code"]=>
  string(2) "US"
  ["country_code3"]=>
  string(3) "USA"
  ["country_name"]=>
  string(13) "United States"
  ["region"]=>
  string(2) "NC"
  ["city"]=>
  string(4) "Cary"
  ["postal_code"]=>
  string(5) "27511"
  ["latitude"]=>
  float(35.7********)
  ["longitude"]=>
  float(-78.7*******)
  ["dma_code"]=>
  int(560)
  ["area_code"]=>
  int(919)
}

Enjoy.

Notes

[1] J’ai volontairement masqué certaines informations pour d’évidentes raisons de confidentialité.

jeudi 4 décembre 2008

Fayotage

[13:13:07] Glooze: hey, doctrine + sf, ca roxxe quand même :p
[13:13:29] NiKo: génial, tu peux faire un billet pour le dire steup ? :D
[13:13:48] Glooze: dès que j’aurai trouvé un pdf de la doc !
[13:13:54] NiKo: un pdf oO
[13:14:05] NiKo: tu connais pomme P -> PDF
[13:14:13] Glooze: Ouais mais non
[13:14:29] Glooze: j’suis pas sous mac tout le temps (notamment mon portable il est sous ubuntu)
[13:14:32] NiKo: ahhh ok môssieur veut un beau livre relié cuir, bien sûr
[13:14:39] NiKo: ben sous ubuntu c’est natif
[13:14:43] NiKo: imprimer -> pdf
[13:14:47] NiKo: depuis firefox
[13:14:50] Glooze: et pis surtout une doc sur 8 pages, ça me fait aller sur chaque et imprimer
[13:14:51] Glooze: :o
[13:14:59] Glooze: Alors une doc plus grosse :p
[13:15:30] NiKo: si tu fais pas de billet, je faillote sur mon blog en faisant un gros copier coller de cette discussion
[13:15:32] NiKo: </menace>
[13:15:36] Glooze: Y a eu des tas de changements (en mieux d’ailleurs) pour la 1.2, donc mon definitive guide (acheté môssieur) il est plus trop utile :p
[13:15:44] Glooze: Mais vas-y, faillote !
[13:15:47] NiKo: ça marche

lundi 1 décembre 2008

Meet Joe Beet

Sorry for the lame title of this blog post, but I’m happy enough to announce the immediate availability of both symfony 1.2 and Jobeet, the new symfony advent calendar!

Jobeet It is a set of 24 tutorials, published day-by-day between December 1st and Christmas. Each tutorial is meant to last one hour, and will be the occasion to see the ongoing development of a web application with symfony, from A to Z.

So don’t hesitate to start reading day one of this great tutorial :-)

Edit: I forgot to mention the simultaneous release of the symfony and Doctrine book :)

- page 2 de 11 -