Monday, 18 February 2008

Creating Zend Framework snippets for TextMate

After finally converting to Mac OS X, I really couldn't resist to fall for the famous TextMate editor. The editor comes with a nice PHP bundle authored by Ciarán Walsh that eases the typing and creation of common language constructs like classes and control structures by entering pre-defined keys and hitting the tab key to insert and customize them. To reduce the typing effort for the most common tasks in creating a Zend Framework based application, which are creating action controllers including their hosted actions and creating new models for accessing the underlying database, I spent some minutes to figure out how to create and add these valuable snippets to the default PHP bundle.

Adding the controller and model snippets

The Bundle Editor of TextMate is a starter aid that can be used to create the intended Zend Framework snippets and categorize them. To add a new snippet simply select the PHP bundle and hit the most left button in the lower left of the Bundle Editor window. Now you are able to create new snippets, edit their content and assign an activation key to them i.e. zfc and also to define the snippet scope i.e. source.php.

To trigger the automatic insertion of the new snippet you just have to type zfc and hit the tab key in an openend PHP file. The next listing shows the snippet code for the creation of a Zend Framework action controller and as you will notice it contains several at first glance variable look-a-likes. All variables prefixed with TM are definable via the TextMate preferences while the numeric indexed variables are used to tab through the code after insertion and to modify the custom parts like the name for the controller class. They start by an index of 1 for the first tab position after the snippet insertion and end with an index of 0 for the last tab position. Thereby the first two tab strikes will allow you to modify the PHPDoc block while the third tab strike will allow you to edit the controller name e.g. News, which whould make the current code artifact your NewsController class.
/**
* ${1:'File-level' PHPDoc Block}
*
* @author ${PHPDOC_AUTHOR:$TM_FULLNAME} <$TM_ORGANIZATION_EMAIL>
*/

/**
* ${2:'Class-level' PHPDoc Block}
*
* @author ${PHPDOC_AUTHOR:$TM_FULLNAME} <$TM_ORGANIZATION_EMAIL>
*/
class ${3:Name}Controller extends Zend_Controller_Action
{
/**
* ${4:'Method-level' PHPDoc Block}
*/
public function ${5:name}Action()
{
$6
}
$0
}//Activation key: zfc tab
As an action contoller surely will increase all allong by several more actions the next listing shows the snippet code to add a single action to a currently opened action controller class by simply entering zfca and striking the tab key.
/**
* ${1:'Method-level' PHPDoc Block}
*/
public function ${2:name}Action()
{
$0
}//Activation key: zfca tab
The next last valuable snippet would refer to the model part of a Zend Framework application, so the listing shows a snippet for a table class, which automatically sets the table name property to the lowercased and prior entered class name. This snippet would be inserted into the current PHP file by entering the activation key zfm and striking the tab key again.
/**
* ${1:'File-level' PHPDoc Block}
* @author ${PHPDOC_AUTHOR:$TM_FULLNAME} <$TM_ORGANIZATION_EMAIL>
*/

/**
* ${2:'Class-level' PHPDoc Block}
* @author ${PHPDOC_AUTHOR:$TM_FULLNAME} <$TM_ORGANIZATION_EMAIL>
*/
class ${3:Name} extends Zend_Db_Table_Abstract
{
protected \$_name = '${3/./\l$0/}';
}//Activation key: zfm tab

Adding a View Helper snippet

The next snippet was provided by a reader called Jeff Federmann, who contacted me via mail as Google ate his code he tried to contribute in a comment. This snippet can get you up to speed when developing custom View Helpers. The workflow for this snippet is to create a new file for the helper, saving it with a purpose indicating name, which will auto-determine the View Helper class name on code insertion, and trigger the snippet by entering the activation key i.e. zfvh plus hitting the tab again. So nuff talk. Here is the snippet contributed by Jeef.
/**
* ${1:'File-level' PHPDoc Block}
*
* @author ${PHPDOC_AUTHOR:$TM_FULLNAME} <$TM_ORGANIZATION_EMAIL>
*/

/**
* ${2:'Class-level' PHPDoc Block}
*
* @author ${PHPDOC_AUTHOR:$TM_FULLNAME} <$TM_ORGANIZATION_EMAIL>
*/
class Zend_View_Helper_${3:`#!/usr/bin/env php
$filename = $_ENV['TM_FILENAME'];
$filename = str_replace('.php', '', $filename);
echo $filename;
?>`} extends Zend_View_Helper_Abstract
{
/**
* ${4:'Method-level' PHPDoc Block}
*/
public function ${3/./\l$0/}()
{
$5
}
$0
}

Housing the Zend Framework snippets

To finally categorize the Zend Framework snippets and avoid bloating the default PHP Bundle the Bundle Editor comes to rescue again, as it allows to add new categories to an existing bundle in which the shown snippets and other compatible and common snippets, e.g. Zend_Registry access to acquire the application's Zend_Log instance, can be housed.

2 comments:

Anonymous said...

cheers for posting

Ryan said...

Excellent work. I recently created a formal bundle and put it up on GitHub. Feel free to push to it.

http://github.com/ybits/Zend-Framework.tmbundle