Introducing a Tool for Namespacing PHP5 Prefixed codebases

Ralph Schindler writes;
Hey All–

Over the past few days, I’ve been work­ing on a tool that I think might help expe­dite the task of con­vert­ing all of our code into a Name­spaced code­base.  Cur­rently, it’s been tested on sim­ple com­po­nents like Zend_Acl and Zend_Filter and over the course of the next few days, we’ll be apply­ing it to some of the more com­plex components.

What it does..

This tool is a com­mand line tool.  First, you may get it from my GitHub repository:

http://github.com/ralphschindler/PHPTools

git clone http://github.com/ralphschindler/PHPTools.git

OR

Just down­load the tar/gz/zip via the github interface.

Inside here you’ll find a bin/ library/ and test/ direc­tory, the name­spacer tool is located in this library.  To get the basic help screen, sim­ply run ‘php bin/php-namespacer.php –h’.  This will give you a cou­ple of avail­able options to under­stand what the var­i­ous com­mand line switches do.  Per­son­ally, I cre­ate a link from some­where in my path, usu­ally $HOME/bin/php-namespacer to path/to/PHPTools/bin/php-namespacer.php, this makes it eas­ier to run the com­mand from any­where in your filesystem.

To be able to get up and run­ning with it quickly, I sug­gest run­ning to tool in the fol­low­ing man­ner to get an idea of what it will produce:

bash> php-namespacer –l=../library/ –d=Zend/Acl –p=Zend –o=./tmp/ –m=./tmp/

Here are is what the above is going to do:

  • with the –l switch, you are telling it the loca­tion of a library this is typ­i­cally the same direc­tory you’d add to an include_path
  • with the –d switch, you are telling the tool that of all the classes it comes across in the library, only work on this par­tic­u­lar directory
  • –p is the pre­fix we want to work on, in our case Zend/
  • –o is the direc­tory where you want your new files writ­ten to
  • –m is the direc­tory where an XML file will be pro­duced of all the name trans­la­tions that have taken place.

The full library is sup­plied so that the tool can iter­ate all files for class names that might be present inside the work­ing directory’s files.

This is so that it knows what those trans­la­tions will be once those com­po­nents are even­tu­ally con­verted (assum­ing they are part of the same effort).

This tool will OPTIONALLY con­vert names in docblock ONLY if you have the pecl ext/docblock exten­sion com­piled and enabled (http://pecl.php.net/package/docblock).  Oth­er­wise it will not touch the docblocks.

I have to admit that even though we are see­ing some great results so far, this tool is still in it’s infancy.  We are open­ing it up to you for feed­back and if you find it use­ful, to run it over your own code to see if it does the job of con­vert­ing pre­fixed PHP5 code into name­spaced PHP 5.3 code.

I’ll let every­one get a chance to run the tool and pro­duce some out­put first before we start dis­cussing the actual out­put and file contents.

For dis­cus­sion pur­poses, we’ve been heav­ily look­ing at sim­pler com­po­nents like Zend_Acl, Zend_Filter and Zend_Config– but don’t let that stop you from run­ning it over other com­po­nents or even your own code.

Let me know what you think!

Ralph Schindler

Tags: , ,

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes