Archive for the ‘PIWI’ Category

Welcome Zeta Components!

Wednesday, June 2nd, 2010

Before a while, there was a very promising Proposal called Zeta Components at the Apache Incubator list. It described a set of PHP components similar which eases the life of us all. It has already progressed very good before developers decided to switch to Apache. In the past days this library has been developed under the name eZ Components and was wellknown to the community.

In my opinion this new project will be very important to the ASF. Apache only hosts two other PHP projects, Log4PHP and Shindig. Since PHP is becoming more and more popular for big business (think on Facebook) I think its necessary to attract more developers and bring them into the Apache community. Many PHP projects out there use the GPL license, and that is sometimes very difficult to combine with commerical work. Good that we now have a component framework under ASL! Together with Log4PHP it’s a good start for much proprietary work.

More in future, I think that even the PIWI Framework will benefit of this change. I already had some trouble with dependent GPL code in it and hopefully Zeta can address most of my wishes. PIWI built upon Log4PHP and Zeta would be charming. But as already said, this will need a while.

At the moment, Zeta Components do already have a mailinglists and status page on the incubator. Jira will follow and after finally the CLAs of the initial developers have been processed, work can begin. However, if you are interest in helping this project subscribe to the user and to the dev list mentioned in the status page.

Lucky me I have been elected into the Apache Incubator PMC and can now serve as a mentor to this project. It’s not only honour to me, it’s also a good chance to work with obviously very nice people and excellent technicians. Also it makes it easy to me to step into this project and learn all I need.

So, welcome Zeta Components!! I am glad you arrived at the Apache Software Foundation!

New print articles this month

Monday, May 17th, 2010

Currently I have a good interest in PHP development and for that reason I wrote some articles in that areas. Two of them are now published. In the current PHP User (a magazine for PHP beginners) appeared a small “who is who” article. And in the PHP Magazin (expert programmers) appeared an introduction about PIWI, written by Daniel Palme and myself. PHP Magazin

Dependency Injection – a Design Pattern

Thursday, April 16th, 2009

Design Patterns are useful and a must have. Everybody working seriously on software projects should know about them. End of 2007 I decided to use the powerful Spring Framework at my new enterprise project.  The project, which ended these days for me, was a huge success. We made complex changes easy, had very good unit testing and all that stuff. We had up to 12 developers working on the code, I am really thinking that Spring did lots for our success. It was so easy to seperate all the modules and integrate new features within minutes. I am writing this in my article about Dependency Injection, cause Spring highly depends on this design pattern and PIWI will do that, too.

Design Patterns – all just theory?

If students make their exams and come as junior programmers to my projects, they often believe a design pattern is something to eat. Or something theoretical. Or simply a joke. I usually start with explaining how to use it and such, but at the first glance it seems to abstract to them. Later, with the expierence, comes the joy.

This is true for patterns like Singleton or Factory or maybe Model-View-Controller. Coming to Dependency Injection – which is not read very often in Books about good software design – most people NEVER have joy. In this kind it behaves like the Proxy pattern. One really cannot use this out of the box. I mean, a singleton can be implemented within minutes, and it does what it does. But Dependency Injection? It’s more a concept of software design than a design pattern. Can you implement a singleton? Yes. Can you implement Dependency Injection? No, of course not. Having that said, the joy and the power comes when you are using a fitting framework which supports the design pattern. Like Spring. Or PIWI, in the latest trunk version. Cause we PIWI-folks simply have stolen much of the ideas of Spring and now use it not only in Java, but in PHP5 too.

What’s all about it?

Basically Dependency Injection simply says: don’t make a class dependent from another, inject the dependency. So what? some will say. But it’s simple. Imagine the following PHP lines:

class Car {
   private $driver = null;

   public crash() {
      $this->driver->saySomething();
   }
}

You see, there is a private member called driver in the car class. If your use case is that each car is a driver, it’s quite easy in most cases. Just fill the member in the constructor. This is, what most people do. This makes $driver nullsafe.

class Car {
   private $driver = null;

   public __construct() {
      $this->driver = new Driver();
   }

   public crash() {
      $this->driver->saySomething();
   }
}

OK. From this point on the class Car depends on the class Driver, cause you need the Driver class to instanciate Car. After you did that and all works well, your boss looks round the corner and tells you that we need to distinguish between female drivers and male drivers. He is sorry for saying that so late, but it’s like this. Cause if you call the new method crash(), a female driver would simply say:”you crashed me!” and a male driver would say something like:”guy i have a gun!”. And, that’s the point, your client A just wants male drivers in their systems, while client B just needs female drivers.

People start doing:

class Car {
   private $driver = null;

   public __construct($client) {
      if($client == "A") {
         $this->driver = new MaleDriver();
      } else if($client == "B") {
         $this->driver = new FemaleDriver();
      }
}

   public crash() {
      $this->driver->saySomething();
   }
}

All is good. But what if you need to port the system for somebody who just let childs drive? Or people with glasses upon their noses? In fact, we had the requirement to create several modules for several clients – customization. Yes, the example above with male and female drivers is not the best, but I am sitting in a car while typing this.

Problem on the approach above is that you need a new if else if you ever have more Driver types than above. And you have to put this in class where such dependencies exist. Maybe it’s not pretty much, but in our case we had about 20 modules – without customizations. I really was afraid before such a switch.

Dependency Injection for the solution

As I allready told you, we had Spring and Java 6 in our system. But we ported dependency injection to PIWI. And if you are using one of these frameworks, you know that both heavily use XML. Imagine you could say in an XML which driver you want to use. More better: you really would define an interface for all the driver classes and would make sure that you operate on that interface only.

See this:

interface Driver {
   public saySomething();
}

class FemaleDriver implements Driver {
   public saySomething() {
      echo "You crashed me!";
   }
}

(imagine something similar for MaleDriver).

You could do something like that:

class Car {
   private $driver = null;

   public __construct($driver) {
      $this->driver = $driver;
   }

   public crash() {
      $this->driver->saySomething();
   }
}

Isn’t your code much cleaner now? You know, if else means death to object orientation! This way your Car class doesn’t depend to MaleDriver or FemaleDriver. In case of PHP5 it just depends on an untyped object. In case of Java it depends on the Interface Driver.

By the way: you can force types in PHP5 (in some level of course). You could write the constructor like this:

public __construct(Driver $driver) {
   $this->driver = $driver;
}

This forces the caller to put an object inside which implements the interface Driver. And this in fact is the way you should go! And this is all about the Dependency Injection pattern. It says, make your classes dependend on interfaces, not on classes.

So, isn’t it nice? But how do you decide to bring your dependency in? This is another point, and this is why I said you have to check out Spring or PIWI for this. The example above is working gorgeous, but if you don’t find a mechanism to bring your dependencies into the game, all is lost. I promised XML like Spring does, and here it is: PIWIs XML file for Dependency Injection in PHP.

<bean id="xmlPage" class="XMLPage" scope="request">
   <property name="contentPath" php="$GLOBALS['PIWI_ROOT'].CONTENT_PATH" />
   <property name="siteFilename" value="site.xml" />
</bean>

<bean id="siteSelector" class="SiteSelector" scope="request">
   <property name="page" ref="xmlPage" />
</bean>

We call our objects beans, like Spring does. It’s Spring-Beans in Spring, but PIWI-Beans in PIWI. We are shameless. First, look at the bean definition with the id xmlPage. You give it a class and a scope, and some properties.

Basically, PIWI (and Spring too) creates the objects for you. That has many benefits. I will write an blog article why instanciating objects yourself is sometimes a bad idea some day. However, PIWI makes an object and stores it in our context, the BeanFactory. It’s stored under the id xmlPage, and you can get the object manually if you call:

$xmlPage = BeanFactory::getBeanById('xmlPage');

This brings you the ready bean. Additionally you tell the BeanFactory that it should set the property “siteFilename” to “site.xml”. And one line above you even give it an PHP expression. After PIWi made the object from the class XMLPage, it checks if a member variable is accessible named siteFilename. If it isn’t it looks if there is a method called setSiteFilename($o); or siteFileName($o). If PIWI found that method, it calls it with the value.

This is all done by reflection, which has been shipped with PHP5. No dependencies to other libs, no problem. Just plain PHP5. Same goes to Java. Plain Java. No Magic. Just some reflection.

If you would have called this line instead of the one above:

$siteSelector = BeanFactory::getBeanById('siteSelector');

the BeanFactory would have created the siteSelector bean. If you look into it, you see this:

<property name="page" ref="xmlPage" />

If the BeanFactory gets aware of this (and it does!), it will look in the context for a PIWI Bean with the ID xmlPage. If there isn’t one, it will try to create such a bean. Same rules as above. After creation of xmlPage it will be set into SiteSelector’s setPage($xmlPage) method.

And now imagine how your boss would love it, if you tell him that such a customization is just a matter of some XML?

<bean id="maleDriver" class="MaleDriver" scope="request" />
<bean id="femaleDriver" class="FemaleDriver" scope="request" />

<bean id="car" class="Car" scope="request">
   <property name="driver" ref="maleDriver" />
</bean>

Everthing done!

For the sake of completness – what do we mean with scope? Well, scope is in which scope your object lives. Defining request means, the we create maleDriver only once while the whole request. If you define another class which needs maleDriver, it would get – exactly the same -. This is, what some people call a Singleton in PHP. But Singletons are worth an own Blog entry, since some people – esspecially who have just written code with PHP and not for Java Enterprise Systems – say, Singletons are evil. Well, they are not. If developers are not thinking while coding, that is evil ;-) You can misuse everything, if you want to.

Here is another example for using the driver more than once:

<bean id="maleDriver" class="MaleDriver" scope="request" />
<bean id="femaleDriver" class="FemaleDriver" scope="request" />

<bean id="car" class="Car" scope="request">
   <property name="driver" ref="maleDriver" />
</bean>

<bean id="truck" class="Truck" scope="request">
   <property name="driver" ref="maleDriver" />
</bean>

In this case, only the same instance of maleDriver makes it to the truck and to the car. Decide yourself if this is your usecase. In the MaleDriver class are no states defined – so one object is enough.

OK – Dependencies are out. Instanciation is out of the code too. But is this the whole benefit? Not really. This stuff makes it possible to create dynamic proxies and use aspect orientation, even with PHP. And be sure, PIWI will have features some day. And Spring still got this cool stuff. But that’s another story.

Classloading in PHP – and how it is done in PIWI

Monday, April 13th, 2009

In PHP4 world a developer wrote scripts and included this scripts in other scripts with the wellknown include(), include_once(), require() or require_once() statements. As a PHP developer for long time I remember the pain: thousands of scripts, thousand ways to name them and tons of includes. It hasn’t become better with PHP4 first support of objectoriented programming. Actually it has gone even more worse, since a OOP developer usually uses one file per class. Again, the thousands of includes become more and more and finally we all used required_once() since we couldn’t know if another class allready included the file we want now.

Addionally, include (and similar, from now on I always use “include” if I refer for such kind of statements) is very static. If you want to move your class into another folder you have to search/replace some stuff. Sometimes I even found an empty file when a file has been moved, just with another include with the new location! Really, include is very bad. In the past I developed several loading mechanisms, but they always were quite hard and complex and didn’t turn on automatically.

Things changed with PHP5. There is a new magic method called __autoload(). And guess what – this method is usually called from the interpreter if you call a class which definition hasn’t been found in memory yet. We at PIWI use this new function as a core concept. Let me explain how.

Basically we don’t use any include statements anymore. They are evil and static. And of course we don’t have any procedural scripts in PIWI (except the starter file index.php). The latter one gives us the option to say:”whatever we need to be included, it must be a class or an interface”. That is the point were the __autoload function steps in. If we need a class or an interface, this method is called and our own Classloader concept is working.

To make things happen, we wrote a ClassLoader class which does the dirty work. It’s the only include statement we have. And that’s OK.

/** ClassLoader which makes other includes dispensable. */
require_once ("lib/piwi/classloader/ClassLoader.class.php");

Then we need to define the __autoload method. You should know that only one __autoload method definition is allowed per script. I would say it’s a good style if you only have one algorithm for looking up your classes per project.

Then we create the variable which holds the reference to the classloader.

/** Instance of the Classloader. */
$classloader = null;

As you may have noticed, I do this outside of the __autoload function. This is cause I don’t want to instanciate my ClassLoader each time I call the __autoload. To see how we make sure, that this variable is only filled once, we have to take a look into the __autoload method itself:

function __autoload($class) {
        global $classloader;
        if ($classloader == null) {
                $classloader = new ClassLoader($GLOBALS['PIWI_ROOT'] . 'cache/classloader.cache.xml');
        }

        $directorys = array ('lib', CUSTOM_CLASSES_PATH);

        foreach ($directorys as $directory) {
                $result = $classloader->loadClass($directory, $class);
                if ($result == true) {
                        return;
                }
        }
}

First we make $classloader global to the function. Then we check if it isn’t null. If it has been allready filled, we go on. If not, we create the ClassLoader object. You see in the above example, we are using a cache file too. Its an XML file and we put an entry into it whenever we found a location to a class.

After that we iterate about our class folders and call loadClass on each one. This method looks in the cache first. If the class is found in the cache, it is simply returned. If it’s found in the cache and the class has moved, the cache entry is deleted and normal load procedure starts. If even this fails, an error occurs.

The ClassLoader checks recursive in the parameter classfolder for the class. It loads the first file with the name of the class and if.php or class.php or even only .php (since PIWI 0.0.9) into the system. Example: if you lookup a class name MyClass, it should be in the file MyClass.class.php, MyClass.if.php or MyClass.php.

We recommend using .class.php for Classes, .if.php for Interfaces. We support .php only cause other projects may not have this convention – like log4php. Integraton is important to PIWI, since there are numberous projects outside we want to support and use ourselves.

You can easily use the ClassLoader classes yourself, even if you don’t use PIWI. Just download the PIWI package and grab the necessary classes in the ClassLoader folder.

However, there are some drawbacks of using __autoload. Reading files from the harddisk (that is what you do if you include classes) is not very performant compared to having everything in one huge file. But for the sake of readability, we always had success with putting one class per file. I never came to the point were this is really important. Additionally we have a html cache at PIWI working. This reduces waste of performance.

Second, but not really important (imho): __autoload can only load classes of course :-) Half procedural, half objectoriented is no more. If you are a traditional developer and use classes only where necessary, you probably will not have much benefits from __autoload.

Last thing to know: be rigoros with your exceptions in ClassLoader: log them (with Log4php) whereever they occur – those exception occuring in __autoload cannot be catched!

PIWI 0.0.9 released!

Sunday, April 12th, 2009

Yesterday we released PIWI at version 0.0.9 – the last of the 0.0.x series :-)

Since the first serious projects started to use PIWI, we had lots of valuable feedback for our development. Changes are:

PIWI does now logging with the great Log4PHP framework from Apache. Together with the improved exception handling people can reproduce PIWIs workflow much better than before. To be honest, for our users it was quite difficult to understand was PIWI does. Now it’s easier :-)

FormProcessing, JSONSerializer, TextSerializer now support more functions in the case of todays AJAX usage. We really were surprised what our Users did with PIWI – in fact this new implementations are the result of ongoing feedback. Thanks to all e-mail and contributions!

Additionally we have fixed lots of small issues which makes life much more easier for all PIWI users and devs.

As allready mentioned, the next release will have the version number 0.1.0. We are developing our dependency injection container and will refactor PIWIs core to make it more extensible. Dependency Injection in PIWIs case means that we try to adopt the ideas of Springs dependency injection.

PIWI 0.0.8 – Refactoring

Monday, February 9th, 2009

This is just a small release which refactores several folder structures and some interfaces (unfortunatly). We think that everything is more clear and easier to understand now. The next release aims to fix some issues which come up with using PIWI, for example how to include JavaScript or some complex Form issues.

PIWI 0.0.7 Crawling the site

Monday, December 8th, 2008

Inspired by Apache Forrest PIWI 0.0.7 now contains a brandnew Crawler which produces static HTML pages from your system. This means PIWI is now a publishing framework too :-) However we don’t recommend adding forms to a crawled page at the moment.

This could be the last release which has been done as final exams by Daniel – for the next release I will do some coding again too :-)

PIWI 0.0.6 – Authentification

Tuesday, November 25th, 2008

A new role and authentification system is now available with PIWI 0.0.6. You can now add your security mechanisms in the site.xml. Additionally you are able to create your own RoleProvider and get your reles whereever you want.

A Word- and a Excel-Serializer has been added. Those are very simple but will do their job.

Meanwhile the configuration of a PIWI site is to complex to keep it within the site.xml. It has been extracted to config.xml. Ant-file will simplify the build and Unit tests make everything more stable now (hopefully :-) )

PIWI 0.0.5 – Introducing Forms

Monday, November 3rd, 2008

PIWI 0.0.5 introduces form handling! With the simple definition of a form xml you can include and validate forms as you like. Besides that, a new PDF serializer has been introduced into the codebase. Unfortunatly this has been done with html2pdf, which is under GPL license. Due to licensing issues I guess we have to start an own subproject for creating PDF with PHP under Apache License at some day. But for now… :-)

PIWI 0.0.4 released!

Wednesday, October 1st, 2008

PIWI is jumping forward! Daniel Palme decided to write his final exams about PIWI and started to write code for the project. This means I step back for a while, just talking with him about concepts and the “doing” while he cares about implementation.

The new PIWI version contains lots of refactorings and improvements. Esspecially schema validation of XML files and the extraction of the Serializer components are important to mention. A Serializer transforms the PIWI-XML (which is a XHTML subset) into HTML – and later into PDF or whatever.

People who want to step into PIWI will be glad about the tons of docs Daniel wrote. Good work!

  • Recent Posts

  • Tags

  • Categories

  • Archives

  • Wave Notifications

    Download Adobe Wave now!

    This application requires Adobe® AIR™ to be installed for
    Mac OS or Windows.




Feeds