<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" gd:etag="W/&quot;A0IFR389fyp7ImA9WhBVEE0.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171</id><updated>2013-04-15T09:05:16.167+02:00</updated><category term="AOP" /><category term="Continuous Integration" /><category term="XP" /><category term="REST" /><category term="NoSql" /><category term="EXSLT" /><category term="Zend Framework" /><category term="Xinc" /><category term="Prototype" /><category term="Metatags" /><category term="PHP_CodeSniffer" /><category term="Ajax" /><category term="Refactoring" /><category term="Ruby/Rails" /><category term="Json" /><category term="Best Practice" /><category term="PHP" /><category term="Redis" /><category term="GitHub" /><category term="TDD" /><category term="Reflection API" /><category term="Ruby" /><category term="BDD" /><category term="Phing" /><category term="MongoDb" /><category term="TextMate" /><category term="Xml command line tool" /><category term="PHPUnit" /><category term="Recordshelf" /><category term="Rake" /><category term="Automation" /><category term="Book reviews" /><title>&lt;raphael.on.php/&gt;</title><subtitle type="html">PHP web development blog by Raphael Stolt</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://raphaelstolt.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>58</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/raphaelonphp" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="raphaelonphp" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;Ak8DQX85eip7ImA9WhRTGE4.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-80452318459294480</id><published>2011-04-22T17:44:00.001+02:00</published><updated>2011-11-09T12:41:10.122+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-09T12:41:10.122+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Enforcing target descriptions within build files with a Git hook</title><content type="html">When automating mundane tasks of a project or development environment with a build tool like &lt;a href="http://phing.info/"&gt;Phing&lt;/a&gt; or &lt;a href="http://ant.apache.org/"&gt;Ant&lt;/a&gt;, the driving build file will naturally accumulate several targets and tasks over time. To ease the build file acceptance within a team and at a later stage also the contribution rate by team members, it's crucial that all build targets have a description attribute to provide at least a rough outline of the build features at hand. When these attributes are in place the (potential) build file user will get such an outline by executing the build tool's list command (&lt;em class="command"&gt;phing -l&lt;/em&gt; or &lt;em class="command"&gt;ant -p&lt;/em&gt;). To get a better picture of the problem at hand imagine a project poorly covered with tests and your personal attitude towards extending it or just take a peek at the screenshot below showing a very poorly documented build file.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/5643674328/" title="A poorly documented build file in Phing's list view von Raphael Stolt bei Flickr"&gt;&lt;img alt="A poorly documented build file in Phing's list view" border="0" height="314" src="http://farm6.static.flickr.com/5026/5643674328_2e293600a6.jpg" width="500" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
To overcome this accumulation of some sort of technical debt (i.e. poorly documented targets) there are various options at hand. The first one, not covered in this blog post, would be to add a pursuant test which verifies the existence of a description for every target/task of the build file under test. As it's very uncommon, at least from what I've heard, to have your build files covered by tests; the next thinkable approach would be to use a &lt;a href="http://git-scm.com/"&gt;Git&lt;/a&gt; pre-commit hook to guard your repository/ies against the creeping in of such poorly documented build files.&lt;br /&gt;
&lt;br /&gt;
The next listing shows such a Git hook (also &lt;a href="https://gist.github.com/936903"&gt;available&lt;/a&gt; via GitHub) scribbled away in PHP, which detects any build file(s) following a common build file naming schema (i.e. build.xml|build.xml.dist|personal-build.xml|…) , prior to the actual commit. For every target element in the detected build file(s) it's then verified that it has a description attribute and that it's actual content is long enough to carry some meaning. If one of those two requirements aren't met, the commit is rejected while revealing the build file smells to the committer, so she can fix it, as shown in the outro screenshot. Happy build file sniffing.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;#!/usr/bin/php
&amp;lt;?php
define('DEPENDENT_EXTENSION', 'SimpleXML');

if (!extension_loaded(DEPENDENT_EXTENSION)) {
    $consoleMessage = sprintf(
        "Skipping build file checks as the '%s' extension isn't available.", 
        DEPENDENT_EXTENSION
    );
    echo $consoleMessage . PHP_EOL;
    exit(0);
}

define('MIN_TARGET_DESCRIPTION_LENGTH', 10);
define('TARGET_DESCRIPTION_ATTRIBUTE', 'description');
define('TARGET_NAME_ATTRIBUTE', 'name');
define('CHECK_DESCRIPTION_LENGTH', true);

$possibleBuildFileNames = array(
    'build.xml.dist',
    'build.xml-dist',
    'build-dist.xml',
    'build.xml',
    'personal-build.xml'
);

$violations = getAllBuildFileViolationsOfCommit($possibleBuildFileNames);
fireBackPossibleViolationsAndExitAccordingly($violations);

function getAllBuildFileViolationsOfCommit(array $possibleBuildFileNames)
{
    $filesOfCommit = array();
    $gitCommand = 'git diff --cached --name-only';
    
    exec($gitCommand, $filesOfCommit, $commandReturnCode);
    
    $allViolations = array();
    foreach ($filesOfCommit as $file) {
      if (in_array(basename($file), $possibleBuildFileNames)) {
          $violations = checkBuildFileForViolations($file);
          if (count($violations) &amp;gt; 0) {
            $allViolations[$file] = $violations;
          }
      }
    }

    return $allViolations;
}

/**
 *  @param  array $allViolations
 *  @return void
 */
function fireBackPossibleViolationsAndExitAccordingly(array $allViolations)
{
    if (count($allViolations) &amp;gt; 0) {
        foreach ($allViolations as $buildFile =&amp;gt; $violations) {

            $buildFileConsoleMessageHeader = sprintf("Build file '%s':", $buildFile);
            echo $buildFileConsoleMessageHeader . PHP_EOL;

            foreach ($violations as $violationMessage) {
                $buildFileConsoleMessageLine = sprintf(" + %s", $violationMessage);
                echo $buildFileConsoleMessageLine . PHP_EOL;
            }
        }
        if (count($allViolations) &amp;gt; 1) {
            $rejectCommitConsoleMessage = sprintf(
                "Therefore rejecting the commit of build files [ %s ].", 
                implode(', ', array_keys($allViolations))
            );
        } else {
            $rejectCommitConsoleMessage = sprintf(
                "Therefore rejecting the commit of build file [ %s ].", 
                implode(', ', array_keys($allViolations))
            );
        }

        echo $rejectCommitConsoleMessage . PHP_EOL;
        exit(1);
    }
    exit(0);
}
/**
 *  @param  string $buildfile
 *  @return array
 */
function checkBuildFileForViolations($buildFile) {
    if (!file_exists($buildFile)) {
        return array();
    }

    $buildfileXml = file_get_contents($buildFile);
    $buildXml = new SimpleXMLElement($buildfileXml);
    $allBuildTargets = $buildXml-&amp;gt;xpath("//target");
    $violations = array();

    if (count($allBuildTargets) &amp;gt; 0) {

        $targetsWithNoDescription = $targetsWithTooShortDescription = array();

        foreach ($allBuildTargets as $buildTarget) {

            $actualTragetAttributes = $buildTarget-&amp;gt;attributes();
            $allUsedTragetAttributes = array();
            $actualTargetName = null;

            foreach ($actualTragetAttributes as $attribute =&amp;gt; $value) {
                $allUsedTragetAttributes[] = $attribute;

                if ($attribute === TARGET_NAME_ATTRIBUTE) {
                    $actualTargetName = $value;
                }

                if (CHECK_DESCRIPTION_LENGTH === true &amp;amp;&amp;amp; 
                    $attribute === TARGET_DESCRIPTION_ATTRIBUTE &amp;amp;&amp;amp; 
                    strlen($value) &amp;lt; MIN_TARGET_DESCRIPTION_LENGTH) 
                {
                    $targetsWithTooShortDescription[] = $actualTargetName;
                }
            }   

            if (!in_array(TARGET_DESCRIPTION_ATTRIBUTE, $allUsedTragetAttributes)) {
                $targetsWithNoDescription[] = $actualTargetName;
            }
        }
        if (count($targetsWithNoDescription) &amp;gt; 0) {
            if (count($targetsWithNoDescription) &amp;gt; 1) {
                $violations[] = sprintf(
                    "Build targets [ %s ] don't have mandatory descriptions.", 
                    implode(', ', $targetsWithNoDescription)
                );
            } else {
                $violations[] = sprintf(
                    "Build target [ %s ] doesn't have a mandatory description.", 
                    implode(', ', $targetsWithNoDescription)
                );
            }
        }

        if (count($targetsWithTooShortDescription) &amp;gt; 0) {
            if (count($targetsWithTooShortDescription) &amp;gt; 1) {
                $violations[] = sprintf(
                    "Build targets [ %s ] don't have an adequate target description length.", 
                    implode(', ', $targetsWithTooShortDescription),
                    MIN_TARGET_DESCRIPTION_LENGTH
                );
            } else {
                $violations[] = sprintf(
                    "Build target [ %s ] doesn't have an adequate target description length.", 
                    implode(', ', $targetsWithTooShortDescription),
                    MIN_TARGET_DESCRIPTION_LENGTH
                );
            }
        }
    }
    return $violations;
}&lt;/pre&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/5643107165/" title="Non-Descriptive Phing build files rejected by a Git hook von Raphael Stolt bei Flickr"&gt;&lt;img alt="Non-Descriptive Phing build files rejected by a Git hook" border="0" height="147" src="http://farm6.static.flickr.com/5046/5643107165_4e6a34bfe9.jpg" width="500" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/7-9MVSSUPGI" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=80452318459294480" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/80452318459294480?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/80452318459294480?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2011/04/enforcing-target-descriptions-within.html" title="Enforcing target descriptions within build files with a Git hook" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://farm6.static.flickr.com/5026/5643674328_2e293600a6_t.jpg" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0MESHk9fyp7ImA9WhRTGE4.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-5852289239698386107</id><published>2010-11-20T16:30:00.003+01:00</published><updated>2011-11-09T12:50:09.767+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-09T12:50:09.767+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Measuring &amp; displaying Phing build times with buildhawk</title><content type="html">Recently I installed a Ruby gem called &lt;a href="http://github.com/xaviershay/buildhawk"&gt;buildhawk&lt;/a&gt; which allows to measure and display the build times of &lt;a href="http://rake.rubyforge.org/"&gt;Rake&lt;/a&gt; driven builds. As I like the idea behind this tool a lot but mostly use &lt;a href="http://phing.info/"&gt;Phing&lt;/a&gt; for build orchestration, it was time to explore the possibility to interconnect them both. In this blog post I'll show an implementation of an apposite Phing Logger gathering the buildhawk compatible build times via &lt;a href="http://progit.org/2010/08/25/notes.html"&gt;git note&lt;/a&gt;(s) and how to put the interplay between those two tools to work.&lt;br /&gt;
&lt;h4 class="custom"&gt;Logging on&lt;/h4&gt;As mentioned above the build time of each build is stored as a git note and associated to the repository's HEAD, reflecting the current state of the system under build (SUB), which assumes that the SUB is versioned via Git. The next shown Phing Logger (i.e. BuildhawkLogger) grabs the overall build time by hooking into the &lt;i&gt;buildFinished&lt;/i&gt; method of the extended &lt;i&gt;DefaultLogger&lt;/i&gt; class, transforms it into a buildhawk specific format and finally adds it as a git note.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;&amp;lt;?php

require_once 'phing/listener/DefaultLogger.php';

/**
 *  Writes a build event to the console and store the build time as a git notes in the   
 *  project's repository HEAD.
 *
 *  @author    Raphael Stolt &amp;lt;raphael.stolt@gmail.com&amp;gt;
 *  @see       BuildEvent
 *  @link      https://github.com/xaviershay/buildhawk Buildhawk on GitHub
 *  @package   phing.listener
 */
class BuildhawkLogger extends DefaultLogger {
    
    /**
     *  @var string
     */
    private $_gitNotesCommandResponse = null;

    /**
     *  Behaves like the original DefaultLogger, plus adds the total build time 
     *  as a git note to current repository HEAD.
     *
     *  @param  BuildEvent $event
     *  @see    BuildEvent::getException()
     *  @see    DefaultLogger::buildFinished
     *  @link   http://www.kernel.org/pub/software/scm/git/docs/git-notes.html
     */
    public function buildFinished(BuildEvent $event) {
        parent::buildFinished($event);
        if ($this-&amp;gt;_isProjectGitDriven($event)) {
            $error = $event-&amp;gt;getException();
            if ($error === null) {
                $buildtimeForBuildhawk = $this-&amp;gt;_formatBuildhawkTime(
                    Phing::currentTimeMillis() - $this-&amp;gt;startTime
                );
                if (!$this-&amp;gt;_addBuildTimeAsGitNote($buildtimeForBuildhawk)) {
                    $message = sprintf(
                        "Failed to add git note due to '%s'",
                        $this-&amp;gt;_gitNotesCommandResponse
                    );
                    $this-&amp;gt;printMessage($message, $this-&amp;gt;err, Project::MSG_ERR);
                }
            }
        }
    }
    
    /**
     * Checks (rudimentary) if the project is Git driven
     *
     *  @param  BuildEvent $event
     *  @return boolean
     */
    private function _isProjectGitDriven(BuildEvent $event)
    {
        $project = $event-&amp;gt;getProject();
        $projectRelativeGitDir = sprintf(
            '%s/.git', $project-&amp;gt;getBasedir()-&amp;gt;getPath()
        );
        return file_exists($projectRelativeGitDir) &amp;amp;&amp;amp; is_dir($projectRelativeGitDir);
    }
    
    /**
     *  Formats a time micro integer to buildhawk readable format.
     *
     *  @param  integer The time stamp
     */
    private function _formatBuildhawkTime($micros) {
        return sprintf("%0.3f", $micros);
    }
    
    /**
     *  Adds the build time as a git note to the current repository HEAD
     *
     *  @param  string  $buildTime The build time of the build
     *  @return mixed   True on sucess otherwise the command failure response
     */
    private function _addBuildTimeAsGitNote($buildTime) {
        $gitNotesCommand = sprintf(
            "git notes --ref=buildtime add -f -m '%s' HEAD 2&amp;gt;&amp;amp;1",
            $buildTime
        );
        $gitNotesCommandResponse = exec($gitNotesCommand, $output, $return);
        if ($return !== 0) {
            $this-&amp;gt;_gitNotesCommandResponse = $gitNotesCommandResponse;
            return false;
        }
        return true;
    }
}&lt;/pre&gt;&lt;h4 class="custom"&gt;Putting the Logger to work&lt;/h4&gt;As the buildhawk logger is &lt;a href="https://gist.github.com/707868"&gt;available&lt;/a&gt; via GitHub you can easily grab it by issuing &lt;i&gt;sudo curl -s http://gist.github.com/raw/707868/BuildhawkLogger.php -o $PHING_HOME/listener/BuildhawkLogger.php&lt;/i&gt;. The next step, making the build times loggable, is achieved by using the &lt;i&gt;-logger&lt;/i&gt; command line argument of the Phing Cli and specifying the buildhawk logger name or the path to it. In case you want the buildhawk logger to be used per default (it behaves like the default logger if the SUB isn't Git driven/managed) you can also add it to the Phing shell script.&lt;br /&gt;
&lt;br /&gt;
The next console command issued in the directory of the SUB shows a Phing call utilizing the BuildhawkLogger, assumed it has been installed at &lt;i&gt;$PHING_HOME/listener/BuildhawkLogger.php&lt;/i&gt; and not been made the default logger.&lt;br /&gt;
&lt;pre class="consoleOutput"&gt;phing -logger phing.listener.BuildhawkLogger&lt;/pre&gt;&lt;h4 class="custom"&gt;Looking at them Phing build times&lt;/h4&gt;Now it's time to switch to buildhawk and let it finally perform it's designated task, rendering an with the commit SHAs, commit messages, and build times fed &lt;a href="http://ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html"&gt;Erb&lt;/a&gt; template into an informative, viewable HTML page. To install it you simply have to run &lt;i&gt;sudo gem install buildhawk&lt;/i&gt; and you're good to go.&lt;br /&gt;
&lt;br /&gt;
The next console command shows the buildhawk call issued in the SUB's directory to produce it's build time report page.&lt;br /&gt;
&lt;pre class="consoleOutput"&gt;buildhawk --title 'Examplr' &amp;gt; examplr-build-times.html&lt;/pre&gt;The outro screenshot below gives you a peek at a rendered build time report.&lt;a href="http://www.flickr.com/photos/raphaelstolt/5192234632/" title="Buildhawk report for a Phing driven build von Raphael Stolt bei Flickr"&gt;&lt;img alt="Buildhawk report for a Phing driven build" border="0" height="270" src="http://farm5.static.flickr.com/4091/5192234632_736ff764e9.jpg" width="500" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/H5BeHt7-80I" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=5852289239698386107" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/5852289239698386107?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/5852289239698386107?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2010/11/measuring-displaying-phing-build-times.html" title="Measuring &amp; displaying Phing build times with buildhawk" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://farm5.static.flickr.com/4091/5192234632_736ff764e9_t.jpg" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;AkUCSXkyeCp7ImA9Wx9TEUw.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-1900391005162546906</id><published>2010-06-03T05:05:00.005+02:00</published><updated>2010-11-18T23:11:08.790+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-18T23:11:08.790+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="PHPUnit" /><title>Growling PHPUnit's test status</title><content type="html">&lt;a href="http://www.flickr.com/photos/raphaelstolt/4428732239/" title="PHPUnit Growl TestListener by Raphael Stolt, on Flickr"&gt;&lt;img alt="PHPUnit Growl TestListener" border="0" src="http://farm5.static.flickr.com/4022/4428732239_b35eefcbf0_o.gif" style="cursor: pointer; float: left; height: 110px; margin: 0pt 10px 10px 0pt; width: 268px;" title="PHPUnit Growl TestListener" /&gt;&lt;/a&gt;Two years ago I &lt;a href="http://raphaelstolt.blogspot.com/2008/04/hooking-growl-publisher-plugin-into.html" target="_self"&gt;blogged&lt;/a&gt; about a &lt;a href="http://code.google.com/p/xinc/" target="_self"&gt;Xinc&lt;/a&gt; (R.I.P?) plugin that &lt;a href="http://growl.info/" target="_self"&gt;growls&lt;/a&gt; each build status for any via Xinc continuously integrated project. Since I'm using &lt;a href="http://www.phpunit.de/" target="_self"&gt;PHPUnit&lt;/a&gt; more and more lately, especially in continuous testing sessions (sprints without hitting the continuous integration server), my dependence on a fast and more visual feedback loop rose. In this post I'll provide an easy solution that meets these requirements by utilizing PHPUnit's test listener feature.&lt;br /&gt;
&lt;br /&gt;
&lt;h4 class="custom"&gt;What's the motivation, yo?&lt;/h4&gt;While doing story or feature sprints embedded in a continuous testing approach I first used a combination of &lt;a href="http://gist.github.com/159470" target="_self"&gt;stakeout.rb&lt;/a&gt; and PHPUnit's --colors option to radiate the tests status, but soon wasn't that satisfied with the chosen route as it happened that the console window got superimposed with other opened windows (e.g. API Browser, TextMate etc.) especially on my 13,3" MacBook. &lt;br /&gt;
&lt;br /&gt;
To overcome this misery I decided to utilize PHPUnit's ability to write custom test listeners and to implement one that radiates the test status in a more prominent and sticky spot via Growl.&lt;br /&gt;
&lt;h4 class="custom"&gt;Implementing the Growl test listener&lt;/h4&gt;Similar to the ticket listener plugin mechanism I blogged about &lt;a href="http://raphaelstolt.blogspot.com/2010/01/closing-and-reopening-github-issues-via.html" target="_self"&gt;earlier&lt;/a&gt; PHPUnit also provides one for test listeners. This extension mechanism allows to bend the test result formatting and output to the given needs and scenarios a developer might face and therefore is a perfect match. &lt;br /&gt;
&lt;br /&gt;
To customize the test feedback and visualization the test listener has to implement the provided PHPUnit_Framework_Testlistener interface. A few keystrokes later I ended up with the next shown implementation, which is also available via a GitHub &lt;a href="http://gist.github.com/241303" target="_self"&gt;gist&lt;/a&gt;, supporting the previous stated requirements.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;&amp;lt;?php

class PHPUnit_Extensions_TestListener_GrowlTestListener 
    implements PHPUnit_Framework_Testlistener
{
    const TEST_RESULT_COLOR_RED = 'red';
    const TEST_RESULT_COLOR_YELLOW = 'yellow';
    const TEST_RESULT_COLOR_GREEN = 'green';
    
    private $_errors = array();
    private $_failures = array();
    private $_incompletes = array();
    private $_skips = array();
    private $_tests = array();
    private $_suites = array();
    private $_endedSuites = 0;
    private $_assertionCount = 0;
    private $_startTime = 0;

    private $_successPicturePath = null;
    private $_incompletePicturePath = null;
    private $_failurePicturePath = null;

    /**
     * @param string $successPicturePath
     * @param string $incompletePicturePath
     * @param string $failurePicturePath
     */
    public function __construct($successPicturePath, $incompletePicturePath, 
        $failurePicturePath)
    {
        $this-&amp;gt;_successPicturePath = $successPicturePath;
        $this-&amp;gt;_incompletePicturePath = $incompletePicturePath;
        $this-&amp;gt;_failurePicturePath = $failurePicturePath;
    }

    public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
    {
        $this-&amp;gt;_errors[] = $test-&amp;gt;getName();
    }
    
    public function addFailure(PHPUnit_Framework_Test $test, 
        PHPUnit_Framework_AssertionFailedError $e, $time) 
    {     
        $this-&amp;gt;_failures[] = $test-&amp;gt;getName();
    }
    
    public function addIncompleteTest(PHPUnit_Framework_Test $test, 
        Exception $e, $time)
    {
        $this-&amp;gt;_incompletes[] = $test-&amp;gt;getName();
    }
    
    public function addSkippedTest(PHPUnit_Framework_Test $test, 
        Exception $e, $time) 
    {
        $this-&amp;gt;_skips[] = $test-&amp;gt;getName();
    }
    
    public function startTest(PHPUnit_Framework_Test $test)
    {
    
    }
    
    public function endTest(PHPUnit_Framework_Test $test, $time) 
    { 
        $this-&amp;gt;_tests[] = array('name' =&amp;gt; $test-&amp;gt;getName(), 
            'assertions' =&amp;gt; $test-&amp;gt;getNumAssertions()
        );
        $this-&amp;gt;_assertionCount+= $test-&amp;gt;getNumAssertions();
    }
    
    public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        if (count($this-&amp;gt;_suites) === 0) {
            PHP_Timer::start();
        }
        $this-&amp;gt;_suites[] = $suite-&amp;gt;getName();
    }
    
    public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
    {
        $this-&amp;gt;_endedSuites++;
        
        if (count($this-&amp;gt;_suites) &amp;lt;= $this-&amp;gt;_endedSuites)
        {
            $testTime = PHP_Timer::secondsToTimeString(
                PHP_Timer::stop());

            if ($this-&amp;gt;_isGreenTestResult()) {
                $resultColor = self::TEST_RESULT_COLOR_GREEN;
            }
            if ($this-&amp;gt;_isRedTestResult()) {
                $resultColor = self::TEST_RESULT_COLOR_RED;
            }
            if ($this-&amp;gt;_isYellowTestResult()) {
                $resultColor = self::TEST_RESULT_COLOR_YELLOW;
            }

            $suiteCount = count($this-&amp;gt;_suites);
            $testCount = count($this-&amp;gt;_tests);
            $failureCount = count($this-&amp;gt;_failures);
            $errorCount = count($this-&amp;gt;_errors);
            $incompleteCount = count($this-&amp;gt;_incompletes);
            $skipCount = count($this-&amp;gt;_skips);

            $resultMessage = '';

            if ($suiteCount &amp;gt; 1) {
                $resultMessage.= "Suites: {$suiteCount}, ";
            }
            $resultMessage.= "Tests: {$testCount}, ";
            $resultMessage.= "Assertions: {$this-&amp;gt;_assertionCount}";

            if ($failureCount &amp;gt; 0) {
                $resultMessage.= ", Failures: {$failureCount}";
            } 

            if ($errorCount &amp;gt; 0) {
                $resultMessage.= ", Errors: {$errorCount}";
            }

            if ($incompleteCount &amp;gt; 0) {
                $resultMessage.= ", Incompletes: {$incompleteCount}";
            }

            if ($skipCount &amp;gt; 0) {
                $resultMessage.= ", Skips: {$skipCount}";
            }
            $resultMessage.= " in {$testTime}.";
            $this-&amp;gt;_growlnotify($resultColor, $resultMessage);
        }
    }

    /**
     * @param string $resultColor
     * @param string $message
     * @param string $sender The name of the application that sends the notification
     * @throws RuntimeException When growlnotify is not available
     */
    private function _growlnotify($resultColor, $message = null, $sender = 'PHPUnit')
    {
        if ($this-&amp;gt;_isGrowlnotifyAvailable() === false) {
            throw new RuntimeException('The growlnotify tool is not available');
        }
        $notificationImage = $this-&amp;gt;_getNotificationImageByResultColor(
            $resultColor);
        $command = "growlnotify -w -s -m '{$message}' "
                 . "-n '{$sender}' "
                 . "-p 2 --image {$notificationImage}";
        exec($command, $response, $return);
    }

    /**
     * @return boolean
     */
    private function _isGrowlnotifyAvailable()
    {
        exec('growlnotify -v', $reponse, $status);
        return ($status === 0);
    }

    /**
     * @param string $color 
     * @return string
     */
    private function _getNotificationImageByResultColor($color)
    {
        switch ($color) {
            case self::TEST_RESULT_COLOR_RED:
                return $this-&amp;gt;_failurePicturePath;
                break;
            case self::TEST_RESULT_COLOR_GREEN:
                return $this-&amp;gt;_successPicturePath;
                break;
            default:
                return $this-&amp;gt;_incompletePicturePath;
        }
    }

    /**
     * @return boolean
     */
    private function _isGreenTestResult()
    {
        return count($this-&amp;gt;_errors) === 0 &amp;&amp; 
               count($this-&amp;gt;_failures) === 0 &amp;&amp;
               count($this-&amp;gt;_incompletes) === 0 &amp;&amp;
               count($this-&amp;gt;_skips) === 0;
    }

    /**
     * @return boolean
     */
    private function _isRedTestResult()
    {
        return count($this-&amp;gt;_errors) &amp;gt; 0 ||
               count($this-&amp;gt;_failures) &amp;gt; 0;
    }

    /**
     * @return boolean
     */
    private function _isYellowTestResult()
    {
        return count($this-&amp;gt;_errors) === 0 &amp;&amp;
               count($this-&amp;gt;_failures) === 0 &amp;&amp;
               (count($this-&amp;gt;_incompletes) &amp;gt; 0 ||
                count($this-&amp;gt;_skips) &amp;gt; 0);
    }
}&lt;/pre&gt;&lt;h4 class="custom"&gt;Hooking the Growl test listener into the PHPUnit ecosystem&lt;/h4&gt;To make use of the just outlined test listener it's necessary to add an entry to PHPUnit's &lt;a href="http://www.phpunit.de/manual/current/en/appendixes.configuration.html" target="_self"&gt;XML configuration file&lt;/a&gt; telling PHPUnit which test listener class to utilize and where it's located in the file system. In a next step the &lt;a href="http://thelucid.com/files/autotest_images.zip" target="_parent"&gt;images&lt;/a&gt; for the three possible Growl notifications have to be added to the local file system, and as the Growl test listener constructor takes these as arguments they have also to be injected in the PHPUnit XML configuration file (i.e. &lt;i&gt;phpunit-offline.xml&lt;/i&gt;). Take a peek yourself how this is done in the next listing.&lt;br /&gt;
&lt;pre class="xmlSnippet"&gt;&amp;lt;phpunit backupGlobals="false"
         backupStaticAttributes="true"
         bootstrap="bootstrap.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="true"
         stopOnFailure="true"
         syntaxCheck="true"
         testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"&amp;gt;
  &amp;lt;testsuites&amp;gt; 
    &amp;lt;testsuite name="Zend_Service_GitHub Offline Testsuite"&amp;gt;
      &amp;lt;directory&amp;gt;Zend/Service/GitHub&amp;lt;/directory&amp;gt;
      &amp;lt;directory&amp;gt;Zend/Service&amp;lt;/directory&amp;gt;
    &amp;lt;/testsuite&amp;gt;
  &amp;lt;/testsuites&amp;gt;
  &amp;lt;groups&amp;gt;
    &amp;lt;include&amp;gt;
      &amp;lt;group&amp;gt;offline&amp;lt;/group&amp;gt;
    &amp;lt;/include&amp;gt;
  &amp;lt;/groups&amp;gt;
  &amp;lt;listeners&amp;gt;
    &amp;lt;listener class="PHPUnit_Extensions_TestListener_GrowlTestListener" 
              file="/Users/stolt/Work/GrowlTestListener.php"&amp;gt;
     &amp;lt;arguments&amp;gt;
       &amp;lt;string&amp;gt;$HOME/Pictures/pass.png&amp;lt;/string&amp;gt;
       &amp;lt;string&amp;gt;$HOME/Pictures/pending.png&amp;lt;/string&amp;gt;
       &amp;lt;string&amp;gt;$HOME/Pictures/fail.png&amp;lt;/string&amp;gt;
     &amp;lt;/arguments&amp;gt;
    &amp;lt;/listener&amp;gt;
  &amp;lt;/listeners&amp;gt;
&amp;lt;/phpunit&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Putting the Growl test listener to work&lt;/h4&gt;&lt;i&gt;Attention shameless plug!&lt;/i&gt; As an example application for a continuous testing session I chose a &lt;a href="http://framework.zend.com/" target="_self"&gt;Zend Framework&lt;/a&gt; Service component I'm currently working on. To set up the continuously testing workflow, stakeout.rb is still my #1 choice, but in a recent blog &lt;a href="http://www.negativemargins.com/2010/05/02/automaticlly-run-phpunit-tests/" target="_self"&gt;post&lt;/a&gt; Andy Stanberry shows another tool dubbed &lt;a href="http://github.com/alloy/kicker" target="_self"&gt;Kicker&lt;/a&gt; which seems to be coequal. The following console snippet shows in a concrete scenario how to utilize stakeout.rb to watch for any changes on the &lt;a href="http://github.com/raphaelstolt/github-api-client" target="_self"&gt;Zend_Service_GitHub&lt;/a&gt; component or it's backing tests which immediately trigger the test suite execution if one is detected.&lt;br /&gt;
&lt;pre class="consoleOutput"&gt;stakeout.rb 'phpunit --configuration phpunit-offline.xml' **/*.{php} ../Zend/**/*.php ../Zend/Service/**/*.php
&lt;/pre&gt;In the classic TDD cycle we start with a failing test. Creating the test, adding the assertions that the system under test (SUT) has to fulfill and saving the according test class automatically triggers the test suite execution which ends up in the next shown Growl notification.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/4665080708/" title="Growl notice for failed tests by Raphael Stolt, on Flickr"&gt;&lt;img alt="Growl notice for failed tests" border="0" height="95" src="http://farm2.static.flickr.com/1283/4665080708_dba552b811_b.jpg" width="510" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Nest a très important client call comes in and since we are clever, a quick TextMate shortcut marks the currently worked on test as incomplete. This step &lt;i&gt;might&lt;/i&gt; be a bit controversy as it's also suggested to leave the last worked on test broken, but I got to show you the pending/incomplete Growl notification ;D&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/4665080614/" title="Growl notice for incomplete tests by Raphael Stolt, on Flickr"&gt;&lt;img alt="Growl notice for incomplete tests" border="0" height="95" src="http://farm5.static.flickr.com/4002/4665080614_661b6ede54_b.jpg" width="510" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
After finishing the 'interruptive' client call aka context switch we can continue to work on the feature of the SUT until it fulfills the expected behavior which will be radiated via the next shown Growl notification. Happy Growl flavored testing!&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/4665080660/" title="Growl notice for successful tests by Raphael Stolt, on Flickr"&gt;&lt;img alt="Growl notice for successful tests" border="0" height="95" src="http://farm2.static.flickr.com/1297/4665080660_6e28e651bf_b.jpg" width="510" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;i&gt;* As you might notice in the shown Growl notification images there's a test suite count of 9 while we are only operating on a single one, this seems to be a possible PHPUnit &lt;a href="http://www.phpunit.de/ticket/1061" target="_self"&gt;bug&lt;/a&gt;, or just a misconfiguration of my testing environment. &lt;br /&gt;
&lt;br /&gt;
In case you got a solution for this problem feel free to add an illuminating comment.&lt;/i&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/O9-djBg_zFU" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=1900391005162546906" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/1900391005162546906?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/1900391005162546906?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2010/06/growling-phpunits-test-status.html" title="Growling PHPUnit's test status" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://farm2.static.flickr.com/1283/4665080708_dba552b811_t.jpg" height="72" width="72" /><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;CkQHSHc9eSp7ImA9WhdRE08.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-7560273028706822494</id><published>2010-05-15T07:53:00.002+02:00</published><updated>2011-08-02T23:25:39.961+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-02T23:25:39.961+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="NoSql" /><category scheme="http://www.blogger.com/atom/ns#" term="Redis" /><title>Installing the PHP redis extension on Mac OS X</title><content type="html">Recently I took a look at &lt;a href="http://code.google.com/p/redis/" target="_self"&gt;Redis&lt;/a&gt;, a popular and advanced key-value store. Peeking at the &lt;a href="http://code.google.com/p/redis/#Supported_languages" target="_self"&gt;supported languages&lt;/a&gt; section of the project's website you'll notice a lot of client libraries available for PHP. Two out of them caught my particular attention: &lt;a href="http://rediska.geometria-lab.net/" target="_self"&gt;Rediska&lt;/a&gt; due to it's impressive &lt;a href="http://framework.zend.com" target="_self"&gt;Zend Framework&lt;/a&gt; integration and &lt;a href="https://github.com/nicolasff/phpredis" target="_self"&gt;phpredis&lt;/a&gt; as it's a native PHP extension written in C and therefore supposed to be blazingly faster than vanilla PHP client libraries. The following blog post will show how to install and configure the aforementioned, native PHP extension on a Mac OS X system.&lt;br /&gt;
&lt;br /&gt;
The next steps assume that you've installed redis on your machine. In case you are using &lt;a href="http://www.macports.org" target="_self"&gt;MacPorts&lt;/a&gt; and haven't installed the key-value store yet, all it takes are the following two commands and you're good to go. In case you prefer &lt;a href="http://mxcl.github.com/homebrew/" target="_self"&gt;Homebrew&lt;/a&gt; for managing your package/software installations, there's also a Formula for redis &lt;a href="http://github.com/mxcl/homebrew/blob/master/Library/Formula/redis.rb" target="_self"&gt;available&lt;/a&gt; that allows you to install it via &lt;em&gt;brew install redis&lt;/em&gt;.&lt;pre class="consoleOutput"&gt;sudo port install redis
sudo launchctl load -w /Library/LaunchDaemons/org.macports.redis.plist&lt;/pre&gt;The very first step for building the native PHP redis extension is to get the source code by cloning the GitHub &lt;a href="http://github.com/owlient/phpredis/tree/master" target="_self"&gt;repository&lt;/a&gt; of the extension without it's history revisions.&lt;pre class="consoleOutput"&gt;mkdir phpredis-build
cd phpredis-build
git clone --depth 1 git://github.com/nicolasff/phpredis.git
cd phpredis&lt;/pre&gt;The next task is to compile the extension with the following batch of commands.&lt;pre class="consoleOutput"&gt;phpize
./configure
make
sudo make install&lt;/pre&gt;The next to last step is to alternate your php.ini, use &lt;em&gt;php --ini | grep 'Loaded'&lt;/em&gt; to get the location of it on your system, so that the redis module/extension is available to your PHP ecosystem. Therefor simply add &lt;em&gt;extension=redis.so&lt;/em&gt; in the &lt;em&gt;Dynamic Extensions&lt;/em&gt; section of your php.ini. Afterwards you can verify that the redis module is loaded and available via one of the following commands.&lt;pre class="consoleOutput"&gt;php -m | grep redis
php -i | grep 'Redis Support'&lt;/pre&gt;To make the extension also available to the running Apache PHP module you'll need to restart the Apache server. Looking at &lt;em&gt;phpinfo()&lt;/em&gt;'s output in a browser you should see the entry shown in the next image.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/4607738861/" title="Enabled redis extension by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm4.static.flickr.com/3320/4607738861_d24901cd7d_o.gif" width="608" height="199" border="0" alt="Enabled redis extension" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
For testing the communication between the just installed redis extension and the running Redis server, I further created a simple test script called &lt;a href="http://gist.github.com/402018" target="_self"&gt;redis-glue-test.php&lt;/a&gt; you can fetch from GitHub and run via the next commands.&lt;pre class="consoleOutput"&gt;curl -s http://gist.github.com/raw/402018/redis-glue-test.php -o redis-glue-test.php
php redis-glue-test.php&lt;/pre&gt;When you see the following shown console output you're good to go. Happy Redising!&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/4608312060/" title="Output of redis-glue-test.php by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm2.static.flickr.com/1295/4608312060_9de6daae3f_o.gif" width="627" height="304" border="0" alt="Output of redis-glue-test.php" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/UxCC3Emi53A" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=7560273028706822494" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7560273028706822494?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7560273028706822494?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2010/05/installing-php-redis-extension-on-mac.html" title="Installing the PHP redis extension on Mac OS X" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>4</thr:total></entry><entry gd:etag="W/&quot;DEUHRnc7fip7ImA9WxFUE0Q.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-6041005483752087407</id><published>2010-03-16T22:32:00.009+01:00</published><updated>2010-06-24T18:17:17.906+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-06-24T18:17:17.906+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="MongoDb" /><title>Using MongoHq in Zend Framework based applications</title><content type="html">&lt;a href="http://www.flickr.com/photos/raphaelstolt/4429498884/" title="MongoHq logo"&gt;&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 140px; height: 150px;" src="http://farm5.static.flickr.com/4056/4429498884_d39554767c_o.gif" alt="MongoHq logo" title="MongoHq logo" border="0" /&gt;&lt;/a&gt;As the name slightly foreshadows &lt;a href="https://app.mongohq.com/home" target="_self"&gt;MongoHq&lt;/a&gt; is a &lt;em&gt;currently&lt;/em&gt; bit pricey cloud-based hosting solution for &lt;a href="http://www.mongodb.org" target="_self"&gt;MongoDb&lt;/a&gt; databases provided by &lt;a href="http://www.commonthread.com" target="_self"&gt;CommonThread&lt;/a&gt;. Since they went live a few weeks ago I signed up for the small plan and started to successfully re-thinker with it in an exploratory &lt;a href="http://framework.zend.com" target="_self"&gt;Zend Framework&lt;/a&gt; based application. &lt;br /&gt;&lt;br /&gt;Therefore the following post will show how to bootstrap such an instance into a Zend Framework based application and how to use it from there in some simple scenarios like storing data coming from a Zend_Form into a designated collection and vice versa fetching it from there.&lt;br /&gt;&lt;br /&gt;&lt;h4 class="custom"&gt;Bootstrapping a MongoHq enabled connection&lt;/h4&gt;To establish and make the MongoDb connection application-wide available the almighty &lt;a href="http://framework.zend.com/manual/en/zend.application.html" target="_self"&gt;Zend_Application&lt;/a&gt; component came to the rescue again. After reading Matthew Weier O'Phinney's enlightening blog &lt;a href="http://weierophinney.net/matthew/archives/231-Creating-Re-Usable-Zend_Application-Resource-Plugins.html" target="_self"&gt;post&lt;/a&gt; about creating re-usable Zend_Application resource plugins and deciding to use MongoDb in some more exploratory projects, I figured it would be best to create such a plugin and ditch the also possible resource method approach. &lt;br /&gt;&lt;br /&gt;The next code listing shows a possible implementation of the MongoDb resource plugin initializing a &lt;a href="http://php.net/manual/en/class.mongo.php" target="_self"&gt;Mongo&lt;/a&gt; instance for the given &lt;em&gt;APPLICATION_ENV&lt;/em&gt; (i.e. production) mode. &lt;br /&gt;&lt;br /&gt;For the other application environment modes (development | testing | staging) it's currently assumed that no database authentication is enabled, which is also the default when using MongoDb, so you might need to adapt the plugin to your differing needs; and since I'm currently only rolling on the &lt;a href="https://app.mongohq.com/signup" target="_self"&gt;small&lt;/a&gt; plan the support for multiple databases is also not accounted for.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 265px;"&gt;library/Recordshelf/Resource/MongoDb.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class Recordshelf_Resource_MongoDb &lt;br /&gt;    extends Zend_Application_Resource_ResourceAbstract&lt;br /&gt;{&lt;br /&gt;    /**&lt;br /&gt;     * Definable Mongo options.&lt;br /&gt;     *&lt;br /&gt;     * @var array&lt;br /&gt;     */&lt;br /&gt;    protected $_options = array(&lt;br /&gt;        'hostname'     =&amp;gt; '127.0.0.1',&lt;br /&gt;        'port'         =&amp;gt; '27017',&lt;br /&gt;        'username'     =&amp;gt; null,&lt;br /&gt;        'password'     =&amp;gt; null,&lt;br /&gt;        'databasename' =&amp;gt; null,&lt;br /&gt;        'connect'      =&amp;gt; true&lt;br /&gt;    );&lt;br /&gt;    /**&lt;br /&gt;     * Initalizes a Mongo instance.&lt;br /&gt;     *&lt;br /&gt;     * @return Mongo&lt;br /&gt;     * @throws Zend_Exception&lt;br /&gt;     */&lt;br /&gt;    public function init()&lt;br /&gt;    {&lt;br /&gt;        $options = $this-&amp;gt;getOptions();&lt;br /&gt;&lt;br /&gt;        if (null !== $options['username'] &amp;&amp; &lt;br /&gt;            null !== $options['password'] &amp;&amp;&lt;br /&gt;            null !== $options['databasename'] &amp;&amp;&lt;br /&gt;            'production' === APPLICATION_ENV) {&lt;br /&gt;            // Database Dns with MongoHq credentials&lt;br /&gt;            $mongoDns = sprintf('mongodb://%s:%s@%s:%s/%s',&lt;br /&gt;                $options['username'],&lt;br /&gt;                $options['password'],&lt;br /&gt;                $options['hostname'],&lt;br /&gt;                $options['port'],&lt;br /&gt;                $options['databasename']&lt;br /&gt;            );&lt;br /&gt;        } elseif ('production' !== APPLICATION_ENV) {&lt;br /&gt;            $mongoDns = sprintf('mongodb://%s:%s/%s',&lt;br /&gt;                $options['hostname'],&lt;br /&gt;                $options['port'],&lt;br /&gt;                $options['databasename']&lt;br /&gt;            );&lt;br /&gt;        } else {&lt;br /&gt;            $exceptionMessage = sprintf(&lt;br /&gt;                'Recource %s is not configured correctly',&lt;br /&gt;                __CLASS__&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        try {&lt;br /&gt;            return new Mongo($mongoDns, array('connect' =&amp;gt; $options['connect']));&lt;br /&gt;        } catch (MongoConnectionException $e) {&lt;br /&gt;            throw new Zend_Exception($e-&amp;gt;getMessage());&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;With the MongoDb resource plugin in the place to be, it's time to make it known to the boostrapping mechanism which is done by registering the resource plugin in the application.ini. &lt;br /&gt;&lt;br /&gt;Further the MongoHq credentials, which are available in the &lt;em&gt;MongoHq &gt; My Database&lt;/em&gt; section, and the main database name are added to the configuration file which will be used to set the definable resource plugin ($_)options and to connect to the hosted database.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 205px;"&gt;application/configs/application.ini&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&lt;br /&gt;[production]&lt;br /&gt;pluginPaths.Recordshelf_Resource = "Recordshelf/Resource"&lt;br /&gt;resources.mongodb.username = __MONGOHQ_USERNAME__&lt;br /&gt;resources.mongodb.password = __MONGOHQ_PASSWORD__&lt;br /&gt;resources.mongodb.hostname = __MONGOHQ_HOSTNAME__&lt;br /&gt;resources.mongodb.port = __MONGOHQ_PORT__&lt;br /&gt;resources.mongodb.databasename = __MONGOHQ_DATABASENAME__&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Cloudifying documents into collections&lt;/h4&gt;Having the MongoHq enabled connection in the bootstrapping mechanism it can now be picked up from there and used in any Zend Framework application context. &lt;br /&gt;&lt;br /&gt;The example action method (i.e. proposeAction) assumes data (i.e. a tech talk proposal to revive the &lt;strong&gt;example&lt;/strong&gt; domain from my &lt;a href="http://raphaelstolt.blogspot.com/2010/02/utilizing-twitter-lists-with.html" target="_self"&gt;last&lt;/a&gt; blog post) coming from a &lt;a href="http://framework.zend.com/manual/en/zend.form.html" target="_self"&gt;Zend_Form&lt;/a&gt; which will be stored in a collection named &lt;em&gt;proposals&lt;/em&gt;, a table in &lt;em&gt;old&lt;/em&gt; relational database think. &lt;br /&gt;&lt;br /&gt;The next code listings states the action method innards to do so by injecting the valid form values into a model class which provides accessors and mutators for the domain model's properties and can transform them into a proposal document aka an array structure.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 275px;"&gt;application/controllers/ProposalController.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class ProposalController extends Zend_Controller_Action&lt;br /&gt;{&lt;br /&gt;    public function indexAction()&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;view-&amp;gt;form = new Recordshelf_Form_Proposal();&lt;br /&gt;    }&lt;br /&gt;    public function thanksAction()&lt;br /&gt;    {&lt;br /&gt;    }&lt;br /&gt;    public function proposeAction()&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_helper-&amp;gt;viewRenderer-&amp;gt;setNoRender();&lt;br /&gt;        $form = new Recordshelf_Form_Proposal();&lt;br /&gt;        &lt;br /&gt;        $request = $this-&amp;gt;getRequest();&lt;br /&gt;        &lt;br /&gt;        if ($this-&amp;gt;getRequest()-&amp;gt;isPost()) {&lt;br /&gt;            if ($form-&amp;gt;isValid($request-&amp;gt;getPost())) {&lt;br /&gt;                $model = new Recordshelf_Model_Proposal($form-&amp;gt;getValues());&lt;br /&gt;                $mapper = new Recordshelf_Model_ProposalMapper();&lt;br /&gt;                if ($mapper-&amp;gt;insert($model)) {&lt;br /&gt;                    return $this-&amp;gt;_helper-&amp;gt;redirector('thanks');&lt;br /&gt;                }&lt;br /&gt;                $this-&amp;gt;view-&amp;gt;form = $form;&lt;br /&gt;                return $this-&amp;gt;render('index');&lt;br /&gt;            } else {&lt;br /&gt;                $this-&amp;gt;view-&amp;gt;form = $form;&lt;br /&gt;                return $this-&amp;gt;render('index');&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;Next the model/data mappper is initialized, which triggers the picking of the MongoHq enabled Mongo connection instance and the auto-determination of the collection name to use based on the mapper's class name. Subsequently the populated model instance is passed into the mappper's &lt;em&gt;insert&lt;/em&gt; method which is pulling the document (array structure) and doing the actual insert into the &lt;em&gt;proposals&lt;/em&gt; collection. &lt;br /&gt;&lt;br /&gt;To give you an idea of the actual document structure it's shown in the next listing, followed by the model/data mapper implementation.&lt;pre class="codeSnippet"&gt;Array&lt;br /&gt;(&lt;br /&gt;    [state] =&amp;gt; new&lt;br /&gt;    [created] =&amp;gt; MongoDate Object&lt;br /&gt;        (&lt;br /&gt;            [sec] =&amp;gt; 1268774242&lt;br /&gt;            [usec] =&amp;gt; 360831&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [submitee] =&amp;gt; Array&lt;br /&gt;        (&lt;br /&gt;            [title] =&amp;gt; Mr&lt;br /&gt;            [firstname] =&amp;gt; John&lt;br /&gt;            [familyname] =&amp;gt; Doe&lt;br /&gt;            [email] =&amp;gt; john.doe@gmail.com&lt;br /&gt;            [twitter] =&amp;gt; johndoe&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    [title] =&amp;gt; How to get a real name&lt;br /&gt;    [description] =&amp;gt; Some descriptive text...&lt;br /&gt;    [topictags] =&amp;gt; Array&lt;br /&gt;        (&lt;br /&gt;            [0] =&amp;gt; John&lt;br /&gt;            [1] =&amp;gt; Doe&lt;br /&gt;            [2] =&amp;gt; Anonymous&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 240px;"&gt;application/models/ProposalMapper.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class Recordshelf_Model_ProposalMapper&lt;br /&gt;{&lt;br /&gt;    private $_mongo;&lt;br /&gt;    private $_collection;&lt;br /&gt;    private $_databaseName;&lt;br /&gt;    private $_collectionName;&lt;br /&gt;&lt;br /&gt;    public function __construct()&lt;br /&gt;    {&lt;br /&gt;        $frontController = Zend_Controller_Front::getInstance();&lt;br /&gt;        $this-&amp;gt;_mongo = $frontController-&amp;gt;getParam('bootstrap')&lt;br /&gt;                                        -&amp;gt;getResource('mongoDb');&lt;br /&gt;        $config       = $frontController-&amp;gt;getParam('bootstrap')&lt;br /&gt;                                        -&amp;gt;getResource('config');&lt;br /&gt;        &lt;br /&gt;        $this-&amp;gt;_databaseName   = $config-&amp;gt;resources-&amp;gt;mongodb-&amp;gt;get('databasename');&lt;br /&gt;        &lt;br /&gt;        $replaceableClassNameparts = array(&lt;br /&gt;            'recordshelf_model_', &lt;br /&gt;            'mapper'&lt;br /&gt;        ); &lt;br /&gt;        $this-&amp;gt;_collectionName = str_replace($replaceableClassNameparts, '',&lt;br /&gt;            strtolower(__CLASS__) . 's');&lt;br /&gt;&lt;br /&gt;        $this-&amp;gt;_collection = $this-&amp;gt;_mongo-&amp;gt;selectCollection(&lt;br /&gt;            $this-&amp;gt;_databaseName, &lt;br /&gt;            $this-&amp;gt;_collectionName&lt;br /&gt;        );&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Inserts a proposal document/model into the proposals collection.&lt;br /&gt;     *&lt;br /&gt;     * @param  Recordshelf_Model_Proposal $proposal The proposal document/model.&lt;br /&gt;     * @return MongoId&lt;br /&gt;     * @throws Zend_Exception&lt;br /&gt;     */&lt;br /&gt;    public function insert(Recordshelf_Model_Proposal $proposal)&lt;br /&gt;    {&lt;br /&gt;        $proposalDocument = $proposal-&amp;gt;getValues();&lt;br /&gt;        try {&lt;br /&gt;            if ($this-&amp;gt;_collection-&amp;gt;insert($proposalDocument, true)) {&lt;br /&gt;                return $proposalDocument['_id'];&lt;br /&gt;            }&lt;br /&gt;        } catch (MongoCursorException $mce) {&lt;br /&gt;            throw new Zend_Exception($mce-&amp;gt;getMessage());&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Querying and retrieving the cloudified data&lt;/h4&gt;As what comes in must come out, the next interaction with the Document Database Management System (DocDBMS) is about retrieving some afore-stored talk proposal documents from the collection so they can be rendered to the application's user. This isn't really MongoHq specific anymore, like most of the previous model parts, and is just here to round up this blog post and use some more of that MongoDb goodness. &lt;em&gt;Looks like I have to look for an anonymous self-help group that stuff is highly addictive.&lt;/em&gt; &lt;br /&gt;&lt;br /&gt;Anyway the next listing shows the action method fetching all stored documents available in the &lt;em&gt;proposals&lt;/em&gt; collection. To save some CO2 on this blog post all documents are fetched, which ends up in the most trivial query but as you can figure the example domain provides a bunch of query examples like only proposals for a given topic tag, specific talk title or a given proposal state which can be easily created via passed-through Http request parameters.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 275px;"&gt;application/controllers/ProposalController.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class ProposalController extends Zend_Controller_Action&lt;br /&gt;{&lt;br /&gt;    ...&lt;br /&gt;    &lt;br /&gt;    public function listAction()&lt;br /&gt;    {&lt;br /&gt;        $mapper = new Recordshelf_Model_ProposalMapper();&lt;br /&gt;        $proposals = $mapper-&amp;gt;fetchAll();&lt;br /&gt;        // For iterating the Recordshelf_Model_Proposal's in the view&lt;br /&gt;        $this-&amp;gt;view-&amp;gt;proposals = $proposals;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;The last code listing shows the above used &lt;em&gt;fetchAll&lt;/em&gt; method of the data mapper class returning an array of stored proposal documents mapped to their domain model (i.e. Recordshelf_Model_Proposal) in the application.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 240px;"&gt;application/models/ProposalMapper.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class Recordshelf_Model_ProposalMapper&lt;br /&gt;{&lt;br /&gt;    ...&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Fetches all stored talk proposals.&lt;br /&gt;     *&lt;br /&gt;     * @return array&lt;br /&gt;     */&lt;br /&gt;    public function fetchAll()&lt;br /&gt;    {&lt;br /&gt;        $cursor = $this-&amp;gt;_collection-&amp;gt;find();&lt;br /&gt;        $proposals = array();&lt;br /&gt;        &lt;br /&gt;        foreach ($cursor as $documents) {&lt;br /&gt;            $proposal = new Recordshelf_Model_Proposal();&lt;br /&gt;            foreach ($documents as $property =&amp;gt; $value) {&lt;br /&gt;                if ('submitee' === $property) {&lt;br /&gt;                    $proposal-&amp;gt;submitee = new Recordshelf_Model_Submitee($value);&lt;br /&gt;                } else {&lt;br /&gt;                    $proposal-&amp;gt;$property = $value;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            $proposals[] = $proposal;&lt;br /&gt;        }&lt;br /&gt;        return $proposals;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/KB_q-B4jrzY" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=6041005483752087407" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6041005483752087407?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6041005483752087407?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2010/03/using-mongohq-in-zend-framework-based.html" title="Using MongoHq in Zend Framework based applications" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;CEIFQXk7fip7ImA9WxBWE04.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-4028008204091903175</id><published>2010-02-05T02:05:00.002+01:00</published><updated>2010-02-05T02:15:10.706+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-05T02:15:10.706+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><title>Utilizing Twitter lists with Zend_Service_Twitter</title><content type="html">&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 280px; height: 176px;" src="http://farm3.static.flickr.com/2727/4329909679_49489b5345_o.gif" alt="Twitter lists with the Zend Framework" border="0" /&gt;Several months ago &lt;a href="http://twitter.com/" target="_self"&gt;Twitter&lt;/a&gt; added the list feature to it's public &lt;a href="http://apiwiki.twitter.com/Twitter-API-Documentation" target="_self"&gt;API&lt;/a&gt;. While debating some use cases for an event registration application I stumbled upon an interesting feature, which adds participants automatically to a Twitter list upon registration. This way registered and interested users can discover like-minded individuals and get in touch prior to any pre-social event activities. This post will show how this feature can be implemented by utilizing the &lt;a href="http://framework.zend.com/manual/en/zend.service.twitter.html" target="_self"&gt;Zend_Service_Twitter&lt;/a&gt; component, and how it then can be used in a &lt;a href="http://framework.zend.com" target="_self"&gt;Zend Framework&lt;/a&gt; based application.&lt;h4 class="custom"&gt;Implementing the common list features&lt;/h4&gt;Looking at the three relevant parts of the Twitter list API some common features emerged and had to be supported to get the feature out of the door. These are namely the creation, deletion of new lists and the addition, removal of list members (i.e. event participants). Since the current Twitter component doesn't support these list operations out of the box it was time to put that develeoper hat on and get loose; which was actually a joy due to the elegance of the extended Zend_Service_Twitter component laying all the groundwork. &lt;br /&gt;&lt;br /&gt;A non-feature-complete implementation is shown in the next code listing and can alternatively be &lt;a href="http://github.com/raphaelstolt/zf-hacks/blob/master/Recordshelf/Service/Twitter/List.php" target="_self"&gt;pulled&lt;/a&gt; from &lt;a href="http://github.com/" target="_self"&gt;GitHub&lt;/a&gt;. Currently it only supports the above stated common operations plus the ability to get the lists of a Twitter account and it's associated members; but feel free to fork it or even turn it into an official proposal.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;require_once 'Zend/Service/Twitter.php';&lt;br /&gt;require_once 'Zend/Service/Twitter/Exception.php';&lt;br /&gt;&lt;br /&gt;class Recordshelf_Service_Twitter_List extends Zend_Service_Twitter&lt;br /&gt;{&lt;br /&gt;    const LIST_MEMBER_LIMIT = 500;&lt;br /&gt;    const MAX_LIST_NAME_LENGTH = 25;&lt;br /&gt;    const MAX_LIST_DESCRIPTION_LENGTH = 100;&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * Initializes the service and adds the list to the method types &lt;br /&gt;     * of the parent service class.&lt;br /&gt;     *&lt;br /&gt;     * @param string $username The Twitter account name.&lt;br /&gt;     * @param string $password The Twitter account password.&lt;br /&gt;     * @see Zend_Service_Twitter::_methodTypes&lt;br /&gt;     */&lt;br /&gt;    public function __construct($username = null, $password = null)&lt;br /&gt;    {&lt;br /&gt;        parent::__construct($username, $password);&lt;br /&gt;        $this-&amp;gt;_methodTypes[] = 'list';&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Creates a list associated to the current user.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The listname to create.&lt;br /&gt;     * @param array $options The options to set whilst creating the list. &lt;br /&gt;     * Allows to set the list creation mode (public|private) &lt;br /&gt;     * and the list description.&lt;br /&gt;     * @return Zend_Rest_Client_Result&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    public function create($listname, array $options = array())&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_init();&lt;br /&gt;        &lt;br /&gt;        if ($this-&amp;gt;_existsListAlready($listname)) {&lt;br /&gt;            $exceptionMessage = 'List with name %s exists already';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, $listname);&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $_options = array('name' =&amp;gt; $this-&amp;gt;_validListname($listname));&lt;br /&gt;        foreach ($options as $key =&amp;gt; $value) {&lt;br /&gt;            switch (strtolower($key)) {&lt;br /&gt;                case 'mode':&lt;br /&gt;                    $_options['mode'] = $this-&amp;gt;_validMode($value);&lt;br /&gt;                    break;&lt;br /&gt;                case 'description':&lt;br /&gt;                    $_options['description'] = $this-&amp;gt;_validDescription($value);&lt;br /&gt;                    break;&lt;br /&gt;                default:&lt;br /&gt;                    break;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        $path = '/1/%s/lists.xml';&lt;br /&gt;        $path = sprintf($path, $this-&amp;gt;getUsername());&lt;br /&gt;        &lt;br /&gt;        $response = $this-&amp;gt;_post($path, $_options);&lt;br /&gt;        return new Zend_Rest_Client_Result($response-&amp;gt;getBody());&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Deletes an owned list of the current user.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The listname to delete.&lt;br /&gt;     * @return Zend_Rest_Client_Result&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    public function delete($listname)&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_init();&lt;br /&gt;        &lt;br /&gt;        if (!$this-&amp;gt;_isListAssociatedWithUser($listname)) {&lt;br /&gt;            $exceptionMessage = 'List %s is not associate with user %s ';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, &lt;br /&gt;                $listname, &lt;br /&gt;                $this-&amp;gt;getUsername()&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        $_options['_method'] = 'DELETE';&lt;br /&gt;        $path = '/1/%s/lists/%s.xml';&lt;br /&gt;        $path = sprintf($path, &lt;br /&gt;            $this-&amp;gt;getUsername(), &lt;br /&gt;            $this-&amp;gt;_validListname($listname)&lt;br /&gt;        );&lt;br /&gt;        $response = $this-&amp;gt;_post($path, $_options);&lt;br /&gt;        return new Zend_Rest_Client_Result($response-&amp;gt;getBody());&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Adds a member to a list of the current user.&lt;br /&gt;     *&lt;br /&gt;     * @param integer $userId The numeric user id of the member to add.&lt;br /&gt;     * @param string $listname The listname to add the member to.&lt;br /&gt;     * @return Zend_Rest_Client_Result&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    public function addMember($userId, $listname)&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_init();&lt;br /&gt;        &lt;br /&gt;        if (!$this-&amp;gt;_isListAssociatedWithUser($listname)) {&lt;br /&gt;            $exceptionMessage = 'List %s is not associate with user %s ';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, &lt;br /&gt;                $listname, &lt;br /&gt;                $this-&amp;gt;getUsername()&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $_options['id'] = $this-&amp;gt;_validInteger($userId); &lt;br /&gt;        $path = '/1/%s/%s/members.xml';&lt;br /&gt;        $path = sprintf($path, &lt;br /&gt;            $this-&amp;gt;getUsername(), &lt;br /&gt;            $this-&amp;gt;_validListname($listname)&lt;br /&gt;        );&lt;br /&gt;        &lt;br /&gt;        if ($this-&amp;gt;_isListMemberLimitReached($listname)) {&lt;br /&gt;            $exceptionMessage = 'List can contain no more than %d members';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, &lt;br /&gt;                self::LIST_MEMBER_LIMIT&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $response = $this-&amp;gt;_post($path, $_options);&lt;br /&gt;        return new Zend_Rest_Client_Result($response-&amp;gt;getBody());&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Removes a member from a list of the current user.&lt;br /&gt;     *&lt;br /&gt;     * @param integer $userId The numeric user id of the member to remove.&lt;br /&gt;     * @param string $listname The listname to remove the member from.&lt;br /&gt;     * @return Zend_Rest_Client_Result&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    public function removeMember($userId, $listname)&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_init();&lt;br /&gt;        &lt;br /&gt;        if (!$this-&amp;gt;_isListAssociatedWithUser($listname)) {&lt;br /&gt;            $exceptionMessage = 'List %s is not associate with user %s ';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, &lt;br /&gt;                $listname, &lt;br /&gt;                $this-&amp;gt;getUsername()&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $_options['_method'] = 'DELETE';&lt;br /&gt;        $_options['id'] = $this-&amp;gt;_validInteger($userId);        &lt;br /&gt;        $path = '/1/%s/%s/members.xml';&lt;br /&gt;        $path = sprintf($path, &lt;br /&gt;            $this-&amp;gt;getUsername(), &lt;br /&gt;            $this-&amp;gt;_validListname($listname)&lt;br /&gt;        );&lt;br /&gt;        $response = $this-&amp;gt;_post($path, $_options);&lt;br /&gt;        return new Zend_Rest_Client_Result($response-&amp;gt;getBody());&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Fetches the list members of the current user.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The listname to fetch members from.&lt;br /&gt;     * @return Zend_Rest_Client_Result&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    public function getMembers($listname) {&lt;br /&gt;        $this-&amp;gt;_init();&lt;br /&gt;        $path = '/1/%s/%s/members.xml';&lt;br /&gt;        $path = sprintf($path, &lt;br /&gt;            $this-&amp;gt;getUsername(), &lt;br /&gt;            $this-&amp;gt;_validListname($listname)&lt;br /&gt;        );&lt;br /&gt;        $response = $this-&amp;gt;_get($path);&lt;br /&gt;        return new Zend_Rest_Client_Result($response-&amp;gt;getBody());&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Fetches the list of the current user or any given user.&lt;br /&gt;     *&lt;br /&gt;     * @param string $username The username of the list owner.&lt;br /&gt;     * @return Zend_Rest_Client_Result&lt;br /&gt;     */&lt;br /&gt;    public function getLists($username = null)&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_init();&lt;br /&gt;        $path = '/1/%s/lists.xml';&lt;br /&gt;        if (is_null($username)) {&lt;br /&gt;            $path = sprintf($path, $this-&amp;gt;getUsername());&lt;br /&gt;        } else {&lt;br /&gt;            $path = sprintf($path, $username);&lt;br /&gt;        }&lt;br /&gt;        $response = $this-&amp;gt;_get($path);&lt;br /&gt;        return new Zend_Rest_Client_Result($response-&amp;gt;getBody());&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Checks if the list exists already to avoid number &lt;br /&gt;     * indexed recreations.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The list name.&lt;br /&gt;     * @return boolean&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    private function _existsListAlready($listname)&lt;br /&gt;    {&lt;br /&gt;        $_listname = $this-&amp;gt;_validListname($listname);&lt;br /&gt;        $lists = $this-&amp;gt;getLists();&lt;br /&gt;        $_lists = $lists-&amp;gt;lists;&lt;br /&gt;        foreach ($_lists-&amp;gt;list as $list) {&lt;br /&gt;            if ($list-&amp;gt;name == $_listname) {&lt;br /&gt;                return true;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Checks if the list is associated with the current user.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The list name.&lt;br /&gt;     * @return boolean&lt;br /&gt;     */&lt;br /&gt;    private function _isListAssociatedWithUser($listname) &lt;br /&gt;    {&lt;br /&gt;        return $this-&amp;gt;_existsListAlready($listname);&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Checks if the list member limit is reached.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The list name.&lt;br /&gt;     * @return boolean&lt;br /&gt;     */&lt;br /&gt;    private function _isListMemberLimitReached($listname)&lt;br /&gt;    {&lt;br /&gt;        $members = $this-&amp;gt;getMembers($listname);&lt;br /&gt;        return self::LIST_MEMBER_LIMIT &amp;lt; count($members-&amp;gt;users-&amp;gt;user);&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Returns the list creation mode or returns the private mode when invalid.&lt;br /&gt;     * Valid values are private or public.&lt;br /&gt;     *&lt;br /&gt;     * @param string $creationMode The list creation mode.&lt;br /&gt;     * @return string&lt;br /&gt;     */&lt;br /&gt;    private function _validMode($creationMode)&lt;br /&gt;    {&lt;br /&gt;        if (in_array($creationMode, array('private', 'public'))) {&lt;br /&gt;            return $creationMode;&lt;br /&gt;        }&lt;br /&gt;        return 'private';&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Returns the list name or throws an Exception when invalid.&lt;br /&gt;     *&lt;br /&gt;     * @param string $listname The list name.&lt;br /&gt;     * @return string&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    private function _validListname($listname)&lt;br /&gt;    {&lt;br /&gt;        $len = iconv_strlen(trim($listname), 'UTF-8');&lt;br /&gt;        if (0 == $len) {&lt;br /&gt;            $exceptionMessage = 'List name must contain at least one character';&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        } elseif (self::MAX_LIST_NAME_LENGTH &amp;lt; $len) {&lt;br /&gt;            $exceptionMessage = 'List name must contain no more than %d characters';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, &lt;br /&gt;                self::MAX_LIST_NAME_LENGTH&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        return trim($listname);&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Returns the list description or throws an Exception when invalid.&lt;br /&gt;     *&lt;br /&gt;     * @param string $description The list description.&lt;br /&gt;     * @return string&lt;br /&gt;     * @throws Zend_Service_Twitter_Exception&lt;br /&gt;     */&lt;br /&gt;    private function _validDescription($description)&lt;br /&gt;    {&lt;br /&gt;        $len = iconv_strlen(trim($description), 'UTF-8');&lt;br /&gt;        if (0 == $len) {&lt;br /&gt;            return '';&lt;br /&gt;        } elseif (self::MAX_LIST_DESCRIPTION_LENGTH &amp;lt; $len) {&lt;br /&gt;            $exceptionMessage = 'List description must contain no more than %d characters';&lt;br /&gt;            $exceptionMessage = sprintf($exceptionMessage, &lt;br /&gt;                self::MAX_LIST_DESCRIPTION_LENGTH&lt;br /&gt;            );&lt;br /&gt;            throw new Zend_Service_Twitter_Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        return trim(strip_tags($description));&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Adding the 'auto list' feature&lt;/h4&gt;For using the above implemented &lt;em&gt;add member&lt;/em&gt; feature it's assumed that a participant has provided a valid and existing Twitter username, his approval of being added to the event list (i.e. zfweekend) and that he further has been registered effectively. To have the name of the Twitter list to act on and the account credentials available corresponding configuration entries are set as shown next.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 205px;"&gt;application/configs/application.ini&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&lt;br /&gt;[production]&lt;br /&gt;twitter.username = __USERNAME__&lt;br /&gt;twitter.password = __PASSWORD__&lt;br /&gt;twitter.auto.listname = zfweekend&lt;/pre&gt;With the Twitter credentials and the list name available it's now possible to pull this feature into the register method of the register Action Controller, where it's applied as shown in the outro listing. As you will see, besides some bad practices due to demonstration purposes, the register Form makes use of a custom &lt;em&gt;TwitterScreenName&lt;/em&gt; &lt;a href="http://framework.zend.com/manual/en/zend.validate.html" target="_self"&gt;validator&lt;/a&gt; and &lt;a href="http://framework.zend.com/manual/en/zend.filter.html" target="_self"&gt;filter&lt;/a&gt; which are also &lt;a href="http://github.com/raphaelstolt/zf-hacks/tree/master/Recordshelf/" target="_self"&gt;available&lt;/a&gt; via GitHub. Happy Twitter listing!&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class RegisterController extends Zend_Controller_Action&lt;br /&gt;{   &lt;br /&gt;    /**&lt;br /&gt;     * @badpractice Push this into a specific Form class.&lt;br /&gt;     * @return Zend_Form&lt;br /&gt;     */&lt;br /&gt;    private function _getForm()&lt;br /&gt;    {&lt;br /&gt;        $form = new Zend_Form();&lt;br /&gt;        $form-&amp;gt;setAction('/register/register')&lt;br /&gt;             -&amp;gt;setMethod('post');&lt;br /&gt;        $twitterScreenName = $form-&amp;gt;createElement('text', 'twitter_screen_name', &lt;br /&gt;            array('label' =&amp;gt; 'Twittername: ')&lt;br /&gt;        );&lt;br /&gt;        $twitterScreenName-&amp;gt;addValidator(&lt;em&gt;new Recordshelf_Validate_TwitterScreenName()&lt;/em&gt;)&lt;br /&gt;            -&amp;gt;setRequired(true)&lt;br /&gt;            -&amp;gt;setAllowEmpty(false) &lt;br /&gt;            -&amp;gt;addFilter(&lt;em&gt;new Recordshelf_Filter_TwitterScreenName()&lt;/em&gt;);&lt;br /&gt;&lt;br /&gt;        $autoListApproval = $form-&amp;gt;createElement('checkbox', 'auto_list_approval', &lt;br /&gt;            array('label' =&amp;gt; 'I approved to be added to the event Twitter list: ')&lt;br /&gt;        );&lt;br /&gt;        &lt;br /&gt;        $form-&amp;gt;addElement($twitterScreenName)&lt;br /&gt;             -&amp;gt;addElement($autoListApproval)&lt;br /&gt;             -&amp;gt;addElement('submit', 'register', array('label' =&amp;gt; ' Register '));&lt;br /&gt;        &lt;br /&gt;        return $form;&lt;br /&gt;    }&lt;br /&gt;    public function indexAction()&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;view-&amp;gt;form = $this-&amp;gt;_getForm();&lt;br /&gt;    } &lt;br /&gt;    public function thanksAction()&lt;br /&gt;    {&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * @badpractice Handle possible Exception of&lt;br /&gt;     * Recordshelf_Service_Twitter_List::addMember.&lt;br /&gt;     * @return Recordshelf_Service_Twitter_List&lt;br /&gt;     */&lt;br /&gt;    public function registerAction()&lt;br /&gt;    {&lt;br /&gt;        $this-&amp;gt;_helper-&amp;gt;viewRenderer-&amp;gt;setNoRender();&lt;br /&gt;        $form = $this-&amp;gt;_getForm();&lt;br /&gt;        $request = $this-&amp;gt;getRequest();&lt;br /&gt;                &lt;br /&gt;        if ($this-&amp;gt;getRequest()-&amp;gt;isPost()) {&lt;br /&gt;            if ($form-&amp;gt;isValid($request-&amp;gt;getPost())) {&lt;br /&gt;                &lt;br /&gt;                $model = new Recordshelf_Model_Participant($form-&amp;gt;getValues());&lt;br /&gt;                $model-&amp;gt;save();&lt;br /&gt;&lt;br /&gt;                if ($form-&amp;gt;getElement('auto_list_approval')-&amp;gt;isChecked()) {&lt;br /&gt;                    $twitterScreenName = $form-&amp;gt;getValue('twitter_screen_name');&lt;br /&gt;                    $twitter = $this-&amp;gt;_getTwitterListService();&lt;br /&gt;                    $response = $twitter-&amp;gt;user-&amp;gt;show($twitterScreenName);&lt;br /&gt;                    $userId = (string) $response-&amp;gt;id;&lt;br /&gt;                    $response = $twitter-&amp;gt;list-&amp;gt;addMember($userId, &lt;br /&gt;                        $this-&amp;gt;_getTwitterListName());&lt;br /&gt;                    &lt;br /&gt;                    $model-&amp;gt;hasBeenAddedToTwitterList(true);&lt;br /&gt;                    $model-&amp;gt;update();&lt;br /&gt;&lt;br /&gt;                    return $this-&amp;gt;_helper-&amp;gt;redirector('thanks');&lt;br /&gt;                }&lt;br /&gt;            } else {&lt;br /&gt;                return $this-&amp;gt;_helper-&amp;gt;redirector('index');&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * @badpractice Push this into a dedicated Helper or something similar.&lt;br /&gt;     * @return Recordshelf_Service_Twitter_List&lt;br /&gt;     */&lt;br /&gt;    private function _getTwitterListService()&lt;br /&gt;    {&lt;br /&gt;        $config = Zend_Registry::get('config');&lt;br /&gt;        return new Recordshelf_Service_Twitter_List(&lt;br /&gt;            $config-&amp;gt;twitter-&amp;gt;get('username'),&lt;br /&gt;            $config-&amp;gt;twitter-&amp;gt;get('password')&lt;br /&gt;        );&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * @badpractice Push this into a dedicated Helper or something similar.&lt;br /&gt;     * @return string&lt;br /&gt;     */&lt;br /&gt;    private function _getTwitterListName()&lt;br /&gt;    {&lt;br /&gt;        $config = Zend_Registry::get('config');&lt;br /&gt;        return $config-&amp;gt;twitter-&amp;gt;auto-&amp;gt;get('listname');&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/QdY0rYtmTTw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=4028008204091903175" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4028008204091903175?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4028008204091903175?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2010/02/utilizing-twitter-lists-with.html" title="Utilizing Twitter lists with Zend_Service_Twitter" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>5</thr:total></entry><entry gd:etag="W/&quot;AkUFR3s5fCp7ImA9WxBVGUw.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-7540160445395478734</id><published>2010-01-19T22:41:00.003+01:00</published><updated>2010-02-23T10:43:36.524+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-02-23T10:43:36.524+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="GitHub" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="PHPUnit" /><title>Closing and reopening GitHub issues via PHPUnit tests</title><content type="html">&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 268px; height: 110px;" src="http://farm5.static.flickr.com/4010/4276887996_3190275b8b_o.gif" alt="PHPUnit GitHub TicketListener" title="PHPUnit GitHub TicketListener" border="0" /&gt; Since &lt;a href="http://www.phpunit.de" target="_self"&gt;PHPUnit&lt;/a&gt; 3.4.0 a new extension point for interacting with issue tracking systems (TTS) based on the test results has been added to PHP's first choice &lt;a href="http://en.wikipedia.org/wiki/XUnit" target="_self"&gt;xUnit&lt;/a&gt; framework. The extension point has been introduced by an abstract PHPUnit_Extensions_TicketListener class, which allows developer to add tailor-made ticket listeners supporting their favoured TTS. Currently PHPUnit ships with a single ticket listener for &lt;a href="http://trac.edgewall.org" target="_self"&gt;Trac&lt;/a&gt; as it's still the used TTS for the framework itself. As I start to become more and more accustomed to use &lt;a href="http://github.com" target="_self"&gt;GitHub&lt;/a&gt; for some of my exploratory projects and hacks, the following blog post will contain a GitHub_TicketListener implementation and a showcase of it's usage.&lt;h4 class="custom"&gt;Annotating tests with ticket meta data&lt;/h4&gt;As you might know, it's considered to be a best practice to write a test for each new ticket representing a bug and drive the system under test (SUT) till the issue is resolved. This extension of test-driven development is also known as &lt;a href="http://xunitpatterns.com/test-driven%20bug%20fixing.html" target="_self"&gt;test-driven bug fixing&lt;/a&gt;. To create a relation between these tests and their associated tickets, PHPUnit provides a new @ticket annotation which will be analyzed before each test is run. The following code listing shows such an annotated test.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once 'PHPUnit/Framework.php';&lt;br /&gt; &lt;br /&gt;class ExampleTest extends PHPUnit_Framework_TestCase&lt;br /&gt;{&lt;br /&gt;    ....&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @ticket 2&lt;br /&gt;     * @test&lt;br /&gt;     */&lt;br /&gt;    public function shouldGuarantyThatTheSutHandlesTheIssueCorrectly()&lt;br /&gt;    {&lt;br /&gt;        // test code&lt;br /&gt;    }&lt;br /&gt;    ....&lt;/pre&gt;&lt;h4 class="custom"&gt;Peeking at the GitHub_TicketListener implementation&lt;/h4&gt;The current version (3.4.6) of PHPUnit has a &lt;a href="http://www.phpunit.de/ticket/953" target="_self"&gt;pending issue&lt;/a&gt; regarding the abstract TicketListener class, so the first step is to apply an 'exploratory' &lt;a href="http://gist.github.com/281297" target="_self"&gt;patch&lt;/a&gt;, which might break the functionality of the shipped Trac ticket listener but will enable the use of the one for GitHub's TTS. &lt;br /&gt;&lt;br /&gt;The next step en route to a working GitHub_TicketListener is to extend the patched abstract PHPUnit_Extensions_TicketListener class. This abstract class contains two abstract methods named &lt;em&gt;getTicketInfo&lt;/em&gt; and &lt;em&gt;updateTicket&lt;/em&gt; which have to be implemented by the specific ticket listener class, and will be responsible for the interaction with the TTS.&lt;br /&gt;&lt;br /&gt;The implementation of the &lt;em&gt;getTicketInfo&lt;/em&gt; method retrieves the ticket status for the annotated ticket, while the &lt;em&gt;updateTicket&lt;/em&gt; method is responsible for changing the ticket status based on the test result and the former ticket state. Both implementations make use of the relevant &lt;a href="http://develop.github.com/p/issues.html" target="_self"&gt;TTS part&lt;/a&gt; of the &lt;a href="http://develop.github.com" target="_self"&gt;GitHub API&lt;/a&gt; by utilizing PHP's curl extension as shown in the next code listing which alternatively is available via this &lt;a href="http://gist.github.com/281317" target="_self"&gt;gist&lt;/a&gt;.&lt;pre class="codeSnippet"&gt;&amp;lt?php&lt;br /&gt;require_once('PHPUnit/Extensions/TicketListener.php');&lt;br /&gt;PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * A ticket listener that interacts with the GitHub issue API.&lt;br /&gt; */&lt;br /&gt;class PHPUnit_Extensions_TicketListener_GitHub extends &lt;br /&gt;    PHPUnit_Extensions_TicketListener&lt;br /&gt;{&lt;br /&gt;    const STATUS_CLOSE = 'closed';&lt;br /&gt;    const STATUS_REOPEN = 'reopened';&lt;br /&gt;    &lt;br /&gt;    private $_username = null;&lt;br /&gt;    private $_apiToken = null;&lt;br /&gt;    private $_repository = null;&lt;br /&gt;    private $_apiPath = null;&lt;br /&gt;    private $_printTicketStateChanges = false;&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @param string $username   The username associated with the GitHub account.&lt;br /&gt;     * @param string $apiToken   The API token associated with the GitHub account.&lt;br /&gt;     * @param string $repository The repository of the system under test (SUT) on GitHub.&lt;br /&gt;     * @param string $printTicketChanges Boolean flag to print the ticket state &lt;br /&gt;     * changes in the test result.&lt;br /&gt;     * @throws RuntimeException&lt;br /&gt;     */&lt;br /&gt;    public function __construct($username, $apiToken, $repository, &lt;br /&gt;        $printTicketStateChanges = false)&lt;br /&gt;    {&lt;br /&gt;        if ($this-&gt;_isCurlAvailable() === false) {&lt;br /&gt;            throw new RuntimeException('The dependent curl extension is not available');&lt;br /&gt;        }&lt;br /&gt;        if ($this-&gt;_isJsonAvailable() === false) {&lt;br /&gt;            throw new RuntimeException('The dependent json extension is not available');&lt;br /&gt;        }&lt;br /&gt;        $this-&gt;_username = $username;&lt;br /&gt;        $this-&gt;_apiToken = $apiToken;&lt;br /&gt;        $this-&gt;_repository = $repository;&lt;br /&gt;        $this-&gt;_apiPath = 'http://github.com/api/v2/json/issues';&lt;br /&gt;        $this-&gt;_printTicketStateChanges = $printTicketStateChanges;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @param  integer $ticketId &lt;br /&gt;     * @return string&lt;br /&gt;     * @throws PHPUnit_Framework_Exception&lt;br /&gt;     */&lt;br /&gt;    public function getTicketInfo($ticketId = null) &lt;br /&gt;    {&lt;br /&gt;        if (!ctype_digit($ticketId)) {&lt;br /&gt;            return $ticketInfo = array('status' =&gt; 'invalid_ticket_id');&lt;br /&gt;        }                &lt;br /&gt;        $ticketInfo = array();&lt;br /&gt;        &lt;br /&gt;        $apiEndpoint = "{$this-&gt;_apiPath}/show/{$this-&gt;_username}/"&lt;br /&gt;            . "{$this-&gt;_repository}/{$ticketId}";&lt;br /&gt;            &lt;br /&gt;        $issueProperties = $this-&gt;_callGitHubIssueApiWithEndpoint($apiEndpoint, true);&lt;br /&gt;&lt;br /&gt;        if ($issueProperties['state'] === 'open') {&lt;br /&gt;            return $ticketInfo = array('status' =&gt; 'new');&lt;br /&gt;        } elseif ($issueProperties['state'] === 'closed') {&lt;br /&gt;            return $ticketInfo = array('status' =&gt; 'closed');&lt;br /&gt;        } elseif ($issueProperties['state'] === 'unknown_ticket') {&lt;br /&gt;            return $ticketInfo = array('status' =&gt; $issueProperties['state']);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param string $ticketId   The ticket number of the ticket under test (TUT).&lt;br /&gt;     * @param string $statusToBe The status of the TUT after running the associated test.&lt;br /&gt;     * @param string $message    The additional message for the TUT.&lt;br /&gt;     * @param string $resolution The resolution for the TUT.&lt;br /&gt;     * @throws PHPUnit_Framework_Exception&lt;br /&gt;     */&lt;br /&gt;    protected function updateTicket($ticketId, $statusToBe, $message, $resolution)&lt;br /&gt;    {&lt;br /&gt;        $apiEndpoint = null;&lt;br /&gt;        $acceptedResponseIssueStates = array('open', 'closed');&lt;br /&gt;        &lt;br /&gt;        if ($statusToBe === self::STATUS_CLOSE) {&lt;br /&gt;            $apiEndpoint = "{$this-&gt;_apiPath}/close/{$this-&gt;_username}/"&lt;br /&gt;                . "{$this-&gt;_repository}/{$ticketId}";&lt;br /&gt;        } elseif ($statusToBe === self::STATUS_REOPEN) {&lt;br /&gt;            $apiEndpoint = "{$this-&gt;_apiPath}/reopen/{$this-&gt;_username}/"&lt;br /&gt;                . "{$this-&gt;_repository}/{$ticketId}";&lt;br /&gt;        }&lt;br /&gt;        if (!is_null($apiEndpoint)) {&lt;br /&gt;            $issueProperties = $this-&gt;_callGitHubIssueApiWithEndpoint($apiEndpoint);&lt;br /&gt;            if (!in_array($issueProperties['state'], $acceptedResponseIssueStates)) {&lt;br /&gt;                throw new PHPUnit_Framework_Exception(&lt;br /&gt;                    'Recieved an unaccepted issue state from the GitHub Api');&lt;br /&gt;            }&lt;br /&gt;            if ($this-&gt;_printTicketStateChanges) {&lt;br /&gt;                printf("\nUpdating GitHub issue #%d, status: %s\n", $ticketId, &lt;br /&gt;                    $statusToBe);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @return boolean &lt;br /&gt;     */&lt;br /&gt;    private function _isCurlAvailable()&lt;br /&gt;    {&lt;br /&gt;        return extension_loaded('curl');&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @return boolean &lt;br /&gt;     */&lt;br /&gt;    private function _isJsonAvailable()&lt;br /&gt;    {&lt;br /&gt;        return extension_loaded('json');&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param string  $apiEndpoint API endpoint to call against the GitHub issue API.&lt;br /&gt;     * @param boolean $isShowMethodCall Show method of the GitHub issue API is called? &lt;br /&gt;     * @return array&lt;br /&gt;     * @throws PHPUnit_Framework_Exception&lt;br /&gt;     */&lt;br /&gt;    private function _callGitHubIssueApiWithEndpoint($apiEndpoint, &lt;br /&gt;        $isShowMethodCall = false) &lt;br /&gt;    {&lt;br /&gt;            $curlHandle = curl_init();&lt;br /&gt;&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_URL, $apiEndpoint);&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_FAILONERROR, true);&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_FRESH_CONNECT, true);&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_HTTPPROXYTUNNEL, true);&lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_USERAGENT, __CLASS__);  &lt;br /&gt;            curl_setopt($curlHandle, CURLOPT_POSTFIELDS,&lt;br /&gt;                "login={$this-&gt;_username}&amp;token={$this-&gt;_apiToken}");&lt;br /&gt;&lt;br /&gt;            $response = curl_exec($curlHandle);&lt;br /&gt;            &lt;br /&gt;            // Unknown tickets throw a 403 error&lt;br /&gt;            if (!$response &amp;&amp; $isGetTicketInfoCall) {&lt;br /&gt;                return array('state' =&gt; 'unknown_ticket');&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            if (!$response) {&lt;br /&gt;                $curlErrorMessage = curl_error($curlHandle);&lt;br /&gt;                $exceptionMessage = "A failure occured while talking to the "&lt;br /&gt;                    . "GitHub issue Api. {$curlErrorMessage}.";&lt;br /&gt;                throw new PHPUnit_Framework_Exception($exceptionMessage);&lt;br /&gt;            }&lt;br /&gt;            $issue = (array) json_decode($response);&lt;br /&gt;            $issueProperties = (array) $issue['issue'];&lt;br /&gt;            curl_close($curlHandle);&lt;br /&gt;            return $issueProperties;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Plugging the GitHub_TicketListener into the PHPUnit test environment&lt;/h4&gt;To hook the GitHub ticket listener into the test runtime environment PHPUnit provides several approaches to do so. The chosen approach makes use of a &lt;a href="http://www.phpunit.de/manual/current/en/appendixes.configuration.html" target="_self"&gt;XML configuration file&lt;/a&gt; which allows an injection of the ticket listener in a declarative manner. As you will see in the configuration file snippet, the GitHub ticket listener is initialized with four parameters: The first one is the GitHub username, followed by the GitHub API token, the associated GitHub project, and a boolean flag for displaying the ticket status changes in the test result.&lt;pre class="xmlSnippet"&gt;&amp;lt;phpunit&amp;gt;&lt;br /&gt;  &amp;lt;listeners&amp;gt;&lt;br /&gt;    &amp;lt;listener class="PHPUnit_Extensions_TicketListener_GitHub" &lt;br /&gt;              file="/path/to/GitHubTicketListener.php"&amp;gt;&lt;br /&gt;      &amp;lt;arguments&amp;gt;&lt;br /&gt;        &amp;lt;string&amp;gt;raphaelstolt&amp;lt;/string&amp;gt;&lt;br /&gt;        &amp;lt;string&amp;gt;API_TOKEN&amp;lt;/string&amp;gt;&lt;br /&gt;        &amp;lt;string&amp;gt;PROJECT_NAME&amp;lt;/string&amp;gt;&lt;br /&gt;        &amp;lt;boolean&amp;gt;true&amp;lt;/boolean&amp;gt;&lt;br /&gt;      &amp;lt;/arguments&amp;gt;&lt;br /&gt;    &amp;lt;/listener&amp;gt;&lt;br /&gt;  &amp;lt;/listeners&amp;gt;&lt;br /&gt;&amp;lt;/phpunit&amp;gt;&lt;/pre&gt;To run the tests against a SUT and see the PHPUnit GitHub TTS interaction at work, all it takes is the forthcoming PHPUnit Cli call.&lt;pre class="consoleOutput"&gt;phpunit --configuration github-ticketlistener.xml ExampleTest.php&lt;/pre&gt;The outro screenshot shows the test result for an example SUT along with a GitHub TTS interaction due to a passing test which is associated with a open ticket in the TTS. &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;A final note:&lt;/strong&gt; As the interaction with an TTS adds some overhead to the test execution and thereby might cause &lt;a href="http://xunitpatterns.com/Slow%20Tests.html" target="_self"&gt;Slow Tests&lt;/a&gt;, ticket listener should only be considered in non time-critical test scenarios (e.g. nightly builds). &lt;br/&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/4288246677/" title="PHPUnit closing a GitHub issue"&gt;&lt;img src="http://farm5.static.flickr.com/4034/4288246677_9d373ab585_o.gif" border="0" width="627" height="304" alt="PHPUnit closing a GitHub issue" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/e9ENvRbYiQI" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=7540160445395478734" title="5 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7540160445395478734?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7540160445395478734?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2010/01/closing-and-reopening-github-issues-via.html" title="Closing and reopening GitHub issues via PHPUnit tests" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>5</thr:total></entry><entry gd:etag="W/&quot;DUAAQnc-eCp7ImA9WxNWFUg.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-7619783294518881515</id><published>2009-10-14T23:22:00.005+02:00</published><updated>2009-10-15T00:02:23.950+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-10-15T00:02:23.950+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Book reviews" /><title>Zend Framework 1.8 Web Application Development book review</title><content type="html">&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 190px; height: 228px;" src="http://farm3.static.flickr.com/2487/4011785234_2348120baa_o.gif" alt="Zend Framework 1.8 Web Application Development" title="Zend Framework 1.8 Web Application Development" border="0" /&gt;As the days are rapidly getting shorter, my reading appetite grows potentially and this evening I finished the 'Zend Framework 1.8 Web Application Development' book written by &lt;a href="http://www.thepopeisdead.com" target="_self"&gt;Keith Pope&lt;/a&gt;. While Keith worked on the book, I peeked several times at it's &lt;a href="http://code.google.com/p/zendframeworkstorefront/" target="_self"&gt;tutorial application&lt;/a&gt;, dubbed the Storefront, to get me going with the new Zend_Application component. Looking at it's code made me feel certain to get another great digest of the new features and components of version 1.8, and also a different practical perspective on web application development with the &lt;a href="http://framework.zend.com/" target="_self"&gt;Zend Framework&lt;/a&gt;, once the book has been published. Therefor I got in touch with the publisher &lt;a href="http://www.packtpub.com" target="_self"&gt;Packt&lt;/a&gt; and fortunately got a &lt;a href="http://www.packtpub.com/zend-framework-1-8-web-application-development/book" target="_self"&gt;copy&lt;/a&gt; of which I'd like to share a personal review in this blog post.&lt;h4 class="custom"&gt;What's in it?&lt;/h4&gt;The book opens with a quick run-through of the Model-View-Controller (MVC) architecture by creating a project structure via &lt;a href="http://framework.zend.com/manual/en/zend.tool.framework.html" target="_self"&gt;Zend_Tool&lt;/a&gt; and building a first very basic web application. While this introduction intentionally skips over a lot of details, the following chapter provides very detailed insights into the Zend Framework's MVC components by explaining the surrounded objects, the Design Patterns they are based upon and their interactions. &lt;br /&gt;&lt;br /&gt;After laying out that hefty block of theory the aforementioned tutorial application is introduced and built incrementally over several chapters; each one going into more detail for the specific application aspect. The highlight content of these chapters reach from introducing the Fat Model Skinny Controller concept, thoughts on Model design strategies which are reflected in a custom Storefront Model design, to developing application specific Front Controller Plugins, Action-Helpers, and View-Helpers. The application walk-through is completed by looking at general techniques to optimize the Storefront application and by building an automated &lt;a href="http://www.phpunit.de" target="self"&gt;PHPUnit&lt;/a&gt; Test Suite of functional tests utilizing &lt;a href="http://framework.zend.com/manual/en/zend.test.html" target="_self"&gt;Zend_Test&lt;/a&gt; to keep the Zend Framework based application self-reliant and refactorable.&lt;h4 class="custom"&gt;Conclusion&lt;/h4&gt;The book by Keith Pope provides any interested PHP developer, who's not already sold on a specific framework, a thorough introduction to the vivid Zend Framework and it's use in a MVC based web application development context. The content of the book is delivered in a fluent, very enthusiastic and 'knowledge-pillowed' writing tone. By implementing or working through the Storefront application seasoned web developers using older versions of the Framework will get a good blue sheet on new components like &lt;a href="http://framework.zend.com/manual/en/zend.application.html" target="_self"&gt;Zend_Application&lt;/a&gt; and it's implication in the bootstrapping process; while new developers tending towards picking up the Zend Framework will get a current and well compiled guide, which might first start off with a steep learning-curve but will turn into profund knowledge once hanging in there.&lt;br /&gt;&lt;br /&gt;The only thing that seemed a bit odd to me, was the utilization of &lt;a href="http://ant.apache.org" target="_self"&gt;Ant&lt;/a&gt; instead of &lt;a href="http://phing.info" target="_self"&gt;Phing&lt;/a&gt; as the build tool for the Storefront application to set the application environment, to remove all require_once statements from the framework library and to run the PHPUnit Test Suite; but this might also be inflicted by my Phing &lt;a href="http://raphaelstolt.blogspot.com/search/label/Phing" target="_self"&gt;nuttiness&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/UdIjktpIl9A" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=7619783294518881515" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7619783294518881515?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7619783294518881515?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/10/zend-framework-18-web-application.html" title="Zend Framework 1.8 Web Application Development book review" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>6</thr:total></entry><entry gd:etag="W/&quot;CkQGSH47fCp7ImA9WhRTGEk.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-6528440024079434503</id><published>2009-09-19T21:00:00.018+02:00</published><updated>2011-11-09T13:05:29.004+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-09T13:05:29.004+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="MongoDb" /><title>Logging to MongoDb and accessing log collections with Zend_Tool</title><content type="html">Influenced by a recent &lt;a href="http://taukon.de/2009/zend_log-mit-mongodb/" target="_self" title="Zend_Log mit mongoDB"&gt;blog post&lt;/a&gt; of a colleague of mine and by being kind of broke on a Saturday night; I tinkered with the just recently discovered &lt;a href="http://www.mongodb.org/display/DOCS/Home" target="_self"&gt;MongoDb&lt;/a&gt; and hooked it into the &lt;a href="http://framework.zend.com/manual/en/zend.log.html" target="_self"&gt;Zend_Log&lt;/a&gt; environment by creating a dedicated Zend_Log_Writer. The following post will therefore present a peek at a &lt;em&gt;prototypesque&lt;/em&gt; implementation of this writer and show how the afterwards accumulated log entries can be accessed and filtered with a &lt;a href="http://raphaelstolt.blogspot.com/2009/07/scaffolding-implementing-and-using.html" target="_self" title="Scaffolding, implementing and using project specific Zend_Tool_Project_Providers"&gt;custom Zend_Tool project provider&lt;/a&gt;.&lt;h4 class="custom"&gt;Logging to a MongoDb database&lt;/h4&gt;The following steps assume that an instance of a MongoDb server is running and that the required PHP MongoDb module is also &lt;a href="http://www.mongodb.org/display/DOCS/Installing+the+PHP+Driver" target="_self"&gt;installed&lt;/a&gt; and loaded. To by-pass log entries to a MongoDb database there is a need to craft a proper Zend_Log_Writer. This can be achieved by extending the Zend_Log_Writer_Abstract class, injecting a &lt;a href="http://php.net/manual/en/class.mongo.php" target="_self"&gt;Mongo connection&lt;/a&gt; instance and implementing the actual write functionality as shown in the next listing.&lt;pre class="codeSnippet"&gt;&amp;lt;?php
require_once 'Zend/Log/Writer/Abstract.php';

class Recordshelf_Log_Writer_MongoDb extends Zend_Log_Writer_Abstract
{
    private $_db;
    private $_connection;

   /**
    * @param Mongo $connection The MongoDb database connection
    * @param string $db The MongoDb database name
    * @param string $collection The collection name string the log entries 
    */
    public function __construct(Mongo $connection, $db, $collection)
    {
        $this-&gt;_connection = $connection;
        $this-&gt;_db = $this-&gt;_connection-&gt;selectDB($db)-&gt;createCollection(
            $collection
        );
    }
    public function setFormatter($formatter)
    {
        require_once 'Zend/Log/Exception.php';
        throw new Zend_Log_Exception(get_class() . ' does not support formatting');
    }
    public function shutdown()
    {
        $this-&gt;_db = null;
        $this-&gt;_connection-&gt;close();
    }
    protected function _write($event)
    {
        $this-&gt;_db-&gt;insert($event);
    }
   /**
    * Create a new instance of Recordshelf_Log_Writer_MongoDb
    * 
    * @param  array|Zen_Config $config
    * @return Recordshelf_Log_Writer_MongoDb
    * @throws Zend_Log_Exception
    * @since  Factory Interface available since release 1.10.0
    */
    static public function factory($config) 
    {
        $exceptionMessage = 'Recordshelf_Log_Writer_MongoDb does not currently '
            . 'implement a factory';
        throw new Zend_Exception($exceptionMessage);
    }
}&lt;/pre&gt;With the MongoDb writer available and added to the library directory of the application it's now possible to utilize this new storage backend as usual with the known Zend_Log component. The Mongo connection injected into the writer is configured via &lt;a href="http://framework.zend.com/manual/en/zend.config.html" target="_self"&gt;Zend_Config&lt;/a&gt; and initialized via the &lt;a href="http://framework.zend.com/manual/en/zend.application.html" target="_self"&gt;Zend_Application&lt;/a&gt; bootstrapping facility as shown in the listings below.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="refactoringStatus" style="width: 205px;"&gt;application/configs/application.ini&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;[production]
app.name = recordshelf

....

log.mongodb.db = zf_mongo
log.mongodb.collection = recordshelf_log
log.mongodb.server = localhost
log.priority = Zend_Log::CRIT

....&lt;/pre&gt;&lt;div class="refactoringStatus" style="width: 154px;"&gt;application/Bootstrap.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected $_logger;

    protected function _initConfig()
    {
        Zend_Registry::set('config', new Zend_Config($this-&gt;getOptions()));
    }

    protected function _initLogger()
    {
        $this-&gt;bootstrap(array('frontController', 'config'));
        $config = Zend_Registry::get('config');     

        $applicationName = $config-&gt;app-&gt;get('name', 'recordshelf');
        $mongoDbServer = $config-&gt;log-&gt;mongodb-&gt;get('server', '127.0.0.1');
        $mongoDbName = $config-&gt;log-&gt;mongodb-&gt;get('db', "{$applicationName}_logs");
        $mongoDbCollection = $config-&gt;log-&gt;mongodb-&gt;get('collection', 'entries');

        $logger = new Zend_Log();
        $writer = new Recordshelf_Log_Writer_MongoDb(new Mongo($mongoDbServer), 
        $mongoDbName, $mongoDbCollection);

        if ('production' === $this-&gt;getEnvironment()) {
            $priority = constant($config-&gt;log-&gt;get('priority', Zend_Log::CRIT));
            $filter = new Zend_Log_Filter_Priority($priority);
            $logger-&gt;addFilter($filter);
        }
        $logger-&gt;addWriter($writer);
        $this-&gt;_logger = $logger;
        Zend_Registry::set('log', $logger);
    }
}&lt;/pre&gt;&lt;div class="refactoringStatus" style="width: 205px;"&gt;controllers/ExampleController.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php

class ExampleController extends Zend_Controller_Action
{
    private $_logger = null;

    public function init()
    {
        $this-&gt;_logger = Zend_Registry::get('log');
    }

    public function fooAction()
    {
        $this-&gt;_logger-&gt;log('A debug log message from within action ' . 
            $this-&gt;getRequest()-&gt;getActionName(), Zend_Log::DEBUG);
    }

    public function barAction()
    {
        $this-&gt;_logger-&gt;log('A debug log message from within ' . 
            __METHOD__, Zend_Log::DEBUG);
    }
}&lt;/pre&gt;&lt;h4 class="custom"&gt;Accessing the log database with a Zend_Tool project provider&lt;/h4&gt;After handling the application-wide logging with the MongoDb writer sooner or later the issue to access the gathered log entries will rise. For this mundane and recurring use case the ProjectProvider provider of the Zend_Tool framework is an acceptable candidate to hook a custom action into the Zend_Tool environment of a given project. Therefor a new Zend_Tool_Project Project provider is first scaffolded via the forthcoming command.&lt;pre class="consoleOutput"&gt;sudo zf create project-provider mongodb-logs filter&lt;/pre&gt;Second the generated provider skeleton its filter action is enliven with the logic to query the MongoDb database and the stored log collection. The action to come accepts three arguments to filter the stored log entry results by a specific date in the format of 'YYYY-MM-DD' and a given Zend_Log priority (currently limited to the constants defined in Zend_Log) in a specific application environment. The next listing shows the implementation of the import  action of the MongodbLogsProvider project provider; which is clearly, as it's length indicates, in need for a clean-up task.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="refactoringStatus" style="width: 225px;"&gt;providers/Mongodb-logsProvider.php&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php
require_once 'Zend/Tool/Project/Provider/Abstract.php';
require_once 'Zend/Tool/Project/Provider/Exception.php';
require_once 'Zend/Date.php';
require_once 'Zend/Validate/Date.php';
require_once 'Zend/Log.php';
require_once 'Zend/Config/Ini.php';

class MongodbLogsProvider extends Zend_Tool_Project_Provider_Abstract
{

    public function filter($date = null, $logPriority = null, 
        $env = 'development')
    {
        $ref = new Zend_Reflection_Class('Zend_Log');
        $logPriorities = $ref-&gt;getConstants();

        if (in_array(strtoupper($date), array_keys($logPriorities)) || 
            in_array(strtoupper($date), array_values($logPriorities))) {
            $logPriority = $date;
            $date = null;
        }
        if (!is_null($date)) {
            $validator = new Zend_Validate_Date();
            if (!$validator-&gt;isValid($date)) {
                $exceptionMessage = "Given date '{$date}' is not a valid date.";
                throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
            }
            $dateArray = array();
            list($dateArray['year'], $dateArray['month'], $dateArray['day']) = 
                explode('-', $date);
            $date = new Zend_Date($dateArray);
        } else {
            $date = new Zend_Date();
        }
        $date = $date-&gt;toString('Y-MM-dd');

        if (!is_null($logPriority)) {
            if (!is_numeric($logPriority)) {
                $logPriority = strtoupper($logPriority);
                if (!in_array($logPriority, array_keys($logPriorities))) {
                    $exceptionMessage = "Given priority '{$logPriority}' is not defined.";
                    throw new Zend_Tool_Project_Provider_Exception($exceptionMessage);
                } else {
                    $logPriority = $logPriorities[$logPriority];
                }
            }
        if (!in_array($logPriority, array_values($logPriorities))) {
            $exceptionMessage = "Given priority '{$logPriority}' is not defined.";
            throw new Zend_Tool_Project_Provider_Exception();
        }
            $priorities = array_flip($logPriorities);
            $priorityName = $priorities[$logPriority];
        }

        if ($env !== 'development' &amp;&amp; $env !== 'production') {
            $exceptionMessage = "Unsupported environment '{$env}' provided.";
            throw new Zend_Tool_Project_Provider_Exception();
        }
        $config = new Zend_Config_Ini('./application/configs/application.ini', 
            $env);

        $applicationName = $config-&gt;app-&gt;get('name', 'recordshelf');
        $mongoDbServer = $config-&gt;log-&gt;mongodb-&gt;get('server', '127.0.0.1');
        $mongoDbName = $config-&gt;log-&gt;mongodb-&gt;get('db', "{$applicationName}_logs");
        $mongoDbCollection = $config-&gt;log-&gt;mongodb-&gt;get('collection', 'entries');

        try {
            $connection = new Mongo($mongoDbServer);
            $db = $connection-&gt;selectDB($mongoDbName)-&gt;createCollection(
            $mongoDbCollection);
        } catch (MongoConnectionException $e) {
            throw new Zend_Tool_Project_Provider_Exception($e-&gt;getMessage());
        }
        $dateRegex = new MongoRegex("/$date.*/i");

        if (is_null($logPriority)) {
            $query = array('timestamp' =&gt; $dateRegex);
            $appendContentForResults = "Found #amountOfEntries# log entrie(s) "
                . "on {$date}";
            $appendContentForNoResults = "Found no log entries on {$date}";
        } else {            
            $query = array('priority' =&gt; (int) $logPriority, 
                           'timestamp' =&gt; $dateRegex
                     );
            $appendContentForResults = "Found #amountOfEntries# log entrie(s) "
                . "for priority {$priorityName} on {$date}";
            $appendContentForNoResults = "Found no log entries for priority "
                . "{$priorityName} on {$date}";
        }

        $cursor = $db-&gt;find($query);
        $amountOfEntries = $cursor-&gt;count();

        if ($amountOfEntries &gt; 0) {
            $content = str_replace('#amountOfEntries#', $amountOfEntries, 
                $appendContentForResults);
            $this-&gt;_registry-&gt;getResponse()-&gt;appendContent($content);
            foreach ($cursor as $id =&gt; $value) {
                $content = "{$id}: {$value['timestamp']} &gt; ";
                if (is_null($logPriority)) {
                    $content.= "[{$value['priorityName']}] ";
                }
                $content.= "{$value['message']}";
                $this-&gt;_registry-&gt;getResponse()-&gt;appendContent($content);
            }
        } else {
            $content = $appendContentForNoResults;
            $this-&gt;_registry-&gt;getResponse()-&gt;appendContent($content);
        }
        $connection-&gt;close();
    }
}&lt;/pre&gt;The coming outro screenshots show two use cases for the filter action of the MongodbLogsProvider issued against the zf command line client. The first screenshot shows the use case where all log entries for the current day are queried, while the second one shows the use case where all log entries for a specific date and log priority are queried and fed back to the user.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/3934270869/" title="All log entries of the current day by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2548/3934270869_fa329bea00_o.gif" width="800" height="380" border="0" alt="All log entries of the current day" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/3935054512/" title="All CRIT log entries for a specific date by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2658/3935054512_e929053892_o.gif" width="613" height="170" border="0" alt="All CRIT log entries for a specific date" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/nXhvw4IJFk4" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=6528440024079434503" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6528440024079434503?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6528440024079434503?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/09/logging-to-mongodb-and-accessing-log.html" title="Logging to MongoDb and accessing log collections with Zend_Tool" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;D0EBRH87fip7ImA9WxNSEU0.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-4987593960043747431</id><published>2009-08-22T15:08:00.004+02:00</published><updated>2009-08-24T11:20:55.106+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-08-24T11:20:55.106+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Automation" /><category scheme="http://www.blogger.com/atom/ns#" term="TextMate" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Kicking off custom Phing task development with TextMate</title><content type="html">As a reader of this blog you migth have noticed that from time to time I like to utilize &lt;a href="http://phing.info" target="_self"&gt;Phing&lt;/a&gt;'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 &lt;a href="http://macromates.com/" target="_self"&gt;TextMate&lt;/a&gt;'s snippet feature to my needs.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://manual.macromates.com/en/snippets#snippets" target="_self"&gt;Snippets&lt;/a&gt; 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.&lt;br /&gt;&lt;br /&gt;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.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once 'phing/Task.php';&lt;br /&gt;&lt;br /&gt;class ${1:CustomName}Task extends Task&lt;br /&gt;{&lt;br /&gt;    private \$_${2:property} = null;&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @param string \$${2:property} ${3:description}&lt;br /&gt;     */&lt;br /&gt;    public function set${2/./\u$0/}(\$${2:property})&lt;br /&gt;    {&lt;br /&gt;        \$this-&gt;_${2:property} = trim(\$${2:property});&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Initializes the task environment if necessary&lt;br /&gt;     */&lt;br /&gt;    public function init()&lt;br /&gt;    {&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Does the task main work or delegates it&lt;br /&gt;     * @throws BuildException&lt;br /&gt;     */&lt;br /&gt;    public function main()&lt;br /&gt;    {&lt;br /&gt;        \$this-&gt;_validateProperties();&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Validates the task properties&lt;br /&gt;     * @throws BuildException&lt;br /&gt;     */&lt;br /&gt;    private function _validateProperties()&lt;br /&gt;    {&lt;br /&gt;        if (is_null(\$this-&gt;_${2:property})) {&lt;br /&gt;            throw new BuildException('${4:message}.');&lt;br /&gt;        }$0&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;To apply the snippet, after installing it, on a PHP source file it can either be selected from the &lt;a href="http://manual.macromates.com/en/bundles#bundles" target="_self"&gt;Bundles&lt;/a&gt; menue or more comfortable via the assigned tab trigger i.e. &lt;em&gt;ctask&lt;/em&gt;. 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 &lt;em&gt;_validateProperties&lt;/em&gt; method.&lt;br /&gt;&lt;br /&gt;The outro image shows the above stated snippet in the TextMate Bundle Editor and it's configuration.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/3844647803/" title="Phing snippet in the TextMate Bundle Editor by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm4.static.flickr.com/3550/3844647803_3175559fed_o.gif" width="616" height="431" border="0" alt="Phing snippet in the TextMate Bundle Editor" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/3x8YY-dRytA" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=4987593960043747431" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4987593960043747431?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4987593960043747431?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/08/kicking-off-custom-phing-task.html" title="Kicking off custom Phing task development with TextMate" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;CkQAQ3c7cSp7ImA9WxJVGEs.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-2974053421730973311</id><published>2009-07-04T16:41:00.011+02:00</published><updated>2009-07-06T08:05:42.909+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-07-06T08:05:42.909+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><title>Scaffolding, implementing and using project specific Zend_Tool_Project_Providers</title><content type="html">Working on a project involving several legacy data migration tasks, I got curious what the &lt;a href="http://framework.zend.com/manual/en/zend.tool.project.html" target="_self"&gt;Zend_Tool_Project&lt;/a&gt; component of the &lt;a href="http://framework.zend.com" target="_self"&gt;Zend Framework&lt;/a&gt; offers to create project specific providers for the above mentioned tasks or ones of similar nature. Therefore the following post will try to show how these providers can be developed in an iterative manner by scaffolding them via the capabilities of the Zend_Tool_Project ProjectProvider provider, enlived with action/task logic, and be used in the project scope.&lt;h4 class="custom"&gt;Scaffolding project specific providers&lt;/h4&gt;All following steps assume there is a project available i.e. &lt;em&gt;recordshelf&lt;/em&gt; initially created with the Zend_Tool_Project Project provider and that the forthcoming commands are issued from the project root directory against the zf command line client. The scaffolding of a project specific provider can be triggered via the &lt;em&gt;create&lt;/em&gt; action of the &lt;em&gt;ProjectProvider&lt;/em&gt; provider by passing in the name of the provider i.e. &lt;em&gt;csv&lt;/em&gt; and it's intended actions. As the next console snippet shows it's &lt;br /&gt;possible to specify several actions as a comma separated list.&lt;pre class="consoleOutput"&gt;sudo zf create project-provider csv importSpecials,importSummersale&lt;/pre&gt;After running the command the project's profile &lt;em&gt;.zfproject.xml&lt;/em&gt; has been modified and a new &lt;em&gt;providers&lt;/em&gt; directory exists in the project root directory containing the scaffolded Csv provider. The next code snippet shows the initial Csv provider class skeleton and its two empty action methods named &lt;em&gt;importSpecials&lt;/em&gt; and &lt;em&gt;importSummersale&lt;/em&gt;. At the point of this writing, using the Zend Framework 1.8.4 and PHP 5.2.10 on a Mac OS X system the generated Csv provider code or the mapping in the &lt;em&gt;.zfproject.xml&lt;/em&gt; is incorrect, but can be fixed by renaming the class from CsvProvider to Csv.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;require_once 'Zend/Tool/Project/Provider/Abstract.php';&lt;br /&gt;require_once 'Zend/Tool/Project/Provider/Exception.php';&lt;br /&gt;&lt;br /&gt;class Csv&lt;strike style="font-weight: bolder;"&gt;Provider&lt;/strike&gt; extends Zend_Tool_Project_Provider_Abstract&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    public function importSpecials()&lt;br /&gt;    {&lt;br /&gt;        /** @todo Implementation */&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public function importSummersale()&lt;br /&gt;    {&lt;br /&gt;        /** @todo Implementation */&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Implementing the action logic&lt;/h4&gt;Having the project provider class skeleton ready to get going, it's time to enliven the actions with their intended features by using either other components of the Zend Framework, any suitable third party library or plain-vanilla PHP. For the sake of brevity I decided to implement only the &lt;em&gt;importSpecials&lt;/em&gt; action which transforms the data of a known CSV file structure into a relevant database table. The CSV parsing steps shown next might not be that sophisticated, as their sole purpose is to illustrate an exemplary implementation of a project specific provider action.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;require_once 'Zend/Tool/Project/Provider/Abstract.php';&lt;br /&gt;require_once 'Zend/Tool/Project/Provider/Exception.php';&lt;br /&gt;&lt;br /&gt;class Csv extends Zend_Tool_Project_Provider_Abstract&lt;br /&gt;{&lt;br /&gt;    private function _isProjectProviderSupportedInProject(Zend_Tool_Project_Profile $profile, &lt;br /&gt;        $projectProviderName)&lt;br /&gt;    {&lt;br /&gt;        $projectProviderResource = $this-&gt;_getProjectProfileResource($profile, &lt;br /&gt;            $projectProviderName);&lt;br /&gt;        return $projectProviderResource instanceof Zend_Tool_Project_Profile_Resource;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private function _isActionSupportedByProjectProvider(Zend_Tool_Project_Profile $profile, &lt;br /&gt;        $projectProviderName, $actionName)&lt;br /&gt;    {&lt;br /&gt;        $projectProviderResource = $this-&gt;_getProjectProfileResource($profile, &lt;br /&gt;            $projectProviderName);&lt;br /&gt;        $projectProviderAttributes = $projectProviderResource-&gt;getContext()&lt;br /&gt;                                                             -&gt;getPersistentAttributes();&lt;br /&gt;        return in_array($actionName, explode(',', $projectProviderAttributes['actionNames']));&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    private function _getProjectProfileResource(Zend_Tool_Project_Profile $profile, &lt;br /&gt;        $projectProviderName)&lt;br /&gt;    {&lt;br /&gt;        $profileSearchParams[] = 'ProjectProvidersDirectory';&lt;br /&gt;        $profileSearchParams['ProjectProviderFile'] = &lt;br /&gt;            array('projectProviderName' =&gt; strtolower($projectProviderName));&lt;br /&gt;        return $profile-&gt;search($profileSearchParams);  &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;em style="font-weight: bolder;"&gt;public function importSpecials($csvFile, $env = 'development')&lt;br /&gt;    {&lt;br /&gt;        $relatedTablename = 'specials';&lt;br /&gt;        &lt;br /&gt;        if (!$this-&gt;_isProjectProviderSupportedInProject($profile, __CLASS__)) {&lt;br /&gt;            throw new Exception("ProjectProvider Csv is not supported in this project.");&lt;br /&gt;        }&lt;br /&gt;        if (!$this-&gt;_isActionSupportedByProjectProvider($profile, __CLASS__, __FUNCTION__)) {&lt;br /&gt;            $exceptionMessage = "Action 'importSpecials' is not supported by "&lt;br /&gt;                . "the Csv ProjectProvider in this project.";&lt;br /&gt;            throw new Exception($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if (!file_exists($csvFile)) {&lt;br /&gt;            throw new Exception("Given csv-file '{$csvFile}' doesn't exist.");&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $importEnvironment = trim($env);&lt;br /&gt;        if ($importEnvironment !== 'development' &amp;&amp; $importEnvironment !== 'production') {&lt;br /&gt;            throw new Exception("Unsupported environment '{$importEnvironment}' provided.");&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $csvHandle = fopen($csvFile, "r");&lt;br /&gt;        &lt;br /&gt;        if (!$csvHandle) {&lt;br /&gt;            throw new Exception("Unable to open given csv-file '{$csvFile}'.");&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        $config = new Zend_Config_Ini('./application/configs/application.ini', &lt;br /&gt;            $importEnvironment);&lt;br /&gt;        $db = Zend_Db::factory($config-&gt;database);&lt;br /&gt;&lt;br /&gt;        $db-&gt;query("TRUNCATE TABLE {$relatedTablename}");&lt;br /&gt;        echo "Truncated the project '{$relatedTablename}' database table." . PHP_EOL;&lt;br /&gt;        &lt;br /&gt;        $rowCount = $insertCount = 0;&lt;br /&gt;        &lt;br /&gt;        while (($csvLine = fgetcsv($csvHandle)) !== false) {&lt;br /&gt;            if ($rowCount &gt; 0) {&lt;br /&gt;                $insertRow = array(&lt;br /&gt;                    'product_name' =&gt; $csvLine[0],&lt;br /&gt;                    'product_image_path' =&gt; $csvLine[1],&lt;br /&gt;                    'price' =&gt; $csvLine[2],&lt;br /&gt;                    'special_until' =&gt; $csvLine[3]&lt;br /&gt;                );&lt;br /&gt;                $db-&gt;insert($relatedTablename, $insertRow);&lt;br /&gt;                ++$insertCount;&lt;br /&gt;            }&lt;br /&gt;            ++$rowCount;&lt;br /&gt;        }&lt;br /&gt;        fclose($csvHandle);&lt;br /&gt;        $importMessage = "Imported {$insertCount} rows into the project "&lt;br /&gt;            . "'{$relatedTablename}' database table.";&lt;br /&gt;        echo $importMessage;&lt;br /&gt;    }&lt;/em&gt;&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Making providers and actions pretendable&lt;/h4&gt;To make project specific providers its actions pretendable and thereby providing some kind of user documentation the provider classes have to implement a marker interface called &lt;em&gt;Zend_Tool_Framework_Provider_Pretendable&lt;/em&gt;. For making a action of a provider pretendable and giving some feedback to the user, the request is checked if the action has been issued in the pretend mode; which is possible by adding &lt;em&gt;-p&lt;/em&gt; option to the issued zf command line client command. The next code snippet shows how the above stated &lt;em&gt;Csv&lt;/em&gt; provider and its &lt;em&gt;importSpecials&lt;/em&gt; action is made pretendable.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;require_once 'Zend/Tool/Project/Provider/Abstract.php';&lt;br /&gt;require_once 'Zend/Tool/Project/Provider/Exception.php';&lt;br /&gt;&lt;br /&gt;class Csv extends Zend_Tool_Project_Provider_Abstract&lt;em style="font-weight: bolder;"&gt; implements &lt;br /&gt;    Zend_Tool_Framework_Provider_Pretendable&lt;/em&gt;&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    public function importSpecials($csvFile, $env = 'development')&lt;br /&gt;    {&lt;br /&gt;        ...&lt;br /&gt;        &lt;br /&gt;        &lt;em style="font-weight: bolder;"&gt;if ($this-&gt;_registry-&gt;getRequest()-&gt;isPretend()) {&lt;br /&gt;            $pretendMessage = "I would import the specials data provided in {$csvFile} "&lt;br /&gt;                . "into the project '{$relatedTablename}' database table.";&lt;br /&gt;            echo $pretendMessage;&lt;br /&gt;        } else {&lt;br /&gt;            ...   &lt;br /&gt;        }&lt;/em&gt;&lt;br /&gt;    }&lt;br /&gt;    ...&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Using project specific providers&lt;/h4&gt;To use the bundled up capabilities of project specific providers, these have to made accessable to the zf command line client by putting them in the &lt;em&gt;include_path&lt;/em&gt;. Currently I discovered no best practice for doing so only for single project scopes and simply added the path to the project to my php.ini and thereby global include_path; another approach might be to add the project name as a prefix to the Provider. After doing so it's possible to get an overview of all with the Zend_Tool_Project shipped providers plus the project specific providers and their offered actions by issuing the &lt;em&gt;zf --help&lt;/em&gt; command as shown in the next screenshot. To ensure that project specific providers and its actions are only runnable in projects which support them, it is necessary to check if these and the offered action exists as resources in the project its profile .zfproject.xml file as shown in the implementation of the &lt;em&gt;importSpecials&lt;/em&gt; action in one of above code snippets.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/3687344640/" title="Provider overview by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2630/3687344640_510d00350a_o.gif" width="606" height="282" border="0" alt="Provider overview" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As shown in the previous screenshot the first character of the project specific providers are omitted, this is another minor bug which might be fixed in one of the forthcoming Zend Framework releases. The current workaround for this issue is simply to type the command exactly as shown in the help. The outro screenshot shows how the import-specials action of the project specific Csv provider is issued against the zf command line client and its provided user feedback after an successfull import against the projects development database.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/3686545463/" title="Calling the import-specials action by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm3.static.flickr.com/2584/3686545463_c93b1b18de_o.gif" width="606" height="282" border="0" alt="Calling the import-specials action" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/3SupioXwB6Q" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=2974053421730973311" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/2974053421730973311?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/2974053421730973311?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/07/scaffolding-implementing-and-using.html" title="Scaffolding, implementing and using project specific Zend_Tool_Project_Providers" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DUIERXw8eip7ImA9Wx9bEEk.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-5110082920105713427</id><published>2009-05-10T16:15:00.017+02:00</published><updated>2011-02-18T17:45:04.272+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-18T17:45:04.272+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="PHPUnit" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Testing Phing buildfiles with PHPUnit</title><content type="html">While &lt;a href="http://raphaelstolt.blogspot.com/2008/07/six-valuable-phing-build-file.html" target="_self"&gt;transforming&lt;/a&gt; some of the &lt;a href="http://ant.apache.org/" target="_self"&gt;Ant&lt;/a&gt; buildfile refactorings described in &lt;a href="http://www.build-doctor.com/" target="_self"&gt;Julian Simpson&lt;/a&gt;'s seminal essay into a &lt;a href="http://phing.info/trac/" target="_self"&gt;Phing&lt;/a&gt; context, it felt plainly wrong that I didn't have any tests for the buildfile to back me up on obtaining the pristine behaviour throughout the process. While Ant users can rely on an Apache project called &lt;a href="http://ant.apache.org/antlibs/antunit/"&gt;AntUnit&lt;/a&gt; there are currently no tailor-made tools available for testing or verifying Phing buildfiles. Therefor I took a weekend off, locked myself in the stuffy lab, and explored the abilities to test Phing buildfiles respectively their included properties, targets and tasks with the &lt;a href="http://phpunit.de/" target="_self"&gt;PHPUnit&lt;/a&gt; testing framework. In case you'd like to take a peek at the emerged &lt;b&gt;lab jottings&lt;/b&gt;, keep on scanning.&lt;br /&gt;
&lt;h4 class="custom"&gt;Introducing the buildfile under test&lt;/h4&gt;The buildfile that will be used as an example is kept simple, and contains several targets ranging from common ones like initializing the build environment by creating the necessary directories to more specific ones like pulling an external artifact from &lt;a href="http://github.com/" target="_self"&gt;GitHub&lt;/a&gt;. To get an overview of the buildfile under test have a look at the following listing.&lt;br /&gt;
&lt;pre class="xmlSnippet"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;project name="test-example" default="build" basedir="."&amp;gt;

  &amp;lt;property name="project.basedir" value="." override="true" /&amp;gt;
  &amp;lt;property name="github.repos.dir" value="${project.basedir}/build/github-repos" override="true" /&amp;gt;

  &amp;lt;target name="clean" depends="clean-github-repos" description="Removes runtime build artifacts"&amp;gt;
    &amp;lt;delete dir="${project.basedir}/build" includeemptydirs="true" verbose="false" failonerror="true" /&amp;gt;
    &amp;lt;delete dir="${project.basedir}/build/reports" includeemptydirs="true" verbose="false" failonerror="true" /&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="clean-github-repos" description="Removes runtime build artifacts"&amp;gt;
    &amp;lt;delete dir="${github.repos.dir}" includeemptydirs="true" failonerror="true" /&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="-log-build" description="A private target which should only be invoked internally"&amp;gt;
    &amp;lt;!-- omitted --&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="build" depends="clean" description="Builds the distributable product"&amp;gt;
    &amp;lt;!-- omitted --&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="database-setup" description="Sets up the database structure"&amp;gt;
    &amp;lt;!-- omitted --&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="init" description="Initalizes the build by creating directories etc"&amp;gt;
    &amp;lt;mkdir dir="${project.basedir}/build/logs/performance/" /&amp;gt;
    &amp;lt;mkdir dir="${project.basedir}/build/doc" /&amp;gt;
    &amp;lt;mkdir dir="${project.basedir}/build/reports/phploc" /&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="init-ad-hoc-tasks" 
          description="Initalizes the ad hoc tasks for reusability in multiple targets"&amp;gt;      
    &amp;lt;adhoc-task name="github-clone"&amp;gt;&amp;lt;![CDATA[
  class Github_Clone extends Task {
    private $repository = null;
    private $destDirectory = null;

    function setRepos($repository) {
      $this-&amp;gt;repository = $repository;
    }
    function setDest($destDirectory) {
      $this-&amp;gt;destDirectory = $destDirectory;
    }
    function main() {
      // Get project name from repos Uri
      $projectName = str_replace('.git', '',
        substr(strrchr($this-&amp;gt;repository, '/'), 1));

      $gitCommand = 'git clone ' . $this-&amp;gt;repository . ' ' .
      $this-&amp;gt;destDirectory . '/' . $projectName;

      exec(escapeshellcmd($gitCommand), $output, $return);

      if ($return !== 0) {
        throw new BuildException('Git clone failed');
      }
      $logMessage = 'Cloned Git repository ' . $this-&amp;gt;repository .
        ' into ' . $this-&amp;gt;destDirectory . '/' . $projectName;
      $this-&amp;gt;log($logMessage);
    }
  }
]]&amp;gt;&amp;lt;/adhoc-task&amp;gt;

    &amp;lt;echo message="Intialized github-clone ad hoc task." /&amp;gt;
  &amp;lt;/target&amp;gt;

  &amp;lt;target name="github" depends="init-ad-hoc-tasks, clean-github-repos" 
          description="Clones given repositories from GitHub"&amp;gt;
    &amp;lt;github-clone repos="git://github.com/raphaelstolt/phploc-phing.git" dest="${github.repos.dir}" /&amp;gt;
  &amp;lt;/target&amp;gt;
&amp;lt;/project&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Testing the buildfile&lt;/h4&gt;All tests for the buildfile under test will be bundled, like 'normal' tests, in a class i.e. BuildfileTest extending the PHPUnit_Framework_TestCase class. When testing buildfiles it's possible to build some tests around the actual buildfile XML &lt;b&gt;structure&lt;/b&gt;, by utilizing the &lt;i&gt;xpath&lt;/i&gt; method of PHP's SimpleXMLElement class and asserting against the &lt;a href="http://www.w3.org/TR/xpath" target="_self"&gt;XPath&lt;/a&gt; query results, or around the dispatching of specific targets and asserting against the expected build &lt;b&gt;artifacts&lt;/b&gt;. Furthermore these two identified groups, structure and artifact, can be used to &lt;a href="http://mikenaberezny.com/2007/09/04/better-phpunit-group-annotations/" target="_self"&gt;organize&lt;/a&gt; the accumulating tests via PHPUnit's @group annotation.&lt;br /&gt;
&lt;br /&gt;
To be able to dispatch specific build targets and feed them with properties if necessary I additionally developed a &lt;i&gt;very&lt;/i&gt; basic build runner shown in the next code listing.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;&amp;lt;?php
class Phing_Buildfile_Runner {

    private $_buildfilePath = null;

    public function __construct($buildfilePath) {
        if (!file_exists($buildfilePath)) {
            throw new Exception("Buildfile '{$buildfilePath}' doesn't exist");
        }
        $this-&amp;gt;buildfilePath = realpath($buildfilePath); 
    }
    public function runTarget($targets = array(), $properties = array()) {
        $runTargetCommand = "phing " . "-f {$this-&amp;gt;buildfilePath} ";
        if (count($targets) &amp;gt; 0) {
            foreach ($targets as $target) {
                $runTargetCommand.= $target . " ";
            }
        }
        if (count($properties) &amp;gt; 0) {
            foreach ($properties as $property =&amp;gt; $value) {
                $runTargetCommand.= "-D{$property}={$value} ";
            }       
        }
        exec(escapeshellcmd($runTargetCommand), $output, $return);
        return array('output' =&amp;gt; $output, 'return' =&amp;gt; $return);
    }
}&lt;/pre&gt;Out of the box PHPUnit's &lt;a href="http://www.phpunit.de/manual/current/en/api.html#api.assert" target="_self"&gt;assertion pool&lt;/a&gt; provides all the utilities to test buildfiles; although it would be cleaner to &lt;a href="http://raphaelstolt.blogspot.com/2008/07/creating-custom-phpunit-assertions.html" target="_self"&gt;create domain specfic assertions&lt;/a&gt; for this testing domain this technique will be ignored for the sake of brevity.&lt;br /&gt;
&lt;br /&gt;
After an initial 1000ft view on how to test buildfiles let's jump into the actual testing of a &lt;b&gt;structural&lt;/b&gt; aspect of the buildfile under test. The test to come shows how to verify that a clean target is defined for playing along in the build orchestra by querying a XPath expression against the buildfile XML and asserting that a result is available.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;/**
 * @test
 * @group structure
 */
public function buildfileShouldContainACleanTarget() {
    $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
    $cleanElement = $xml-&amp;gt;xpath("//target[@name='clean']");
    $this-&amp;gt;assertTrue(count($cleanElement) &amp;gt; 0, "Buildfile doesn't contain a clean target");
}&lt;/pre&gt;The next &lt;b&gt;artifactual&lt;/b&gt; test raises the bar an inch, by verifying that the defined init target of the build does initialize the build environment correctly, or to pick up the orchestra metaphor again that the specific instrument plays along and holds the directed tone. Therefor the build runner executes the target and afterwards asserts a list of expected artifacts against the current state of the build process.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;/**
 * @test
 * @group artifact
 */
public function initTargetShouldCreateInitialBuildArtifacts() {
    $this-&amp;gt;_isTearDownNecessary = true;
    $this-&amp;gt;_buildfileRunner-&amp;gt;runTarget(array('init'));
    $expectedInitArtifacts = array(
        "{$this-&amp;gt;_buildfileBasedir}/build",
        "{$this-&amp;gt;_buildfileBasedir}/build/logs/performance/",
        "{$this-&amp;gt;_buildfileBasedir}/build/doc",
        "{$this-&amp;gt;_buildfileBasedir}/build/reports"
    );

    foreach ($expectedInitArtifacts as $artifact) {
        $this-&amp;gt;assertFileExists($artifact, "Expected file '{$artifact}' doesn't exist");
    }
}&lt;/pre&gt;The next code listing shows the whole picture of the BuildfileTest class containing additional test methods verifying different aspects of the buildfile under test and also the innards of the setup and teardown method.&lt;br /&gt;
&lt;br /&gt;
The main assignment of the setup method is to load the XML of the buildfile under test and to intialize the build runner so an instance is available for an use in artifactual tests. The teardown method its sole responsibility is to reset the build state by running the clean target of the buildfile.&lt;br /&gt;
&lt;pre class="codeSnippet"&gt;&amp;lt;?php
require_once 'PHPUnit/Framework.php';
require_once 'Phing/Buildfile/Runner.php';

class ExampleBuildfileTest extends PHPUnit_Framework_TestCase {

    protected $_buildfileXml = null;
    protected $_buildfileName = null;
    protected $_buildfileBasedir = null;
    protected $_buildfileRunner = null;
    protected $_isTearDownNecessary = false;

    protected function setUp() {
        $this-&amp;gt;_buildfileName = realpath('../../build.xml');        
        $this-&amp;gt;_buildfileBasedir = dirname($this-&amp;gt;_buildfileName);
        $this-&amp;gt;_buildfileXml = file_get_contents($this-&amp;gt;_buildfileName);
        $this-&amp;gt;_buildfileRunner = new Phing_Buildfile_Runner(
        $this-&amp;gt;_buildfileName);
    }

    protected function tearDown() {
        if ($this-&amp;gt;_isTearDownNecessary) {
            $this-&amp;gt;_buildfileRunner-&amp;gt;runTarget(array('clean'));
        }
    }

   /**
    * @test
    * @group structure
    */
    public function targetBuildShouldBeTheDefaultTarget() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $xpath = "//@default";
        $defaultElement = $xml-&amp;gt;xpath($xpath);
        $this-&amp;gt;assertSame('build', trim($defaultElement[0]-&amp;gt;default), 
            "Buildfile doesn't have a default target named 'build'"
        );
    }
   /**
    * @test
    * @group structure
    */
    public function propertyGithubReposDirShouldBeSet() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $xpath = "//property[@name='github.repos.dir']/@value";
        $valueElement = $xml-&amp;gt;xpath($xpath);
        $this-&amp;gt;assertTrue($valueElement[0] instanceof SimpleXMLElement, 
            "Buildfile doesn't contain a 'github.repos.dir' property"
        );
        $this-&amp;gt;assertGreaterThan(1, strlen($valueElement[0]-&amp;gt;value));
    }
   /**
    * @test
    * @group structure
    */
    public function buildfileShouldContainACleanTarget() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $cleanElement = $xml-&amp;gt;xpath("//target[@name='clean']");
        $this-&amp;gt;assertTrue(count($cleanElement) &amp;gt; 0, 
            "Buildfile doesn't contain a clean target"
        );
    }
   /**
    * @test
    * @group structure
    */
    public function targetLogBuildShouldBeAPrivateOne() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $nameElement = $xml-&amp;gt;xpath("//target[@name='-log-build']");
        $this-&amp;gt;assertTrue(count($nameElement) &amp;gt; 0, 
            'Log build target is not a private target'
        );
    }
    /**
     * @test
     * @group structure
     */
    public function targetBuildShouldDependOnCleanTarget() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $xpath = "//target[@name='build']/@depends";
        $dependElement = $xml-&amp;gt;xpath($xpath);
        $this-&amp;gt;assertTrue(count($dependElement) &amp;gt; 0, 
            'Target build contains no depends attribute'
        );
        $dependantTasks = array_filter(explode(' ',
            trim($dependElement[0]-&amp;gt;depends))
        );
        $this-&amp;gt;assertContains('clean', $dependantTasks, "Target build doesn't 
            depend on the clean target"
        );
    }
    /**
     * @test
     * @group structure
     */
    public function allDefinedTargetsShouldHaveADescriptionAttribute() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $xpath = "//target";
        $targetElements = $xml-&amp;gt;xpath($xpath);
        $describedTargetElements = array();
        foreach ($targetElements as $index =&amp;gt; $targetElement) {
            $targetDescription = trim($targetElement-&amp;gt;attributes()-&amp;gt;description); 
            if ($targetDescription !== '') {
                $describedTargetElements[] = $targetDescription;
            }
        }
        $this-&amp;gt;assertEquals(count($targetElements),
            count($describedTargetElements), 
            'Description not for all targets set'
        );
    }
    /**
     * @test
     * @group structure
     */
    public function githubCloneAdhocTaskShouldBeDefined() {
        $xml = new SimpleXMLElement($this-&amp;gt;_buildfileXml);
        $xpath = "//target[@name='init-ad-hoc-tasks']/adhoc-task";
        $adhocElement = $xml-&amp;gt;xpath($xpath);
        $this-&amp;gt;assertSame('github-clone',
            trim($adhocElement[0]-&amp;gt;attributes()-&amp;gt;name), 
            "Ad hoc task 'github-clone' isn't defined"
        );
    }
    /**
    * @test 
    * @group artifact
    */
    public function initTargetShouldCreateInitialBuildArtifacts() {
        $this-&amp;gt;_isTearDownNecessary = true;
        $this-&amp;gt;_buildfileRunner-&amp;gt;runTarget(array('init'));

        $expectedInitArtifacts = array(
            "{$this-&amp;gt;_buildfileBasedir}/build", 
            "{$this-&amp;gt;_buildfileBasedir}/build/logs/performance/", 
            "{$this-&amp;gt;_buildfileBasedir}/build/doc",
            "{$this-&amp;gt;_buildfileBasedir}/build/reports"
        );

        foreach ($expectedInitArtifacts as $artifact) {
            $this-&amp;gt;assertFileExists($artifact, 
                "Expected file '{$artifact}' doesn't exist"
            );
        }
    }
    /**
     * @test
     * @group artifact
     */
    public function sqlFilesForDatabaseSetupTargetShouldBeAvailable() {
        $expectedSqlFiles = array(
            "{$this-&amp;gt;_buildfileBasedir}/sqlfiles", 
            "{$this-&amp;gt;_buildfileBasedir}/sqlfiles/session-storage.sql", 
            "{$this-&amp;gt;_buildfileBasedir}/sqlfiles/acl.sql", 
            "{$this-&amp;gt;_buildfileBasedir}/sqlfiles/log.sql"
        );

        foreach ($expectedSqlFiles as $sqlFile) {
            $this-&amp;gt;assertFileExists($sqlFile, 
                "SQL file '{$sqlFile}' doesn't exist"
            );
        }
    }
    /**
     * @test
     * @group artifact
     */
    public function githubTargetShouldFetchExpectedRepository() {
        $this-&amp;gt;_isTearDownNecessary = true;
        $this-&amp;gt;_buildfileRunner-&amp;gt;runTarget(array('github'));
        $expectedGitRepository = "{$this-&amp;gt;_buildfileBasedir}/build/"
            . "github-repos/phploc-phing/.git";
        $this-&amp;gt;assertFileExists($expectedGitRepository, 
            "Github target doesn't fetch the expected 'phploc-phing' repository"
        );
    }
}&lt;/pre&gt;The outro screenshot shows the above stated test class run against the example buildfile on a Mac OS X system utilizing the --colors option; which by the way comes in really handy in combination with &lt;a href="http://mikenaberezny.com/2007/09/04/faster-tdd-with-stakeout-rb/" target="_self"&gt;Stakeout.rb&lt;/a&gt; during the process of refactoring or extending/creating buildfiles the test-driven way.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.flickr.com/photos/raphaelstolt/3517905691/" title="PHPUnit console output by Raphael Stolt, on Flickr"&gt;&lt;img alt="PHPUnit console output" border="0" height="199" src="http://farm4.static.flickr.com/3559/3517905691_f044985a04_o.gif" width="550" /&gt;&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/UvEGicAWztc" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=5110082920105713427" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/5110082920105713427?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/5110082920105713427?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/05/testing-phing-buildfiles-with-phpunit.html" title="Testing Phing buildfiles with PHPUnit" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEMBQH86eSp7ImA9WxJTEEo.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-4677781334297408181</id><published>2009-04-18T02:48:00.006+02:00</published><updated>2009-04-18T18:34:11.111+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-04-18T18:34:11.111+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Creating and using Phing ad hoc tasks</title><content type="html">Sometimes there are build scenarios where you'll badly need a functionality, like adding a &lt;a href="http://en.wikipedia.org/wiki/MD5#Applications" target="_self"&gt;MD5&lt;/a&gt; checksum file to a given project, that isn't provided neither by the available Phing core nor the optional tasks. Phing supports developers with two ways for extending the useable task pool: by writing 'outline' tasks that will end up in a directory of the Phing installation or by utilizing the &lt;a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixB-CoreTasks.html#AdhocTaskdefTask" target="_self"&gt;AdhocTaskdefTask&lt;/a&gt;, which allows to define custom tasks in the buildfile itself. The following post will try to outline how to define and use these inline tasks, by sketching an ad hoc task that enables the build orchestra to clone Git repositories from GitHub during a hypothetical workbench setup.&lt;br /&gt;&lt;h4 class="custom"&gt;Creating the inline/ad hoc task&lt;/h4&gt;The AdhocTaskdefTask expects a name attribute i.e. &lt;em&gt;github-clone&lt;/em&gt; for the XML element which will later referr to the ad hoc task and a CDATA section hosting the task implementation. Similar to 'outline' tasks the ad hoc task extends Phing's Task class, configures the task via attributes and holds the logic to perform. Unfortunately inline task implementations don't allow to require or include external classes available in the &lt;em&gt;include_path&lt;/em&gt;, like &lt;a href="http://framework.zend.com/manual/en/zend.http.html" target="_self"&gt;Zend_Http_Client&lt;/a&gt; which I initially tried to use for an example task fetching short Urls from is.gd. This limits the available functions and classes to craft the task from to the ones built into PHP. The following buildfile snippet shows the implementation of the github-clone ad hoc task which is wrapped by a &lt;a href="http://raphaelstolt.blogspot.com/2008/03/getting-overview-of-all-targets.html" target="_self"&gt;private target&lt;/a&gt; to encourage reusability and limit it's &lt;a href="http://raphaelstolt.blogspot.com/2008/03/getting-overview-of-all-targets.html" target="_self"&gt;callability&lt;/a&gt;.&lt;pre class="xmlSnippet"&gt;&amp;lt;target name="-init-ad-hoc-tasks" &lt;br /&gt;        description="Initializes the ad hoc task(s)"&amp;gt;&lt;br /&gt;  &amp;lt;adhoc-task name="github-clone"&amp;gt;&amp;lt;![CDATA[&lt;br /&gt;      class Github_Clone extends Task {&lt;br /&gt;&lt;br /&gt;          private $repository = null;&lt;br /&gt;          private $destDirectory = null;&lt;br /&gt;&lt;br /&gt;          function setRepos($repository) {&lt;br /&gt;              $this-&amp;gt;repository = $repository;&lt;br /&gt;          } &lt;br /&gt;          function setDest($destDirectory) {&lt;br /&gt;              $this-&amp;gt;destDirectory = $destDirectory;&lt;br /&gt;          }&lt;br /&gt;          function main() {&lt;br /&gt;              // Get project name from repos Uri&lt;br /&gt;              $projectName = str_replace('.git', '', &lt;br /&gt;                  substr(strrchr($this-&amp;gt;repository, '/'), 1));&lt;br /&gt;&lt;br /&gt;              $gitCommand = 'git clone ' . $this-&amp;gt;repository . ' ' . &lt;br /&gt;                  $this-&amp;gt;destDirectory . '/' . $projectName;&lt;br /&gt;&lt;br /&gt;              exec(escapeshellcmd($gitCommand), $output, $return);&lt;br /&gt;&lt;br /&gt;              if ($return !== 0) {&lt;br /&gt;                  throw new BuildException('Git clone failed');&lt;br /&gt;              }&lt;br /&gt;              $logMessage = 'Cloned Git repository ' . $this-&amp;gt;repository . &lt;br /&gt;                  ' into ' . $this-&amp;gt;destDirectory . '/' . $projectName;&lt;br /&gt;              $this-&amp;gt;log($logMessage);&lt;br /&gt;          }&lt;br /&gt;      }&lt;br /&gt;  ]]&amp;gt;&amp;lt;/adhoc-task&amp;gt;&lt;br /&gt;  &amp;lt;echo message="Initialized github-clone ad hoc task." /&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Using the ad hoc task&lt;/h4&gt;With the ad hoc task in the place to be, it's provided functionality can now be used from any target using the tasks XML element according to the given name i.e. &lt;em&gt;github-clone&lt;/em&gt; in the AdhocTaskdefTask element earlier and by feeding it with the required attributes i.e. &lt;em&gt;repos&lt;/em&gt; and &lt;em&gt;dest&lt;/em&gt;. The next snippet allows you to take a peek at the complete buildfile with the ad hoc task in action.&lt;pre class="xmlSnippet"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;project name="recordshelf" default="init-work-bench" basedir="."&amp;gt;&lt;br /&gt;  &lt;br /&gt;  &amp;lt;property name="github.repos.dir" value="./github-repos" override="true" /&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;target name="init-work-bench" &lt;br /&gt;          depends="-init-ad-hoc-tasks, -clone-git-repos" &lt;br /&gt;          description="Initializes the hypothetical workbench"&amp;gt;&lt;br /&gt;    &amp;lt;echo message="Initialized workbench." /&amp;gt;&lt;br /&gt;  &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;target name="-clean-git-repos" &lt;br /&gt;          description="Removes old repositories before initializing a new workbench"&amp;gt;&lt;br /&gt;    &amp;lt;delete dir="${github.repos.dir}" includeemptydirs="true" failonerror="true" /&amp;gt;&lt;br /&gt;  &amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;target name="-init-ad-hoc-tasks" &lt;br /&gt;          description="Initializes the ad hoc task(s)"&amp;gt;&lt;br /&gt;    &amp;lt;adhoc-task name="github-clone"&amp;gt;&amp;lt;![CDATA[&lt;br /&gt;        class Github_Clone extends Task {&lt;br /&gt;&lt;br /&gt;            private $repository = null;&lt;br /&gt;            private $destDirectory = null;&lt;br /&gt;&lt;br /&gt;            function setRepos($repository) {&lt;br /&gt;                $this-&amp;gt;repository = $repository;&lt;br /&gt;            } &lt;br /&gt;            function setDest($destDirectory) {&lt;br /&gt;                $this-&amp;gt;destDirectory = $destDirectory;&lt;br /&gt;            }&lt;br /&gt;            function main() {&lt;br /&gt;                // Get project name from repos Uri&lt;br /&gt;                $projectName = str_replace('.git', '', &lt;br /&gt;                    substr(strrchr($this-&amp;gt;repository, '/'), 1));&lt;br /&gt;&lt;br /&gt;                $gitCommand = 'git clone ' . $this-&amp;gt;repository . ' ' . &lt;br /&gt;                    $this-&amp;gt;destDirectory . '/' . $projectName;&lt;br /&gt;&lt;br /&gt;                exec(escapeshellcmd($gitCommand), $output, $return);&lt;br /&gt;&lt;br /&gt;                if ($return !== 0) {&lt;br /&gt;                    throw new BuildException('Git clone failed');&lt;br /&gt;                }&lt;br /&gt;                $logMessage = 'Cloned Git repository ' . $this-&amp;gt;repository . &lt;br /&gt;                    ' into ' . $this-&amp;gt;destDirectory . '/' . $projectName;&lt;br /&gt;                $this-&amp;gt;log($logMessage);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    ]]&amp;gt;&amp;lt;/adhoc-task&amp;gt;&lt;br /&gt;    &amp;lt;echo message="Initialized github-clone ad hoc task." /&amp;gt;&lt;br /&gt;  &amp;lt;/target&amp;gt;&lt;br /&gt;  &lt;br /&gt;  &amp;lt;target name="-clone-git-repos" depends="-clean-git-repos"&lt;br /&gt;          description="Clones the needed Git repositories from GitHub"&amp;gt;&lt;br /&gt;    &amp;lt;github-clone repos="git://github.com/abc/abc.git" &lt;br /&gt;                  dest="${github.repos.dir}" /&amp;gt;&lt;br /&gt;    &amp;lt;github-clone repos="git://github.com/xyz/xyz.git" &lt;br /&gt;                  dest="${github.repos.dir}" /&amp;gt;&lt;br /&gt;  &amp;lt;/target&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/project&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Favouring inline over 'outline' tasks?&lt;/h4&gt;The one big advantage of using inline tasks over 'outline' tasks is that they are distributed with the buildfile and are instantly available without the need to modify the Phing installation. Some severe disadvantages of inline tasks are the limitation to use only the core PHP functions and classes for the implementation, the introduction of an additional hurdle to verify the task behaviour via PHPUnit as it's located in a CDATA section of the buildfile and the fact that the use of several inline tasks will blow up the buildfile, and thereby obfuscate the build flow.&lt;br /&gt;&lt;br /&gt;Regrettably Phing doesn't provide an &lt;a href="http://ant.apache.org/manual/CoreTasks/import.html" target="_self"&gt;import&lt;/a&gt; task like Ant which might enable a refactoring to pull the ad hoc task definitions into a seperate XML file and include them at buildtime; in case you might have some expertise or ideas for a suitable workaround hit me with a comment. So far I tried to get it working, with no success, by utilizing Phing's &lt;a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixB-CoreTasks.html#PhingTask"&gt;PhingTask&lt;/a&gt; and XML's &lt;a href="http://www.w3.org/TR/REC-xml/#sec-external-ent"&gt;external entities&lt;/a&gt; declaration.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/YfT5X6hpqo0" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=4677781334297408181" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4677781334297408181?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4677781334297408181?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/04/creating-and-using-phing-ad-hoc-tasks.html" title="Creating and using Phing ad hoc tasks" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;CEMDQXY6fip7ImA9WxJTEEo.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-1912712568674590257</id><published>2009-03-31T16:02:00.061+02:00</published><updated>2009-04-18T18:34:30.816+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-04-18T18:34:30.816+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="Rake" /><title>Using Haml &amp; Sass from a Rake task</title><content type="html">&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://farm4.static.flickr.com/3007/3401813196_6d7d035d7b_o.gif"&gt;&lt;img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 198px; height: 215px;" src="http://farm4.static.flickr.com/3007/3401813196_6d7d035d7b_o.gif" border="0" alt="Haml logo" title="Haml logo" border="0" /&gt;&lt;/a&gt;Some time ago I had the 'lightning' idea to implement &lt;a href="http://raphaelstolt.blogspot.com/2009/01/broadcasting-blog-post-notifications-to.html" target="_self"&gt;another&lt;/a&gt; Rake automation to support my current blogging workflow, which at the moment consists of finding a sparkling idea to blog about, write it out in &lt;a href="http://www.hogbaysoftware.com/products/writeroom" target="_self"&gt;WriteRoom&lt;/a&gt; and refine the post in &lt;a href="http://macromates.com/" target="_self"&gt;TextMate&lt;/a&gt; before publishing. As this process was a recurring and copy &amp; paste driven event, I strove for an automation supporting this workflow. So unsurprisingly the post will show my current solution to achieve this goal by utilizing &lt;a href="http://rake.rubyforge.org/" target="_self"&gt;Rake&lt;/a&gt;, &lt;a href="http://haml.hamptoncatlin.com" target="_self"&gt;Haml&lt;/a&gt; and &lt;a href="http://haml.hamptoncatlin.com/docs/rdoc/classes/Sass.html" target="_self"&gt;Sass&lt;/a&gt;.&lt;h4 class="custom"&gt;So what's that Haml and Sass thingy?&lt;/h4&gt;Haml (HTML Abstraction Markup Language) is a templating language/engine with the primary goal to make Markup &lt;a href="http://en.wikipedia.org/wiki/DRY" target="_self"&gt;DRY&lt;/a&gt;, beautiful and readable again. It has a very shallow learning curve and therefor is perfectly suited for programmers and designers alike. Haml is primarily targeted at making the views of &lt;a href="http://rubyonrails.org/" target="_self"&gt;Ruby on Rails&lt;/a&gt;, &lt;a href="http://merbivore.com/" target="_self"&gt;Merb&lt;/a&gt; or &lt;a href="http://www.sinatrarb.com/" target="_self"&gt;Sinatra&lt;/a&gt; web applications leaner, but as you will see later the Ruby implementation also can be used framework independently.&lt;br /&gt;&lt;br /&gt;Sass (Syntactically Awesome StyleSheets) is a module which comes bundled with Haml providing a meta-language/abstraction on top of CSS sharing the same goals and advantages as Haml.&lt;h4 class="custom"&gt;Gluing Haml and Sass into a Rake task&lt;/h4&gt;To get going you first have to install Haml and Sass by running the gem command shown next.&lt;pre class="consoleOutput"&gt;sudo gem install haml&lt;/pre&gt;With Haml and Sass available it's about time to identify and outline the parts you want to automate, in my case it's the creation of a WriteRoom and/or a XHTML draft document for initial editings. So the parameters to pass into the task to come are the targeted &lt;em&gt;editor(s)&lt;/em&gt;, the &lt;em&gt;title&lt;/em&gt; of the blog post to draft and a list of associated and whitespace separated &lt;em&gt;category tags&lt;/em&gt;.&lt;br /&gt;&lt;br /&gt;The XHTML document skeleton content and it's inline CSS are defined each in a separate Haml and Sass template file and will be rendered into the outcoming document along with the content passed into the Rake task. While the document skeleton for the WriteRoom draft document, due to it's brevity, is defined inside of the task itself. The following snippets are showing the mentioned Haml and Sass templates for the XHTML draft output file, which are located in the same directory as the Rake file.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus"&gt;&amp;nbsp;Haml&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;!!! 1.1&lt;br /&gt;%html&lt;br /&gt;  %head &lt;br /&gt;    %title= "&lt;em&gt;#{title}&lt;/em&gt; - Draft"&lt;br /&gt;    %style{ :type =&gt; 'text/css' }= &lt;em&gt;inline_css&lt;/em&gt;&lt;br /&gt;  %body&lt;br /&gt;    %h3= &lt;em&gt;title&lt;/em&gt;&lt;br /&gt;    %h4.custom sub headline &lt;br /&gt;    %pre.consoleOutput console command&lt;br /&gt;    %pre.codeSnippet code snippet&lt;br /&gt;    %br/&lt;br /&gt;    = "Tags: &lt;em&gt;#{tags.join ', '}&lt;/em&gt;"&lt;/pre&gt;&lt;div class="refactoringStatus"&gt;&amp;nbsp;Sass&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;body&lt;br /&gt;  :margin 5&lt;br /&gt;  :line-height 1.5em&lt;br /&gt;  :font small Trebuchet MS, Verdana, Arial, Sans-serif&lt;br /&gt;  :color #000000&lt;br /&gt;h4&lt;br /&gt;  :margin-bottom 0.3em&lt;br /&gt;.consoleOutput&lt;br /&gt;  :padding 6px &lt;br /&gt;  :background-color #000 &lt;br /&gt;  :color rgb(20, 218, 62)&lt;br /&gt;  :font-size 12px&lt;br /&gt;  :font-weight bolder&lt;br /&gt;.codeSnippet&lt;br /&gt;  :padding 3px&lt;br /&gt;  :background-color rgb(243, 243, 243)&lt;br /&gt;  :color rgb(93, 91, 91)&lt;br /&gt;  :font-size small&lt;br /&gt;  :border 1px solid #6A6565&lt;/pre&gt;To inject the dynamic content into the Haml template and have it rendered into the outcoming document, the values i.e. &lt;em&gt;draft_title&lt;/em&gt;, &lt;em&gt;draft_tags&lt;/em&gt; and &lt;em&gt;draft_inline_css&lt;/em&gt; have to be made available to the template engine by passing them in a bundling Hash into the  &lt;em&gt;to_html&lt;/em&gt; alias method of the Haml Engine object like shown in the next Rake task.&lt;pre class="codeSnippet"&gt;task :default do&lt;br /&gt;  Rake::Task['blog_utils:create_draft_doc'].invoke&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;namespace :blog_utils do&lt;br /&gt;  &lt;br /&gt;  desc 'Create a new draft document for a given title, category tags and editor'&lt;br /&gt;  task :create_draft_doc, [:title, :tags, :editor] do |t, args|&lt;br /&gt;    draft_title = args.title&lt;br /&gt;    draft_tags = args.tags.split(' ')&lt;br /&gt;    draft_target_editor = args.editor&lt;br /&gt;    &lt;br /&gt;    raise_message = 'No title for draft provided'&lt;br /&gt;    raise raise_message if draft_title.nil?&lt;br /&gt;    &lt;br /&gt;    raise_message = 'No tags for draft provided'&lt;br /&gt;    raise raise_message if draft_tags.nil?&lt;br /&gt;    &lt;br /&gt;    draft_target_editor = '*' if draft_target_editor.nil?&lt;br /&gt;    &lt;br /&gt;    raise_message = 'Unsupported target editor provided' &lt;br /&gt;    raise raise_message unless draft_target_editor == 'Textmate' || &lt;br /&gt;      draft_target_editor == 'Writeroom' || draft_target_editor == '*'&lt;br /&gt;    &lt;br /&gt;    if draft_target_editor == 'Writeroom' || draft_target_editor == '*'&lt;br /&gt;      draft_output_file = draft_title.gsub(' ', '_') + '.txt'&lt;br /&gt;      &lt;br /&gt;      File.open(draft_output_file, 'w') do |draft_file_txt|&lt;br /&gt;        draft_file_txt.puts draft_title&lt;br /&gt;        draft_file_txt.puts&lt;br /&gt;        draft_file_txt.puts "Tags: #{draft_tags.join ', '}"&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    if draft_target_editor == 'Textmate' || draft_target_editor == '*'&lt;br /&gt;      &lt;br /&gt;      template_sass_content, template_haml_content = ''&lt;br /&gt;      &lt;br /&gt;      ['haml', 'sass'].each do |template_type|&lt;br /&gt;        template = File.dirname(__FILE__) + "/draft_template.#{template_type}"&lt;br /&gt;        raise_message = "#{template_type.capitalize} template '#{template}' not found"&lt;br /&gt;        raise raise_message if !File.exists?(template)&lt;br /&gt;        &lt;br /&gt;        template_sass_content = File.read(template) if template_type === 'sass'&lt;br /&gt;        template_haml_content = File.read(template) if template_type === 'haml'&lt;br /&gt;      end&lt;br /&gt;      &lt;em&gt;&lt;br /&gt;      require 'sass'&lt;br /&gt;      require 'haml'&lt;br /&gt;&lt;br /&gt;      draft_inline_css = Sass::Engine.new(template_sass_content).to_css&lt;br /&gt;      draft_document_content = Haml::Engine.new(template_haml_content).to_html(&lt;br /&gt;        Object.new, { :title =&gt; draft_title , :tags =&gt; draft_tags ,&lt;br /&gt;          :inline_css =&gt; draft_inline_css } )&lt;/em&gt;&lt;br /&gt;      &lt;br /&gt;      draft_output_file = draft_title.gsub(' ', '_') + '.html'      &lt;br /&gt;      File.open(draft_output_file, 'w') do |draft_file_html|&lt;br /&gt;        draft_file_html.puts(draft_document_content)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;  end&lt;br /&gt;end&lt;/pre&gt;&lt;h4 class="custom"&gt;Easing invocation pain with alias&lt;/h4&gt;Now as the Rake task is implemented and waiting for demands it can be invoked by calling the task as shown in the next console snippet. &lt;pre class="consoleOutput"&gt;sudo rake -f $HOME/Automations/Rakefile.rb blog_utils:create_draft_doc['Title','Tag1 TagN','Editor']&lt;/pre&gt;As I'm not even close to being a console ninja and probably will have forgotten the task call structure before initiating the next blog post, I decided to add an easing and more memorizable alias to &lt;em&gt;$HOME/.profile&lt;/em&gt; as shown next.&lt;pre class="codeSnippet"&gt;alias createdraft='sudo rake -f $HOME/Automations/Rakefile.rb blog_utils:create_draft_doc[$title,$tags,$editor]'&lt;/pre&gt;The created alias now allows to invoke the Rake task in a nice and easy way as shown in the next console command.&lt;pre class="consoleOutput"&gt;createdraft title='Using Haml &amp; Sass from a Rake task' tags='Rake Ruby' editor='Textmate'&lt;/pre&gt;&lt;h4 class="custom"&gt;Taking a peek at the generated draft document&lt;/h4&gt;After running the described Rake task I end up with the XHTML document shown in the outro code snippet, which then can be used for the further editing process. Of course I could have setup a &lt;a href="http://raphaelstolt.blogspot.com/2008/02/creating-zend-framework-snippets-for.html" target="_self"&gt;TextMate Snippet&lt;/a&gt; to get me going, but that way I would have missed the opportunity to mess around with another amazing Ruby tool.&lt;pre class="codeSnippet"&gt;&amp;lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&amp;gt;&lt;br /&gt;&amp;lt;html&amp;gt;&lt;br /&gt;  &amp;lt;head&amp;gt;&lt;br /&gt;    &amp;lt;title&amp;gt;&lt;em&gt;Using Haml &amp; Sass from a Rake task&lt;/em&gt; - Draft&amp;lt;/title&amp;gt;&lt;br /&gt;    &amp;lt;style type='text/css'&amp;gt;&lt;br /&gt;      &lt;em&gt;body {&lt;br /&gt;        margin: 5;&lt;br /&gt;        line-height: 1.5em;&lt;br /&gt;        font: small Trebuchet MS, Verdana, Arial, Sans-serif;&lt;br /&gt;        color: #000000; }&lt;br /&gt;&lt;br /&gt;      h4 {&lt;br /&gt;        margin-bottom: 0.3em; }&lt;br /&gt;&lt;br /&gt;      .consoleOutput {&lt;br /&gt;        padding: 6px;&lt;br /&gt;        background-color: #000;&lt;br /&gt;        color: rgb(20, 218, 62);&lt;br /&gt;        font-size: 12px;&lt;br /&gt;        font-weight: bolder; }&lt;br /&gt;      &lt;br /&gt;      .codeSnippet {&lt;br /&gt;        padding: 3px;&lt;br /&gt;        background-color: rgb(243, 243, 243);&lt;br /&gt;        color: rgb(93, 91, 91);&lt;br /&gt;        font-size: small;&lt;br /&gt;        border: 1px solid #6A6565; }&lt;/em&gt;&lt;br /&gt;    &amp;lt;/style&amp;gt;&lt;br /&gt;  &amp;lt;/head&amp;gt;&lt;br /&gt;  &amp;lt;body&amp;gt;&lt;br /&gt;    &amp;lt;h3&amp;gt;&lt;em&gt;Using Haml &amp; Sass from a Rake task&lt;/em&gt;&amp;lt;/h3&amp;gt;&lt;br /&gt;    &amp;lt;h4&amp;gt;sub headline&amp;lt;/h4&amp;gt;&lt;br /&gt;    &amp;lt;pre class='consoleOutput'&amp;gt;console command&amp;lt;/pre&amp;gt;&lt;br /&gt;    &amp;lt;pre class='codeSnippet'&amp;gt;code snippet&amp;lt;/pre&amp;gt;&lt;br /&gt;    &amp;lt;br /&amp;gt;&lt;br /&gt;    Tags: &lt;em&gt;Rake, Ruby&lt;/em&gt;&lt;br /&gt;  &amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/xxIO7herpB8" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=1912712568674590257" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/1912712568674590257?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/1912712568674590257?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/03/using-haml-sass-from-rake-task.html" title="Using Haml &amp;amp; Sass from a Rake task" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEMNQHY4cCp7ImA9WxJTEEo.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-6206018638608847732</id><published>2009-02-22T14:35:00.007+01:00</published><updated>2009-04-18T18:34:51.838+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-04-18T18:34:51.838+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Phplocing your projects with Phing</title><content type="html">When I started to play around with &lt;a href="http://rubyonrails.org/" target="_self"&gt;Ruby on Rails&lt;/a&gt;, my attention got somehow soon drawn to it's &lt;a href="http://rake.rubyforge.org/" target="_self"&gt;Rake&lt;/a&gt; stats task, which provides developers or more likely project managers with an overview of the actual project size. Exactly one month ago Sebastian Bergmann, of PHPUnit fame, started to implement a similar tool dubbed &lt;a href="http://github.com/sebastianbergmann/phploc/tree/master" target="_self"&gt;phploc&lt;/a&gt; which can give you an overview of the size for any given PHP project. As I wanted to automate the invocation of this handy tool and collect it's report output out of a &lt;a href="http://phing.info/trac/" target="_self"&gt;Phing&lt;/a&gt; buildfile, I invested some time to develop a custom Phing task doing so. Thereby the following post will show you a possible implementation of this task and it's use in a buildfile.&lt;h4 class="custom"&gt;Installing phploc&lt;/h4&gt;To setup phploc on your system simply install the phploc PEAR package available from the &lt;em&gt;pear.phpunit.de&lt;/em&gt; channel as shown in the next commands. In case you already have installed PHPUnit via PEAR you can omit the channel-discover command.&lt;pre class="consoleOutput"&gt;sudo pear channel-discover pear.phpunit.de&lt;br /&gt;sudo pear install phpunit/phploc&lt;/pre&gt;&lt;h4 class="custom"&gt;Implementing the phploc task&lt;/h4&gt;As I already blogged about &lt;a href="http://raphaelstolt.blogspot.com/2007/03/rolling-your-own-phing-task.html" target="_self"&gt;developing custom Phing task&lt;/a&gt; I'm only going to show the actual implementation and not dive into any details; alternatively you can also grab it from &lt;a href="http://github.com/raphaelstolt/phploc-phing/tree/master" target="_self"&gt;this&lt;/a&gt; public GitHub repository.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once 'phing/Task.php';&lt;br /&gt;require_once 'phing/BuildException.php';&lt;br /&gt;require_once 'PHPLOC/Analyser.php';&lt;br /&gt;require_once 'PHPLOC/Util/FilterIterator.php';&lt;br /&gt;require_once 'PHPLOC/TextUI/ResultPrinter.php';&lt;br /&gt;&lt;br /&gt;class PHPLocTask extends Task&lt;br /&gt;{&lt;br /&gt;    protected $suffixesToCheck = null;&lt;br /&gt;    protected $acceptedReportTypes = null;&lt;br /&gt;    protected $reportDirectory = null;&lt;br /&gt;    protected $reportType = null;&lt;br /&gt;    protected $fileToCheck = null;&lt;br /&gt;    protected $filesToCheck = null;&lt;br /&gt;    protected $reportFileName = null;&lt;br /&gt;    protected $fileSets = null;&lt;br /&gt;    &lt;br /&gt;    public function init() {&lt;br /&gt;        $this-&amp;gt;suffixesToCheck = array('php');&lt;br /&gt;        $this-&amp;gt;acceptedReportTypes = array('cli', 'txt', 'xml');&lt;br /&gt;        $this-&amp;gt;reportType = 'cli';&lt;br /&gt;        $this-&amp;gt;reportFileName = 'phploc-report';&lt;br /&gt;        $this-&amp;gt;fileSets = array();&lt;br /&gt;        $this-&amp;gt;filesToCheck = array();&lt;br /&gt;    }&lt;br /&gt;    public function setSuffixes($suffixListOrSingleSuffix) {&lt;br /&gt;        if (stripos($suffixListOrSingleSuffix, ',')) {&lt;br /&gt;            $suffixes = explode(',', $suffixListOrSingleSuffix);&lt;br /&gt;            $this-&amp;gt;suffixesToCheck = array_map('trim', $suffixes);&lt;br /&gt;        } else {&lt;br /&gt;            array_push($this-&amp;gt;suffixesToCheck, trim($suffixListOrSingleSuffix));&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    public function setFile(PhingFile $file) {&lt;br /&gt;        $this-&amp;gt;fileToCheck = trim($file);&lt;br /&gt;    }&lt;br /&gt;    public function createFileSet() {&lt;br /&gt;        $num = array_push($this-&amp;gt;fileSets, new FileSet());&lt;br /&gt;        return $this-&amp;gt;fileSets[$num - 1];&lt;br /&gt;    }&lt;br /&gt;    public function setReportType($type) {&lt;br /&gt;        $this-&amp;gt;reportType = trim($type);&lt;br /&gt;    }&lt;br /&gt;    public function setReportName($name) {&lt;br /&gt;        $this-&amp;gt;reportFileName = trim($name);&lt;br /&gt;    }&lt;br /&gt;    public function setReportDirectory($directory) {&lt;br /&gt;        $this-&amp;gt;reportDirectory = trim($directory);&lt;br /&gt;    }&lt;br /&gt;    public function main() {       &lt;br /&gt;        if (!isset($this-&amp;gt;fileToCheck) &amp;&amp; count($this-&amp;gt;fileSets) === 0) {&lt;br /&gt;            $exceptionMessage = "Missing either a nested fileset or the "&lt;br /&gt;                . "attribute 'file' set.";&lt;br /&gt;            throw new BuildException($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        if (count($this-&amp;gt;suffixesToCheck) === 0) {&lt;br /&gt;            throw new BuildException("No file suffix defined.");&lt;br /&gt;        }&lt;br /&gt;        if (is_null($this-&amp;gt;reportType)) {&lt;br /&gt;            throw new BuildException("No report type defined.");&lt;br /&gt;        }&lt;br /&gt;        if (!is_null($this-&amp;gt;reportType) &amp;&amp; &lt;br /&gt;            !in_array($this-&amp;gt;reportType, $this-&amp;gt;acceptedReportTypes)) {&lt;br /&gt;            throw new BuildException("Unaccepted report type defined.");&lt;br /&gt;        }&lt;br /&gt;        if (!is_null($this-&amp;gt;fileToCheck) &amp;&amp; !file_exists($this-&amp;gt;fileToCheck)) {&lt;br /&gt;            throw new BuildException("File to check doesn't exist.");&lt;br /&gt;        }&lt;br /&gt;        if ($this-&amp;gt;reportType !== 'cli' &amp;&amp; is_null($this-&amp;gt;reportDirectory)) {&lt;br /&gt;            throw new BuildException("No report output directory defined.");&lt;br /&gt;        }&lt;br /&gt;        if (count($this-&amp;gt;fileSets) &amp;gt; 0 &amp;&amp; !is_null($this-&amp;gt;fileToCheck)) {&lt;br /&gt;            $exceptionMessage = "Either use a nested fileset or 'file' " &lt;br /&gt;                . "attribute; not both.";&lt;br /&gt;            throw new BuildException($exceptionMessage);&lt;br /&gt;        }&lt;br /&gt;        if (!is_null($this-&amp;gt;reportDirectory) &amp;&amp; !is_dir($this-&amp;gt;reportDirectory)) {&lt;br /&gt;            $reportOutputDir = new PhingFile($this-&amp;gt;reportDirectory);&lt;br /&gt;            $logMessage = "Report output directory does't exist, creating: " &lt;br /&gt;                . $reportOutputDir-&amp;gt;getAbsolutePath() . '.';&lt;br /&gt;            $this-&amp;gt;log($logMessage);&lt;br /&gt;            $reportOutputDir-&amp;gt;mkdirs();&lt;br /&gt;        }&lt;br /&gt;        if ($this-&amp;gt;reportType !== 'cli') {&lt;br /&gt;            $this-&amp;gt;reportFileName.= '.' . trim($this-&amp;gt;reportType);&lt;br /&gt;        }&lt;br /&gt;        if (count($this-&amp;gt;fileSets) &amp;gt; 0) {&lt;br /&gt;            $project = $this-&amp;gt;getProject();&lt;br /&gt;            foreach ($this-&amp;gt;fileSets as $fileSet) {&lt;br /&gt;                $directoryScanner = $fileSet-&amp;gt;getDirectoryScanner($project);&lt;br /&gt;                $files = $directoryScanner-&amp;gt;getIncludedFiles();&lt;br /&gt;                $directory = $fileSet-&amp;gt;getDir($this-&amp;gt;project)-&amp;gt;getPath();&lt;br /&gt;                foreach ($files as $file) {&lt;br /&gt;                    if ($this-&amp;gt;isFileSuffixSet($file)) {&lt;br /&gt;                        $this-&amp;gt;filesToCheck[] = $directory . DIRECTORY_SEPARATOR &lt;br /&gt;                            . $file;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            $this-&amp;gt;filesToCheck = array_unique($this-&amp;gt;filesToCheck);&lt;br /&gt;        }&lt;br /&gt;        if (!is_null($this-&amp;gt;fileToCheck)) {&lt;br /&gt;            if (!$this-&amp;gt;isFileSuffixSet($file)) {&lt;br /&gt;                $exceptionMessage = "Suffix of file to check is not defined in"&lt;br /&gt;                    . " 'suffixes' attribute.";&lt;br /&gt;                throw new BuildException($exceptionMessage);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        $this-&amp;gt;runPhpLocCheck();&lt;br /&gt;    }&lt;br /&gt;    protected function isFileSuffixSet($filename) {&lt;br /&gt;        $pathinfo = pathinfo($filename);&lt;br /&gt;        $fileSuffix = $pathinfo['extension'];&lt;br /&gt;        return in_array($fileSuffix, $this-&amp;gt;suffixesToCheck);&lt;br /&gt;    }&lt;br /&gt;    protected function runPhpLocCheck() {&lt;br /&gt;        $files = $this-&amp;gt;getFilesToCheck();&lt;br /&gt;        $result = $this-&amp;gt;getCountForFiles($files); &lt;br /&gt;        &lt;br /&gt;        if ($this-&amp;gt;reportType === 'cli' || $this-&amp;gt;reportType === 'txt') {&lt;br /&gt;            $printer = new PHPLOC_TextUI_ResultPrinter;&lt;br /&gt;            if ($this-&amp;gt;reportType === 'txt') {&lt;br /&gt;                ob_start();&lt;br /&gt;                $printer-&amp;gt;printResult($result);&lt;br /&gt;                file_put_contents($this-&amp;gt;reportDirectory &lt;br /&gt;                    . DIRECTORY_SEPARATOR . $this-&amp;gt;reportFileName, &lt;br /&gt;                        ob_get_contents());&lt;br /&gt;                ob_end_clean();&lt;br /&gt;                $reportDir = new PhingFile($this-&amp;gt;reportDirectory);&lt;br /&gt;                $logMessage = "Writing report to: " &lt;br /&gt;                    . $reportDir-&amp;gt;getAbsolutePath() . DIRECTORY_SEPARATOR &lt;br /&gt;                        . $this-&amp;gt;reportFileName;&lt;br /&gt;                $this-&amp;gt;log($logMessage);&lt;br /&gt;            } else {&lt;br /&gt;                $printer-&amp;gt;printResult($result);&lt;br /&gt;            }&lt;br /&gt;        } elseif ($this-&amp;gt;reportType === 'xml') {&lt;br /&gt;            $xml = $this-&amp;gt;getResultAsXml($result);&lt;br /&gt;            $reportDir = new PhingFile($this-&amp;gt;reportDirectory);&lt;br /&gt;            $logMessage = "Writing report to: " . $reportDir-&amp;gt;getAbsolutePath()&lt;br /&gt;                . DIRECTORY_SEPARATOR . $this-&amp;gt;reportFileName;&lt;br /&gt;            $this-&amp;gt;log($logMessage);&lt;br /&gt;            file_put_contents($this-&amp;gt;reportDirectory . DIRECTORY_SEPARATOR&lt;br /&gt;                . $this-&amp;gt;reportFileName, $xml);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    protected function getFilesToCheck() {&lt;br /&gt;        if (count($this-&amp;gt;filesToCheck) &amp;gt; 0) {&lt;br /&gt;            $files = array();&lt;br /&gt;            foreach ($this-&amp;gt;filesToCheck as $file) {&lt;br /&gt;                $files[] = new SPLFileInfo($file);&lt;br /&gt;            }&lt;br /&gt;        } elseif (!is_null($this-&amp;gt;fileToCheck)) {&lt;br /&gt;            $files = array(new SPLFileInfo($this-&amp;gt;fileToCheck));&lt;br /&gt;        }&lt;br /&gt;        return $files;&lt;br /&gt;    }&lt;br /&gt;    protected function getCountForFiles($files) {&lt;br /&gt;        $count = array('files' =&amp;gt; 0, 'loc' =&amp;gt; 0, 'cloc' =&amp;gt; 0, 'ncloc' =&amp;gt; 0,&lt;br /&gt;            'eloc' =&amp;gt; 0, 'interfaces' =&amp;gt; 0, 'classes' =&amp;gt; 0, 'functions' =&amp;gt; 0);&lt;br /&gt;        $directories = array();&lt;br /&gt;&lt;br /&gt;        foreach ($files as $file) {&lt;br /&gt;            $directory = $file-&amp;gt;getPath();&lt;br /&gt;            if (!isset($directories[$directory])) {&lt;br /&gt;                $directories[$directory] = TRUE;&lt;br /&gt;            }          &lt;br /&gt;            PHPLOC_Analyser::countFile($file-&amp;gt;getPathName(), $count);&lt;br /&gt;        }&lt;br /&gt;        &lt;br /&gt;        if (!function_exists('parsekit_compile_file')) {&lt;br /&gt;            unset($count['eloc']);&lt;br /&gt;        }&lt;br /&gt;        $count['directories'] = count($directories) - 1;&lt;br /&gt;        return $count;&lt;br /&gt;    }&lt;br /&gt;    protected function getResultAsXml($result) {        &lt;br /&gt;        $newline = "\n";&lt;br /&gt;        $newlineWithSpaces = sprintf("\n%4s",'');&lt;br /&gt;        $xml = '&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;';&lt;br /&gt;        $xml.= $newline . '&amp;lt;phploc&amp;gt;'; &lt;br /&gt;        &lt;br /&gt;        if ($result['directories'] &amp;gt; 0) {&lt;br /&gt;            $xml.= $newlineWithSpaces . '&amp;lt;directories&amp;gt;' . $result['directories'] . '&amp;lt;/directories&amp;gt;';&lt;br /&gt;            $xml.= $newlineWithSpaces . '&amp;lt;files&amp;gt;' . $result['files'] . '&amp;lt;/files&amp;gt;';&lt;br /&gt;        }&lt;br /&gt;        $xml.= $newlineWithSpaces . '&amp;lt;loc&amp;gt;' . $result['loc'] . '&amp;lt;/loc&amp;gt;';&lt;br /&gt;        &lt;br /&gt;        if (isset($result['eloc'])) {&lt;br /&gt;            $xml.= $newlineWithSpaces . '&amp;lt;eloc&amp;gt;' . $result['eloc'] . '&amp;lt;/eloc&amp;gt;';&lt;br /&gt;        }&lt;br /&gt;        $xml.= $newlineWithSpaces . '&amp;lt;cloc&amp;gt;' . $result['cloc'] . '&amp;lt;/cloc&amp;gt;';&lt;br /&gt;        $xml.= $newlineWithSpaces . '&amp;lt;ncloc&amp;gt;' . $result['ncloc'] . '&amp;lt;/ncloc&amp;gt;';&lt;br /&gt;        $xml.= $newlineWithSpaces . '&amp;lt;interfaces&amp;gt;' . $result['interfaces'] . '&amp;lt;/interfaces&amp;gt;';&lt;br /&gt;        $xml.= $newlineWithSpaces . '&amp;lt;classes&amp;gt;' . $result['classes'] . '&amp;lt;/classes&amp;gt;';&lt;br /&gt;        $xml.= $newlineWithSpaces . '&amp;lt;methods&amp;gt;' . $result['functions'] . '&amp;lt;/methods&amp;gt;' . $newline;&lt;br /&gt;        $xml.= '&amp;lt;/phploc&amp;gt;';&lt;br /&gt;        return $xml;&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Hooking the phploc task into Phing&lt;/h4&gt;To use the task in your Phing builds simply copy it into the &lt;em&gt;phing/tasks/my&lt;/em&gt; directory and make it available via the &lt;a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixB-CoreTasks.html#TaskdefTask" target="_self"&gt;taskdef task&lt;/a&gt;. The next table shows the available task attributes and the values they can take to configure it's behaviour and output. As you will see it also provides the ability to generate reports in a XML format; I chose to implement this feature to have the possibilty to transform the report results into HTML documents by applying for example a XSLT stylesheet. This way they can provide more value to non-technical project members or can be made accessible in a CI system dashboard if desired.&lt;br /&gt;&lt;br /&gt;&lt;table class="listing" cellpadding="1"cellspacing="1"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Default&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="content" valign="top"&gt;reportType&lt;/td&gt;&lt;td class="content" valign="top"&gt;string&lt;/td&gt;&lt;td class="content" valign="top"&gt;The type of the report. Available types are cli|txt|xml.&lt;/td&gt;&lt;td class="content" valign="top"&gt;cli&lt;/td&gt;&lt;td class="content" valign="top"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="content" valign="top"&gt;reportName&lt;/td&gt;&lt;td class="content" valign="top"&gt;string&lt;/td&gt;&lt;td class="content" valign="top"&gt;The name of the report type without a file extension.&lt;/td&gt;&lt;td class="content" valign="top"&gt;phploc-report&lt;/td&gt;&lt;td class="content" valign="top"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="content" valign="top"&gt;reportDirectory&lt;/td&gt;&lt;td class="content" valign="top"&gt;string&lt;/td&gt;&lt;td class="content" valign="top"&gt;The directory to write the report file to.&lt;/td&gt;&lt;td class="content" valign="top"&gt;false&lt;/td&gt;&lt;td class="content" valign="top"&gt;Yes, when report type txt or xml is defined.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="content" valign="top"&gt;file&lt;/td&gt;&lt;td class="content" valign="top"&gt;string&lt;/td&gt;&lt;td class="content" valign="top"&gt;The name of the file to check.&lt;/td&gt;&lt;td class="content" valign="top"&gt;n/a&lt;/td&gt;&lt;td class="content" valign="top"&gt;Yes, when no nested fileset is defined.&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="content" valign="top"&gt;suffixes&lt;/td&gt;&lt;td class="content" valign="top"&gt;string&lt;/td&gt;&lt;td class="content" valign="top"&gt;A comma-separated list of file suffixes to check.&lt;/td&gt;&lt;td class="content" valign="top"&gt;php&lt;/td&gt;&lt;td class="content" valign="top"&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Supported Nested Tags:&lt;/span&gt;&lt;br /&gt;&lt;ul type="square" style="padding-left: 25px; line-height: 15px;"&gt;&lt;li&gt;fileset&lt;/li&gt;&lt;/ul&gt;The closing buildfile extract shows an example phploc task configuration and is also available at the &lt;a href="http://github.com/raphaelstolt/phploc-phing/tree/master" target="_self"&gt;public GitHub repository&lt;/a&gt;. Happy phplocing!&lt;pre class="xmlSnippet"&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;&amp;lt;project name="example" default="phploc" basedir="."&amp;gt;&lt;br /&gt;    &amp;lt;taskdef name="phploc" classname="phing.tasks.my.PHPLocTask" /&amp;gt;&lt;br /&gt;    &amp;lt;target name="phploc"&amp;gt;&lt;br /&gt;      &amp;lt;tstamp&amp;gt;&lt;br /&gt;        &amp;lt;format property="check.date.time" pattern="%Y%m%d-%H%M%S" locale="en_US"/&amp;gt;&lt;br /&gt;      &amp;lt;/tstamp&amp;gt;&lt;br /&gt;      &amp;lt;phploc reportType="txt" reportName="${check.date.time}-report"&lt;br /&gt;              reportDirectory="phploc-reports"&amp;gt;&lt;br /&gt;        &amp;lt;fileset dir="."&amp;gt;&lt;br /&gt;          &amp;lt;include name="**/*.php" /&amp;gt;&lt;br /&gt;          &amp;lt;include name="*.php" /&amp;gt;&lt;br /&gt;        &amp;lt;/fileset&amp;gt;&lt;br /&gt;      &amp;lt;/phploc&amp;gt;&lt;br /&gt;    &amp;lt;/target&amp;gt;&lt;br /&gt;&amp;lt;/project&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/qjZVsS94wcg" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=6206018638608847732" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6206018638608847732?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6206018638608847732?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/02/phplocing-your-projects-with-phing.html" title="Phplocing your projects with Phing" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;DEYCRng_eip7ImA9WxJSGUs.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-8847983296330423769</id><published>2009-01-24T06:08:00.032+01:00</published><updated>2009-05-10T16:36:07.642+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-05-10T16:36:07.642+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Ruby" /><category scheme="http://www.blogger.com/atom/ns#" term="Rake" /><title>Broadcasting blog post notifications to Twitter with Ruby and Rake</title><content type="html">&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.flickr.com/photos/raphaelstolt/3228209607/" title="Blogger to Twitter Logo by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm4.static.flickr.com/3421/3228209607_f9e31cfb5b_m.jpg" style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 240px; height: 103px;" width="240" height="103" border="0" alt="Blogger to Twitter Logo" /&gt;&lt;/a&gt;During my latest blogging absence I had some time to tinker around with Ruby. For an introductory challenge I chose to implement a real life feature which currently isn't supported by Blogger.com and screams siren-like for an one-button automation: Broadcasting the latest blog entry to my Twitter account. As I didn't want to sign up for a &lt;a href="http://twitterfeed.com/" target="_self"&gt;Twitterfeed&lt;/a&gt; account and couldn't resort to the &lt;a href="http://alexking.org/projects/wordpress" target="_self"&gt;Twitter Tools&lt;/a&gt; plugin like WordPress users, I had to perform these broadcasting steps manually, until now. To see how this repetitive and time-stealing process was transformed into a semi-automated one by utilizing Ruby, a splash of &lt;a href="http://code.whytheluckystiff.net/hpricot/" target="_self"&gt;Hpricot&lt;/a&gt;, Ruby's excellent &lt;a href="http://twitter.rubyforge.org/rdoc/" target="_self"&gt;Twitter Api wrapper&lt;/a&gt; and &lt;a href="http://rake.rubyforge.org/" target="_self"&gt;Rake&lt;/a&gt;, read on my dear.&lt;h4 class="custom"&gt;Installing the required RubyGems&lt;/h4&gt;Prior to diving into the implementation details of the given scenario I had to install the required &lt;a href="http://www.rubygems.org/" target="_self"&gt;RubyGems&lt;/a&gt; like shown in the next console snippet. The installation of the twitter gem might take a while due to it's dependency on several other gems.&lt;pre class="consoleOutput"&gt;sudo gem install hpricot rake twitter&lt;/pre&gt;&lt;h4 class="custom"&gt;Scraping the latest blog post details with Hpricot&lt;/h4&gt;The initial implementation step was to gather relevant metadata (Url, title and used tags) of the latest blog post. I first took the route to get it by grabbing the blog's RSS feed and extracting the metadata from there, but soon stumbled into problems getting an outdated feed from Feedburner. The next alternative was to scrape the needed metadata directly from the blog landing page. As I went this route &lt;a href="http://raphaelstolt.blogspot.com/2008/10/scraping-websites-with-zenddomquery.html" target="_self"&gt;before&lt;/a&gt; with the Zend_Dom_Query component of the Zend Framework I decided to use something similar from the Ruby toolbox. Some Google hops later I was sold to Hpricot, a HTML Parser for Ruby and as you can see in the first code snippet, showing an extract of the Rake file to come, this is done in just 13 lines of code.&lt;pre class="codeSnippet"&gt;doc = Hpricot(open(blog_landing_page, scrape_options))&lt;br /&gt;latest_post_url = doc.at('h3.post-title &gt; a')['href']&lt;br /&gt;latest_post_title = doc.at('h3.post-title &gt; a').inner_html&lt;br /&gt;label_doc = Hpricot(doc.search('span.post-labels').first.to_s)&lt;br /&gt;label_links = label_doc.search('span.post-labels &gt; a').each do |label_link|&lt;br /&gt;  label = label_link.inner_html.gsub(' ', '').downcase&lt;br /&gt;  if label.include?('/')&lt;br /&gt;    labels = label.split('/')&lt;br /&gt;    labels.each { |label| last_post_labels.push(label) }&lt;br /&gt;  else&lt;br /&gt;    last_post_labels.push(label)&lt;br /&gt;  end&lt;br /&gt;end&lt;/pre&gt;&lt;h4 class="custom"&gt;Outstanding tasks&lt;/h4&gt;With the metadata available the oustanding tasks to implement were:&lt;ul type="square" style="padding-left: 25px; line-height: 15px;"&gt;&lt;li&gt;to get a short Url for the actual blog post by utilzing a public API of an Url shortening service i.e. &lt;a href="http://is.gd/" target="_self"&gt;is.gd&lt;/a&gt;&lt;/li&gt;&lt;li&gt;to build the tweet to broadcast by injecting the available metadata into a tweet template&lt;/li&gt;&lt;li&gt;to broadcast the notification tweet to the given Twitter account&lt;/li&gt;&lt;li&gt;to log the broadcasted blog title to prevent spamming or duplication scenarios&lt;/li&gt;&lt;/ul&gt;As a guy sold to build tools and eager to learn something new I subverted Rake, Ruby's number one build language, to glue the above mentioned tasks and their implementation together, to manage their sequential dependencies and to have a comfortable invocation interface. The nice thing about Rake is that it allows you to implement each tasks unit of work by using the Ruby language; and there is no need to follow a given structure to implement custom tasks like it's the case for &lt;a href="http://raphaelstolt.blogspot.com/2007/03/rolling-your-own-phing-task.html" target="_self"&gt;custom Phing tasks&lt;/a&gt;. As you will see in the forthcoming complete Rakefile some of the tasks are getting quite long and complex; therefor some of them are pending candidates for Refactoring activities like for example extract task units of work into helper/worker classes.&lt;pre class="codeSnippet"&gt;  require 'rubygems'&lt;br /&gt;  require 'hpricot'&lt;br /&gt;  require 'open-uri'&lt;br /&gt;  require 'twitter'&lt;br /&gt;&lt;br /&gt;  task :default do&lt;br /&gt;    Rake::Task['blog_utils:broadcast_notification'].invoke&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  namespace :blog_utils do&lt;br /&gt;&lt;br /&gt;    scrape_options = { 'UserAgent' =&gt; "Ruby/#{RUBY_VERSION}" }&lt;br /&gt;    blog_landing_page = 'http://raphaelstolt.blogspot.com'&lt;br /&gt;    latest_post_short_url, latest_post_url, latest_post_title = nil&lt;br /&gt;    notification_tweet = nil&lt;br /&gt;    last_post_labels = []&lt;br /&gt;    broadcast_log_file = File.dirname(__FILE__) + '/broadcasted_posts.log'&lt;br /&gt;    twitter_credentials = { :user =&gt; 'raphaelstolt', :pwd =&gt; 'thatsasecret'}&lt;br /&gt;&lt;br /&gt;    desc 'Scrape metadata of latest blog post from landing page'&lt;br /&gt;    task :scrape_actual_post_metadata do&lt;br /&gt;      doc = Hpricot(open(blog_landing_page, scrape_options))&lt;br /&gt;      latest_post_url = doc.at('h3.post-title &gt; a')['href']&lt;br /&gt;      latest_post_title = doc.at('h3.post-title &gt; a').inner_html&lt;br /&gt;      label_doc = Hpricot(doc.search('span.post-labels').first.to_s)&lt;br /&gt;      label_links = label_doc.search('span.post-labels &gt; a').each do |label_link|&lt;br /&gt;        label = label_link.inner_html.gsub(' ', '').downcase&lt;br /&gt;        if label.include?('/')&lt;br /&gt;          labels = label.split('/')&lt;br /&gt;          labels.each { |label| last_post_labels.push(label) }&lt;br /&gt;        else&lt;br /&gt;          last_post_labels.push(label)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc 'Shorten the Url of the latest blog post'&lt;br /&gt;    task :shorten_post_url =&gt; [:scrape_actual_post_metadata] do&lt;br /&gt;      raise_message = 'No Url for latest blog post available'&lt;br /&gt;      raise raise_message if latest_post_url.nil?&lt;br /&gt;      url_shorten_service_call = "http://is.gd/api.php?longurl=#{latest_post_url}"&lt;br /&gt;      latest_post_short_url = open(url_shorten_service_call, scrape_options).read&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc 'Check if generate shorten Url references the latest blog post url'&lt;br /&gt;    task :check_shorten_url_references_latest do&lt;br /&gt;      url_referenced_by_short_url = nil&lt;br /&gt;      open(latest_post_short_url, scrape_options) do |f|&lt;br /&gt;        url_referenced_by_short_url = f.base_uri.to_s&lt;br /&gt;      end &lt;br /&gt;      raise_message = "Generated short Url '#{latest_post_short_url}' does not"&lt;br /&gt;      raise_message &lt;&lt; " reference actual blog post url '#{latest_post_url}'"&lt;br /&gt;      raise raise_message unless url_referenced_by_short_url.eql?(latest_post_url)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc 'Check if latest blog post has already been broadcasted'&lt;br /&gt;    task :check_logged_broadcasts do&lt;br /&gt;      logged_broadcasts = []&lt;br /&gt;      if  File.exist?(broadcast_log_file)&lt;br /&gt;        File.open(broadcast_log_file, 'r') do |f|&lt;br /&gt;          logged_broadcasts = f.readlines.collect { |line| line.chomp }&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      raise_message = "Blog post '#{latest_post_title}' has already been "&lt;br /&gt;      raise_message &lt;&lt; "broadcasted"&lt;br /&gt;      raise raise_message if logged_broadcasts.include?(latest_post_title)&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc 'Build notification tweet by injecting scraped metadata into template'&lt;br /&gt;    task :build_notification_tweet =&gt; [:shorten_post_url, &lt;br /&gt;      :check_shorten_url_references_latest] do&lt;br /&gt;      raise_message = 'Required metadata to build tweet is not available'&lt;br /&gt;      raise raise_message if latest_post_title.nil? || latest_post_short_url.nil? &lt;br /&gt;      raise raise_message if last_post_labels.nil?&lt;br /&gt;&lt;br /&gt;      notification_tweet = "Published a new blog post '#{latest_post_title}' "&lt;br /&gt;      notification_tweet &lt;&lt; "available at #{latest_post_short_url}."&lt;br /&gt;&lt;br /&gt;      raise_message = 'Broadcast for latest blog post exceeds 140 characters'&lt;br /&gt;      raise raise_message if notification_tweet.length &gt; 140&lt;br /&gt;&lt;br /&gt;      last_post_labels.each do |tag|&lt;br /&gt;        notification_tweet &lt;&lt; " ##{tag}" unless notification_tweet.length + &lt;br /&gt;          " ##{tag}".length &gt; 140&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc 'Broadcast latest blog post notification to twitter'&lt;br /&gt;    task :broadcast_notification_to_twitter =&gt; [:build_notification_tweet, &lt;br /&gt;      :check_logged_broadcasts] do&lt;br /&gt;      raise_message = "Notification tweet to broadcast is not available"&lt;br /&gt;      raise raise_message if notification_tweet.nil?&lt;br /&gt;      puts "Broadcasting '#{notification_tweet}'"&lt;br /&gt;      http_auth = Twitter::HTTPAuth.new(twitter_credentials[:user], twitter_credentials[:pwd])&lt;br /&gt;      Twitter::Base.new(http_auth).update(notification_tweet)&lt;br /&gt;      #Twitter::Base.new(twitter_credentials[:user], twitter_credentials[:pwd]).post(notification_tweet)&lt;br /&gt;      Rake::Task['blog_utils:log_broadcast_title'].invoke&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    desc 'Log broadcasted blog post title'&lt;br /&gt;    task :log_broadcast_title do&lt;br /&gt;      puts "Logging latest post title to #{broadcast_log_file}"&lt;br /&gt;      File.open(broadcast_log_file, 'a') do |f|&lt;br /&gt;        f.puts latest_post_title&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;  end&lt;/pre&gt;&lt;h4 class="custom"&gt;Putting the Rake task(s) to work&lt;/h4&gt;The next step was to put the Rakefile into my $HOME directory; and after publishing a new blog post I'm now able to broadcast an automated notification by firing up the console and calling the Rake task like shown next.&lt;pre class="consoleOutput"&gt;sudo rake -f $HOME/Automations/Rakefile.rb blog_utils:broadcast_notification&lt;/pre&gt;And as I'm too lazy to type this lengthy command everytime I further added an alias to the $HOME/.profile file which allows me to call the task via the associated alias i.e. blogger2twitter shown in the .profile excerpt.&lt;pre class="codeSnippet"&gt;alias blogger2twitter='sudo rake -f $HOME/Automations/Rakefile.rb blog_utils:broadcast_notification'&lt;/pre&gt;After running the Rake task against this blog post the notification gets added to the given Twitter timeline like shown in the outro image.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/3261723626/" title="Notification tweet by Raphael Stolt, on Flickr"&gt;&lt;img src="http://farm4.static.flickr.com/3434/3261723626_56516e74f4_o.gif" width="523" height="89" border="0" alt="Notification tweet screenshot" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/dtGq_oxLh20" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=8847983296330423769" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/8847983296330423769?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/8847983296330423769?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/01/broadcasting-blog-post-notifications-to.html" title="Broadcasting blog post notifications to Twitter with Ruby and Rake" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://farm4.static.flickr.com/3421/3228209607_f9e31cfb5b_t.jpg" height="72" width="72" /><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;CE4FRH4_fyp7ImA9WxVRF0s.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-8224899344727821893</id><published>2009-01-23T14:19:00.032+01:00</published><updated>2009-01-24T03:08:35.047+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-01-24T03:08:35.047+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><title>Installing Zend_Tool on Mac OS X</title><content type="html">Yesterday I decided to tiptoe into the development of custom Zend_Tool Providers as the introductional &lt;a href="http://devzone.zend.com/article/4124-Zend_Tool-for-the-Developer" target="_self"&gt;article&lt;/a&gt; series by Ralph Schindler motivated me to learn more about it and I already have some useful use cases on my mind. Therefor I prior had to install the Zend_Tool component and it's driving CLI scripts on my MacBook. The following brief instruction describes a possible approach that got me running in no time on a Mac OS X system. Once the Zend Framework has an official PEAR channel most of the forthcoming steps should be obsolete and entirely performed by the PEAR package installer command.&lt;h4 class="custom"&gt;Fetching and installing the Zend_Tool component&lt;/h4&gt;First I tried to install the 1.8.0(devel) version of the Zend Framework via the &lt;a href="http://pear.zfcampus.org/" target="_self"&gt;pear.zfcampus.org&lt;/a&gt; PEAR channel but it currently only delivers the 1.7.3PL1(stable) package; even after switching the stability state of the PEAR config. To dodge the include_path setting hassle and for a further use when customizing other tools like Phing tasks I decided to keep the installed package.&lt;pre class="consoleOutput"&gt;sudo pear channel-discover pear.zfcampus.org&lt;br /&gt;sudo pear install zfcampus/zf-devel&lt;/pre&gt;The next commands are showing the footwork I had to do to get the Zend_Tool component into the PEAR Zend Framework package installed in &lt;em&gt;/opt/local/lib/php/Zend&lt;/em&gt;.&lt;pre class="consoleOutput"&gt;sudo svn co http://framework.zend.com/svn/framework/standard/incubator/library/Zend/Tool/ $HOME/Cos/Zend/Tool&lt;br /&gt;sudo rsync -r --exclude=.svn $HOME/Cos/Zend/Tool /opt/local/lib/php/Zend&lt;/pre&gt;&lt;h4 class="custom"&gt;Putting the Zend_Tool CLI scripts to work&lt;/h4&gt;The next steps were to fetch the CLI scripts from the public Subversion repository and to link them into the system path &lt;em&gt;/opt/local/bin&lt;/em&gt; as shown in the next commands. &lt;pre class="consoleOutput"&gt;sudo svn co http://framework.zend.com/svn/framework/standard/incubator/bin $HOME/Cos/Zend/bin&lt;br /&gt;sudo ln $HOME/Cos/Zend/bin/zf.sh /opt/local/bin/zf&lt;br /&gt;sudo ln $HOME/Cos/Zend/bin/zf.php /opt/local/bin/zf.php&lt;/pre&gt;&lt;h4 class="custom"&gt;Checking the installation&lt;/h4&gt;With everything hopefully in place it was time to verify the success of the installation via the below stated provider action call; and as I got the version of the installed Zend Framework as a response of the executed action/command I'm good to go.&lt;pre class="consoleOutput"&gt;zf show version&lt;/pre&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/Ev8OQ2xeZPM" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=8224899344727821893" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/8224899344727821893?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/8224899344727821893?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2009/01/installing-zendtool-on-mac-os-x.html" title="Installing Zend_Tool on Mac OS X" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUACQX4zcCp7ImA9WxRbF0g.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-4175798713421271826</id><published>2008-11-13T16:21:00.007+01:00</published><updated>2008-12-08T17:29:20.088+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-12-08T17:29:20.088+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Book reviews" /><category scheme="http://www.blogger.com/atom/ns#" term="Ruby/Rails" /><title>Rails for PHP Developers book review</title><content type="html">&lt;img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 190px; height: 228px;" src="http://farm3.static.flickr.com/2151/2274548280_0d6516d8a4_o.jpg" alt="Rails for PHP Developers" title="Rails for PHP Developers" border="0" /&gt;The e-book version of the Pragmatic Programmers release &lt;a href="http://www.pragprog.com/titles/ndphpr/rails-for-php-developers" target="_self"&gt;Rails for PHP Developers&lt;/a&gt; written by Derek DeVries and Mike Naberezny occupies now some of my scarce hard drive space for several months, and today I managed to hit the last page of it. In case you're interested in knowing if it's worthy to sacrifice some rare hard drive or bookshelf space for this book read on.&lt;h4 class="custom"&gt;What's in it?&lt;/h4&gt;The book consists of three main parts which are addressing open-minded developers with a PHP background tempted to add the Ruby language and the thereupon built Rails 2.0 framework to their toolset. &lt;br /&gt;&lt;br /&gt;The first part introduces the classic and nowadays omnipresent MVC pattern, the concepts and conventions of Rails by converting a simple PHP newsletter application into a Rails based one. The follow-up chapters of the first part are covering the basics of the Ruby language by looking at known PHP language features and constructs, and how they translate to their Ruby counterparts. Reading these chapters you will get a thorough understanding of the Ruby language and be able to apply unique features like blocks or the reopening of existing classes. The communicated knowledge builds the foundation to accelerate the use and understanding of the Rails framework which is covered in-depth through-out the book's second part. &lt;br /&gt;&lt;br /&gt;While teaming up with their imaginary buddy Joe the authors walk you through building a Rails user group application. The chapters of the second part are covering a lot of ground reaching from domain modeling, putting the particular MVC parts to work, ensuring quality by utilizing the Test::Unit library to finally deploying the application into a &lt;strike&gt;productive&lt;/strike&gt; production environment. &lt;br /&gt;&lt;br /&gt;The first two chapters of the final and reference part cover the differences and similarities between PHP and Ruby data structures, operations and language constructs. The final chapter of the book closes with a web development specific comparision of PHP constructs and approaches to the ones used by the Rails framework. The book is accompanied by a dedicated &lt;a href="http://railsforphp.com/"&gt;blog&lt;/a&gt; and a &lt;a href="http://railsforphp.com/reference" target="_self"&gt;PHP to Rails online reference&lt;/a&gt; to satisfy severe thirst for more knowledge.&lt;h4 class="custom"&gt;Conclusion&lt;/h4&gt;The book provides interested PHP developers a thorough introduction to the Ruby language and the Rails framework in a fluent and enjoyable writing tone. By implementing the example application of the second book part any decent PHP developer will derive a solid understanding of the Rails framework, he can build upon and that puts him in the position to make reasonable judgments for using/flaming it or not. IMHO this book is so far one of the best PHP related book releases of the out fading year 2008, and can be a real motivator to extend the just gained knowledge by diving deeper into the Ruby/Rails ocean. &lt;br /&gt;&lt;br /&gt;So be prepared to see one or another Ruby related post popping up in the future timeline of this blog; I just added another costly addiction to the medicine cupboard. Word!&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/66LMbdRejE4" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=4175798713421271826" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4175798713421271826?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/4175798713421271826?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/11/rails-for-php-developers-book-review.html" title="Rails for PHP Developers book review" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;CkMBRXw8cSp7ImA9WxRWFEQ.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-3117637992915026495</id><published>2008-10-31T22:44:00.002+01:00</published><updated>2008-10-31T23:00:54.279+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-31T23:00:54.279+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><title>Tinyizing URLs with Zend_Http_Client</title><content type="html">While doing some initial research for a blog related automation task to implement I learned some more about services which transform long URLs into short ones. The well-knownst of these services, due to the Twitter hype, is probably &lt;a href="http://tinyurl.com/" target="_self"&gt;TinyURL&lt;/a&gt; which can be accessed via a classic webinterface or by calling a public API. In a recent &lt;a href="http://www.davedevelopment.co.uk/2008/10/13/zend-framework-and-the-twitter-api/" target="_self"&gt;blog post&lt;/a&gt; Dave Marshall outlined a quick workaround for tweeting via the &lt;a href="http://framework.zend.com/manual/en/zend.http.html#zend.http.client" target="_self"&gt;Zend_Http_Client&lt;/a&gt; component which is a reasonable approach for calling services that aren't in the Zend Framework core yet like &lt;a href="http://framework.zend.com/wiki/display/ZFPROP/Zend_Service_Twitter" target="_self"&gt;Zend_Service_Twitter&lt;/a&gt; or are not supported out of the box. Therefore this post will try to describe a Zend Framework way of creating tinyized URLs.&lt;h4 class="custom"&gt;Getting tiny tiny y'all&lt;/h4&gt;According to Wikipedia there are numerous services available e.g. &lt;a href="http://www.rubyurl.com/home" target="_self"&gt;RubyUrl&lt;/a&gt; providing the same feature as TinyURL, so to be prepared for the future and thereby maybe violating the &lt;a href="http://en.wikipedia.org/wiki/YAGNI" target="_self"&gt;YAGNI principle&lt;/a&gt; I decided to declare a very basic interface first in case of switching the service provider someday.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;/** &lt;br /&gt; * 'Interface-level' PHPDoc Block   &lt;br /&gt; */&lt;br /&gt;interface Recordshelf_Service_UrlShortener_Interface&lt;br /&gt;{&lt;br /&gt;    public function __construct($serviceEndpoint = '');&lt;br /&gt;    public function shortenize($url);&lt;br /&gt;}&lt;/pre&gt;The next code snippet shows the implementation for the TinyURL service programmed against the interface and hosting an additional alias method called &lt;em&gt;tinyize&lt;/em&gt; which is simply wrapping the actual worker method. The service utilizes Zend_Http_Client by setting the endpoint of the service, transmitting a GET request parameterized with the URL to shorten against it and returning the response containing the tinyized URL.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once('Zend/Http/Client.php');&lt;br /&gt;require_once('Recordshelf/Service/UrlShortener/Interface.php');&lt;br /&gt;/** &lt;br /&gt; * 'Class-level' PHPDoc Block   &lt;br /&gt; */&lt;br /&gt;class Recordshelf_Service_TinyUrl implements&lt;br /&gt;    Recordshelf_Service_UrlShortener_Interface&lt;br /&gt;{&lt;br /&gt;    /**&lt;br /&gt;     * The service endpoint&lt;br /&gt;     *&lt;br /&gt;     * @var string&lt;br /&gt;     */&lt;br /&gt;    private $_serviceEndpoint = null;&lt;br /&gt;    /**&lt;br /&gt;     * Recordshelf service tinyURL constructor&lt;br /&gt;     *&lt;br /&gt;     * @param string $serviceEndpoint&lt;br /&gt;     */&lt;br /&gt;    public function __construct(&lt;br /&gt;        $serviceEndpoint = 'http://tinyurl.com/api-create.php')&lt;br /&gt;    {&lt;br /&gt;        $this-&gt;_serviceEndpoint = $serviceEndpoint;&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Shortenizes a given Url&lt;br /&gt;     *&lt;br /&gt;     * @param string $url&lt;br /&gt;     * @return string&lt;br /&gt;     * @throws Exception&lt;br /&gt;     */&lt;br /&gt;    public function shortenize($url) {&lt;br /&gt;        if (is_null($this-&gt;_serviceEndpoint)) {&lt;br /&gt;            throw new Exception('No service endpoint set');&lt;br /&gt;        }&lt;br /&gt;        $client = new Zend_Http_Client($this-&gt;_serviceEndpoint);&lt;br /&gt;        $client-&gt;setParameterGet('url', $url)&lt;br /&gt;               -&gt;setMethod(Zend_Http_Client::GET);&lt;br /&gt;        try {&lt;br /&gt;            $response = $client-&gt;request();&lt;br /&gt;        } catch (Exception $e) {&lt;br /&gt;            throw $e;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if (200 === $response-&gt;getStatus()) {&lt;br /&gt;            return $response-&gt;getBody();&lt;br /&gt;        } else {&lt;br /&gt;            throw new Exception($response-&gt;getStatus() . ": " .&lt;br /&gt;                $response-&gt;getMessage());&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Alias method for the shortenize method&lt;br /&gt;     *&lt;br /&gt;     * @param string $url&lt;br /&gt;     * @throws Exception&lt;br /&gt;     * @see shortenize&lt;br /&gt;     */&lt;br /&gt;    public function tinyize($url)&lt;br /&gt;    {&lt;br /&gt;        return $this-&gt;shortenize($url);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;Now with everything hopefully operating smoothly it's time for a test-drive, yeah I'm lazy and cut that development approach called TDD, by creating a service instance and requesting a TinyURL for the Zend Framework website as shown in the outro listing.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;$service = new Recordshelf_Service_TinyUrl();&lt;br /&gt;$service-&gt;tinyize('http://framework.zend.com');&lt;br /&gt;// =&gt; http://tinyurl.com/nf8kf&lt;/pre&gt;In case off considering or favouring a more framework independent approach there are also other blends available like one via &lt;a href="http://snippets.dzone.com/posts/show/4720" target="_self"&gt;file_get_contents&lt;/a&gt; or via &lt;a href="http://strategicv.com/2008/09/29/code-snippet-tinyurl-link-creation-api/" target="_self"&gt;curl&lt;/a&gt;. Happy tinyizing!&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/IoX7BeWomBY" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=3117637992915026495" title="6 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/3117637992915026495?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/3117637992915026495?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/10/tinyizing-urls-with-zendhttpclient.html" title="Tinyizing URLs with Zend_Http_Client" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>6</thr:total></entry><entry gd:etag="W/&quot;CkQHSH45eSp7ImA9WxRWEEg.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-5045155133811560111</id><published>2008-10-26T20:28:00.004+01:00</published><updated>2008-10-26T20:45:39.021+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-10-26T20:45:39.021+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Getting a visualization of a Phing buildfile</title><content type="html">Today I spent some time to get a tool running to visualize Phing buildfiles as this can come in handy for maintaing, &lt;a href="http://raphaelstolt.blogspot.com/2008/07/six-valuable-phing-build-file.html" target="_self"&gt;refactoring&lt;/a&gt; or extending large buildfiles. Out of the box the Phing &lt;em&gt;-l&lt;/em&gt; option can be used to &lt;a href="http://raphaelstolt.blogspot.com/2008/03/getting-overview-of-all-targets.html" target="_self"&gt;get a first overview of all available targets in a given buildfile&lt;/a&gt; but it doesn't untangle the target dependencies and sometimes a picture is still worth a thousand words. Luckily the Ant community already provides several tools to accomplish the visualization of Ant buildfiles, reaching from solutions that apply a Xslt stylesheet upon a given buildfile e.g. &lt;a href="http://ant2dot.sourceforge.net/" target="_self"&gt;ant2dot&lt;/a&gt; to those ones that take a programmatically approach e.g. &lt;a href="http://www.ggtools.net/grand/" target="_self"&gt;Grand&lt;/a&gt;. All these solutions utilize &lt;a href="http://www.graphviz.org/" target="_self"&gt;Graphiz&lt;/a&gt; to generate a graphic from a DOT file representing the buildfile structure, it's targets and their dependencies. As Phing is a very close descendant of Ant the Xslt approach was best suited and the one with the least effort because their buildfile markup is very similar. The following post will walk you through on how to get a simple Phing buildfile visualization tool running in just a few minutes.&lt;h4 class="custom"&gt;Grabbing the Xslt file&lt;/h4&gt;The first step is to get the ant2dot Xslt &lt;a href="http://ant2dot.sourceforge.net/xsl/ant2dot.xsl" target="_self"&gt;stylesheet&lt;/a&gt; and put it into the same directory as the visualization buildfile and target to come. Due to the aforementioned Phing and Ant buildfile markup similarities it can be used without any modfications.&lt;h4 class="custom"&gt;Setting up the buildfile visualization target&lt;/h4&gt;The next step is to create a Phing target that utilizes the &lt;a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixB-CoreTasks.html#XsltTask" target="_self"&gt;Xslt task&lt;/a&gt; to transfrom the fed buildfile into a DOT file which gets passed further to a platform dependent &lt;a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixB-CoreTasks.html#ExecTask" target="_self"&gt;Exec task&lt;/a&gt; handling the final transformation into a PNG image. To make the visualization target independent from the buildfile to visualize it's hosted in an own buildfile and the target accepts the buildfile to be transformed as a property passed to the Phing Cli or if none given uses the default build.xml. Further the Xslt stylesheet accepts several parameters to add extended data to the resulting DOT file/PNG image which can be set in the &amp;lt;param&amp;gt; tags of the Xslt task. For a list of possible parameters have a look at the &lt;a href="http://ant2dot.sourceforge.net/#options" target="_self"&gt;options&lt;/a&gt; section of ant2dot. The following codesnippet shows the visualization buildfile and the &lt;em&gt;visualize&lt;/em&gt; target doing the Whodini like magic.&lt;pre class="xmlSnippet"&gt;&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;project name="buildfile-visualizer" default="visualize" basedir="."&amp;gt;    &lt;br /&gt;&lt;br /&gt;  &amp;lt;target name="visualize" &lt;br /&gt;          description="Generates a visualization(PNG image) of a given buildfile"&amp;gt;&lt;br /&gt;    &amp;lt;property name="buildfile" value="build.xml" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="phing2dot.xsl" value="${project.basedir}/ant2dot.xsl" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="dot.file" value="${buildfile}.dot" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="png.file" value="${buildfile}.png" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="dot.command.win" value="dot.exe -Tpng ${dot.file} -o ${png.file}" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="dot.command.mac" value="dot -Tpng ${dot.file} -o ${png.file}" /&amp;gt;&lt;br /&gt;    &amp;lt;!-- Transform buildfile into DOT file --&amp;gt;&lt;br /&gt;    &amp;lt;xslt file="${buildfile}" tofile="${project.basedir}/${dot.file}" &lt;br /&gt;          style="${phing2dot.xsl}" overwrite="true"&amp;gt; &lt;br /&gt;      &amp;lt;param name="graph.label" expression="${buildfile}" /&amp;gt;&lt;br /&gt;      &amp;lt;param name="use.target.description" expression="true" /&amp;gt;&lt;br /&gt;    &amp;lt;/xslt&amp;gt;&lt;br /&gt;    &amp;lt;!-- Generate image from DOT file --&amp;gt;&lt;br /&gt;    &amp;lt;exec command="${dot.command.win}" &lt;br /&gt;          dir="${project.basedir}" os="WINNT" /&amp;gt;&lt;br /&gt;    &amp;lt;exec command="${dot.command.mac}" &lt;br /&gt;          dir="${project.basedir}" os="Darwin" /&amp;gt;&lt;br /&gt;    &lt;br /&gt;    &amp;lt;delete file="${project.basedir}/${dot.file}" /&amp;gt;&lt;br /&gt;  &amp;lt;/target&amp;gt;&lt;br /&gt;    &lt;br /&gt;&amp;lt;/project&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Running the buildfile visualization target&lt;/h4&gt;Now as mostly all necessary pieces are available it's time to check if the DOT command is available on the targeted platform by running a &lt;em&gt;dot(.exe) -V&lt;/em&gt; on the console. If it isn't available it has to be installed, this might take several minutes depending on the given platform. Finally with everything in place the visualization process/target can be kicked off by calling Phing the&lt;br /&gt;following way.&lt;pre class="consoleOutput"&gt;triton:tmp stolt$ phing -f buildfile-visualizer.xml [-Dbuildfile=&amp;lt;targeted-buildfile.xml&amp;gt;]&lt;/pre&gt;The last picture shows the visualization of the simple buildfile described in the &lt;a href="http://phing.info/docs/guide/current/" target="_self"&gt;Phing Userguide&lt;/a&gt; but it's also possible to get a meaningful &lt;a href="http://www.flickr.com/photos/raphaelstolt/2974505537/" target="_self"&gt;visualization&lt;/a&gt; of larger buildfiles like the one I currently use for &lt;a href="http://raphaelstolt.blogspot.com/2007/08/setting-up-zend-framework applications.html" target="_self"&gt;setting up Zend Framework based projects&lt;/a&gt;.&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/2974636277/" title="Visualization of a simple buildfile by Raphael Stolt, on Flickr"&gt;&lt;br /&gt;&lt;img src="http://farm4.static.flickr.com/3030/2974636277_e31efcd536_o.png" width="350" height="133" alt="Visualization of a simple buildfile" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/8UYp3_B9qRw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=5045155133811560111" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/5045155133811560111?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/5045155133811560111?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/10/getting-visualization-of-phing.html" title="Getting a visualization of a Phing buildfile" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>1</thr:total></entry><entry gd:etag="W/&quot;CUMBQXoyeCp7ImA9WxVWFEs.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-383329830967405324</id><published>2008-10-16T20:49:00.007+02:00</published><updated>2009-02-24T09:17:30.490+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-02-24T09:17:30.490+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Zend Framework" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><title>Scraping websites with Zend_Dom_Query</title><content type="html">Today I stumbled upon an interesting and reportable scenario where I had to extract information of the weekly published Drum and Bass &lt;a href="http://www.bbc.co.uk/1xtra/drumbass/chart/" target="_self"&gt;charts&lt;/a&gt; provided by BBC 1Xtra. As this information currently isn't available in any consumer friendly format like for example a RSS feed, I had to go that scraping route but didn't want to hustle with a regex approach. Since version 1.6.0 the &lt;a href="http://framework.zend.com/manual/en/zend.dom.query.html" target="_self"&gt;Zend_Dom_Query&lt;/a&gt; component has been added to the framework mainly to support functional testing of MVC applications, but it also can be used for rolling custom website scrapers in a snap. Woot, perfect match!&lt;br /&gt;&lt;br /&gt;The following code snippets are showing the Bbc_DnbCharts_Scraper class I came up with and an example of its usage. The class utilizes curl to read the website holding the desired data, which will be passed to Zend_Dom_Query to execute queries upon it. For querying the former loaded XHTML &lt;a href="http://en.wikipedia.org/wiki/Document_Object_Model" target="_self"&gt;Document Object Model&lt;/a&gt; it's possible to either utilize XPath or CSS selectors. So I had to pick my poison, and decided to go with the CSS selectors as them were best suited for the document to query and will be more familiar to most jQuery or Prototype users. The query returns a result set of all matching &lt;a href="http://de3.php.net/manual/de/class.domelement.php" target="_self"&gt;DOMElement&lt;/a&gt;s which are further unpuzzled via a private helper method returning just the desired charts data as shown in the closing listing. As you can see the implementation of the scraping can be done with a minimum of effort and these are exactly the moments I love the Zend Framework for.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once('Zend/Dom/Query.php');&lt;br /&gt;/** &lt;br /&gt; * 'Class-level' PHPDoc Block   &lt;br /&gt; */&lt;br /&gt;class Bbc_DnbCharts_Scraper&lt;br /&gt;{&lt;br /&gt;    private $_url = null;&lt;br /&gt;    private $_xhtml = null;&lt;br /&gt;&lt;br /&gt;    /**&lt;br /&gt;     * @param string $url&lt;br /&gt;     */&lt;br /&gt;    public function __construct($url)&lt;br /&gt;    {&lt;br /&gt;        $this-&gt;_url = $url;&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Scrapes off the drum and bass charts content from the BBC 1Xtra website.     &lt;br /&gt;     *&lt;br /&gt;     * @return array&lt;br /&gt;     * @throws Exception&lt;br /&gt;     */&lt;br /&gt;    public function scrape()&lt;br /&gt;    {&lt;br /&gt;        try {&lt;br /&gt;            $dom = new Zend_Dom_Query($this-&gt;_getXhtml());&lt;br /&gt;        } catch (Exception $e) {&lt;br /&gt;            throw $e;        &lt;br /&gt;        }&lt;br /&gt;        $results = $dom-&gt;query('div.chart div');&lt;br /&gt;        $chartDetails = array();&lt;br /&gt;        foreach ($results as $index =&gt; $result) {&lt;br /&gt;            /* @var $result DOMElement */&lt;br /&gt;            if ($result-&gt;nodeValue !== '') { //filter out &amp;lt;br /&amp;gt; element&lt;br /&gt;                $chartDetails[] = $result-&gt;nodeValue;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return $this-&gt;_unpuzzleChartDetails($chartDetails, true);&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Unpuzzles the chart details and groups them by their chart position, &lt;br /&gt;     * if desired with associative keys.&lt;br /&gt;     *  &lt;br /&gt;     * @param array $details&lt;br /&gt;     * @param boolean $associative&lt;br /&gt;     * @return array&lt;br /&gt;     */&lt;br /&gt;    private function _unpuzzleChartDetails(array $details, $associative = false)&lt;br /&gt;    {&lt;br /&gt;        if (0 === count($details)) {&lt;br /&gt;            return array();&lt;br /&gt;        } else {  &lt;br /&gt;            $nextChartRank = 2;&lt;br /&gt;            $charts = array();&lt;br /&gt;            $groupedChartDetails = array();&lt;br /&gt;            &lt;br /&gt;            foreach ($details as $index =&gt; $chartDetail)&lt;br /&gt;            { &lt;br /&gt;                if ($index &lt;= $nextChartRank) {&lt;br /&gt;                    $groupedChartDetails[] = $chartDetail;&lt;br /&gt;                }&lt;br /&gt;                if ($index == $nextChartRank) {&lt;br /&gt;                    $nextChartRank+=3;&lt;br /&gt;                    $charts[] = $groupedChartDetails;&lt;br /&gt;                    unset($groupedChartDetails);&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            if ($associative) {&lt;br /&gt;                $associatives = array('artist', 'tune', 'label');&lt;br /&gt;                foreach ($charts as $chartsIndex =&gt; $chart) {&lt;br /&gt;                    unset($charts[$chartsIndex]);&lt;br /&gt;                    foreach ($chart as $chartIndex =&gt; $chartDetails) {&lt;br /&gt;                        $charts[$chartsIndex][$associatives[$chartIndex]] = &lt;br /&gt;                            $chartDetails;&lt;br /&gt;                    }&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;            return $charts;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    /**&lt;br /&gt;     * Gets the XHTML document via curl &lt;br /&gt;     * &lt;br /&gt;     * @return string&lt;br /&gt;     * @throws Exception&lt;br /&gt;     */&lt;br /&gt;    private function _getXhtml()&lt;br /&gt;    {&lt;br /&gt;        $curl = curl_init();&lt;br /&gt;        if (!$curl) {&lt;br /&gt;            throw new Exception('Unable to init curl. ' . curl_error($curl));&lt;br /&gt;        }&lt;br /&gt;        curl_setopt($curl, CURLOPT_URL, $this-&gt;_url);&lt;br /&gt;        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);&lt;br /&gt;        // Faking user agent&lt;br /&gt;        curl_setopt($curl, CURLOPT_USERAGENT, &lt;br /&gt;            'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');&lt;br /&gt;        $xhtml = curl_exec($curl);&lt;br /&gt;        if (!$xhtml) {&lt;br /&gt;            throw new Exception('Unable to read XHTML. ' . curl_error($curl));&lt;br /&gt;        }&lt;br /&gt;        curl_close($curl);&lt;br /&gt;        return $xhtml; &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Usage demo&lt;br /&gt;$scraper = new Bbc_DnbCharts_Scraper('http://www.bbc.co.uk/1xtra/drumbass/chart/');&lt;br /&gt;$charts = $scraper-&gt;scrape();&lt;/pre&gt;The closing code snippet shows an extract of the Drum and Bass charts from &lt;a href="http://www.bbc.co.uk/1xtra/" target="_self"&gt;BBC 1Xtra&lt;/a&gt; scraped off around the 16th October 2008.&lt;pre class="codeSnippet"&gt;Array&lt;br /&gt;(&lt;br /&gt;    [0] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [artist] =&gt; Chase &amp; Status Ft Plan B&lt;br /&gt;            [tune] =&gt; Pieces&lt;br /&gt;            [label] =&gt; Ram Records&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;br /&gt;    [9] =&gt; Array&lt;br /&gt;        (&lt;br /&gt;            [artist] =&gt; Zen&lt;br /&gt;            [tune] =&gt; Full Effect&lt;br /&gt;            [label] =&gt; Flipmode Audio&lt;br /&gt;        )&lt;br /&gt;&lt;br /&gt;)&lt;/pre&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/fQQ9MPD5B9s" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=383329830967405324" title="14 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/383329830967405324?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/383329830967405324?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/10/scraping-websites-with-zenddomquery.html" title="Scraping websites with Zend_Dom_Query" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>14</thr:total></entry><entry gd:etag="W/&quot;DkIHQnY6eCp7ImA9WxdUEks.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-472328189184808571</id><published>2008-07-28T18:38:00.006+02:00</published><updated>2008-07-28T19:02:13.810+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-07-28T19:02:13.810+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Refactoring" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="PHPUnit" /><category scheme="http://www.blogger.com/atom/ns#" term="Best Practice" /><title>Creating custom PHPUnit assertions</title><content type="html">While developing PHP applications and applying developer testing the applications safety net will grow along the timeline, and as normal code, test code should be in a fresh, odour free state too. A common test code smell, amongst others, is the duplication of assertion logic which can reduce reusability, readability and thereby obscure the specific verification intention of tests. To subdue this special smell several &lt;a href="http://xunitpatterns.com/"&gt;patterns and refactorings&lt;/a&gt; are available to acquaint the test code with the DRY principle. So in this blog post I'd like to set the focus on some of the aspects of the &lt;a href="http://xunitpatterns.com/Custom%20Assertion.html"&gt;Custom Assertion&lt;/a&gt; pattern, by showing how to create custom &lt;a href="http://www.phpunit.de/"&gt;PHPUnit&lt;/a&gt; assertions, which attacks the above mentioned smell and its retroactive effects with a huge antiperspirant flagon, while also providing the chance to build a customer friendly and domain related test vocabulary.&lt;br /&gt;&lt;br /&gt;The first introductive code snippet shows an example of &lt;em&gt;unwanted&lt;/em&gt; code duplications and smells in a PHPUnit(i.e. version: 3.2.21) test case class spreading over several test methods. The first test code duplications smell is present when verifying that a given bag is having an expected item count and an explicit 'intent obscuration' smell can be spotted when verifying the bags stock id against an assumed convention via a 'distracting' regular expression.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once 'PHPUnit/Framework.php';&lt;br /&gt;require_once 'Record/Bag.php';&lt;br /&gt;require_once 'Record/Item.php';&lt;br /&gt;&lt;br /&gt;class Record_Bag_Test extends PHPUnit_Framework_TestCase &lt;br /&gt;{&lt;br /&gt;    private $_bag = null;&lt;br /&gt;    &lt;br /&gt;    protected function setUp() &lt;br /&gt;    {&lt;br /&gt;        /**&lt;br /&gt;         * Creates a new named bag with an unique stock id&lt;br /&gt;         * in a format of AAA-NN-AA-NNNNNNNN e.g. XDS-76-YS-00000124, &lt;br /&gt;         * where A stands for an alphanumeric and N for a numeric character.&lt;br /&gt;         */&lt;br /&gt;        $this-&gt;_bag = new Record_Bag('Test_Bag');&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @test&lt;br /&gt;     */&lt;br /&gt;    public function bagShouldNotContainDuplicateItems() &lt;br /&gt;    {&lt;br /&gt;        &lt;em&gt;$this-&gt;assertTrue(0 === $this-&gt;_bag-&gt;getItemQuantity(),&lt;/em&gt; &lt;br /&gt;            &lt;em&gt;'New bag not empty on creation.');&lt;/em&gt;&lt;br /&gt;            &lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 1'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 2'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 2'));&lt;br /&gt;        &lt;br /&gt;        &lt;em&gt;$this-&gt;assertTrue(2 === $this-&gt;_bag-&gt;getItemQuantity(),&lt;/em&gt; &lt;br /&gt;            &lt;em&gt;'Bag does contain duplicated items.');&lt;/em&gt;&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @test&lt;br /&gt;     */&lt;br /&gt;    public function bagShouldBeReducedByOneItemAfterRemoval()&lt;br /&gt;    {&lt;br /&gt;        &lt;em&gt;$this-&gt;assertTrue(0 === $this-&gt;_bag-&gt;getItemQuantity(),&lt;/em&gt; &lt;br /&gt;            &lt;em&gt;'New bag not empty on creation.');&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 1'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 2'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 3'));&lt;br /&gt;&lt;br /&gt;        $this-&gt;_bag-&gt;removeItem('Dubplate 2');&lt;br /&gt;&lt;br /&gt;        &lt;em&gt;$this-&gt;assertTrue(2 === $this-&gt;_bag-&gt;getItemQuantity(),&lt;/em&gt; &lt;br /&gt;            &lt;em&gt;'Former three items bag does not contain two items after removal of one.');&lt;/em&gt;  &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @test &lt;br /&gt;     */&lt;br /&gt;    public function bagStockIdShouldFollowAgreedConvention()     &lt;br /&gt;    {    &lt;br /&gt;        &lt;em&gt;$stockIdPattern = '/^[A-Z]{3}-\d{2}-[A-Z]{2}-\d{8}/U';&lt;/em&gt;&lt;br /&gt;        $this-&gt;assertRegExp($stockIdPattern, $this-&gt;_bag-&gt;getStockId(), &lt;br /&gt;            'Stock id &amp;lt;string:' . $this-&gt;_bag-&gt;getStockId() &lt;br /&gt;                . '&amp;gt; does not follow the agreed convention.'); &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    ....&lt;br /&gt;    &lt;br /&gt;    protected function tearDown() &lt;br /&gt;    {&lt;br /&gt;        unset($this-&gt;_bag);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Mechanics of rolling custom assertions&lt;/h4&gt;The 'Custom Assertion' pattern can be applied from the very first test code creation activities, in sense of a &lt;a href="http://en.wikipedia.org/wiki/Prefactoring"&gt;prefactoring&lt;/a&gt;, or refactored towards by extracting the assert duplications and verification intention obscurations into tailormade and intent revealing named assertions.&lt;br /&gt;&lt;br /&gt;To define custom assertions are several approaches available, the first and easiest one is to define custom assertions merely for a single test class and make them private (inline) methods of this specific class by facading/wrapping the &lt;a href="http://www.phpunit.de/pocket_guide/3.3/en/api.html#api.assert.tables.assertions"&gt;standard&lt;/a&gt; PHPUnit assertion. This approach is outlined in code snippet &lt;em&gt;a)&lt;/em&gt;. &lt;br /&gt;&lt;br /&gt;Another approach is to create/introduce an &lt;a href="http://www.phpunit.de/pocket_guide/3.3/en/extending-phpunit.html#extending-phpunit.Assert"&gt; Assert Class&lt;/a&gt; to promote a cleaner reusability in other test case classes or scenarios, this approach might get chosen to collect and organize the evolving domain specific assertions. You will see a code sketch of this approach in code listing b). &lt;br /&gt;&lt;br /&gt;Other and more complex approaches would be the utilisation of PHPUnits' Constraint feature which is available since release 3.0.0 or in the near future the use of the &lt;a href="http://sebastian-bergmann.de/archives/735-Getting-Started-with-Hamcrest.html"&gt;Hamcrest&lt;/a&gt; test matcher features which might be available from PHPUnit 4.0.0.&lt;br /&gt;&lt;br /&gt;Either way the purpose specific assertions should further provide a default and convention conform assertion message, which will be raised on a verification failure, and also the feature to feed in a custom one to avoid gambling annoying &lt;a href="http://xunitpatterns.com/Assertion%20Roulette.html"&gt;Assertion Roulette&lt;/a&gt; rounds.&lt;br /&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 170pt;"&gt;a) Assert definition inside Test Case:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;require_once 'PHPUnit/Framework.php';&lt;br /&gt;require_once 'Record/Bag.php';&lt;br /&gt;require_once 'Record/Item.php';&lt;br /&gt;&lt;br /&gt;class Record_Bag_Test extends PHPUnit_Framework_TestCase &lt;br /&gt;{&lt;br /&gt;    private $_bag = null;&lt;br /&gt;    &lt;br /&gt;    protected function setUp() &lt;br /&gt;    {&lt;br /&gt;        $this-&gt;_bag = new Record_Bag('Test_Bag');&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    ....&lt;br /&gt;    &lt;br /&gt;    private function assertBagItemCount($expectedCount, $bag, &lt;br /&gt;        $message = 'Expected bag item count &amp;lt;integer:#1&amp;gt; does not match actual count of &amp;lt;integer:#2&amp;gt; items.')&lt;br /&gt;    {&lt;br /&gt;        if (strpos($message, '#1')) {&lt;br /&gt;            $message = str_replace('#1', $expectedCount, $message);        &lt;br /&gt;        }&lt;br /&gt;        if (strpos($message, '#2')) {&lt;br /&gt;            $message = str_replace('#2', $bag-&gt;getItemQuantity(), $message);        &lt;br /&gt;        }&lt;br /&gt;        $this-&gt;assertTrue($expectedCount === $bag-&gt;getItemQuantity(), $message);&lt;br /&gt;    }    &lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;div class="refactoringStatus" style="width: 170pt;"&gt;b) Assert definition in Assert Class:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;?php&lt;br /&gt;require_once 'PHPUnit/Framework/Assert.php';&lt;br /&gt;&lt;br /&gt;class Record_Bag_Assert extends PHPUnit_Framework_Assert &lt;br /&gt;{&lt;br /&gt;    /**&lt;br /&gt;     * Verifies that a given bag stock id follows the agreed convention.&lt;br /&gt;     *&lt;br /&gt;     * @param string $stockId&lt;br /&gt;     * @param string $message&lt;br /&gt;     * @see Record_Bag::createUniqueStockId()&lt;br /&gt;     */&lt;br /&gt;    public function assertStockIdFollowsConvention($stockId, &lt;br /&gt;        $message = 'Stock id &amp;lt;string:##&amp;gt; does not follow the agreed convention.') &lt;br /&gt;    {&lt;br /&gt;        if (strpos($message, '##')) {&lt;br /&gt;            $message = str_replace('##', $stockId, $message);        &lt;br /&gt;        }&lt;br /&gt;        $stockIdPattern = '/^[A-Z]{3}-\d{2}-[A-Z]{2}-\d{8}/U';&lt;br /&gt;        $this-&gt;assertRegExp($stockIdPattern, $stockId, $message);        &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    ....&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Putting the custom assertions to work&lt;/h4&gt;Now as the tailormade assertions are available all verification work can be delegated to them the same way it would be done with any of the standard PHPUnit assertions. In case the custom assertions are hosted in an 'Assert Class' they can be &lt;em&gt;required_once&lt;/em&gt; or loaded via &lt;em&gt;__autoload()&lt;/em&gt; in the test setup, otherwise if they are defined inside the test case class itself they can be used like regular private methods of that class. The last code extract illustrates the use of the prior outlined 'Assert Class' assertion for verifing the stock id format alongside the use of the 'inline' custom assertion for verifying the amount of items in a given bag.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;require_once 'PHPUnit/Framework.php';&lt;br /&gt;require_once 'Record/Bag/Assert.php';&lt;br /&gt;&lt;br /&gt;class Record_Bag_Test extends PHPUnit_Framework_TestCase &lt;br /&gt;{&lt;br /&gt;    &lt;br /&gt;    ....&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @test&lt;br /&gt;     */&lt;br /&gt;    public function bagShouldNotContainDuplicateItems() &lt;br /&gt;    {&lt;br /&gt;        $this-&gt;assertBagItemCount(0, $bag,  'New bag not empty on creation.');&lt;br /&gt;            &lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 1'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 2'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 2'));&lt;br /&gt;        &lt;br /&gt;        $this-&gt;assertBagItemCount(2, $bag, 'Bag does contain duplicate items.');&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @test&lt;br /&gt;     */&lt;br /&gt;    public function bagShouldBeReducedByOneItemAfterRemoval()&lt;br /&gt;    {&lt;br /&gt;        $this-&gt;assertBagItemCount(0, $bag, 'New bag not empty on creation.');&lt;br /&gt;&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 1'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 2'));&lt;br /&gt;        $this-&gt;_bag-&gt;addItem(new Record_Item('Dubplate 3'));&lt;br /&gt;&lt;br /&gt;        $this-&gt;_bag-&gt;removeItem('Dubplate 2');&lt;br /&gt;&lt;br /&gt;        $this-&gt;assertBagItemCount(2, $bag, &lt;br /&gt;            'Former three items bag does not contain two items after removal of one.');   &lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    /**&lt;br /&gt;     * @test &lt;br /&gt;     */&lt;br /&gt;    public function bagStockIdShouldFollowAgreedConvention()     &lt;br /&gt;    {    &lt;br /&gt;        Record_Bag_Assert::assertStockIdFollowsConvention(&lt;br /&gt;            $this-&gt;_bag-&gt;getStockId());&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    ....&lt;br /&gt;    &lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Adding value through a domain specific test vocabulary&lt;/h4&gt;As you might have noticed the readability and intention communication of the test code has been improved significantly from the introductive code snippet towards the last one. Furthermore by distilling a ubiquitous(see &lt;a href="http://domaindrivendesign.org/books/index.html"&gt;Domain Driven Design book&lt;/a&gt; by Eric Evans) test language domain experts, which are mostly not fluent in the targeted programming language i.e. PHP, are enabled to read test code and provide valuable feedback or contribute changes to test scenarios affecting their domain. &lt;br /&gt;&lt;br /&gt;Another common area where domain specific test vocabularies are used, by providing their own assertion and constraint sets, test case classes and additional &lt;a href="http://xunitpatterns.com/Test%20Helper.html"&gt;test helpers&lt;/a&gt;, are extensions to xUnit frameworks e.g. DBUnit for PHPUnit or the Zend Framework &lt;a href="http://framework.zend.com/manual/en/zend.test.html"&gt;MVC testing scaffold&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/-fGnjqR7BBs" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=472328189184808571" title="3 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/472328189184808571?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/472328189184808571?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/07/creating-custom-phpunit-assertions.html" title="Creating custom PHPUnit assertions" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>3</thr:total></entry><entry gd:etag="W/&quot;C0IFRns5fCp7ImA9WxVXGEo.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-6867408109374725659</id><published>2008-07-04T01:16:00.008+02:00</published><updated>2009-02-17T12:51:57.524+01:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-02-17T12:51:57.524+01:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Refactoring" /><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Best Practice" /><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Six valuable Phing build file refactorings</title><content type="html">Some &lt;strike&gt;weeks&lt;/strike&gt; months ago I finally got my hands on the &lt;a href="http://www.pragprog.com/titles/twa/thoughtworks-anthology"&gt;ThoughtWorks Anthology&lt;/a&gt; and got immediately hooked on one of the featured essays called 'Refactoring Ant Build Files' contributed by Julian Simpson aka the &lt;a href="http://www.build-doctor.com"&gt;build doctor&lt;/a&gt;. After absorbing and studying the provided catalogue of overall 24 refactorings, I spent some time to transform a few health-promoting ones to the &lt;a href="http://phing.info/trac/"&gt;Phing&lt;/a&gt; universe. So the following post will outline six &lt;strike&gt;five&lt;/strike&gt; basic, but valuable Phing build file refactorings by showing the smelly example first, followed by the scentless one and a closing refactoring description.&lt;h4 class="custom"&gt;Making build files 'first-class' codebase citizens&lt;/h4&gt;You might ask yourself if there even is any need to refactor and care about mostly poorly treated project artifacts like build files. Well according to the book market there are growing needs and catalogues for refactoring non-sourcecode matters like &lt;a href="http://www.amazon.com/Refactoring-Databases-Evolutionary-Addison-Wesley-Signature/dp/0321293533/"&gt;databases&lt;/a&gt; and nowadays even &lt;a href="http://www.amazon.com/Refactoring-HTML-Improving-Applications-Addison-Wesle/dp/0321503635"&gt;(X)HTML&lt;/a&gt;, and as build tools are often used to automate the whole build and delivery process of complete projects, their feed build files should be readable and clear, painless maintainable and easily customizable as the controlled project takes new directions and hurdles. &lt;br /&gt;&lt;br /&gt;Today build files are also often used to drive the Continuous Integration(CI) process and are heavily used to run local development builds prior to commiting the finished development tasks, therefor messy and clotty build files will have a counterproductive impact on the build management lifecycle and on making required changes to it. So when striving for codebase citizen equality agree upon a build file coding standard (e.g. &lt;a href="#ref5"&gt;5. Introduce distinct target naming&lt;/a&gt; or a common element indentation), reside all build files of a project in a SCM system like Subversion or &lt;a href="http://www.pragprog.com/titles/tsgit"&gt;Git&lt;/a&gt; and try not to neglect the build files constantly just because they aren't actual business logic.&lt;h4 class="custom"&gt;Trapeze balancing without a safety net&lt;/h4&gt;While developing an applications business logic 'ideally' automated behaviour verifying tests are written, whether in a test-first or test-last approach, and these are building the implicit and required safety net for all follow-up refactorings. For build files there are currently no tailored testing tools/frameworks available to warrant their external behaviour during and after a refactoring, though it might be possible to create a basic safety net by using for example a combination of PHPUnit's &lt;em&gt;assertFileExists&lt;/em&gt; and &lt;em&gt;assertContains&lt;/em&gt; assertions. And even if there were such tools available, it's rather questionable that these would be applied to test-drive mostly simple starting and incremental evolving build files. So currently this flavour of refactoring needs to be applied with much more descipline and caution than classic sourcecode refactorings, even 'old-school' manual tests have to be run frequently and only with a proceeding practice more and more agressive refactorings will become an everyday tool. After this short note of caution, let us jump right into the refactoring catalogue extract.&lt;h4 class="custom"&gt;1. Extract target&lt;/h4&gt;&lt;div class="refactoringDescription"&gt;Take parts of a large target, declare them as independent targets and preserve dependencies by using the &lt;br /&gt;target &lt;em&gt;depends&lt;/em&gt; attribute.&lt;/div&gt;&lt;div class="refactoringStatus"&gt;Before&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;target name="what-a-clew"&amp;gt;&lt;br /&gt;  &lt;br /&gt;  &amp;lt;phplint&amp;gt;&lt;br /&gt;    &amp;lt;fileset dir="${build.src}"&amp;gt;&lt;br /&gt;      &amp;lt;include name="**/*.php"/&amp;gt;&lt;br /&gt;    &amp;lt;/fileset&amp;gt;&lt;br /&gt;  &amp;lt;/phplint&amp;gt;&lt;br /&gt;  &lt;br /&gt;  &amp;lt;phpcodesniffer standard="PEAR" format="summary"&amp;gt;&lt;br /&gt;    &amp;lt;fileset dir="${build.src}"&amp;gt;&lt;br /&gt;      &amp;lt;include name="**/*.php"/&amp;gt;&lt;br /&gt;    &amp;lt;/fileset&amp;gt;&lt;br /&gt;  &amp;lt;/phpcodesniffer&amp;gt;&lt;br /&gt;  &lt;br /&gt;  &amp;lt;phpunit&amp;gt;&lt;br /&gt;    &amp;lt;formatter todir="reports" type="xml"/&amp;gt;&lt;br /&gt;    &amp;lt;batchtest&amp;gt;&lt;br /&gt;      &amp;lt;fileset dir="="${build.tests}"&amp;gt;&lt;br /&gt;        &amp;lt;include name="**/*Test*.php"/&amp;gt;&lt;br /&gt;      &amp;lt;/fileset&amp;gt;&lt;br /&gt;    &amp;lt;/batchtest&amp;gt;&lt;br /&gt;  &amp;lt;/phpunit&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;div class="refactoringStatus"&gt;After:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;target name="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phplint&amp;gt;&lt;br /&gt;    ....&lt;br /&gt;  &amp;lt;/phplint&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="sniff-report" depends="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phpcodesniffer standard="PEAR" format="summary"&amp;gt;&lt;br /&gt;    ....&lt;br /&gt;  &amp;lt;/phpcodesniffer&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="test-report" depends="sniff-report"&amp;gt;&lt;br /&gt;  &amp;lt;phpunit&amp;gt;&lt;br /&gt;    ....&lt;br /&gt;  &amp;lt;/phpunit&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;The &lt;em&gt;Extract target&lt;/em&gt; refactoring is similar to the well-known &lt;a href="http://www.refactoring.com/catalog/extractMethod.html"&gt;Extract method&lt;/a&gt; refactoring catalogued by Martin Fowler and should be applied to unclutter long targets, which can become hard to understand and troubleshoot while maintaining or extending a build file. This refactoring is achieved by taken each atomic task (e.g. phplint) of the cluttered target and provide them each a own target (e.g. phplint-report) while the former tasks execution sequence can be obtained by utilizing the target &lt;em&gt;depends&lt;/em&gt; attribute. You can compare this refactoring to the technique of tackling a method that's to large and infringes upon the &lt;a href="http://en.wikipedia.org/wiki/Single_responsibility_principle"&gt; single responsibility principle&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;While twiddling with this refactoring I came up with a follow-up and hand in hand refactoring that might go by the name of &lt;em&gt;Introduce facade target&lt;/em&gt;, which simply orchestrates the target execution sequence so you can remove the &lt;em&gt;depends&lt;/em&gt; attribute of all orchestrated targets and thereby use them separately if needed and advisable. The following build file extract shows the result of this refactoring in action.&lt;h4 class="custom"&gt;1.1 Introduce facade target&lt;/h4&gt;&lt;div class="refactoringDescription"&gt;Provide a facade target to obtain the task execution sequence and to make each involved target a single callable unit.&lt;/div&gt;&lt;div class="refactoringStatus"&gt;After:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;target name="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phplint&amp;gt;&lt;br /&gt;    ....&lt;br /&gt;  &amp;lt;/phplint&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="sniff-report" &lt;strike&gt;depends="phplint-report"&lt;/strike&gt;&amp;gt;&lt;br /&gt;  &amp;lt;phpcodesniffer standard="PEAR" format="summary"&amp;gt;&lt;br /&gt;    ....&lt;br /&gt;  &amp;lt;/phpcodesniffer&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="test-report" &lt;strike&gt;depends="sniff-report"&lt;/strike&gt;&amp;gt;&lt;br /&gt;  &amp;lt;phpunit&amp;gt;&lt;br /&gt;    ....&lt;br /&gt;  &amp;lt;/phpunit&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="quality-report" depends="lint-report, sniff-report, test-report" &lt;br /&gt;        description="Generates the overall projects quality report" /&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;2. Introduce property file&lt;/h4&gt;&lt;div class="refactoringDescription"&gt;Move infrequently changing properties from the build file body to a flat file.&lt;/div&gt;&lt;div class="refactoringStatus"&gt;Before:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;property name="db.port" value="3306" /&amp;gt;&lt;br /&gt;&amp;lt;property name="db.name" value="example" /&amp;gt;&lt;br /&gt;&amp;lt;property name="db.user" value="funkdoc" /&amp;gt;&lt;br /&gt;....&lt;br /&gt;&amp;lt;property name="runtime.property.x" value="default" /&amp;gt;&lt;br /&gt;....&lt;/pre&gt;&lt;div class="refactoringStatus"&gt;After:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&lt;br /&gt;build.poperties file&lt;br /&gt;[example properties]&lt;br /&gt;db.port = 3306&lt;br /&gt;db.name = example&lt;br /&gt;db.user = funkdoc&lt;br /&gt;....&lt;br /&gt;&lt;br /&gt;build file&lt;br /&gt;&amp;lt;property file="build.properties" /&amp;gt;&lt;br /&gt;&amp;lt;property name="runtime.property.x" value="default" /&amp;gt;&lt;br /&gt;....&lt;/pre&gt;The &lt;em&gt;Introduce property file&lt;/em&gt; refactoring can be applied for moving infrequently changing or static properties out of the main build file body to raise the overall legibility and keep them distinct from runtime properties. The downside of this refactoring is a lost of property visibility and breaking up the former single build file into multiple units, which is contradictory to the third &lt;a href="http://www.onjava.com/pub/a/onjava/2003/12/17/ant_bestpractices.html"&gt;ANT best practice&lt;/a&gt; named 'Prefer a Single Buildfile' of an older best practice catalogue compiled by Eric M. Burke. So in this case, like in any case, you have to make your own choice based on your needs and requirements.&lt;h4 class="custom"&gt;3. Replace comment with description&lt;/h4&gt;&lt;div class="refactoringDescription"&gt;Annotate elements(targets) with the description attribute instead of XML comments.&lt;/div&gt;&lt;div class="refactoringStatus"&gt;Before:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;!-- This target runs the PHP_Codesniffer task and reports coding standard violations --!&amp;gt;&lt;br /&gt;&amp;lt;target name="sniff-report"&amp;gt;&lt;br /&gt;  ....&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;div class="refactoringStatus"&gt;After:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;target name="sniff-report" description="Runs the PHP_Codesniffer task and reports coding standard violations"&amp;gt;&lt;br /&gt;  ....&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;Often build files are accentuated with plain XML comments to retain the mechanics and purpose of build file elements(i.e. targets) and can become a diversionary/obscuring source while maintaining or extending a build file. By using the available &lt;em&gt;description&lt;/em&gt; attribute of the target element to annotate its purpose it's possible to reduce that kind of noise and even better, if used constantly, they can provide valuable &lt;a href="http://raphaelstolt.blogspot.com/2008/03/getting-overview-of-all-targets.html"&gt;information about all accumulated&lt;/a&gt; targets of a build file when phing is called with the &lt;em&gt;-l(ist)&lt;/em&gt; option. As you can see the &lt;em&gt;Replace comment with description&lt;/em&gt; refactoring requires a minimum of effort/investment to achieve a very valuable impact.&lt;h4 class="custom"&gt;4. Reuse elements by id&lt;/h4&gt;&lt;div class="refactoringDescription"&gt;Declare an instance e.g. a fileset once and make references to it elsewhere to reduce duplication and increase clarity.&lt;/div&gt;&lt;div class="refactoringStatus"&gt;Before:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;target name="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phplint&amp;gt;&lt;br /&gt;    &amp;lt;fileset dir="${build.src}"&amp;gt;&lt;br /&gt;      &amp;lt;include name="**/*.php"/&amp;gt;&lt;br /&gt;    &amp;lt;/fileset&amp;gt;&lt;br /&gt;  &amp;lt;/phplint&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt; &lt;br /&gt;&lt;br /&gt;&amp;lt;target name="sniff-report" depends="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phpcodesniffer standard="PEAR" format="summary"&amp;gt;&lt;br /&gt;    &amp;lt;fileset dir="${build.src}"&amp;gt;&lt;br /&gt;      &amp;lt;include name="**/*.php"/&amp;gt;&lt;br /&gt;    &amp;lt;/fileset&amp;gt;&lt;br /&gt;  &amp;lt;/phpcodesniffer&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;div class="refactoringStatus"&gt;After:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;fileset id="src_artifacts" dir="${build.src}"&amp;gt;&lt;br /&gt;  &amp;lt;include name="**/*.php"/&amp;gt;&lt;br /&gt;&amp;lt;/fileset&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phplint&amp;gt;&lt;br /&gt;    &amp;lt;fileset refid="src_artifacts" /&amp;gt;&lt;br /&gt;  &amp;lt;/phplint&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt; &lt;br /&gt;&lt;br /&gt;&amp;lt;target name="sniff-report" depends="phplint-report"&amp;gt;&lt;br /&gt;  &amp;lt;phpcodesniffer standard="PEAR" format="summary"&amp;gt;&lt;br /&gt;    &amp;lt;fileset refid="src_artifacts" /&amp;gt;&lt;br /&gt;  &amp;lt;/phpcodesniffer&amp;gt;&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;The &lt;em&gt;Reuse elements by id&lt;/em&gt; refactoring is, as the short description states, tailor-made to increase clarity while reducing code duplication, which is when present a risk that an alternation made to one element will be skipped for the other duplicates, by declaring top-level elements once by assigning an &lt;em&gt;id&lt;/em&gt; attribute to it and then referring to it thoughout the rest of the build file. This refactoring is best compared to the classic sourcecode refactoring called &lt;a href="http://www.refactoring.com/catalog/pullUpMethod.html"&gt;Pull Up Method&lt;/a&gt; also catalogued by Martin Fowler and moreover it enforces the compliance with the &lt;a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;DRY principle&lt;/a&gt; by providing a single point of change for futurities alternations.&lt;a name="ref5"&gt;&lt;/a&gt;&lt;h4 class="custom"&gt;5. Introduce distinct target naming&lt;/h4&gt;&lt;div class="refactoringDescription"&gt;Use a different punctuation for targets and properties to enhance readability.&lt;/div&gt;&lt;div class="refactoringStatus"&gt;Before:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;property name="example.property1" value="abc" /&amp;gt;&lt;br /&gt;&amp;lt;property name="example_property2" value="def" /&amp;gt;&lt;br /&gt;&amp;lt;property name="example-Property3"  value="ghi" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="example.target1"&amp;gt;&lt;br /&gt;   &amp;lt;echo msg="${example.property1}" /&amp;gt;&lt;br /&gt;   ....&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="example-target2"&amp;gt;&lt;br /&gt;   &amp;lt;echo msg="${example-Property3}" /&amp;gt;&lt;br /&gt;   ....&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;&lt;div class="refactoringStatus"&gt;After:&lt;/div&gt;&lt;pre class="codeSnippetRefactoring"&gt;&amp;lt;property name="example.property1" value="abc" /&amp;gt;&lt;br /&gt;&amp;lt;property name="example.property2" value="def" /&amp;gt;&lt;br /&gt;&amp;lt;property name="example.property3" value="ghi" /&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="example-target1"&amp;gt;&lt;br /&gt;   &amp;lt;echo msg="${example.property1}" /&amp;gt;&lt;br /&gt;   ....&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;target name="example-target2"&amp;gt;&lt;br /&gt;   &amp;lt;echo msg="${example.property3}" /&amp;gt;&lt;br /&gt;   ....&lt;br /&gt;&amp;lt;/target&amp;gt;&lt;/pre&gt;The &lt;em&gt;Introduce distinct target naming&lt;/em&gt; refactoring once again tackles the improvement of readability in a build file by applying a constant and different punctuation on the common elements: targets and properties. The appliance of this refactoring leaves you and coworkers with an immediate reply whether you're looking at a property value or a target and can lead towards an agreed upon in-house/project &lt;a href="http://wiki.apache.org/ant/TheElementsOfAntStyle"&gt;build file coding standard&lt;/a&gt;. For target names underscores and dashes are suitable, although dashes are preferred by me as a hypen is used when &lt;a href="http://raphaelstolt.blogspot.com/2008/03/getting-overview-of-all-targets.html"&gt;enforcing internal targets&lt;/a&gt;, while for the build properties dots should be considered to build namespaces and their names should 'always' be lowercased except for environment variables/properties.&lt;br /&gt;&lt;br /&gt;In case this blog post whetted your appetite for more, heavier and here uncovered build file refactorings, you might consider &lt;a href="http://www.build-doctor.com"&gt;picking up&lt;/a&gt; a copy of the ThoughtWorks Anthology book. Last but not least a shout out goes to the build doctor for the remix permission and until the next post I'm ghost like dog.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/GAdkdRDjd3Q" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=6867408109374725659" title="9 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6867408109374725659?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6867408109374725659?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/07/six-valuable-phing-build-file.html" title="Six valuable Phing build file refactorings" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>9</thr:total></entry><entry gd:etag="W/&quot;DkIERH4yeip7ImA9WxZbE0Q.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-6391166006609930210</id><published>2008-04-17T02:37:00.009+02:00</published><updated>2008-04-17T03:08:25.092+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2008-04-17T03:08:25.092+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="PHP" /><category scheme="http://www.blogger.com/atom/ns#" term="Xinc" /><category scheme="http://www.blogger.com/atom/ns#" term="Continuous Integration" /><title>Hooking a Growl publisher plugin into Xinc</title><content type="html">This week I finally had the time to setup &lt;a href="http://code.google.com/p/xinc/" target="_blank"&gt;Xinc&lt;/a&gt;, PHP's shiny new Continuous Integration(CI) server, on my &lt;u&gt;MacBook&lt;/u&gt; and started to fiddle with it's publisher plugins and plugin architecture. While still down with 'tool envy' influenza I recently came across some nice and inspiring blog posts from the Ruby/Rails &lt;i&gt;camp&lt;/i&gt;, showing how to employ &lt;a href="http://growl.info/about.php"&gt;Growl&lt;/a&gt; as an &lt;a href="http://railstips.org/2007/7/23/autotest-growl-pass-fail-notifications" target="_blank"&gt;useful&lt;/a&gt; and &lt;a href="http://szeryf.wordpress.com/2007/07/30/way-beyond-cool-autotest-growl-doomguy/" target="_blank"&gt;fun&lt;/a&gt; feedback radiator for test/spec results. Since then the idea of building a Growl publisher plugin for Xinc was travelling my mind repeatedly, so the following post will break this circle and show a possible approach to build such a plugin, which can be used to notify the build result for continuously integrated projects and thereby provide an on-point/immediate feedback.&lt;br /&gt;&lt;h4 class="custom"&gt;Building the Growl publisher plugin&lt;/h4&gt;Xinc comes with an elegant plugin system, allowing you to easily &lt;a href="http://code.google.com/p/xinc/wiki/HowToCreateAPlugin" target="_blank"&gt; role your own&lt;/a&gt; one. The following code shows the plugin class and is the place where the actual Growl notifications are executed/raised by calling the growlnotify CLI. The images in the public &lt;i&gt;growl()&lt;/i&gt; method, used to accent the two main build statuses(failure and success) in the Growl notification, are borrowed from &lt;a href="http://www.thelucid.com/articles/2007/07/30/autotest-growl-fail-pass-smilies" target="_blank"&gt;this&lt;/a&gt; blog post and should be located in the &lt;em&gt;$HOME/Pictures&lt;/em&gt; directory.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;/**&lt;br /&gt; * A Growl publisher plugin for Xinc&lt;br /&gt; *&lt;br /&gt; * @package Xinc.Plugin&lt;br /&gt; * @author Raphael Stolt&lt;br /&gt; * @version 2.0&lt;br /&gt; * @copyright 2008 Raphael Stolt, Constance&lt;br /&gt; * @license  http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php&lt;br /&gt; */&lt;br /&gt;require_once 'Xinc/Plugin/Base.php';&lt;br /&gt;require_once 'Xinc/Plugin/Repos/Publisher/Growl/Task.php';&lt;br /&gt;&lt;br /&gt;class Xinc_Plugin_Repos_Publisher_Growl extends Xinc_Plugin_Base&lt;br /&gt;{&lt;br /&gt;    public function validate()&lt;br /&gt;    {&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    public function getTaskDefinitions()&lt;br /&gt;    {&lt;br /&gt;        return array(new Xinc_Plugin_Repos_Publisher_Growl_Task($this));&lt;br /&gt;    }&lt;br /&gt;    private function _sendGrowlNotification($message, $image, $name)&lt;br /&gt;    {&lt;br /&gt;        $command = "growlnotify -w -m '{$message}' "&lt;br /&gt;                 . "-n '{$name}' "&lt;br /&gt;                 . "-p 2 --image {$image}";&lt;br /&gt;&lt;br /&gt;        Xinc_Logger::getInstance()-&gt;info('Executed growlnotify command: ' . $command);&lt;br /&gt;&lt;br /&gt;        exec($command, $response, $return);&lt;br /&gt;&lt;br /&gt;        if ($return === 0) {&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;    public function growl(Xinc_Project &amp;$project, $message, $buildstatus, $name = 'Xinc')&lt;br /&gt;    {&lt;br /&gt;        if ($buildstatus === Xinc_Build_Interface::PASSED) {&lt;br /&gt;            $image = '$HOME/Pictures/pass.png';&lt;br /&gt;            $buildstatus = 'PASSED';&lt;br /&gt;        } elseif ($buildstatus === Xinc_Build_Interface::FAILED) {&lt;br /&gt;            $image = '$HOME/Pictures/fail.png';&lt;br /&gt;            $buildstatus = 'FAILED';&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        $project-&gt;info('Executing Growl publisher with content '&lt;br /&gt;                      ."\nMessage: " . $message&lt;br /&gt;                      ."\nBuildstatus: " . $buildstatus&lt;br /&gt;                      ."\nImage: " . $image);&lt;br /&gt;&lt;br /&gt;        return $this-&gt;_sendGrowlNotification($message, $image, $name);&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;The next big code chunk shows the task definition of the plugin and is the place where the publisher plugin name and is acceptable and required attributes are programmaticly defined, as you can see the Growl task currently only takes a required message attribute which can be set for the Growl publisher within the Xinc &lt;a href="#conf"&gt;project configuration file&lt;/a&gt;.&lt;pre class="codeSnippet"&gt;&amp;lt;?php&lt;br /&gt;/**&lt;br /&gt; * A Growl publisher plugin task for Xinc&lt;br /&gt; *&lt;br /&gt; * @package Xinc.Plugin&lt;br /&gt; * @author Raphael Stolt&lt;br /&gt; * @version 2.0&lt;br /&gt; * @copyright 2008 Raphael Stolt, Constance&lt;br /&gt; * @license  http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php&lt;br /&gt; */&lt;br /&gt;require_once 'Xinc/Plugin/Repos/Publisher/AbstractTask.php';&lt;br /&gt;&lt;br /&gt;class Xinc_Plugin_Repos_Publisher_Growl_Task extends Xinc_Plugin_Repos_Publisher_AbstractTask&lt;br /&gt;{&lt;br /&gt;    private $_message;&lt;br /&gt;&lt;br /&gt;    public function setMessage($message)&lt;br /&gt;    {&lt;br /&gt;        $this-&gt;_message = $message;&lt;br /&gt;    }&lt;br /&gt;    public function getName()&lt;br /&gt;    {&lt;br /&gt;        return 'growl';&lt;br /&gt;    }&lt;br /&gt;    private function _isGrowlnotifyAvailable()&lt;br /&gt;    {&lt;br /&gt;        exec('growlnotify -v', $reponse, $return);&lt;br /&gt;&lt;br /&gt;        if ($return === 0) {&lt;br /&gt;            return true;&lt;br /&gt;        }&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;    public function validateTask()&lt;br /&gt;    {&lt;br /&gt;        if (!$this-&gt;_isGrowlnotifyAvailable()) {&lt;br /&gt;            $message = 'The growlnotify command seems to be not available '&lt;br /&gt;                     . 'on this system';&lt;br /&gt;            Xinc_Logger::getInstance()-&gt;error($message);&lt;br /&gt;            throw new RuntimeException($message);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        if (!isset($this-&gt;_message)) {&lt;br /&gt;              $message = 'Element publisher/growl - required attribute '&lt;br /&gt;                       . '\'message\' is not set';&lt;br /&gt;              throw new Xinc_Exception_MalformedConfig($message);&lt;br /&gt;        }&lt;br /&gt;        return true;&lt;br /&gt;    }&lt;br /&gt;    public function publish(Xinc_Build_Interface &amp;$build)&lt;br /&gt;    {&lt;br /&gt;        $statusBefore = $build-&gt;getStatus();&lt;br /&gt;        $res = $this-&gt;_plugin-&gt;growl($build-&gt;getProject(), $this-&gt;_message, $build-&gt;getStatus());&lt;br /&gt;        if (!$res &amp;&amp; $statusBefore == Xinc_Build_Interface::PASSED ) {&lt;br /&gt;            /**&lt;br /&gt;             * Status was PASSED, but now the publish process made it fail&lt;br /&gt;             */&lt;br /&gt;            $build-&gt;setStatus(Xinc_Build_Interface::FAILED);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;h4 class="custom"&gt;Hooking the Growl publisher plugin into the build system&lt;/h4&gt;To make the just crafted Growl publisher plugin available to the Xinc build system you have to add the plugin file and class name/path to the &lt;em&gt;/etc/xinc/system.xml&lt;/em&gt; file. After restarting the Xinc server the Growl publisher should be available to use and also be listed in the section of the &lt;em&gt;/var/log/xinc.log&lt;/em&gt; file stating all registered plugins.&lt;br /&gt;&lt;br /&gt;The next listing shows the Growl publisher plugin added to the aforementioned XML file.&lt;pre class="codeSnippet"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;xinc&amp;gt;&lt;br /&gt;  &amp;lt;configuration&amp;gt;&lt;br /&gt;    &amp;lt;setting name="loglevel" value="2"/&amp;gt;&lt;br /&gt;    &amp;lt;setting name="timezone" value="Europe/Berlin"/&amp;gt;&lt;br /&gt;  &amp;lt;/configuration&amp;gt;&lt;br /&gt;  &amp;lt;plugins&amp;gt;&lt;br /&gt;    &amp;lt;plugin filename="Xinc/Plugin/Repos/ModificationSet.php" classname="Xinc_Plugin_Repos_ModificationSet"/&amp;gt;&lt;br /&gt;    ...&lt;br /&gt;    &lt;b&gt;&amp;lt;plugin filename="Xinc/Plugin/Repos/Publisher/Growl.php" classname="Xinc_Plugin_Repos_Publisher_Growl"/&amp;gt;&lt;/b&gt;&lt;br /&gt;    ...&lt;br /&gt;    &amp;lt;plugin filename="Xinc/Contrib/Warko/Plugin/ModificationSet/SvnTag.php"&lt;br /&gt;            classname="Xinc_Contrib_Warko_Plugin_ModificationSet_SvnTag"/&amp;gt;&lt;br /&gt;  &amp;lt;/plugins&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;engines&amp;gt;&lt;br /&gt;    &amp;lt;engine classname="Xinc_Engine_Sunrise" filename="Xinc/Engine/Sunrise.php" default="default"/&amp;gt;&lt;br /&gt;  &amp;lt;/engines&amp;gt;&lt;br /&gt;&amp;lt;/xinc&amp;gt;&lt;/pre&gt;&lt;h4 class="custom"&gt;Putting the Xinc Growl publisher plugin to action&lt;/h4&gt;Now that the Growl publisher plugin is available it can be used in the build cycle. The following Xinc project configuration file shows the utilization of the Growl publisher plugin in both of the main publisher realms &lt;em&gt;onsuccess&lt;/em&gt; and &lt;em&gt;onfailure&lt;/em&gt;.&lt;a name="conf"&gt;&lt;/a&gt;&lt;pre class="codeSnippet"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;&amp;lt;xinc engine="Sunrise"&amp;gt;&lt;br /&gt;  &amp;lt;project name="recordshelf"&amp;gt;&lt;br /&gt;    &amp;lt;configuration&amp;gt;&lt;br /&gt;      &amp;lt;setting name="loglevel" value="2"/&amp;gt;&lt;br /&gt;    &amp;lt;/configuration&amp;gt;&lt;br /&gt;    &amp;lt;property name="dir" value="${projectdir}/${project.name}" /&amp;gt;&lt;br /&gt;    &amp;lt;property name="build.failure.message" value="Build for project ${project.name} failed!"/&amp;gt;&lt;br /&gt;    &amp;lt;schedule interval="180" /&amp;gt;&lt;br /&gt;    &amp;lt;modificationset&amp;gt;&lt;br /&gt;      &amp;lt;svn directory="${dir}" update="true" /&amp;gt;&lt;br /&gt;    &amp;lt;/modificationset&amp;gt;&lt;br /&gt;    &amp;lt;builders&amp;gt;&lt;br /&gt;      &amp;lt;phingbuilder buildfile="${dir}/build.xml" target="main"/&amp;gt;&lt;br /&gt;    &amp;lt;/builders&amp;gt;&lt;br /&gt;    &amp;lt;publishers&amp;gt;&lt;br /&gt;      &amp;lt;onfailure&amp;gt;&lt;br /&gt;        &amp;lt;email to="example@example.com" subject="[${project.name}] Build failure"&lt;br /&gt;               message="${build.failure.message}" /&amp;gt;&lt;br /&gt;        &lt;b&gt;&amp;lt;growl message="${build.failure.message}" /&amp;gt;&lt;/b&gt;&lt;br /&gt;      &amp;lt;/onfailure&amp;gt;&lt;br /&gt;      &amp;lt;onsuccess&amp;gt;&lt;br /&gt;        &amp;lt;email to="example@example.com" subject="[${project.name}] Build success"&lt;br /&gt;               message="Build for project was successful" /&amp;gt;&lt;br /&gt;        &lt;b&gt;&amp;lt;growl message="Build for project ${project.name} was successful." /&amp;gt;&lt;/b&gt;&lt;br /&gt;      &amp;lt;/onsuccess&amp;gt;&lt;br /&gt;    &amp;lt;/publishers&amp;gt;&lt;br /&gt;  &amp;lt;/project&amp;gt;&lt;br /&gt;&amp;lt;/xinc&amp;gt;&lt;/pre&gt;The next two images are finally showing the Growl notifications for a successful and a failed build. To minimize the notification noise it's a good and common practice to only 'ring the alarm' for failed builds, which can be achieved by using the Growl publisher only in the &lt;em&gt;onfailure&lt;/em&gt; publisher realm of the Xinc project configuration file. Nuff talk, happy Xincing!&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/2419110337/" title="Growl notification for a successful build"&gt;&lt;br /&gt;    &lt;img src="http://farm3.static.flickr.com/2398/2419110337_5f3df4f7ac_o.jpg" width="370" height="96" alt="Growl notification for a successful build" border="0" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;a href="http://www.flickr.com/photos/raphaelstolt/2419924614/" title="Growl notification for failed build"&gt;&lt;br /&gt;    &lt;img src="http://farm3.static.flickr.com/2271/2419924614_acb6372459_o.jpg" width="370" height="96" alt="Growl notification for failed build" border="0" /&gt;&lt;br /&gt;&lt;/a&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/7Kh9H0mbOdw" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=6391166006609930210" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6391166006609930210?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/6391166006609930210?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/04/hooking-growl-publisher-plugin-into.html" title="Hooking a Growl publisher plugin into Xinc" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;Ck8AQH8-eSp7ImA9WxJTEEs.&quot;"><id>tag:blogger.com,1999:blog-8420118650236071171.post-7078640050186351982</id><published>2008-03-29T19:04:00.006+01:00</published><updated>2009-04-18T15:20:41.151+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2009-04-18T15:20:41.151+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Phing" /><title>Getting an overview of all targets accumulated in a Phing build file</title><content type="html">Looking for an equivalent to &lt;a href="http://ant.apache.org" target="_blank"&gt;Ant&lt;/a&gt;'s &lt;i&gt;-p(rojecthelp)&lt;/i&gt; command-line option in Phing, I dug up that you can use &lt;a href="http://phing.info/trac/" target="_blank"&gt;Phing&lt;/a&gt;'s &lt;i&gt;-l(ist)&lt;/i&gt; option to get a quick overview of all targets hosted in a given build file. This comes in handy when you are maintaining build files or have to get a raw picture of the provided targets of a project specific build file. The following console output shows the targets hosted in a example build file. As you will see, by using the -l command-line option you get an overview of the default target, the main targets and further subtargets.&lt;pre class="consoleOutput"&gt;triton:work stolt$ phing -l&lt;br /&gt;Buildfile: /Volumes/USB DISK/work/build.xml&lt;br /&gt;Build file for recordshelf project&lt;br /&gt;Default target:&lt;br /&gt;-------------------------------------------------------------------------------&lt;br /&gt; help                Displays the help for this build file&lt;br /&gt;&lt;br /&gt;Main targets:&lt;br /&gt;-------------------------------------------------------------------------------&lt;br /&gt; build               Builds the project&lt;br /&gt; clean               Removes data left over by former build&lt;br /&gt; cs-coding-standard  Runs the coding standard code inspection&lt;br /&gt; help                Display the help for this build file&lt;br /&gt; init                Initializes the build process&lt;br /&gt; layout              Creates the project layout for the build&lt;br /&gt; lint-project        Lints all code artifacts&lt;br /&gt; test-components     Runs the automated component tests&lt;br /&gt; test-integration    Runs the automated integration tests&lt;br /&gt; test-units          Runs the automated unit tests&lt;br /&gt;&lt;br /&gt;Subtargets:&lt;br /&gt;-------------------------------------------------------------------------------&lt;br /&gt; build-initial-model&lt;/pre&gt;A target becomes a main target when it has an non-empty &lt;em&gt;description&lt;/em&gt; attribute, otherwise it will be a subtarget. This can be used to define the visibility and thereby 'callability' of targets, similar as you would do when defining access specifiers for class members of a PHP class. Main targets would become the public targets of your build file and all subtargets could be considered as private targets, which shouldn't be called directly as they might/should be used to provide specific helper functionality to public main targets or target wrappers. To definitively veto the call to a target, which never should be run directly via the Phing CLI(e.g. &lt;em&gt;phing tragetname&lt;/em&gt;), just define an invalid target name(i.e. &lt;em&gt;-build-initial-model&lt;/em&gt; instead of build-initial-model) so the Phing CLI will respond with an error and thereby not run the build file. The next console output shows the Phing CLI response when calling a private target.&lt;pre class="consoleOutput"&gt;triton:work stolt$ phing -build-initial-model&lt;br /&gt;&lt;em&gt;Unknown argument: -build-initial-model&lt;/em&gt;&lt;br /&gt;phing [options] [target [target2 [target3] ...]]&lt;br /&gt;Options:&lt;br /&gt;  -h -help               print this message&lt;br /&gt;  -l -list               list available targets in this project&lt;br /&gt;  -v -version            print the version information and exit&lt;br /&gt;  -q -quiet              be extra quiet&lt;br /&gt;  -verbose               be extra verbose&lt;br /&gt;  -debug                 print debugging information&lt;br /&gt;  -logfile &amp;lt;file&amp;gt;        use given file for log&lt;br /&gt;  -logger &amp;lt;classname&amp;gt;    the class which is to perform logging&lt;br /&gt;  -f -buildfile &amp;lt;file&amp;gt;   use given buildfile&lt;br /&gt;  -D&amp;lt;property&amp;gt;=&amp;lt;value&amp;gt;   use value for given property&lt;br /&gt;  -find &amp;lt;file&amp;gt;           search for buildfile towards the root of the&lt;br /&gt;                         filesystem and use it&lt;br /&gt;  -inputhandler &amp;lt;file&amp;gt;   the class to use to handle user input&lt;br /&gt;&lt;br /&gt;Report bugs to &amp;lt;dev@phing.tigris.org&amp;gt;&lt;/pre&gt;This post is somehow a partial and PHP flavoured remix of a &lt;a href="http://www.build-doctor.com/2008/03/ant-best-practices-provide-good-help.html" target="_blank"&gt;post&lt;/a&gt; by Julian Simpson, who runs the &lt;a href="http://www.build-doctor.com/" target="_blank"&gt;build doctor&lt;/a&gt; blog and is also the author of the '&lt;i&gt;Refactoring Ant Build Files&lt;/i&gt;' contribution to the upcoming &lt;a href="http://www.pragprog.com/titles/twa" target="_blank"&gt;ThoughtWorks Anthology&lt;/a&gt; book.&lt;br /&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/raphaelonphp/~4/1vNrX7uNcQE" height="1" width="1"/&gt;</content><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8420118650236071171&amp;postID=7078640050186351982" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7078640050186351982?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8420118650236071171/posts/default/7078640050186351982?v=2" /><link rel="alternate" type="text/html" href="http://raphaelstolt.blogspot.com/2008/03/getting-overview-of-all-targets.html" title="Getting an overview of all targets accumulated in a Phing build file" /><author><name>Raphael Stolt</name><uri>http://www.blogger.com/profile/07949831701855458792</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="31" src="http://farm1.static.flickr.com/127/423512067_d1718c3a31_o.gif" /></author><thr:total>2</thr:total></entry></feed>
