<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.symforc.com/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Symfony Resources Central - cocoa</title>
  <link>http://www.symforc.com/</link>
  <description>A place that groups many original symfony resources, along with my personal experiments of this great PHP5 MVC framework.</description>
  <language>en</language>
  <pubDate>Sat, 26 Jul 2008 19:34:33 +0200</pubDate>
  <copyright>All rights reserved</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Overview of symfony 1.1 event dispatcher</title>
    <link>http://www.symforc.com/post/2008/04/30/Overview-of-symfony-11-event-dispatcher</link>
    <guid isPermaLink="false">urn:md5:a6dc9a4f45b15c54b5b47a85f8484d45</guid>
    <pubDate>Wed, 30 Apr 2008 23:56:00 +0200</pubDate>
    <dc:creator>Romain Dorgueil</dc:creator>
        <category>how-to</category>
        <category>cocoa</category><category>event dispatcher</category><category>plugins</category><category>symfony1.1</category>    
    <description>    &lt;p&gt;As you may now by now, Symfony 1.1 introduces a new powerfull event dispatcher inspired by Apple Cocoa's NotificationCenter. Basically, it allows any entity to &quot;listen&quot; to events, and get a call on the registered callback if this event ever happens.&lt;/p&gt;


&lt;p&gt;Symfony 1.1 provides &lt;a href=&quot;http://trac.symfony-project.com/wiki/Symfony11Events&quot; hreflang=&quot;en&quot;&gt;some default events&lt;/a&gt; you can listen to, but of course you can create your own events if you need.&lt;/p&gt;


&lt;h2&gt;Listen to an event&lt;/h2&gt;


&lt;p&gt;To listen to an event, you need to use the &quot;connect&quot; method on the event dispatcher instance. The first parameter is the event name, and the second is a PHP callable that will get called if the event happens.&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$dispatcher&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;connect&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'user.change_culture'&lt;/span&gt;, &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/array&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'listenToChangeCultureEvent'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;h2&gt;Create a custom event&lt;/h2&gt;


&lt;p&gt;To use the dispatcher for your own needs, you just need to define your event name in your project specifications, and send notifications to it. Depending on the behaviour needed, three options are offered:&lt;/p&gt;


&lt;h3&gt;Simple notifications&lt;/h3&gt;


&lt;p&gt;The simpliest way is to notify all listeners with the -&amp;gt;notify method.&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$dispatcher&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;notify&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; sfEvent&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'my.super.cool.event'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;h3&gt;Notifications until something&lt;/h3&gt;


&lt;p&gt;Sometimes, you prefer to notify all listeners until one says &quot;Ok guys, I handled this one. Don't worry about it anymore&quot;.&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$dispatcher&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;notifyUntil&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; sfEvent&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'my.super.cool.event'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;The first listener that will return non-false value will stop the event chain.&lt;/p&gt;


&lt;h3&gt;Filtering notifications&lt;/h3&gt;


&lt;p&gt;The last notifying method is called filtering. You set this up when you want to permit anything to act as a filter on something, meaning any listener can modify a source object.&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$dispatcher&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; sfEvent&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'my.super.cool.event'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, &lt;span style=&quot;color: #0000ff;&quot;&gt;$objectToFilter&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;Every listener must return the filtered value (or the original object if nothing was done) to pass to the next listener.&lt;/p&gt;


&lt;h2&gt;Practical use: Register routes in your plugins&lt;/h2&gt;


&lt;p&gt;One of the first practical applications that came to me was the new way of registering routes in plugins. In symfony 1.0, a coincidence made possible to use the routing in a plugin's config.php but that's not possible anymore in symfony 1.1, so you have to use the event dispatcher. To accomplish this, we're going to set up a routing.load_configuration listener in the plugin's config.php:&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;$this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;dispatcher&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;connect&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'routing.load_configuration'&lt;/span&gt;, &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/array&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'myPluginRouting'&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'listenToRoutingLoadConfigurationEvent'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;Then you just need to create the callback class/method:&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&lt;code class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; myPluginRouting&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**&lt;br /&gt;
&amp;nbsp; &amp;nbsp;* Listens to the routing.load_configuration event.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;*&lt;br /&gt;
&amp;nbsp; &amp;nbsp;* @param sfEvent An sfEvent instance&lt;br /&gt;
&amp;nbsp; &amp;nbsp;*/&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/static&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;static&lt;/span&gt;&lt;/a&gt; public &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;function&lt;/span&gt; listenToRoutingLoadConfigurationEvent&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;sfEvent &lt;span style=&quot;color: #0000ff;&quot;&gt;$event&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$r&lt;/span&gt; = &lt;span style=&quot;color: #0000ff;&quot;&gt;$event&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;getSubject&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// preprend our routes&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000ff;&quot;&gt;$r&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: #006600;&quot;&gt;prependRoute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'my_route'&lt;/span&gt;, &lt;span style=&quot;color: #ff0000;&quot;&gt;'/my_plugin/:action'&lt;/span&gt;, &lt;a target=&quot;_blank&quot; href=&quot;http://www.php.net/array&quot;&gt;&lt;span style=&quot;color: #000066;&quot;&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'module'&lt;/span&gt; =&amp;gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'myPluginAdministrationInterface'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;


&lt;p&gt;Here we go :-D&lt;/p&gt;</description>
    
    
    
          <comments>http://www.symforc.com/post/2008/04/30/Overview-of-symfony-11-event-dispatcher#comment-form</comments>
      <wfw:comment>http://www.symforc.com/post/2008/04/30/Overview-of-symfony-11-event-dispatcher#comment-form</wfw:comment>
      <wfw:commentRss>http://www.symforc.com/feed/rss2/comments/74</wfw:commentRss>
      </item>
    
</channel>
</rss>