Showing posts with label TextMate. Show all posts
Showing posts with label TextMate. Show all posts

Saturday, 22 August 2009

Kicking off custom Phing task development with TextMate

As a reader of this blog you migth have noticed that from time to time I like to utilize Phing's ability to write custom tasks. Though that's not an everyday routine for me and therefor I might, depending on my form of the day, end up with some real smelly code where for example the task's properties validation is handled in the task's main worker method. This is actually a bad habit/practice I'm aware of and to improve my future endeavours in custom Phing task development, I bended TextMate's snippet feature to my needs.

Snippets in TextMate are a very powerful feature that can be used to insert code that you do not want to type again and again, or like in my case might have forgotten over a certain time.

The next code listing shows the snippet providing a basic custom Phing task class skeleton which can be utilized over and over at the beginning of the implementation activities.

<?php
require_once 'phing/Task.php';

class ${1:CustomName}Task extends Task
{
private \$_${2:property} = null;

/**
* @param string \$${2:property} ${3:description}
*/
public function set${2/./\u$0/}(\$${2:property})
{
\$this->_${2:property} = trim(\$${2:property});
}
/**
* Initializes the task environment if necessary
*/
public function init()
{
}
/**
* Does the task main work or delegates it
* @throws BuildException
*/
public function main()
{
\$this->_validateProperties();
}
/**
* Validates the task properties
* @throws BuildException
*/
private function _validateProperties()
{
if (is_null(\$this->_${2:property})) {
throw new BuildException('${4:message}.');
}$0
}
}
To apply the snippet, after installing it, on a PHP source file it can either be selected from the Bundles menue or more comfortable via the assigned tab trigger i.e. ctask. After triggering the snippet it's possible to properly name the task under development and dynamically set it's first property, which is also treated as a mandatory property in the extracted _validateProperties method.

The outro image shows the above stated snippet in the TextMate Bundle Editor and it's configuration.

Phing snippet in the TextMate Bundle Editor

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.