Hello everybody, it’s been a while. Just a quick post to announce that I released a new project which is a bash script to install a “theme integration”. It’s a combination of themes, applications and tweaks. It creates a spacious desktop where maximized applications take the full screen, perfect for netbook. It uses docky and gnome-do for window/application management.

Find out more at http://code.google.com/p/spatial-desktop

Posted in Unclassified at November 22nd, 2009. Comments.

I recently had to write a little script to check the integrity of references inside a database.
Notice: This was done as part of my work at Tribal Nova. Thanks for letting me share!

The script (command line only) is very simple and aims to check integrity of references (foreign keys or soft references).  It’s for MySQL only and requires PHP5 with PDO.
Foreign keys are automatically discovered and you can specify additional references in a mapping.xml file.

A report will be displayed. Upon this, you can choose to execute an action to resolve issues. You can

  • update values using a mapping.xml file which maps old values with new ones
  • delete broken references
  • follow the on update or on delete behaviour

To run it simply call it from the command line with connection info: php integrity.php –database=my_db –username=root –password=root

You can get some help using php integrity.php –help.

You can download it by clicking here.

Posted in Unclassified at September 1st, 2009. Comments.

Hello everybody, it’s been a while.
Just a quick post to say that I released some packages for wampserver to use vc9 binaries of apache and php 5.3.
You can find more information on the wampserver forum

Posted in Unclassified at July 28th, 2009. Comments.

It’s been quite a long time since I blogged anything. I merely touch at a computer for the past two weeks but I hope to resume the work on Atomik as soon as possible for the 2.2 release.

Meanwhile, I want to present you the new Model plugin that will come with this version.
A quick reminder to begin. Putting it simply, models are a way to map database tables to classes. This allow to manipulate data from the database as objects in our application. An ORM (Object Relational Mapper) is the library/tool that allows you to do such a thing. The Model plugin brings the Atomik_Model library which is an ORM.

I tried to create an ORM which is not full of big enterprisy features (there are already some very good ones) but one that is more suited to the everyday developer. Models are defined as classes and annotations are used to configure it. By annotations I mean using PHP’s doc comment and @ keywords (like php documentor documentation).
A big feature of Atomik_Model is that the data do not have to come from the database. Each model is bound to one adapter where it will fetch its data. This can be an xml file or a web service for example. Relations between models (like foreign keys in an RDBMS) are also supported.
Finally, you can also generate forms for each model (this uses Atomik_Form / Form plugin).

So, how to define a model ? Assuming that our default adapter (the data source) is the database:

class User extends Atomik_Model
{
    public $username;
}

This will be mapped to the User table. The primary key is automatically generated (and named id, this can be changed). You can change the table name using the @table annotation:

/**
 * @table users
 */
class User extends Atomik_Model
{ /* ... */ }

The Model plugin creates some console commands to automatically generate tables associated to the models (the Console plugin is needed). You won’t have to write a line of sql!

php indexp.php db-create

Now let’s create another model named Article and add a relation between the two (a user has many articles).

/**
 * @has many Article as articles
 */
class User extends Atomik_Model
{
    public $name;
}
 
/**
 * @has parent User as author
 */
class Article extends Atomik_Model
{
    public $title;
    public $text;
}

As you can see, the @has annotation is used. It follows this schema: @has [relation type] [model name] as [property]. The relation type can be one, many or parent. The property will be the object property used to access the associated model.

Now let’s query these models! There are three static methods of Atomik_Model to do so: find(), findOne() and findAll(). All takes as first argument the model name. find() takes as second a model id. findOne() and findAll() works the same way except that findOne() returns the first record only. They take as parameter an array of fields/value for conditions.

$users = Atomik_Model::findAll('User');
echo '<ul>';
foreach ($users as $user) {
    echo '<li>' . $user->name . '<ul>';
    foreach ($user->articles as $article) {
        echo '<li>' . $article->title . '</li>';
    }
    echo '</ul></li>';
}
echo '</ul>';

As you can see, we’re getting the user’s articles by simply using its articles property.

This was a quick look at what’s coming up. There’s much more to discover in Atomik_Model as well as in the new Atomik Library. I hope you’ll like it!

Posted in Unclassified at June 16th, 2009. Comments.

I’ll be giving a talk on Atomik Framework the 7th of May at a PHP Quebec meeting. The talk will be about the 2.2 version, so hopefully it will be already released.
You can find more information at http://www.phpquebec.org/modules/piCal/index.php?smode=Daily&action=View&event_id=0000000892&caldate=2009-4-18

Posted in Unclassified at April 18th, 2009. Comments.

I said that I released it before the end of March. I’m one day late but it’s here: the first milestone for Atomik 2.2! This new version has great new features which will make Atomik really simple to use. The core is mostly finished and I’m now working on plugins. I can’t give a release date yet as there’s still lot of work.

So what’s new in this release?

  • File extension support in routes
  • View contexts: allow to serve the same view in different format
  • Actions targeted to specific HTTP methods (to do RESTful websites)
  • View helpers
  • Pluggable applications (the biggest improvement of the core I think)
  • Register methods on the Atomik class (PHP 5.3 only)

And the cool think is that it’s already fully documented!

Be aware that there is a big compatibility break with the configuration. New configuration keys are listed in Appendix A of the documentation.

But the things I’m the most excited about in this new release are the new plugins. Using the new pluggable applications feature, I created the Backend plugin which adds, as its name implies, a backend to your website. Plugins can then create administration interfaces in this backend. For example, the Db plugin will now come with a database management interface! It will become very easy to create simply administrable website.

Another plugin that I’m hardly working one is the Db one. From version 2.2 it will include models support. I tried to create a very simple (yet powerful) way to define your models. The great thing is that their data source won’t have to be a database. I’ll talk more about that later.

From Atomik 2.2, there will also be a sub-project called Atomik Lib. When I create a plugin, I always try to create the associated libraries as reusable as possible. These will now be available as independent packages or all together, like the Zend Framework.

Note that there is no documentation for new plugins as well as for Atomik Lib yet. These new plugins are also in heavy development so don’t expect much of them for now. You can still download them to have a look.

Download Atomik 2.2 M1
Documentation (see bottom of the page)

Posted in Unclassified at April 1st, 2009. Comments.

At work, I’m currently working with Wordpress and I was amazed at how most plugins are really bad written! PHP developers should really start to make an effort about that and this is even more true for popular projects like Wordpress. At least, the core or the Akismet plugin, which comes already installed, should give an example. But no! Have you ever had a look at this plugin source code? I nearly puked! Come on guys, not even comments ? Don’t be surprised why enterprise are a bit reluctant to use PHP.

Most plugins don’t use classes, don’t comment their code, HTML is in the middle of PHP code…all anti pattern or bad practices can be found in these. While Wordpress does not force you and does not provide means to facilitate clean programming, it is still not a reason to not use it. It is fairly simple to separate HTML from php files and use a class for the plugin. Here how I did it (and I’m sure there’s even a better way but at least this one is cleaner than most plugin).

In your plugin file, starts by creating a class. I use a start() method which will register all hooks and one method for each of them.

/**
 * explain what my plugin does
 */
class MyWordpressPlugin
{
    /**
     * Initialize the plugin and register hooks
     */
    public static function start()
    {
        add_action('hook', 'MyWordpressPlugin::onHook');
    }
 
    /**
     * Callback for the action "hook"
     */
    public static function onHook()
    {
        // do stuff
    }
}
 
// starts the plugin
MyWordpressPlugin::start();

That’s it! Far more cleaner than putting everything in the global scope.

All my HTML content is saved as php files with the phtml extension in an html subfolder. Then for each of them, I create a renderSomething() method which simply includes the phtml file. This method can, before the include, do some logic, like retreiving some data. There should be only presentation logic inside the phtml file.

I also create a separate file with a new class when it comes to create some administration pages. My plugin needed an option page so I create a class called MyWordpressPluginAdmin which registers hooks and do all the suff associated with displaying my option page. Then I include this file in the plugin file and call MyWordpressPluginAdmin::start() from the MyWordpressPlugin::start() method. Here is how it looks like:

/**
 * Register the option page
 */
class MyWordpressPluginAdmin
{
    /**
     * Register hooks
     */
    public static function start()
    {
        add_action('admin_menu', 'MyWordpressPluginAdmin::onAdminMenu');
    }
 
    /**
     * Register the option page
     */
    public static function onAdminMenu()
    {
        add_options_page('My plugin', 'My plugin', 8, __FILE__, 'MyWordpressPluginAdmin::renderOptionPage');
    }
 
    /**
     * Render the option page
     */
    public static function renderOptionPage()
    {
        include dirname(__FILE__) . '/html/option_page.phtml';
    }
}

I think, and correct me if I’m wrong, that this way of doing things is better. The code is commented, easily maintenable, designer could work on their own files… Do you think this is a good solution? the comments are yours!

PS: sorry for the harsh beginning but I was quite angry and disappointed when I saw all that.

Posted in Unclassified at March 27th, 2009. Comments.

Following my previous article on object oriented programming in Javascript, I released yesterday a small script which allows you to manage namespaces. I know that solutions already exist. Not considering the one from libraries (I wanted a library independent script), I was not satisfied by any of them.

Namespace.js is very simple to use. It contains all common actions encountered when dealing with namespaces. It can import objects in the global scope or include remote scripts based on their namespace. It is very small (5.4 Kb minified), open source and of course library independent.

I published the project on google code at http://code.google.com/p/namespacedotjs

Here are some examples:

Namespace('foo.bar');
foo.bar.myFunction = function() {};
 
Namespace('com.example', {
   MyClass: function() { return {}; }
});
var obj = new com.example.MyClass();
 
Namespace.use('com.example.MyClass');
var obj2 = new MyClass();
 
// include com/example/RemoteClass.js
Namespace.include('com.example.RemoteClass');
var obj3 = new com.example.RemoteClass();
 
Namespace.registerNativeExtensions();
'com.foo.bar'.namespace();
Posted in Unclassified at February 26th, 2009. Comments.

We’ve seen a lot of talk lately about OO javascript. I won’t come back on that as there’s many tutorial and blog posts about it. However, I rarely seen articles about the OO capabilities of Mootools. While every Mootools users are probably aware about it ,Mootools is not the most used framework, and not all javascript developers may now its great features in that field.

Mootools introduces the Class object which allows you to, obviously, create classes. To create a class, you need to create a new instance of this object. Beware that we define the class here, even if we’re using the new keyword. The Class’s constructor takes as argument an object defining methods and properties.

var MyFirstClass = new Class({
    message: "hello world",
    sayHello: function() {
        alert(this.message);
    }
});

As you can see, we can use the this keyword to reference the instance. Creating an instance of the class is straightforward:

var obj = new MyFirstClass();
obj.sayHello();

While this should seems pretty “standard” for any OO developer, it is not for javascript. The protoype (not the framework) way of doing things, while powerful, is quite awkward! And as we’re going to see, Mootools offers much more regarding OO features.

If you’re class needs a constructor, you can define a method named initialize.

var HelloWord = new Class({
    initialize: function(name) {
        alert('hello ' + name);
    }
});
 
new HelloWorld('peter');

Classes can of course extend each other. Mootools supports only one parent class. It can be defined by adding a property named Extends to your class definition. The property’s value must be an object.

var Animal = new Class({
    initialize: function(name) {
        this.name = name;
    },
    eat: function() {
        alert('yummie');
    }
});
 
var Human = new Class({
    Extends: Animal,
    speak: function() {
        alert(this.name + 'said bla bla bla');
    }
});
 
var peter = new Human('peter');
peter.eat();
peter.speak();

When subclassing, a method (not only the constructor) can call the same method from its parent class using the this.parent() function.

var Peter = new Class({
    Extends: Human,
    initialize: function() {
        this.parent('peter');
    }
});

Class can also use the special Implements property. Do not confuse this with the java or php implements keyword as it does not have the same behavior. In Mootools classes, Implements will merge properties from one or many objects into your class, overriding any method with the same name. It is useful to implement extra class features.

Indeed, Mootools comes with three extra class features: Chain, Events and Options. They can be added to your class using the Implements properties.

var MyClass = new Class({ Implements: [Chain, Events] });

Events adds events support with common methods like addEvent(), removeEvent() … It also adds the fireEvent() method which, as its name implies, fires an event.

var Paul = new Class({
    Extends: Human
    Implements: Events,
    initialize: function() {
        this.parent('paul');
    },
    speak: function() {
        this.parent();
        this.fireEvent('speaking');
    }
});
 
var paul = new Paul();
paul.addEvent('speaking', function() { /* ... */ });

The Chain and Options feature are also pretty neat but I let you check the Mootools documentation on these one.

I’ve gone through the documented features and we’ll now see some less known ones. For example, adding static properties (even if it seems pretty obvious).

var MyClass = new Class({ /* ... */ });
MyClass.staticProperty = '';
MyClass.staticMethod = function() {};

Don’t forget to use the class name when calling them (you can’t use the this keyword).
A common design pattern in OO is Singleton. This can easily be implemented using a closure.

var MyClass = (function() {
    var MyClassSingleton = new Class({ /* ... */ });
    var instance;
    return function() {
        return instance ? instance : instance = new MyClassSingleton();
    }
})();
 
var obj1 = new MyClass();
var obj2 = new MyClass(); // same instance, obj1 == obj2

The Extends and Implements properties are called mutators and you can define you’re own ones. This can be done by adding a property to the Class.Mutators object named after your special property. This property should be a function with two parameters: the first one will be an object with the class properties and the second one the value of the mutator property. The function must return an object with the class properties.

Class.Mutators.MyMutator = function(properties, mutatorValue) { /* ... */ };
var MyClass = new Class({ MyMutator: "mutator value" });

I hope I covered most of what is to know about Mootools and classes. I’ll soon, probably tomorrow, write an article about javascript and namespaces using a custom library I made.

Posted in Unclassified at February 11th, 2009. Comments.

I’ve been pretty busy these days working on Atomik 2.1. I released a second beta yesterday and it’s shaping up very well. The final release should be out very soon.

Today, I updated my CV and it is now available in English. You can download it on the Resume page.

Posted in Unclassified at February 5th, 2009. Comments.