XHTML.net

Technology talks by Loïc d’Anterroches

News, articles, PHP, scripts, XHTML/CSS, …

  1. Home
  2. PHP: Hypertext Preprocessor

The use of the middleware in Pluf

The 2007-02-21 at 15:12 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor.

The best technical choice I made for the structure of Pluf was to follow the lead of the Django Project. I created Pluf because I really needed to have the Django speed of development on systems where using Python was not an option. This was not an option because I had to implement a solution to be then maintained by people not having the right knowledge. I learnt the hard way in the past that the best technical solution is also the one that can be handled by the people in house.

Ok, back to the topic, what I really enjoyed with Django was that the data are nicely encapsulated with basically:

  • A request object representing the query with GET/POST data.
  • A dispatcher that will call a given view function based on the request URI.
  • A response object created in the view function and sent back to the client.

On top of that you have middleware. Middleware are men in the middle, they take a request object and modify it before giving it to the view, or, take a response object and modify it before giving it back to the client.

At the moment, on the main conference website, I am using 2 middleware, a maintenance middleware and a session middleware.

The session middleware handles session, nothing really new in it and the maintenance middleware is a really small but practical middleware. What it does is that if a MAINTENANCE file is found at a given place on the server, it automatically intercept the request and returns a page telling that the server is in maintenance. Basically, when I want to perform a complex upgrade which cannot be performed fully live, I just go in the right folder and touch MAINTENANCE, perform the upgrade and then remove the file.

But what is really interesting is the ability to modify the response after the view generated it. I have upgraded the Tidy middleware to modify the </body> tag of the page and append just before the log of the errors when running Tidy against the content of the page. It means that now, I do not have to think when the page is not valid, I have the error messages right away! I just take a look at the source of the page, find the error, fix the code, reload the page. It is fast and fun, no need to painfully submit your code to a validator or copy/paste the code to validate it.

When developing, I am using a Debug middleware to display the peak memory usage of the page, the execution time and the database queries. It is very practical to spot the weaknesses in the code. As you can see in the following screenshot, the peak memory is provided using the Xdebug xdebug_peak_memory_usage function and when you click on the DB query # link, you can see all the queries (it is just a little javascript to toggle the visibility style of the <div> containing the queries).

Screenshot of the Pluf debug middleware

Something I often miss in organisations, is that we tend to act a lot based on feelings and not based on factual data. Very often it is easy to gather the data to support your decisions, but people are not doing it. So enjoy the Pluf Middleware to know what is going on in your code and improve based on real data.

Solar PHP framework

The 2007-01-09 at 16:28 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor.

The Solar PHP Framework is the framework which helped me to create the Pluf Framework. Pluf is heavily inspired by Django with views, a nice ORM and templating system, but what really kicked out Pluf is the Solar idea (pioneered by PEAR as Paul M Jones reminded me) that when you name a class My_Wonderfull_Class the class will be available in the file My/Wonderfull/Class.php. Then with the power of PHP5 you can create an __autoload function (PHP doc of __autoload) to basically automatically load the class based on its name. End result of the autoload trick:

  • No more include_once nightmare in your code, you load the necessary files and only them.
  • A very clean code layout because you know where the classes are.
  • More freedom when using objects because you know that you do not have to define a priori which objects are going to be loaded, you do not need any complex logic to load the corresponding .php files anymore, you use more objects and your code is cleaner.
  • Clean code, good encapsulation of the logic in classes, your code is now easy to read and maintain.
  • Bingo!

Think about it when starting a project in PHP.

Gettext and PHP, the good, the bad and the hugly

The 2006-12-19 at 15:20 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor.

Yesterday I took a look at gettext to be used in the core of the next version of Plume CMS. I was ready to use it until I discovered some annoying things:

  • You need to have it to use it. Not really a problem as nowadays the gettext extension is installed by nearly all the provider.
  • In some cases, you need to restart Apache to reload the new translations.
  • You need to have the corresponding locale installed on the system to be able to use a given language. That is, if your provider has only the English locale available, sorry, it won’t work.

Check the comments on the gettext PHP manual page to see all the problems you can have. As Plume CMS is supposed to be used in a non controlled environment, I had to find another solution. Looking around, I found a nice gettext benchmark. The results from the benchmark together with the interesting comments can be summarized as:

  • the pure PHP implementation of gettext solving the problems is slow.
  • an array with translation strings is nearly as fast as gettext.
  • loading the translation strings from a .ini like file is convenient, but very slow.

The solution I found is basically to reuse the approach of the current Plume CMS translation system and extend it. At the moment, the translation system does not support plural forms, so I created it. The partial implementation is available in Pluf. Pluf is the MVC framework that will be used as the core of the next release of Plume CMS. Now you can do the following:

// Translate a single string
echo __('Hello World!'); 
// Translate a plural form
echo sprintf(_n('Here is %d apple.', 'Here are %d apples.', $n), $n);

The file storing the translations is an easy to read and write .ini like file and the framework will automatically cache the strings in a dynamically generated PHP file ready for inclusion and parsing by the PHP tokenizer, thus really fast.

Here is an example of a .lang file:

# -*- coding: utf-8 -*-
# (C) 2004-2006 Cédric Arrabie, Loïc d'Anterroches
;Welcome on the Plume CMS installation
Bienvenue sur l'assistant d'installation de Plume CMS

;Next
Suivant

;Nothing to see here.
p;You have some things to see here.
0;Rien à voir ici.
1;Une chose à voir ici.
2;Plusieurs choses à voir ici.

Basically, we will be able to use it like gettext and ngettext. Of course it is a very simple approach and I am not an expert in internationalization, but today Plume CMS is already translated in several languages and this approach is just improving the current approach, so basically translators will be happy (I hope).

PHP Naive Bayesian Filter

The 2008-05-26 at 18:39 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor and Scripts.

Le sujet à la mode est le filtrage des commentaires, pingbacks ou autres trackbacks de votre site. Je ne joue pas trop avec cela, mais le principe d’un filtre reposant sur le théorème de Bayes m’intrigait trop pour résister à une implémentation en PHP.

XULit! CMS

The 2004-02-20 at 16:25 by Loïc d'Anterroches filed under Scripts and PHP: Hypertext Preprocessor.

Le web est pour tous et toutes, même les aveugles. Voilà un CMS qui répond à cette problématique d’accessibilité en ayant tout, de la partie d’administration au site publique par défaut d’accessible.

Utilisation de l'API SOAP de Google en PHP

The 2003-08-11 at 19:07 by Loïc d'Anterroches filed under Scripts and PHP: Hypertext Preprocessor.

L’API SOAP de Google vous permet d’intégrer dans votre site un moteur de recherches avec un simple script PHP. Vous pouvez faire des recherches dans votre site ou dans l’ensemble de la base de Google.

MyBackup, sauvegarde des bases MySQL

The 2003-09-17 at 19:24 by Loïc d'Anterroches filed under Scripts and PHP: Hypertext Preprocessor.

Script PHP simple à mettre en place pour la sauvegarde et la restauration d’un nombre illimité de bases MySQL.

Compteur Live pour un site

The 2004-08-09 at 20:03 by Loïc d'Anterroches filed under PHP: Hypertext Preprocessor and Scripts.

LiveCount est un script PHP et MySQL permettant d’évaluer le nombre de personnes actuellement en ligne sur votre site en temps réel. Les limitations du système sont présentées avec les détails de l’implémentation.


Logo of Plume CMS