<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  <channel>
    <title>Tag: symfony :: phly, boy, phly</title>
    <description>Tag: symfony :: phly, boy, phly</description>
    <pubDate>Fri, 14 Jan 2011 13:53:52 +0000</pubDate>
    <generator>Zend_Feed_Writer 2.1.4dev (http://framework.zend.com)</generator>
    <link>http://mwop.net/blog/tag/symfony.html</link>
    <atom:link rel="self" type="application/rss+xml" href="http://mwop.net/blog/tag/symfony-rss.xml"/>
    <item>
      <title>Aspects, Filters, and Signals, Oh, My!</title>
      <pubDate>Fri, 14 Jan 2011 13:53:52 +0000</pubDate>
      <link>http://mwop.net/blog/251-Aspects,-Filters,-and-Signals,-Oh,-My!.html</link>
      <guid>http://mwop.net/blog/251-Aspects,-Filters,-and-Signals,-Oh,-My!.html</guid>
      <author>me@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>
Last month, during <a href="http://phpadvent.org">PHP Advent</a>,
<a href="http://ohloh.net/accounts/gwoo">gwoo</a> wrote an interesting post on
<a href="http://phpadvent.org/2010/aspect-oriented-design-by-garrett-woodworth">
Aspect-Oriented Design</a>, or Aspect Oriented Programming (AOP) as it is more
commonly known. The article got me to thinking, and revisiting what I know about
AOP, Intercepting Filters, and Signal Slots -- in particular, what use cases I
see for them, what the state of current PHP offerings are, and where the future
may lie.
</p>

<p>
But first, some background is probably in order, as this is a jargon-heavy post.
</p><h2 id="toc_1.1">Aspect Oriented Programming</h2>

<p>
I was first introduced to AOP in 2006 via an <a href="http://www.phparch.com/magazine/2006-2/april/">April 2006 php|architect article by Dmitry Sheiko</a>. That article detailed adding calls at various
places in a method where you might want to hook into functionality -- for
instance, to log, cache, etc. Expanding on this, I considered other
possibilities: manipulating incoming arguments, validation, ACL checks,
implementing write-through caching strategies, and more. The approach is
daunting, however; typical, naive implementations lead to a lot of boiler-plate code:
</p>

<div class="example"><pre><code lang="php">
interface Listener
{
    public function notify($signal, $argv = null);
}

class Foo
{
    protected $listeners;

    public function attach($signal, Listener $listener)
    {
        $this-&gt;listeners[$signal][] = $listener;
    }

    public function doSomething($arg1, $arg2)
    {
        foreach ($this-&gt;listeners as $listener) {
            $listener-&gt;notify('preDoSomething', func_get_args());
        }
        
        // do some work
        
        foreach ($this-&gt;listeners as $listener) {
            $listener-&gt;notify('postDoSomething', $result);
        }
    }
}
</code></pre></div>

<p>
The article didn't go into any real details on how you might short-circuit the
filters or handle return values from them, and aspect handling itself was not detailed completely. As such, the code begins to add up, particularly if many
classes and/or methods implement the functionality. 
</p>

<h2 id="toc_1.2">Intercepting Filters</h2>

<p>
A similar concept to AOP is the idea of
<a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/InterceptingFilter.html">Intercepting</a> 
<a href="http://msdn.microsoft.com/en-us/library/ff647251.aspx">Filters</a>.
Like AOP, the idea is to separate cross-cutting concerns such as logging,
debugging, and more from the actual logic the component exposes. The difference
is that typically Intercepting Filters are language independent, have a standard
implementation in a given framework, and can be re-used.  The approach gwoo used
in his post falls more under this category.
</p>

<p>
<a href="http://lithify.me/">Lithium</a>, a PHP 5.3 framework and the reference for gwoo's
article, has a very intriguing approach. Instead of calling the filters
explicitly within the body of the code, they suggest that the body of the code
simply becomes one of the filters, via a closure:
</p>

<div class="example"><pre><code lang="php">
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
    // do something...
    return $chain-&gt;next($self, $params, $chain);
});
</code></pre></div>

<p>
In Lithium, each filter is responsible for calling the next (each filter
receives the chain as its third and final argument); as soon as one doesn't call
<code>next()</code>, execution is stopped, and the result returned (or at least that's how
I read the source). You can call the chain either before or after the code you
want to execute in each filter; placement will determine whether it's a pre- or
a post-filter. The approach answers a number of the concerns I outlined
previously -- namely, standardization of approach, and the ability to
short-circuit execution.
</p>

<p>
The above example defines a filter that will run when the <code>run()</code> method of the
<code>Dispatcher</code> class is executed. <code>$self</code> will typically be the object instance,
<code>$params</code> an array of the parameters passed to the method, and <code>$chain</code> as
described above. The method itself will execute any filters -- typically with
something like this:
</p>

<div class="example"><pre><code lang="php">
use lithium/core/Object as BaseObject;

class Foo extends BaseObject
{
    public function doSomething($with, $these, $args)
    {
        $params = compact('with', 'these', 'args');
        
        return $this-&gt;_filter(__METHOD__, $params, function ($self, $params) {
            // do the actual work here
            return $result;
        });
    }
}
</code></pre></div>

<p>
(The <code>_filter()</code> method is defined in <code>lithium\core\Object</code>, and basically
passes a local, static chain of filters to Lithium's <code>Filter</code> class for
execution. <code>applyFilter()</code> from the previous example statically adds a callback
under the named method to the chain.)
</p>

<p>
This solution is elegant -- but I see some limitations:
</p>

<ul>
<li>
   First and foremost, I'm not particularly fond of the filtering functionality
   being via static methods on a single class; it introduces a hard-coded,
   hidden dependency. This means you cannot provide alternate filtering
   functionality without extending the class <em>consuming</em> the filters, nor
   without either extending the base filters implementation should you wish to
   provide a compatible API (for instance, to introduce an implementation that
   understands priorities).
   <br /><br />
   Additionally, the easiest way to implement filtering in Lithium is by
   extending the <code>lithium\core\Object</code> class -- I could find no examples
   elsewhere in the documentation that showed how you would compose the <code>Filters</code>
   implementation in your own objects. As such, the easiest way to compose
   filters is now via inheritance, which seems to be counter-productive to the
   whole rationale behind filtering, to my thinking.
</li>
<li>
Second, the approach of making the body of the calling method a closure makes
   it difficult to create non-public helper methods.  Inside the filter, you're
   no longer in the scope of the object, losing the semantics that tie the
   various metadata and functionality of the object together. (The Lithium docs
   provide illustrations of how to accomplish this, but they require extra work,
   and a keen understanding of how references work in PHP.)
</li>
<li>
Third, it's sometimes useful to have access to the return results of <em>all</em>
   the filters (not just the last executed); you may want to aggregate them in
   some way, or branch logic based on the various returns. 
</li>
<li>
Fourth, it's sometimes useful to have multiple call points within the main
   code. As an example, for many caching strategies, you'd check first to see if
   you have a cache hit, and return immediatly if found; otherwise, you'd
   execute the code, and cache the result prior to returning it. This might be
   possible in Lithium with constructs like this:
<div class="example"><pre><code lang="php">
Filters::add('SomeClass::doSomething', function ($method, $self, $params) {
    if (null !== ($content = cache_hit($params))) {
        return $content;
    }
    $content = Filters::next($method, $self, $params);
    return $content;
});
</code></pre></div>
   However, if you have several filters such as this, the order then becomes
   paramount, and that introduces new complexities.
   <br /><br />
   Another example would be with façade methods, where you may wish to introduce
   filters before and after each method call:
<div class="example"><pre><code lang="php">
    public function doSomeWorkflow($message)
    {
        $this-&gt;somePrivateMethod($message);
        $this-&gt;nextPrivateMethod($message);
        $this-&gt;lastPrivateMethod($message);
    }
</code></pre></div>
    <br /><br />
   (I can already hear <a href="http://nateabele.com/">Nate</a> saying "make those all
   filters!" or "filter each method!" -- but that's the problem with simple
   examples - they can't always express the nuances of a use case.)
</li>
<li>
Fifth, it's useful to be able to attach callbacks that are not aware of the
   chain. For instance, you may have code you've already written that works
   perfectly fine in a standalone situation -- e.g., a logger -- and you simply
   want to add it to the chain. In the Lithium paradigm, you'd need to
   <a href="http://en.wikipedia.org/wiki/Currying">curry</a> the calls in, instead of simply
   using the existing method:
<div class="example"><pre><code lang="php">
// This:
SomeClass::applyFilter('doSomething', function ($self, $params, $chain) use ($log) {
    $log-&gt;info($params['message'];
    $chain-&gt;next($self, $params, $chain);
});
// VS:
SomeClass::signals()-&gt;connect('doSomething', $log, 'info');
</code></pre></div>
   Related to this, I personally dislike aggregating the filter parameters into
   a single associative array. I don't like having to test for the existence of
   parameters, and would much rather PHP tell me if I'm missing required
   parameters or if any fail typehints. That said, doing so provides a
   consistent API when filtering.
</li>
</ul>
   
<p>
All in all, however, the approach Lithium provides is very good; it just doesn't
completely suit my tastes or use cases.
</p>

<h2 id="toc_1.3">Signal Slots</h2>

<p>
Interestingly, the capabilities I need are not far from what Lithium provides --
in fact, I'd argue that the Intercepting Filters of Lithium are actually
probably more akin to another pattern,
<a href="http://en.wikipedia.org/wiki/Signals_and_slots">Signal Slots</a>. 
</p>

<p>
With Signal Slots, your code emits <em>signals</em> (Lithium does this -- it emits the
name of the method being called); any handler, or <em>slot</em> (<em>filters</em> in Lithium),
connected to the signal is then executed. 
</p>

<p>
As such, you typically have some sort of signal "manager" object (the <code>Filters</code>
class in Lithium) that aggregates signals and attached slots; this manager is
then composed into the object emitting signals. For those of you familiar with
events in JavaScript or other event-driven languages, this should sound quite
familiar.
</p>

<p>
Such an approach looks like this:
</p>

<div class="example"><pre><code lang="php">
class Foo
{
    protected $signals;

    public function signals(SignalSlot $signals = null)
    {
        if (null === $signals) {
            // No argument? make sure we have a signal manager
            if (null === $this-&gt;signals) {
                $this-&gt;signals = new Signals(); // SignalSlot implementation
            }
        } else {
            // Compose in an instance of a signal manager
            $this-&gt;signals = $signals;
        }
        return $this-&gt;signals;
    }

    public function doSomething($with, $these, $args)
    {
        $this-&gt;signals()-&gt;emit('doSomething.pre', $this, $with, $these, $args);
        
        // do some work
        $this-&gt;signals()-&gt;emit('doSomething.during', $this, $with, $these, $args);

        // do some more work
        // This time, pass the result
        $this-&gt;signals()-&gt;emit('doSomething.post', $this, $result, $with, $these, $args);
        return $result;
    }
}

$f = new Foo();
$f-&gt;signals()-&gt;connect('doSomething.pre', $log, 'info');
$f-&gt;signals()-&gt;connect('doSomething.during', $validator, 'isValid');
$f-&gt;signals()-&gt;connect('doSomething.post', $indexer, 'index');
</code></pre></div>


<p>
Basically, a <code>SignalSlot</code> provides an object in which signals and their attached
slots are aggregated. This allows having a single manager for multiple signals
(which is similar to how Lithium's <code>Filters</code> class works), while also providing
a way to emit multiple signals from a single procedure. Additionally, since it
is simply an object, you can compose it in to classes that may emit signals --
without requiring inheritance.
</p>

<p>
This is the basic approach of the
<a href="https://github.com/zendframework/zf2/tree/master/library/Zend/SignalSlot">ZF2 SignalSlot implementation</a>, as well as that found in
<a href="http://components.symfony-project.org/event-dispatcher/">Symfony 2's Event Dispatcher</a> and
<a href="http://incubator.apache.org/zetacomponents/documentation/trunk/SignalSlot/tutorial.html">Zeta Components' SignalSlot component</a>. 
</p>

<p>
Both Symfony 2's Event Dispatcher and ZF2's <code>SignalSlot</code> component build in the
ability to short-circuit, Symfony via a <code>notifyUntil()</code> method, and ZF2 via an
<code>emitUntil</code> method. With ZF2, each time a signal is emitted, a
<code>ResponseCollection</code> is returned by the manager, containing an aggregate of all
slot responses. Calling <code>emitUntil()</code> will short-circuit execution of remaining
slots if a given slot returns a response that validates against given criteria;
at this point, the collection is marked as "stopped", and you can pull the
"last" response and return it:
</p>

<div class="example"><pre><code lang="php">
$responses = $this-&gt;signals()-&gt;emitUntil(function($response) {
    return ($response instanceof SpecificResultType);
}, 'doSomething.pre', $this, $with, $these, $args);
if ($responses-&gt;stopped()) {
    return $responses-&gt;last();
}
</code></pre></div>

<p>
This introduces extra code in the method emitting the signals -- but meets the
criteria that no given slot need be aware of the chain. 
</p>

<p>
The Signal Slot approach actually supports paradigms similar to those
illustrated in Lithium. For instance, I can make my method body a slot:
</p>

<div class="example"><pre><code lang="php">
class Foo
{
    protected $handlers = array();

    // ... skip signals composition ...
    
    public function doSomething($with, $these, $args)
    {
        $params = compact('with', 'these', 'args');
        
        // connect() returns a signal handler (slot); store it so that we only
        // ever attach it once...
        if (isset($this-&gt;handlers[__FUNCTION__])) {
            $this-&gt;handlers[__FUNCTION__] = $this-&gt;signals()-&gt;connect(__FUNCTION__, function($self, $params) {
                // do the work here!
            });
        }
        
        // Emit the signal, and return the last result
        return $this-&gt;signals()-&gt;emit(__FUNCTION__, $this, $params)-&gt;last();
    }
}
</code></pre></div>

<h2 id="toc_1.4">Concerns</h2>

<p>
Using Signal Slots and Intercepting Filters is not without its concerns, nor is
any given implementation perfect.
</p>

<ul>
<li>
Zeta Components does a fantastic job of handling signal slots.  However, you
   cannot short-circuit execution, nor introspect return values. It does offer
   two features neither ZF2 nor Symfony 2 offer (at this time): the ability to
   <em>statically</em> connect slots to signals, allowing you to wire without having an
   existing instance, nor even caring what object might emit the signal; and the
   ability to add a priority to slots, which allows you to alter the execution
   order. 
</li>
<li>
Lithium does a nice job of providing good standards (signals are method
   names; parameters are predictable for all handlers), but at the price of some
   flexibility (static implementation with no interface for alternate
   implementations; no ability to re-use existing methods and functions with
   differing signatures without currying). 
</li>
<li>
Symfony 2 offers short-ciruiting and flexibility in callbacks, but requires
   that you create an event object to pass to the Event Dispatcher, making the
   usage slightly more verbose, and offers no standardization of signal naming. 
</li>
<li>
ZF2's <code>SignalSlots</code> offer similar benefits (and drawbacks) to Symfony 2's
   implementation, provides standardization of the signal manager response,
   allows registering classes that self-register with the signal handler, but
   lacks static wiring capabilities or prioritization.
</li>
</ul>

<p>
On a more abstract level, signal slots and intercepting filters can lead to
difficulties in learning and mastering code that use them:
</p>

<ul>
<li>
How are signals named?
</li>
<li>
How do you document the parameters available to slots/filters?
</li>
<ul>
<li>
How can those using IDEs discover available signals? and the arguments
      expected?
</li>
</ul>
<li>
Where does the wiring occur?
</li>
<ul>
<li>
For instance, if any wiring is automated, this can potentially lead to
      more difficulty in debugging.
</li>
<li>
If done manually, when, and where? 
</li>
</ul>
<li>
What happens if a slot doesn't receive arguments it needs, or cannot handle
   the arguments it receives?
</li>
</ul>

<p>
In short, while they solve many problems, the implementations also introduce new
concerns -- though this will be true of any extension system, in my experience.
</p>

<h2 id="toc_1.5">Conclusions</h2>

<p>
I personally am a huge fan of Intercepting Filters and Signal Slots. I think
they can make code easier to extend, by providing a standard methodology for
introducing cross-cutting concerns without requiring class extension. They can
also make code quite expressive -- sometimes at the cost of readability -- by
introducing functional programming paradigms. 
</p>

<p>
If you have not investigated these concepts or components before, I highly
recommend doing so; I think they play a fundamental role in the next generation
of PHP frameworks.
</p>

<h2 id="toc_1.6">Caveats</h2>

<p>
I am not an expert, nor well-versed, in all the frameworks listed here, and as
such, some of the information may be incorrect or incomplete. I am the
author of ZF2's current Signal Slot implementation, and am still working on
improvements to it.
</p>

<h2>Updates</h2>
<ul>
    <li><b>2011-01-10 11:35Z-05:00</b> Cal Evans found the original php|architect article I referenced, and I've revised some of the assessments based on re-reading it, as well as linked to the issue.</li>
</ul>]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
    <item>
      <title>Symfony Live 2010</title>
      <pubDate>Sun, 21 Feb 2010 23:53:00 +0000</pubDate>
      <link>http://mwop.net/blog/232-Symfony-Live-2010.html</link>
      <guid>http://mwop.net/blog/232-Symfony-Live-2010.html</guid>
      <author>me@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>
    This week, I've been attending <a
        href="http://www.symfony-live.com/">Symfony Live</a> in Paris, speaking
    on integrating Zend Framework with Symfony. The experience has been quite
    rewarding, and certainly eye-opening for many.
</p>

<p>
    To be honest, I was a little worried about the conference -- many see
    Symfony and ZF as being in competition, and that there would be no
    cross-pollination. I'm hoping that between <a
        href="http://fabien.potencier.org/">Fabien</a>, <a
        href="http://www.leftontheweb.com/">Stefan</a>, and myself, we helped
    dispel that myth this week.
</p><p>
    The fact of the matter is that no single project can be fully comprehensive,
    and do everything perfectly. In my examinations of different frameworks, PHP
    and otherwise, the places where they most differ and which generates the
    most loyalty amongst users are the MVC approaches and tooling support. In
    good frameworks, this is just a portion of the code, and the remainder is in
    support libraries or plugins that extend that functionality.
</p>

<p>
    This is true of both Symfony and Zend Framework. Symfony's development team
    has chosen to focus on a very specific core of functionality related to the
    MVC approach, which makes their maintenance job easier, and leads to a
    stable product. Zend Framework's MVC implementation is offered as a group of
    separate components, with components such as Zend_Application and Zend_Tool
    helping to bring cohesion and structure to them.
</p>

<p>
    What this means is that once you've developed the basic infrastructure of
    your application, the scaffolding, you're now left with decisions about how
    to implement the actual functionality of the application itself. The problem
    as I see it is: how do you do that development? Many developers are myopic
    and will not look beyond the framework they have chosen for for development.
    This can lead to multiple implementations of the same code, and often leads
    to incomplete implementations as well.
</p>

<p>
    My feeling is that whenever you find yourself about to write new code, look
    to see if somebody else has written the code already. Anybody -- don't limit
    yourself to your framework of choice. If I want to do serious HTML sniffing,
    validation, and cleanup, I go to <a
        href="http://htmlpurifier.org/">HTMLPurifier</a>; if I want a workflow
    component, I check out <a
        href="http://www.ezcomponents.org/docs/api/latest/introduction_Workflow.html">eZ
        Components Workflow</a>; I always check <a
        href="http://pear.php.net/">PEAR</a>.
</p>

<p>
    This week, I tried to spread this message within the <a
        href="http://symfony-project.org">Symfony</a> community, showing them
    how easy it is to integrate ZF components within Symfony projects. The
    integration itself is simple: instantiate the Zend autoloader, and start
    using ZF classes. This same technique can be used to load PEAR, or
    eZComponents, or Doctrine 2, etc. The trick is getting out of the "Not
    Invented Here" syndrome, letting go of your ego, and using <em>other</em>
    people's code.
</p>

<p>
    (Yes, I know we have code in ZF duplicating functionality in other
    libraries; in most cases, we try and offer at least a new approach to the
    problem -- but we could do better.)
</p>

<p>
    Fabien also made an interesting announcement. During a Q&amp;A session with
    the Symfony core team, he said that Symfony 2 will not write re-invent the
    wheel when it doesn't need to -- and announced that Symfony 2 will be using
    <code>Zend_Log</code> and <code>Zend_Cache</code> instead of rewriting the
    current Symfony components. I find this admirable -- and it's something I'm
    hoping to do in a few places with Zend Framework 2.0 as well, as I know
    there are features and code that others have, quite simply, written better.
</p>

<p>
    One last note in this ramble: With the various "2.0" versions of frameworks,
    most projects are learning from both mistakes made as well as from the usage
    patters of the developers adopting them. One of those lessons, to my mind,
    is that no one framework can do it all well and by themselves. I fully
    expect to see the next generation of frameworks making it trivial to pull
    features from other frameworks and libraries in order to fill out
    functionality.
</p>]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
  </channel>
</rss>
