<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title><![CDATA[Alessandro Nadalin]]></title>
  <link href="http://odino.org/atom.xml" rel="self"/>
  <link href="http://odino.org/"/>
  <updated>2013-06-18T16:38:16+04:00</updated>
  <id>http://odino.org/</id>
  <author>
    <name><![CDATA[Alessandro Nadalin]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[PHP User Group Dubai, here we start again!]]></title>
    <link href="http://odino.org/php-user-group-dubai-here-we-start-again/"/>
    <updated>2013-06-18T13:38:00+04:00</updated>
    <id>http://odino.org/php-user-group-dubai-here-we-start-again</id>
    <content type="html"><![CDATA[<p>After launching a couple
PHP User Groups in Italy
(<a href="http://roma.grusp.org">Rome</a> and <a href="http://friuli.grusp.org">Friuli</a>),
I started to feel kind of lost here in
Dubai, a place that gathers together lots
of talents but doesn&rsquo;t have that much of
knowledge-sharing culture.</p>

<p>Given this and the pro-active involvement of
some of my colleagues from <a href="http://en-ae.namshi.com">Namshi</a>,
I really wanted to see some sort of community
raise in this desert.</p>

<!-- more -->


<p>So a couple weeks ago I opened a
<a href="https://groups.google.com/forum/?fromgroups#!forum/php-user-group-dubai">google group</a>
for the PHP User Group Dubai &ndash; pretty empty right now &ndash; which is the place
where we&rsquo;re going to discuss meetings, ask for support
and <em>chillax</em> in a nerdy way.</p>

<p>Yesterday we had our first meeting, where 7 of us
discussed on the directions that we should initially take to gain the attention of others in the development community and
which kind of formats to use during our meetings; the main points that came out of the meeting are:</p>

<ul>
<li>we will try to meet once a month</li>
<li>we will try to set and prepare the next meeting before the beginning of Ramadan (~10 July)</li>
<li>everyone will try to bring a new joiner (friend, colleague, etc.) to the next meeting</li>
<li>we will have a talk and a discussion after that, most probably in front of a dinner accompanied by some <em>sheesha</em></li>
<li>most probably, since not everyone is aware of the potential and benefits of automated testing, we will prepare a talk on
PHPUnit</li>
</ul>


<p>So, if you are a PHP developer based in the UAE
&ndash; not necessarily Dubai &ndash; why don&rsquo;t you subscribe
to our <a href="http://groups.google.com/forum/?fromgroups#!forum/php-user-group-dubai">PHP User Group Dubai google group</a> and
introduce yourself? Do you want to understand why testing your applications manually it&rsquo;s a waste of your energies and of money of your company? Do you know about <a href="http://twig.sensiolabs.org">Twig</a>? Have you ever tried developing an application with <a href="http://angularjs.org/">AngularJS</a> and some HTTP APIs as the backend of that app? How can you <a href="http://odino.org/securing-your-http-api-with-javascript-object-signing-and-encryption/">integrate the JWS authentication mechanism in your PHP code</a>?</p>

<p>It would be great to see you at the next meeting!<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup></p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>By the way, we will decide the date, location and topic of the meeting with a discussion in the google group <a href='#fnref:1' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Integrating Twig in your legacy code, Part 2: a less wild approach]]></title>
    <link href="http://odino.org/integrating-twig-in-your-legacy-code-part-2-a-less-wild-approach/"/>
    <updated>2013-06-18T12:07:00+04:00</updated>
    <id>http://odino.org/integrating-twig-in-your-legacy-code-part-2-a-less-wild-approach</id>
    <content type="html"><![CDATA[<p>In my last post I wrote about
<a href="http://odino.org/integrating-twig-in-your-legacy-php-code/">integrating Twig into your legacy code</a>
with a really <strong>wild</strong>
approach.</p>

<p>Today I came up with a better
solution that lets you take advantage
of Twig full features without any hack
(like the <code>partial</code> tag I was
talking about in my previous post).</p>

<!-- more -->


<p>Instead of parsing the generated output
as a string with Twig, we can store it
into a template, which lets us use
features like <code>use</code>, <code>extends</code>, <code>include</code>,
thing that is almost impossible &ndash; in a clean way &ndash;
if we use the Twig string loader:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Framework_Base_Controller</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">render</span><span class="p">(</span><span class="nv">$templateName</span><span class="p">,</span> <span class="k">array</span> <span class="nv">$parameters</span> <span class="o">=</span> <span class="k">array</span><span class="p">())</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="c1">// ....</span>
</span><span class='line'>      <span class="c1">// do stuff to render your template</span>
</span><span class='line'>      <span class="c1">// we have the HTML output in the $templateOutput variable</span>
</span><span class='line'>      <span class="c1">// ...</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$twig</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Environment</span><span class="p">(</span><span class="k">new</span> <span class="nx">Twig_Loader_String</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>          <span class="s1">&#39;autoescape&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">,</span>
</span><span class='line'>      <span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="nv">$templateOutput</span><span class="p">,</span> <span class="nv">$parameters</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Instead of using the <code>Twig_String_Loader</code> we would use an array
loader, and store <code>$templateOutput</code> in a unique template, called <code>__MAIN__</code>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Framework_Base_Controller</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">render</span><span class="p">(</span><span class="nv">$templateName</span><span class="p">,</span> <span class="k">array</span> <span class="nv">$parameters</span> <span class="o">=</span> <span class="k">array</span><span class="p">())</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="c1">// ....</span>
</span><span class='line'>      <span class="c1">// do stuff to render your template</span>
</span><span class='line'>      <span class="c1">// we have the HTML output in the $templateOutput variable</span>
</span><span class='line'>      <span class="c1">// ...</span>
</span><span class='line'>
</span><span class='line'>      <span class="nv">$finder</span>     <span class="o">=</span> <span class="k">new</span> <span class="nx">Symfony\Component\Finder\Finder</span><span class="p">();</span>
</span><span class='line'>      <span class="nv">$templates</span>  <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">foreach</span> <span class="p">(</span><span class="nv">$finder</span><span class="o">-&gt;</span><span class="na">in</span><span class="p">(</span><span class="s1">&#39;/path/to/twig/templates&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="nv">$file</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$file</span><span class="o">-&gt;</span><span class="na">isDir</span><span class="p">())</span> <span class="p">{</span>
</span><span class='line'>              <span class="nv">$templates</span><span class="p">[</span><span class="nv">$file</span><span class="o">-&gt;</span><span class="na">getRelativePathName</span><span class="p">()]</span> <span class="o">=</span> <span class="nv">$file</span><span class="o">-&gt;</span><span class="na">getContents</span><span class="p">();</span>
</span><span class='line'>          <span class="p">}</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>      <span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Loader_Array</span><span class="p">(</span><span class="nv">$templates</span><span class="p">);</span>
</span><span class='line'>        <span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">setTemplate</span><span class="p">(</span><span class="s1">&#39;__MAIN__&#39;</span><span class="p">,</span> <span class="nv">$templateOutput</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>      <span class="nv">$twig</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Environment</span><span class="p">(</span><span class="nv">$loader</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>          <span class="s1">&#39;autoescape&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">,</span>
</span><span class='line'>      <span class="p">));</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">try</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="nv">$templateName</span><span class="p">,</span> <span class="nx">Alice_Component_Registry</span><span class="o">::</span><span class="na">getAll</span><span class="p">());</span>
</span><span class='line'>      <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="nx">Twig_Error_Loader</span> <span class="nv">$e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s2">&quot;__MAIN__&quot;</span><span class="p">,</span> <span class="nx">Alice_Component_Registry</span><span class="o">::</span><span class="na">getAll</span><span class="p">());</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>And that&rsquo;s it!</p>

<p>Now you can write your own <code>$templateName</code>:</p>

<figure class='code'><figcaption><span>/path/to/twig/templates/$templateName</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">{</span> % extends <span class="s1">&#39;__MAIN__&#39;</span> % <span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="o">{</span> % use <span class="s1">&#39;whatever&#39;</span> % <span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="o">{</span> % block somewhat % <span class="o">}</span>
</span><span class='line'>  some content
</span><span class='line'><span class="o">{</span> % endblock % <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Integrating Twig in your legacy PHP code]]></title>
    <link href="http://odino.org/integrating-twig-in-your-legacy-php-code/"/>
    <updated>2013-06-15T23:15:00+04:00</updated>
    <id>http://odino.org/integrating-twig-in-your-legacy-php-code</id>
    <content type="html"><![CDATA[<p>It might happen that you are working
on a legacy code that is years old,
with its own templating mechanism<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>
that doesn&rsquo;t really allow you to take
advantage of the benefits that a structured
and object-oriented engine like
<a href="http://twig.sensiolabs.org/">Twig</a>.</p>

<p>In this situations, when a complete replacement
would cost too much to your organization,
you can take advantage of a <em>wild</em> integration between
this advanced template engine and your
existing code.</p>

<!-- more -->




<p class='info info' data-title='Outdated'>This article is outdated! A better approach was described here:<br/> http://integrating-twig-in-your-legacy-code-part-2-a-less-wild-approach/</p>


<h2>Approach</h2>

<p>The main idea is that you should anyway
have a man function which outputs
what is being rendered on the view, so that
you can capture that output and parse it via
Twig, something like a <code>render</code> function
in your controllers:</p>

<figure class='code'><figcaption><span>Example controller</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">My_Controller</span> <span class="k">extends</span> <span class="nx">Framework_Base_Controller</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">indexAction</span><span class="p">()</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="c1">// ....</span>
</span><span class='line'>      <span class="c1">// do stuff</span>
</span><span class='line'>      <span class="c1">// ...</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;my_template&#39;</span><span class="p">,</span> <span class="nv">$parametersForTheView</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>Example of a base controller</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Framework_Base_Controller</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">render</span><span class="p">(</span><span class="nv">$templateName</span><span class="p">,</span> <span class="k">array</span> <span class="nv">$parameters</span> <span class="o">=</span> <span class="k">array</span><span class="p">())</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="c1">// ....</span>
</span><span class='line'>      <span class="c1">// do stuff to render your template</span>
</span><span class='line'>      <span class="c1">// ...</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">return</span> <span class="nv">$templateOutput</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>      <span class="c1">// will become</span>
</span><span class='line'>      <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="nv">$templateOutput</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this point the only thing that
you need is to inject the Twig engine
into your base controller and parse the
output of your legacy templates with Twig<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// your actual rendering:</span>
</span><span class='line'><span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="nv">$templateOutput</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// which means:</span>
</span><span class='line'><span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;&lt;html&gt;&lt;head&gt;&lt;title&gt;Hello world&lt;/title&gt;...&lt;/html&gt;&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// so that you can actually write twig in your templates:</span>
</span><span class='line'><span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="s1">&#39;&lt;html&gt;&lt;head&gt;&lt;title&gt;{ % block title % }Hello world{ % endblock % }&lt;/title&gt;...&lt;/html&gt;&#39;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>




<p class='info warning' data-title='Attention'>The &#8216;block&#8217; tag in the example above is having a space between curly brackets and the percentage char since my blog engine (octopress) doesn&#8217;t allow those tags them in code blocks.<br/>In all of the next examples you will see Twig tags written like that.</p>


<h2>Rendering content via Twig</h2>

<p>To integrate Twig in your application
it it really a matter of a few minutes:
first, you will have to download and move
the library inside your codebase, then,
thanks to the PSR-0 autoloading (here we
will be using Symfony2&rsquo;s autoloader, but
you can use any PSR-0 compliant autoloader) you just
need to include it and setup Twig&rsquo;s own
autoloader:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">require_once</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/vendor/symfony/Symfony/Component/ClassLoader/UniversalClassLoader.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Symfony\Component\ClassLoader\UniversalClassLoader</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">UniversalClassLoader</span><span class="p">();</span>
</span><span class='line'><span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerNamespaces</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
</span><span class='line'>    <span class="s1">&#39;Twig&#39;</span> <span class="o">=&gt;</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/vendor/twig/lib/&#39;</span><span class="p">,</span>
</span><span class='line'><span class="p">));</span>
</span><span class='line'>
</span><span class='line'><span class="k">require_once</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/vendor/twig/lib/Twig/Autoloader.php&#39;</span><span class="p">;</span>
</span><span class='line'><span class="nx">Twig_Autoloader</span><span class="o">::</span><span class="na">register</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this point, let&rsquo;s get back to our <code>render</code>
function, which we will need to modify in order
to include Twig:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Framework_Base_Controller</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">render</span><span class="p">(</span><span class="nv">$templateName</span><span class="p">,</span> <span class="k">array</span> <span class="nv">$parameters</span> <span class="o">=</span> <span class="k">array</span><span class="p">())</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="c1">// ....</span>
</span><span class='line'>      <span class="c1">// do stuff to render your template</span>
</span><span class='line'>      <span class="c1">// we have the HTML output in the $templateOutput variable</span>
</span><span class='line'>      <span class="c1">// ...</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$twig</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Environment</span><span class="p">(</span><span class="k">new</span> <span class="nx">Twig_Loader_String</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>          <span class="s1">&#39;autoescape&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">,</span>
</span><span class='line'>      <span class="p">));</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="nv">$templateOutput</span><span class="p">,</span> <span class="nv">$parameters</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this point we would be already able to write
Twig code inside our templates:</p>

<figure class='code'><figcaption><span>my_index.html</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">{ % set posts = registry.get(&#39;blog_post&#39;).findByUser($user.id) % }</span>
</span><span class='line'><span class="x">&lt;p&gt;</span>
</span><span class='line'><span class="x">  { % for post in posts % }</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">  { % else % }</span>
</span><span class='line'><span class="x">      This user didn&#39;t write any post</span>
</span><span class='line'><span class="x">  { % endfor % }</span>
</span><span class='line'><span class="x">&lt;/p&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>A new tag</h2>

<p>Unfortunately, to support some kind of
inheritance, which is one of the greatest
features of Twig, the situation becomes a little
bit trickier: first of all, we will need to add
to the parsed HTML some extra content to override
blocks, then we will need to create a new Twig
token parser in order to allow declaring multiple
blocks with the same name, which is not allowed by the
<code>block</code> tag.</p>

<p>Let&rsquo;s say that all of your templates are including
a base layout made of a very clean HTML structure:</p>

<figure class='code'><figcaption><span>Base layout of your framework</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">&lt;html&gt;</span>
</span><span class='line'><span class="x">  &lt;head&gt;</span>
</span><span class='line'><span class="x">      &lt;title&gt;</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$title</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x">&lt;/title&gt;</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">  &lt;/head&gt;</span>
</span><span class='line'><span class="x">  &lt;body&gt;</span>
</span><span class='line'><span class="x">      </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$content</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span>
</span><span class='line'><span class="x">  &lt;/body&gt;</span>
</span><span class='line'><span class="x">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this point, since we are able to parse generated
HTMLs with Twig, you can simply add a couple blocks:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">&lt;html&gt;</span>
</span><span class='line'><span class="x">  &lt;head&gt;</span>
</span><span class='line'><span class="x">      &lt;title&gt;</span>
</span><span class='line'><span class="x">          { % block title % }</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$title</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x">{ % endblock % }</span>
</span><span class='line'><span class="x">      &lt;/title&gt;</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">  &lt;/head&gt;</span>
</span><span class='line'><span class="x">  &lt;body&gt;</span>
</span><span class='line'><span class="x">      { % block content % }</span>
</span><span class='line'><span class="x">          </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$content</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span>
</span><span class='line'><span class="x">      { % endblock% }</span>
</span><span class='line'><span class="x">  &lt;/body&gt;</span>
</span><span class='line'><span class="x">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>After we do it, how can we override these blocks
differently from each controllers&#8217; actions?
You simply include other Twig content at the end of the
generated HTML:</p>

<figure class='code'><figcaption><span>Adding support for basic inheritance</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">Framework_Base_Controller</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">render</span><span class="p">(</span><span class="nv">$templateName</span><span class="p">,</span> <span class="k">array</span> <span class="nv">$parameters</span> <span class="o">=</span> <span class="k">array</span><span class="p">())</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="c1">// ....</span>
</span><span class='line'>      <span class="c1">// do stuff to render your template</span>
</span><span class='line'>      <span class="c1">// we have the HTML output in the $templateOutput variable</span>
</span><span class='line'>      <span class="c1">// ...</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$twig</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Environment</span><span class="p">(</span><span class="k">new</span> <span class="nx">Twig_Loader_String</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>          <span class="s1">&#39;autoescape&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">,</span>
</span><span class='line'>      <span class="p">));</span>
</span><span class='line'>      <span class="nv">$twigTemplate</span> <span class="o">=</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s2">&quot;path/to/twig/templates/%s.twig&quot;</span><span class="p">,</span> <span class="nv">$templateName</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nb">file_exists</span><span class="p">(</span><span class="nv">$twigTemplate</span><span class="p">)))</span> <span class="p">{</span>
</span><span class='line'>          <span class="nv">$templateOutput</span> <span class="o">.=</span> <span class="nb">file_get_contents</span><span class="p">(</span><span class="nv">$twigTemplate</span><span class="p">);</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$twig</span><span class="o">-&gt;</span><span class="na">render</span><span class="p">(</span><span class="nv">$templateOutput</span><span class="p">,</span> <span class="nv">$parameters</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>And then override the content with your own twig template:</p>

<figure class='code'><figcaption><span>path/to/twig/templates/templateName.twig</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">{</span> % block title % <span class="o">}</span>
</span><span class='line'>  About: this is our about page
</span><span class='line'><span class="o">{</span> % endblock % <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>After you setup everything, you will realize that
there is a huge problem here: since Twig doesn&rsquo;t allow
to declare blocks Twig, you can use the <code>block</code> tag!</p>

<p>To overcome the problem, you can simply add a new tag,
<code>partial</code>:</p>

<figure class='code'><figcaption><span>The new tag is implemented via a token parser</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="sd">/**</span>
</span><span class='line'><span class="sd"> * Token parser for the twig engine that adds support to redefinable blocks,</span>
</span><span class='line'><span class="sd"> * under the &#39;partial&#39; alias.</span>
</span><span class='line'><span class="sd"> * </span>
</span><span class='line'><span class="sd"> * IE:</span>
</span><span class='line'><span class="sd"> * { % partial myPartial % }First{ % endpartial %}</span>
</span><span class='line'><span class="sd"> * { % partial myPartial % }Second{ % endpartial %}</span>
</span><span class='line'><span class="sd"> * </span>
</span><span class='line'><span class="sd"> * will render &quot;Second&quot;.</span>
</span><span class='line'><span class="sd"> * </span>
</span><span class='line'><span class="sd"> * This is needed since the { % block % } tag doesnt support redefining blocks</span>
</span><span class='line'><span class="sd"> * with the string loader, it just supports it via inheritance.</span>
</span><span class='line'><span class="sd"> */</span>
</span><span class='line'><span class="k">class</span> <span class="nc">PartialTokenParser</span> <span class="k">extends</span> <span class="nx">Twig_TokenParser_Block</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * Parses the twig token in order to replace the &#39;partial&#39; block.</span>
</span><span class='line'><span class="sd">     * </span>
</span><span class='line'><span class="sd">     * @param Twig_Token $token</span>
</span><span class='line'><span class="sd">     * @return Twig_Node_BlockReference</span>
</span><span class='line'><span class="sd">     * @throws Twig_Error_Syntax</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">parse</span><span class="p">(</span><span class="nx">Twig_Token</span> <span class="nv">$token</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$lineno</span> <span class="o">=</span> <span class="nv">$token</span><span class="o">-&gt;</span><span class="na">getLine</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$stream</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">getStream</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$name</span> <span class="o">=</span> <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">expect</span><span class="p">(</span><span class="nx">Twig_Token</span><span class="o">::</span><span class="na">NAME_TYPE</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">getValue</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">setBlock</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$block</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Node_Block</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="k">new</span> <span class="nx">Twig_Node</span><span class="p">(</span><span class="k">array</span><span class="p">()),</span> <span class="nv">$lineno</span><span class="p">));</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">pushLocalScope</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">pushBlockStack</span><span class="p">(</span><span class="nv">$name</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="p">(</span><span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">test</span><span class="p">(</span><span class="nx">Twig_Token</span><span class="o">::</span><span class="na">BLOCK_END_TYPE</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>            <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">next</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>            <span class="nv">$body</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">subparse</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="s1">&#39;decideBlockEnd&#39;</span><span class="p">),</span> <span class="k">true</span><span class="p">);</span>
</span><span class='line'>            <span class="k">if</span> <span class="p">(</span><span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">test</span><span class="p">(</span><span class="nx">Twig_Token</span><span class="o">::</span><span class="na">NAME_TYPE</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>                <span class="nv">$value</span> <span class="o">=</span> <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">next</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getValue</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>                <span class="k">if</span> <span class="p">(</span><span class="nv">$value</span> <span class="o">!=</span> <span class="nv">$name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>                    <span class="k">throw</span> <span class="k">new</span> <span class="nx">Twig_Error_Syntax</span><span class="p">(</span><span class="nb">sprintf</span><span class="p">(</span><span class="s2">&quot;Expected endblock for block &#39;</span><span class="si">$name</span><span class="s2">&#39; (but %s given)&quot;</span><span class="p">,</span> <span class="nv">$value</span><span class="p">),</span> <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">getCurrent</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">getLine</span><span class="p">(),</span> <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">getFilename</span><span class="p">());</span>
</span><span class='line'>                <span class="p">}</span>
</span><span class='line'>            <span class="p">}</span>
</span><span class='line'>        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>            <span class="nv">$body</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Node</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
</span><span class='line'>                <span class="k">new</span> <span class="nx">Twig_Node_Print</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">getExpressionParser</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">parseExpression</span><span class="p">(),</span> <span class="nv">$lineno</span><span class="p">),</span>
</span><span class='line'>            <span class="p">));</span>
</span><span class='line'>        <span class="p">}</span>
</span><span class='line'>        <span class="nv">$stream</span><span class="o">-&gt;</span><span class="na">expect</span><span class="p">(</span><span class="nx">Twig_Token</span><span class="o">::</span><span class="na">BLOCK_END_TYPE</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="nv">$block</span><span class="o">-&gt;</span><span class="na">setNode</span><span class="p">(</span><span class="s1">&#39;body&#39;</span><span class="p">,</span> <span class="nv">$body</span><span class="p">);</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">popBlockStack</span><span class="p">();</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">parser</span><span class="o">-&gt;</span><span class="na">popLocalScope</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="k">new</span> <span class="nx">Twig_Node_BlockReference</span><span class="p">(</span><span class="nv">$name</span><span class="p">,</span> <span class="nv">$lineno</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getTag</span><span class="p">());</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * Returns the tag this parses will look for.</span>
</span><span class='line'><span class="sd">     * </span>
</span><span class='line'><span class="sd">     * @return string</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getTag</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="s1">&#39;partial&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * Decides when to stop parsing for an open &#39;partial&#39; tag.</span>
</span><span class='line'><span class="sd">     * </span>
</span><span class='line'><span class="sd">     * @param Twig_Token $token</span>
</span><span class='line'><span class="sd">     * @return bool</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">decideBlockEnd</span><span class="p">(</span><span class="nx">Twig_Token</span> <span class="nv">$token</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$token</span><span class="o">-&gt;</span><span class="na">test</span><span class="p">(</span><span class="s1">&#39;endpartial&#39;</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then you just need to tell the Twig environment to
add this token parser:</p>

<figure class='code'><figcaption><span>While bootstrapping Twig:</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$twig</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Twig_Environment</span><span class="p">(</span><span class="k">new</span> <span class="nx">Twig_Loader_String</span><span class="p">(),</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>  <span class="s1">&#39;autoescape&#39;</span> <span class="o">=&gt;</span> <span class="k">false</span><span class="p">,</span>
</span><span class='line'><span class="p">));</span>
</span><span class='line'><span class="nv">$twig</span><span class="o">-&gt;</span><span class="na">addTokenParser</span><span class="p">(</span><span class="k">new</span> <span class="nx">PartialTokenParser</span><span class="p">());</span>
</span></code></pre></td></tr></table></div></figure>


<p>and at this point you can use it in your templates:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="x">&lt;html&gt;</span>
</span><span class='line'><span class="x">  &lt;head&gt;</span>
</span><span class='line'><span class="x">      &lt;title&gt;</span>
</span><span class='line'><span class="x">          { % partial title % }</span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$title</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x">{ % endpartial % }</span>
</span><span class='line'><span class="x">      &lt;/title&gt;</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">      ...</span>
</span><span class='line'><span class="x">  &lt;/head&gt;</span>
</span><span class='line'><span class="x">  &lt;body&gt;</span>
</span><span class='line'><span class="x">      { % partial content % }</span>
</span><span class='line'><span class="x">          </span><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nv">$content</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span>
</span><span class='line'><span class="x">      { % endpartial% }</span>
</span><span class='line'><span class="x">  &lt;/body&gt;</span>
</span><span class='line'><span class="x">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>path/to/twig/templates/templateName.twig</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="o">{</span> % partial title % <span class="o">}</span>
</span><span class='line'>  About: this is our about page
</span><span class='line'><span class="o">{</span> % endpartial % <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>So?</h2>

<p>If you spot any typo /
mistake please do let me know: I wrote the
example code adapting the one I had from a previous
project so it might be that something slipped my
mind.</p>

<p>Since I never dug <strong>that deep</strong> into
Twig it might be that some things
could be done in a cleaner way, so if
you have suggestions or feedbacks I would
strongly encourage you to go
<em>berserk mode</em> in the comments section below.</p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>Being PHP or something like Smarty or xTemplate <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>Unfortunately, to do so we will have to turn off Twig&#8217;s default escaping strategy <a href='#fnref:2' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Configuring a Symfony2 application to support SOA]]></title>
    <link href="http://odino.org/configuring-a-symfony2-application-to-support-soa/"/>
    <updated>2013-06-06T13:00:00+04:00</updated>
    <id>http://odino.org/configuring-a-symfony2-application-to-support-soa</id>
    <content type="html"><![CDATA[<p>When you think in terms of
<a href="http://odino.org/refactoring-your-architecture-go-for-soa/">Service Oriented Architecture</a>
one of the tricky things is to decide
how to organize your development workflow
in order to develop <strong>architecture</strong>,
not a single application: for example, how would
you configure deployments (when you need
to deploy <strong>part</strong> of your architecture, not
just a web application) or push cross-service
features to your SCM?</p>

<!-- more -->


<p>This article gives an overview of the constraints
and preferences that we wanted to implement
in our SOA, which is mainly done with Symfony2,
but most of it can be read in a
framework/language-agnostic key.</p>

<h2>Problems</h2>

<p>We can at least identify 3 problems which pop
up after you decide to layer your architecture
and avoid <a href="http://www.slideshare.net/odino/the-rocket-internet-experience-phptostart-2013-in-turin/103">a monolithic approach</a>:</p>

<ul>
<li><p>given that every of your service will require
some time (1~5 minutes) to be deployed, how do you
ensure that you can release a new version of a service
without the need of updating <strong>all</strong> the other
services?
If you have, for example, 9 machines and 3 services
(A, B and C, 3 machines for each service),
you cant really afford to <strong>deploy everywhere</strong>
when you need to update just the service A, because
you might need to shutdown service B and C during the
deployment, while they dont really need to be updated.
The solution here would be to update just a bunch of
your servers</p></li>
<li><p>how do you create <em>Pull Requests</em> and organize your
repositories? This is not a trivial question: if you
need a feature that involves changes in services A and B,
and you have 2 repositories you will need to add some
overhead on top of every single operation that you
would usually do with a new feature</p></li>
<li><p>is your software able to automatically support SOA?
By this I mean, when you want to add a new service,
how easy is to configure your architecture to be able
to support the new layer? Of course, you would need
something that lets you do this in a matter of a minute</p></li>
</ul>


<h2>Deployments</h2>

<p>As I stated earlier, the solution is to be able
to specify, upon deployments, which services need
to be updated, and your best friend, here, could
be something like <a href="https://github.com/capistrano/capistrano">Capistrano</a>.</p>

<p>If you ever worked with capistrano, you know
that deployments basically depend on the <code>deploy.rb</code> file,
in which you can configure different <strong>stages</strong> of your
architecture:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">set</span> <span class="ss">:stages</span><span class="p">,</span> <span class="sx">%w(live staging)</span>
</span><span class='line'><span class="n">set</span> <span class="ss">:default_stage</span><span class="p">,</span> <span class="s2">&quot;staging&quot;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;capistrano/ext/multistage&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the example, weare declaring that our application
can be deployed on 2 stages, <code>live</code> and <code>staging</code>; by doing a
<code>cap live deploy</code> or <code>cap deploy</code> you are ready to either deploy to your live
servers or staging ones, after configuring the staging
files (<code>live.rb</code> and <code>staging.rb</code>):</p>

<figure class='code'><figcaption><span>An example live.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">role</span> <span class="ss">:web</span><span class="p">,</span>        <span class="s2">&quot;company.com&quot;</span>
</span><span class='line'><span class="n">role</span> <span class="ss">:app</span><span class="p">,</span>        <span class="s2">&quot;company.com&quot;</span><span class="p">,</span> <span class="ss">:primary</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="n">role</span> <span class="ss">:db</span><span class="p">,</span>         <span class="s2">&quot;company.com&quot;</span><span class="p">,</span> <span class="ss">:primary</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'>
</span><span class='line'><span class="n">set</span> <span class="ss">:app_environment</span><span class="p">,</span> <span class="s2">&quot;live&quot;</span>
</span><span class='line'><span class="n">set</span> <span class="ss">:deploy_to</span><span class="p">,</span>   <span class="s2">&quot;/var/www/htdocs/</span><span class="si">#{</span><span class="n">application</span><span class="si">}</span><span class="s2">.</span><span class="si">#{</span><span class="n">app_environment</span><span class="si">}</span><span class="s2">&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This still doesn&rsquo;t solve the problem of
<strong>just deploying a single service</strong>, but to
overcome it, thanks to the capistrano <code>multistage</code>
extension, it&rsquo;s a matter of configuring a few deployment
files.</p>

<p>For example, here&rsquo;s how you would write your deployment
files once you have a couple services (<code>A</code> and <code>B</code>):</p>

<figure class='code'><figcaption><span>deploy.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">set</span> <span class="ss">:stages</span><span class="p">,</span> <span class="sx">%w(a-live a-staging b-live b-staging, live, staging)</span>
</span><span class='line'><span class="n">set</span> <span class="ss">:default_stage</span><span class="p">,</span> <span class="s2">&quot;staging&quot;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;capistrano/ext/multistage&#39;</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>a-live.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">role</span> <span class="ss">:web</span><span class="p">,</span>        <span class="s2">&quot;a.company.com&quot;</span>
</span><span class='line'><span class="n">role</span> <span class="ss">:app</span><span class="p">,</span>        <span class="s2">&quot;a.company.com&quot;</span><span class="p">,</span> <span class="ss">:primary</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'><span class="n">role</span> <span class="ss">:db</span><span class="p">,</span>         <span class="s2">&quot;a.company.com&quot;</span><span class="p">,</span> <span class="ss">:primary</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'>
</span><span class='line'><span class="n">set</span> <span class="ss">:app_environment</span><span class="p">,</span> <span class="s2">&quot;live&quot;</span>
</span><span class='line'><span class="n">set</span> <span class="ss">:deploy_to</span><span class="p">,</span>   <span class="s2">&quot;/var/www/htdocs/</span><span class="si">#{</span><span class="n">application</span><span class="si">}</span><span class="s2">.</span><span class="si">#{</span><span class="n">app_environment</span><span class="si">}</span><span class="s2">&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you see, we are creating a few different stages:</p>

<ul>
<li><code>serviceName-environment</code> (ie. a-live), which includes servers for a
specific environment of a service</li>
<li><code>environment</code> (ie. live), which includes the entire architecture,
useful in those cases when you really want to deploy
the entire architecture<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup></li>
</ul>


<p>For Symfony2, to ease your job, you can use
<a href="http://capifony.org/">capifony</a> which takes
care of configure the remaining,
specific, crucial parts of the deployment for the
framework (such as clearing and warming up the cache,
installing the dependencies via composer and so on).</p>

<h2>Software lifecycle</h2>

<p><img class="left" src="http://odino.org/images/cylinders.png"></p>

<p>Ah, good old SCM problems!</p>

<p>Let&rsquo;s say your are working on a traditional, monolithic application:
you branch to implement a new feature, commit, push, open
a pull request, the PR gets merged, deployed on staging, tested and
then goes to live; very simple as well as efficient:
<strong>zero overhead</strong>.</p>

<p>You might think that in SOAs this is not different: you <code>cd</code> into a
specific&rsquo;s service repository, branch to
implement a new feature, commit, push, open
a pull request, the PR gets merged in that repository, the service gets deployed
on its staging servers, tested and then goes to live;
very simple as well as efficient?</p>

<p>No, at all.</p>

<p>Thing is, often you will need to develop <strong>cross-service
features</strong>, which require code to be updated in N different
repositories: as a result, you will need to open N pull requests;
<span class='pullquote-right' data-pullquote='to be on the safe side, being able to rollback the
entire architecture is a must'>
if you think that this is not a problem, consider what happens
once you pack and test everything and are ready to go live:
a critical bug is found, and it couldn&rsquo;t have been discovered
earlier since it just happens due to some specific configuration
that you have on the live environments: time to <strong>rollback N new services</strong>.</p>

<p>Since rollbacking is a very critical operation<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup>, you
want to rollback each of the affected services separately, which makes your
downtimes N times slower than a usual, monolithic rollback: in these
cases, to be on the safe side, being able to rollback the
entire architecture is a must.
</span></p>

<p>That&rsquo;s why I would advice to keep all of your services under
one repositories, to avoid overheads:</p>

<ul>
<li>N projects in your IDE</li>
<li>N SCM operations (<code>git checkout -b myBranch</code> as well as <code>git push origin myBranch</code>)</li>
<li>N pull requests</li>
<li>N code reviews</li>
</ul>


<p>Due to this, honestly, I don&rsquo;t really see the need of separating services
into different repositories: when you deploy, you deploy a tag of the architecture
itself (only on the servers which host the services to be updated with that tag),
when you rollback, you rollback the entire architecture to a specific
version.</p>

<p><img class="left" src="http://odino.org/images/baby-birds.jpg"></p>

<p>This seems to go against what I preached earlier, while talking
about deployments, but the truth is that you want to be on the safe side
once something goes wrong: you can optimize deployments so that you can
just deploy some services, but in case of rollback, you need to take an
immediate, &ldquo;total&rdquo; action to <strong>restore all of your services</strong>.</p>

<p>Consider the situation from a very similar perspective coming
from a very, very different context: cultivate an healthy colony of newborns
in nature &ndash; as opposed to maintaining an healthy architecture on the internets.</p>

<p>Exactly like the mom of newborn birds, when it comes to feed (update)
them, you would give the weaker ones, in order to help them develop as healthy
as their stronger brothers, the biggest meals; but when it comes
to rescue (rollback) them from a predator, you would crave for having an
option to move them all in one go, without the risk of moving them one by
one, leaving the unluckiest ones defenseless against their own fate.</p>

<h2>Configuring new services in Symfony2</h2>

<p><img class="right" src="http://odino.org/images/cubic-architecture.jpg"></p>

<p>This post has to come to an end dealing with Symfony2,
since, in our experience,
<a href="http://odino.org/why-we-choose-symfony2-over-any-other-php-framework/">we have decided to go SOA with this framework</a>:
all in all, we found that due to the integration with capistrano
and the concept of bundles, together with the ability to have
per-bundle specific hostnames, this framework is pretty friendly
towards the ideas and constraints that we want to implement in
our SOA: what we&rsquo;ve seen is no rocket science, and even
the approach that we are using with Symfony2 is nothing
extraordinary, but it helps maintaining a very clean and
efficient workflow while developing a SOA.</p>

<p>As I said, for deploying you might want to use capifony,
and when it comes to isolate services in just one repository
we realized that a good solution would be to have <strong>one
Symfony2 application</strong> and create <strong>a bundle for each service</strong>
that we need.</p>

<p>Thanks for the capabilities of Symfony2&rsquo;s routing
mechanism, you can also bind a subdomain to a specific
bundle; once you create the bundle, you can tell symfony
that the routes of that specific bundle can be matched only
if the subdomain of the application matches a particular
string:</p>

<figure class='code'><figcaption><span>app/config/routing.yml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>mycompany_service_a:
</span><span class='line'>    resource: <span class="s2">&quot;@AcmeServiceABundle/Resources/config/routing.yml&quot;</span>
</span><span class='line'>    prefix:   /
</span><span class='line'>    host:     service-a.mycompany.com
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>src/Acme/ServiceABundle/Resources/config/routing.yml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>mycompany_service_a_index:
</span><span class='line'>    pattern:  /index/<span class="o">{</span>whateverParameter<span class="o">}</span>
</span><span class='line'>    defaults: <span class="o">{</span> _controller: AcmeServiceABundle:Default:index <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this case, the route <code>mycompany_service_a_index</code> will only
be matched when the URL is using the hostname
<code>service-a.mycompany.com</code>: for example, <code>http://mycompany.com/index/param</code>
won&rsquo;t match it; this is pretty interesting since it gives you the
flexibility to develop features in just once repository, on
<strong>as many services as you want</strong>.</p>

<p>We are still heavily experimenting, but out of a few approaches &ndash;
for example <a href="http://symfony.com/doc/2.0/book/routing.html#prefixing-imported-routes">route prefixing</a> &ndash;
we decided to go on with the ones I explained here for cleanness,
clarity, efficiency and security of your development cycles
and architecture: if your experience suggests something different
or you want to share doubts, feel free to abuse of the comments
section, since I am very open and interested to discuss this topic.</p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>Cases such as disaster recovery or deployments to a brand new servers&#8217; set (for example, if you want to switch from AWS to another provider) <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>Rollbacking, per se, shouldn&#8217;t be a pain in the ass, but you need to focus on it in order to reduce mistakes in an already-critical situation <a href='#fnref:2' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Securing your HTTP API with JavaScript Object Signing and Encryption]]></title>
    <link href="http://odino.org/securing-your-http-api-with-javascript-object-signing-and-encryption/"/>
    <updated>2013-06-04T00:31:00+04:00</updated>
    <id>http://odino.org/securing-your-http-api-with-javascript-object-signing-and-encryption</id>
    <content type="html"><![CDATA[<p>One thing that is always difficult, enough to
deserve <a href="http://www.amazon.com/Ajax-Security-Billy-Hoffman/dp/0321491939">its own book</a>,
is to <strong>secure HTTP API</strong> that interact with client-side
applications: today, after a discussion about how to face
the problem in our company, we bumped into the
<a href="http://datatracker.ietf.org/doc/draft-ietf-jose-json-web-signature/?include_text=1">JOSE</a>
&ndash; JavaScript Object Signing and Encryption &ndash;
specification.</p>

<!-- more -->


<p>Basically, the specification defines 4 entities:</p>

<ul>
<li>JWS, <a href="http://tools.ietf.org/html/draft-jones-json-web-signature-04">JSON Web Signature</a>,
a signed representation of data</li>
<li>JWT, <a href="http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html">JSON Web Token</a>,
a representation of data
(it differs from JWS as JWT is not signed)</li>
<li>JWE, <a href="http://self-issued.info/docs/draft-ietf-jose-json-web-encryption.html">JSON Web Encryption</a>,
an encrypted JSON representation of data</li>
<li>JWA, <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-00">JSON Web Algorithms</a>,
a list of safe algorithms to be used with JWS and JWE</li>
</ul>


<p>For the sake of basic knowledge, we will only have a look
at JWS and JWT / JWE now: the specifications about these
entities are quite extensive and not very straightforward,
so for further details you should <strong>really</strong> give them
a look.</p>

<p><img class="right" src="http://odino.org/images/jws.png"></p>

<h2>JWT</h2>

<p>Basically, the token (JWT) is the simplest structure
that you will deal with while implementing JOSE in our
architecture; it is a string representation of some data
base64 encoded (other types of encoding might be applied, but
this is not madatory): the JWT differs from raw base64-encoded
data since it also includes informations about the encoding
itself, in the token&rsquo;s header; by concatenating the base64-encoded
version of the token header and payload (the actual data) you
obtain what the specification calls <strong>signature input</strong>, which will
then be used to create the signature (JWS).</p>

<h2>JWS and JWE</h2>

<p>After the JWT comes the JWS, which is a signed representation
of the JWT; it differs from the token just because of the
signature; on an higher step of the ladder comes the JWE instead,
which lets you encrypt the data in order to achieve an higher security
level: the <a href="http://self-issued.info/docs/draft-ietf-jose-json-web-encryption.html#JWEExamples">examples in the ietf draft</a>
show you how to create JWEs with a pair of private /
public keys.</p>

<h2>Use case: how to authenticate stateless AJAX calls?</h2>

<p><img class="left" src="http://odino.org/images/jsw-auth.png"></p>

<p>One of the needs that you might have is to,
from JavaScript, make authenticated HTTP calls to
one of your webservices: since you don&rsquo;t want to
expose the WS credentials on the JS service (the
credentials would be readable by any client) a good
solution might be to generate a JWS with a private
OpenSSL key in your webservice, store it into a cookie
accessible to the JS service, which would execute
those calls including that cookie<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>, which you can then
verify while authenticating the call.</p>

<p>This workflow is pretty easy to understand, but the actual
implementation is more than tricky, since the
specification is quite abundant &ndash; especially about
encryption algorithms.</p>

<p>In PHP we can use at least 3 libraries: one of them,
<a href="https://github.com/ritou/php-Akita_JOSE">Akita_JOSE</a>,
is pretty old (since the last commit was more than
7 months ago) but is very understandable and quite
easy to use; another one, <a href="https://packagist.org/packages/gree/jose">gree/jose</a>,
has itw own package on packagist and can be easily
installed via composer: from a fast look at the
<a href="https://github.com/gree/jose">source code on GitHub</a> it looks good,
even though it needs the <a href="http://phpseclib.sourceforge.net/">phpsec</a>
library to be able to work<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup>.</p>

<p>The third option, which is the one that <a href="https://github.com/namshi/jose">I built in the last couple of hours</a>,
is <a href="https://packagist.org/packages/namshi/jose">namshi/jose</a>,
which is very, very easy to use<sup id='fnref:3'><a href='#fn:3' rel='footnote'>3</a></sup>: it currently only
supports the <a href="https://github.com/namshi/jose/blob/master/src/Namshi/JOSE/Signer/RS256.php">RSA algorithm</a>
with <code>sha256</code> hashing, but I guess that implementing other
algorithms is less than trivial.</p>

<p>For example, let&rsquo;s see how you would generate the JWS
to be stored in a cookie:</p>

<figure class='code'><figcaption><span>Generating a JWS after authentication and storing it into a cookie</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Namshi\JOSE\JWS</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="nv">$username</span> <span class="o">==</span> <span class="s1">&#39;correctUsername&#39;</span> <span class="o">&amp;&amp;</span> <span class="nv">$pass</span> <span class="o">=</span> <span class="s1">&#39;ok&#39;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nv">$user</span> <span class="o">=</span> <span class="nx">Db</span><span class="o">::</span><span class="na">loadUserByUsername</span><span class="p">(</span><span class="nv">$username</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="nv">$jws</span>  <span class="o">=</span> <span class="k">new</span> <span class="nx">JWS</span><span class="p">(</span><span class="s1">&#39;RS256&#39;</span><span class="p">);</span>
</span><span class='line'>    <span class="nv">$jws</span><span class="o">-&gt;</span><span class="na">setPayload</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
</span><span class='line'>        <span class="s1">&#39;uid&#39;</span> <span class="o">=&gt;</span> <span class="nv">$user</span><span class="o">-&gt;</span><span class="na">getid</span><span class="p">(),</span>
</span><span class='line'>    <span class="p">));</span>
</span><span class='line'>
</span><span class='line'>    <span class="nv">$privateKey</span> <span class="o">=</span> <span class="nb">openssl_pkey_get_private</span><span class="p">(</span><span class="s2">&quot;file://path/to/private.key&quot;</span><span class="p">);</span>
</span><span class='line'>    <span class="nv">$jws</span><span class="o">-&gt;</span><span class="na">sign</span><span class="p">(</span><span class="nv">$privateKey</span><span class="p">);</span>
</span><span class='line'>    <span class="nx">setcookie</span><span class="p">(</span><span class="s1">&#39;identity&#39;</span><span class="p">,</span> <span class="nv">$jws</span><span class="o">-&gt;</span><span class="na">getTokenString</span><span class="p">());</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>and then the apps that want to execute authenticated
calls on behalf of the user by using this cookie just need
to include it in these calls; the server will just need
to verify that the JWS in the cookie is valid:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Namshi\JOSE\JWS</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$jws</span>        <span class="o">=</span> <span class="nx">JWS</span><span class="o">::</span><span class="na">load</span><span class="p">(</span><span class="nv">$_COOKIE</span><span class="p">[</span><span class="s1">&#39;identity&#39;</span><span class="p">]);</span>
</span><span class='line'><span class="nv">$public_key</span> <span class="o">=</span> <span class="nb">openssl_pkey_get_public</span><span class="p">(</span><span class="s2">&quot;/path/to/public.key&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="p">(</span><span class="nv">$jws</span><span class="o">-&gt;</span><span class="na">verify</span><span class="p">(</span><span class="nv">$public_key</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>    <span class="nv">$paylod</span> <span class="o">=</span> <span class="nv">$jws</span><span class="o">-&gt;</span><span class="na">getPayload</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">echo</span> <span class="nb">sprintf</span><span class="p">(</span><span class="s2">&quot;Hey, my JS app just did an action authenticated as user #%s&quot;</span><span class="p">,</span> <span class="nv">$payload</span><span class="p">[</span><span class="s1">&#39;id&#39;</span><span class="p">]);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s it: far from being a stable library, this is more a
proof of concept that we, an Namshi, would like to see developing
in the next weeks / months.</p>

<p>As always, comments, rants or &ndash; even better &ndash; pull requests are
<strong>more than welcome</strong>!</p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>One of the disadvantages of this approach is that it relies on cookies, only available in the HTTP protocol. If you want to use another protocol for you application - a very rare and extreme use case - this wouldn&#8217;t work for you. <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>I honestly never heard of this library before, so I can&#8217;t really say what it does and why it&#8217;s needed <a href='#fnref:2' rev='footnote'>↩</a></li><li id='fn:3'>Since I&#8217;m not an expert in encryption and security, I would suggest to give it a look and come up with feedbacks <a href='#fnref:3' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why our business choose Symfony2 over any other PHP framework]]></title>
    <link href="http://odino.org/why-we-choose-symfony2-over-any-other-php-framework/"/>
    <updated>2013-05-27T22:16:00+04:00</updated>
    <id>http://odino.org/why-we-choose-symfony2-over-any-other-php-framework</id>
    <content type="html"><![CDATA[<p>Everyone knows that I am a big fan of the
Symfony2 ecosystem, and going SOA with
this framework was a very trivial decision
for us at <a href="http://en-ae.namshi.com">Namshi</a>;
all in all, besides personal preferences,
there is a plethora of reasons to choose
this framework among the others available
in PHP, so I am going to list the most
important factors that influenced our
decision.</p>

<p>Believe it or not, all of the following
factors matter <strong>first for the business</strong>,
then for the developers.</p>

<!-- more -->


<h2>Testing</h2>

<p><img class="right" src="http://odino.org/images/behat.png"></p>

<p>We are firm believers in automated test practices,
and providing a layer that integrates very easily
with testing tools (such as Behat or PHPUnit) is a
must for us.</p>

<p>Symfony2 is a testing-prone framework because:</p>

<ul>
<li>it is well decoupled, so <em>unit testing</em> becomes very easy
since you can mock objects, isolate classes and inject stub
dependencies very easily</li>
<li>it provides a first layer for functional testing (with PHPUnit):
being an HTTP-centric framework, it provides a <a href="http://symfony.com/doc/2.0/book/testing.html#your-first-functional-test">base class</a>
that lets you simulate HTTP requests and examine the output; needless
to say, these kind of tests are way faster than the ones that you
would write with tools like Selenium, since they don&rsquo;t have the
overhead of testing with an actual browser</li>
<li>there is a <a href="http://extensions.behat.org/symfony2/">Behat extension</a>
that lets you integrate the framework with this behavioral testing tool</li>
</ul>


<p>At the end, you can see how Symfony2 and the ecosystem around it
provide the proper toolset to run <strong>unit</strong>, <strong>functional</strong> and
<strong>behavioral</strong> tests.
If you do care about testing, this is already a
huge point: we can&rsquo;t afford our developers to <strong>waste</strong>
a huge portion of their time doing manual testing, and
we don&rsquo;t want to increase overhead to build a
manual QA team; since we are a technology startup,
we should take advantage of technology to automate
expensive tasks that harm the business, like
manual testing<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>.</p>

<h2>Debugging</h2>

<p>I kind of had so much fun when, while still working
with symfony 1.4, I saw people developing with
frameworks like Yii or Zend Framework 1,
beating their heads on their desks tying to
understand which view to modify, var_dumping
SQL queries to output them and so on:
symfony 1.4 already had a very powerful
debug toolbar that would present all of these
informations in order to ease debugging.</p>

<p>Symfony2 goes beyond what we had before,
providing a way more powerful, extensible
toolbar and an integrated profiler.</p>

<p><img class="right" src="http://odino.org/images/symfony2-profiler.png"></p>

<p>Database inspection will let you realize
how many queries you are running and see the
SQL of all of them, with a nice overview of the
time they take, while the profiler itself
includes informations about every step
of the application: for example, with Joomla,
can you tell this easily how long it took to
render a particular view and how much memory
was used to execute a controller&rsquo;s action?</p>

<p>Now, imagine each of your developers
(let&rsquo;s say you have a team of 6),
spending 1 hour (very conservative estimate)
out of 40 (a working week) trying to obtain
informations that debugging tools natively give you:
it&rsquo;s almost a day per week; multiply that day for 52
weeks in a year and you will end up loosing
one of your developers for two moths.</p>

<p>We <strong>all</strong> honestly can&rsquo;t afford to let a guy
leave the company for 2 months for free, so why
would we keep using counterproductive tools?</p>

<h2>Doctrine 2</h2>

<p>It is no news that we, at Namshi,
<a href="http://odino.org/refactoring-your-architecture-go-for-soa/">are working with a Service-Oriented Architecture</a>,
and we are highly benefiting from the easy
integration that Symfony2 provides for
Doctrine.</p>

<p>One of the rules of thumb of designing
SOAs is that you can provide access to the
same data source to different services:
in simple terms, instead of talking via
webservices or messaging queues, services
can simply access the data stored <em>somewhere</em>
by other ones.</p>

<p><img class="left" src="http://odino.org/images/doctrine-cli.png"></p>

<p>Well, Doctrine 2 is the cherry on top of the
cake to access that <em>somewhere</em>: natively
providing support for multiple DB connections
and object-relational mappings, you can safely
use this tool, within Symfony2, to handle read
and writes to different databases without
polluting the domain model of each of the services
that take advantage of Doctrine; in addition to this,
I should enumerate the huge list of good things that
working with a data mapper like Doctrine 2 brings
on the table.</p>

<p>On another note, sharing the data model among different services
helps you overcoming though situations
where webservices or messaging queues are not
enough: think about a service which, due to
an update, needs to modify half a milion records
that &ldquo;belong&rdquo; to another service; of course,
istantly having 500k messages in a queue implies
a long, very long time to process them, while
a webservice might not be fully ok with sending
a huge payload over the HTTP protocol &ndash; and,
moreover, how do you start testing this feature,
when your developers need to send a lot of MB
through their browser? It is painful, believe me.</p>

<p>At this point, the ability of directly accessing
different DBs come out as a swiss-army knife, as
you can directly execute the 500k updates, in
batch, from the original service.</p>

<h2>Deployments</h2>

<p>Symfony2 has an <em>out of the box</em>
<a href="http://capifony.org/">integration with Capistrano</a>,
the most popular automated
deployment tool in the market.</p>

<p>This means that you should forget about
wasting time, money and energy to develop
your own in-house solution to automate
deployments or, even worse, rely on
manual procedure, which are prone to
errors where it hurts the most, on the
&ldquo;live server&rdquo;.</p>

<h2>DIC</h2>

<p>Let&rsquo;s say that you, for example, are
using <a href="http://graylog2.org/">Graylog2</a>
to handle logs in your application:
while you are developing locally, you
won&rsquo;t have a graylog2 server to connect to,
since it might be that you want to keep
your machine a bit cleaner and you might
find more useful to read local logs
from a file in the filesystem, or directly
output them to the browser.</p>

<p>In Symfony2, thanks to the <a href="http://symfony.com/doc/master/book/service_container.html">dependency-injection container</a>,
you can define the logger as a service:</p>

<figure class='code'><figcaption><span>config.yml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>logger:
</span><span class='line'>    class: <span class="s1">&#39;Monolog\Logger&#39;</span>
</span><span class='line'>    arguments:
</span><span class='line'>        name:           <span class="s2">&quot;applicationName-%kernel.environment%&quot;</span>
</span><span class='line'>    calls:
</span><span class='line'>        - <span class="o">[</span> pushHandler, <span class="o">[</span> @monolog_handler.graylog <span class="o">]</span> <span class="o">]</span>
</span><span class='line'>monolog_handler.graylog:
</span><span class='line'>    class: <span class="s1">&#39;Monolog\Handler\GelfHandler&#39;</span>
</span><span class='line'>    arguments:
</span><span class='line'>        publisher: @gelf.message_publisher
</span><span class='line'>        level:     200
</span></code></pre></td></tr></table></div></figure>


<p>and, for development environments, you
can simply override the configuration in
the <code>config_dev.yml</code> file:</p>

<figure class='code'><figcaption><span>config_dev.yml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>monolog_handler.graylog:
</span><span class='line'>    class: <span class="s1">&#39;Monolog\Handler\StreamHandler&#39;</span>
</span><span class='line'>    arguments:
</span><span class='line'>      stream: <span class="s2">&quot;php://stdout&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will allow you to output errors that
would normally go to graylog2 directly to
the developer&rsquo;s browser, easing debugging
when you can afford to display errors in
the browser &ndash; thing that is not possible
who&rsquo;s viewing your application is a
potential customer.</p>

<p>Apart from all the technicalities involved
in using a DIC, I would like to focus on
one point: again, simplicity and speed to implement a
solution to a problem (having different log
handlers depending on the application&rsquo;s environment,
in this case) are a winning factor for your
development team, which is translated in <strong>more
productivity for your company</strong>.</p>

<h2>Bundles</h2>

<p>When we kickstarted our first Symfony2-based
service in our architecture, we decided to meld
together 2 applications that support our CRM
and ERP systems: being inside Symfony2, these
layers are <strong>fully isolated</strong> in separate bundles,
giving us the ability of phisically decoupling them
in 2 installations in a matter of minutes.</p>

<p>Bundles are probably one of the most powerful
concepts of Symfony2, since they are
micro-applications inside your main application:
being able to totally separate logics from different
domains helps you in keeping a clean separation
of concerns and autonomously develop every single
feature of your domain.</p>

<h2>Declarative code</h2>

<p>Consider the following snippet, written using the
<a href="http://symfony.com/doc/master/components/finder.html">Symfony2 Finder component</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Symfony\Component\Finder\Finder</span><span class="p">;</span>
</span><span class='line'><span class="k">use</span> <span class="nx">Zend_Service_Amazon_S3</span> <span class="k">as</span> <span class="nx">Amazon_S3</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$s3</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Amazon_S3</span><span class="p">(</span><span class="nv">$key</span><span class="p">,</span> <span class="nv">$secret</span><span class="p">);</span>
</span><span class='line'><span class="nv">$s3</span><span class="o">-&gt;</span><span class="na">registerStreamWrapper</span><span class="p">(</span><span class="s2">&quot;s3&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$finder</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Finder</span><span class="p">();</span>
</span><span class='line'><span class="nv">$finder</span><span class="o">-&gt;</span><span class="na">name</span><span class="p">(</span><span class="s1">&#39;photos*&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">size</span><span class="p">(</span><span class="s1">&#39;&lt; 100K&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">date</span><span class="p">(</span><span class="s1">&#39;since 1 hour ago&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="k">foreach</span> <span class="p">(</span><span class="nv">$finder</span><span class="o">-&gt;</span><span class="na">in</span><span class="p">(</span><span class="s1">&#39;s3://bucket-name&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="nv">$file</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">print</span> <span class="nv">$file</span><span class="o">-&gt;</span><span class="na">getFilename</span><span class="p">();</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>After your <strong>first</strong> look at this code, you already know
what it is doing: now imagine that your team of developers
need to, instead, try to understand how the Drupal
framework works.</p>

<p>Taken from <a href="https://github.com/drupal/drupal/blob/7.x/modules/comment/comment.admin.inc#L271">Drupal&rsquo;s source code</a>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="sd">/**</span>
</span><span class='line'><span class="sd"> * Process comment_confirm_delete form submissions.</span>
</span><span class='line'><span class="sd"> */</span>
</span><span class='line'><span class="k">function</span> <span class="nf">comment_confirm_delete_submit</span><span class="p">(</span><span class="nv">$form</span><span class="p">,</span> <span class="o">&amp;</span><span class="nv">$form_state</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="nv">$comment</span> <span class="o">=</span> <span class="nv">$form</span><span class="p">[</span><span class="s1">&#39;#comment&#39;</span><span class="p">];</span>
</span><span class='line'>  <span class="c1">// Delete the comment and its replies.</span>
</span><span class='line'>  <span class="nx">comment_delete</span><span class="p">(</span><span class="nv">$comment</span><span class="o">-&gt;</span><span class="na">cid</span><span class="p">);</span>
</span><span class='line'>  <span class="nx">drupal_set_message</span><span class="p">(</span><span class="nx">t</span><span class="p">(</span><span class="s1">&#39;The comment and all its replies have been deleted.&#39;</span><span class="p">));</span>
</span><span class='line'>  <span class="nx">watchdog</span><span class="p">(</span><span class="s1">&#39;content&#39;</span><span class="p">,</span> <span class="s1">&#39;Deleted comment @cid and its replies.&#39;</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">&#39;@cid&#39;</span> <span class="o">=&gt;</span> <span class="nv">$comment</span><span class="o">-&gt;</span><span class="na">cid</span><span class="p">));</span>
</span><span class='line'>  <span class="c1">// Clear the cache so an anonymous user sees that his comment was deleted.</span>
</span><span class='line'>  <span class="nx">cache_clear_all</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">$form_state</span><span class="p">[</span><span class="s1">&#39;redirect&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;node/</span><span class="si">$comment-&gt;nid</span><span class="s2">&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&rsquo;m far from saying that Drupal sucks, but some
questions rise into my mind:</p>

<ul>
<li>why do I have a <code>$form</code> and a separate <code>$form_state</code>?</li>
<li>what is <code>watchdog()</code> doing? Is it used for
logging? Or to display flash messages?</li>
<li>what is a <code>cid</code>? And what about the <code>nid</code>?</li>
<li>why should I clear my entire application&rsquo;s
cache to notify a user of a change?</li>
</ul>


<p>See, you don&rsquo;t want your developers to have to go
through an entire application to understand what
a piece of code does.</p>

<h2>Best practices</h2>

<p>Symfony2 is a framework made to take advantage
of clean and clear tested patterns as well as
tools to improve the final developer&rsquo;s
productivity: imagine your team, working
six months on this framework; how much would
they learn? How many structural changes
would they be able to do on your application without
introducing regressions?</p>

<p><img class="right" src="http://odino.org/images/best-practice.jpg"></p>

<p>For startups, by the way, a huge plus comes from
the fact that being highly decoupled, Symfony2 helps
when you want to drastically replace a piece of software,
or an adapter, with another one: for example,
thanks to the dependency-injection container,
you would be able to replace application services
with others that have the same API, but a different
implementation.</p>

<p>It is clear enough that Symfony2 provides the
flexibility you need to reach a very short
<em>time to market</em> and increases your developers&#8217;
awareness and efficiency by giving them the
guidance and the tools they need to care about
the domain of your services and not about how
many bugs they would introduce by changing an
untested piece of code.</p>

<h2>All in all&hellip;is it Symfony2?</h2>

<p>A very simple question that you should ask
yourself at the end of this reading is:
but, all in all, is this all thanks to
Symfony2 or its surrounding environment?</p>

<p>It is its surrounding environment, which
was born thanks to the framework itself:
when Symfony2 was released, no other framework
had the same level of quality that the open source
product from SensioLabs could offer;
a natural effect of this was that the
majority of well-known open source
PHP developers got amused by this framework
and embraced its way.</p>

<p>Basically, Symfony2 is a framework chosen by
the community, thus it can take advantage of
all the efforts of the OS developers around it:
from automated deployment tools to fully integrated
ORMs, from testing frameworks to tutorials
and best practices, through native, advanced
debugging tools, Symfony2 is, as of today the most
complete framework available in the PHP ecosystem
when you take in consideration learning curve,
integrations, stability and performances (don&rsquo;t forget
that one of <a href="http://highscalability.com/blog/2012/4/2/youporn-targeting-200-million-views-a-day-and-beyond.html">top 100 website in the Alexa rank</a>
is powered by Symfony2<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup>).</p>

<p>Can other frameworks do all of this?</p>

<p>For the benefit of your <strong>business</strong>, this
is the main question that you should ask
yourself.</p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>But - drumroll - since we are an e-commerce company, we always need to ensure that some critical parts of the system, like checkouts, are tested by a human reenacting our customers&#8217; behavior. So yes, for a few, business-critical things, we really *want* to do manual tests. <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>Even though I am sorry for using *that* website as an example, it is a very useful use-case when you consider its technical stack. <a href='#fnref:2' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Automated tests from a novice's perspective]]></title>
    <link href="http://odino.org/automated-tests-from-a-novice-perspective/"/>
    <updated>2013-05-27T22:01:00+04:00</updated>
    <id>http://odino.org/automated-tests-from-a-novice-perspective</id>
    <content type="html"><![CDATA[<p>Today I got one of those moments of pride
when one of our developers, who is working
since &ndash; roughly &ndash; a year, decided to send an
email to the team after working on automated
tests for the first weeks of his career:</p>

<blockquote><p>Automated testing is great idea </p><p>If you have a ticket which requires a lot of changes (20 files, for example) Automated tests are very helpful and save your time, it saves about 30% of your time; even if it&#8217;s still not perfect, its good<br/>For example look at this PR [link to a PR on Github], if i want to test all cases i need about half a day but it only took half an hour</p></blockquote>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Doctrine 80% faster?]]></title>
    <link href="http://odino.org/doctrine-80-percent-faster/"/>
    <updated>2013-04-21T10:54:00+04:00</updated>
    <id>http://odino.org/doctrine-80-percent-faster</id>
    <content type="html"><![CDATA[<p>A post in the <em>doctrine-dev</em> mailing list
caught my attention last week, and I want to
share its insights with you.</p>

<!-- more -->


<p>The good <a href="https://github.com/Ocramius">Marco Pivetta</a>
took initiative in adding proxy generation for hydrators,
and in <a href="https://github.com/Ocramius/ProxyManager/pull/29">a lonely branch</a>
he&rsquo;s pretty far with the progress on the matter: his
latest tests show that the <a href="https://travis-ci.org/Ocramius/ProxyManager/jobs/6485136#L127">hydration process is improved by 80%</a>
which is a very good news, considered metadata-based
software usually need to find a way to drastically
improve performances because of the basic lack of
performances due to having to read metadata.</p>

<p>Hoping to see this changes integrated in a stable
branch very soon!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My experience with our development team in Dubai: in between business, SOA and sun]]></title>
    <link href="http://odino.org/my-experience-with-our-development-team-in-dubai-in-between-business-soa-and-sun/"/>
    <updated>2013-03-29T18:08:00+04:00</updated>
    <id>http://odino.org/my-experience-with-our-development-team-in-dubai-in-between-business-soa-and-sun</id>
    <content type="html"><![CDATA[<p>Today I had the opportunity to share with the
people at the <a href="http://www.phptostart.it/">PHP.TO.START</a>
in Turin my 1-year experience in Namshi, one of
Rocket Internet&rsquo;s ventures in the Middle East.</p>

<!-- more -->


<p><img class="left" src="http://odino.org/images/phptostart2013.jpg"></p>

<p>Luckily, this was a great opportunity to meet some good
old friends and the talk went very well: of course,
the reharsal at the <a href="http://roma.grusp.org">PHP User Group in Rome</a>
earlier this week was a bless.</p>

<p>It basically deals with the phases that I experienced
with our team in Namshi, with some spicy details on
recruiting (from looking for talents to conducting
interviews), how to <strong>change processes to help people improving</strong>
(and not change people to improve processes) and
a big picture on what we were able to plan and achieve
in this year together, from a technical point of view.</p>

<p>At the end of the talk I invited everyone interested, and
I will repeat myself here, to look for the open positions
on <a href="http://en-ae.namshi.com/careers/">our careers site</a>,
since we are currently hiring a Lead Developer and a
PHP Developer (a junior or an intermediate, it&rsquo;s more
about the approach rather than the hands-on experience).</p>

<p>Here are the slides of the talk, in a slightly better (and more colored)
version compared to the ones I used in Rome:</p>

<iframe src="http://www.slideshare.net/odino/slideshelf" width="760px" height="570px" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:none;" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A brief visit to the cold lands of Italy]]></title>
    <link href="http://odino.org/a-brief-visit-to-the-cold-lands-of-italy/"/>
    <updated>2013-03-29T14:14:00+04:00</updated>
    <id>http://odino.org/a-brief-visit-to-the-cold-lands-of-italy</id>
    <content type="html"><![CDATA[<p>I&rsquo;m enjoying a relatively long &ndash; at least for me &ndash; 2 weeks &ldquo;vacation&rdquo;
back in Italy, since I was missing my friends in Rome
since <strong>exactly</strong> a year and the last time my parents saw me it
was 6 months ago.</p>

<!-- more -->


<p>Fun thing is, since I can&rsquo;t miss the opportunity
of getting back in touch with a few people, I will
be joining 4 tech events:</p>

<ul>
<li><a href="http://rome.codemotionworld.com/">Codemotion</a> &ndash; already happened, I was there on the 23rd &ndash; the conference open to
all the programming languages, in Rome</li>
<li><a href="http://roma.grusp.org/">PHP User group Rome</a>, a meetup that is held monthly, for one of
the user groups that I helped launching here in Italy, again in Rome, where I gave
a talk about assembling a mid-sized PHP team</li>
<li><a href="http://www.phptostart.it/">PHP.TO.START</a> &ndash; happening <strong>right now </strong>&ndash; a yearly event for the
north-western italian PHP community, where I&rsquo;ll be presenting the same talk I had at
the PUG Rome</li>
<li><a href="http://friuli.grusp.org/">PHP User Group Friuli</a>, another meetup organized by one of the
italian PHP User Groups, where I&rsquo;ll be presenting an <em>unknown</em> topic (at least for now)</li>
</ul>


<p>An <strong>important thing</strong>: I am actively recruiting for our company,
<a href="http://en-ae.namshi.com/careers/technology/">Namshi</a>, so if you
are interested in one of the open positions that we have right now
(in particular, Lead Developer and PHP Developer), feel free to approach
me if you find me at the upcoming events (in Turin and Udine).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Refactoring your architecture understanding SOA]]></title>
    <link href="http://odino.org/refactoring-your-architecture-go-for-soa/"/>
    <updated>2013-03-23T17:35:00+04:00</updated>
    <id>http://odino.org/refactoring-your-architecture-go-for-soa</id>
    <content type="html"><![CDATA[<p>It is no news that I work for <a href="http://en-ae.namshi.com">a company</a>
supported by <a href="http://www.rocket-internet.de">a mothership</a>
that helps most of his affiliates with know-how
and basic tools.</p>

<p>But to aim expansion, one needs to go beyond those
shared layers and start customizing his
products and services, and in terms of software development
nothing can help you more than
<a href="http://en.wikipedia.org/wiki/Service-oriented_architecture">service-oriented architectures</a>,
or <em>SOA</em>.</p>

<!-- more -->


<p>So, what&rsquo;s the goal of this post? Basically
providing our view on how we are going to
shift from our current architecture, which
is already a composite, to a more powerful
layer of services.</p>

<h2>Identifying the service</h2>

<p>One of the first steps in order to dig
into the implementation is to actually identity
a first bunch of functionalities that should be
incorporated as standalone services.</p>

<p>Usually, opportunities for new services pop up
when it&rsquo;s time to <strong>introduce a new functionality</strong>
or the cost of fixing / implementation of a
change request are too high: for example, if you
want to add the ability to send SMSes from your website,
a good service would be one which just deals with
the receiving an input event, assembling a message
and contacting the <em>real</em> SMS provider via webservice
in order to dispatch the message; another good example is
<strong>identity</strong>: if you are struggling with different userbases
that need to be in sync, a good solution would be to
centralize identities and provide a service which does,
at least, authentication.</p>

<h2>Data</h2>

<p>Another tipical question is how to manage and organize
data when you have a de-centralized architecture.</p>

<p>In SOA terms, usually data is shared among the
services but this doesnt mean that each service can&rsquo;t
have its own data-layer: it is often seen a very old
fashioned RDBMS shared across all the services and
some of them using a less traditional solution, like
a NoSQL DB; this is mainly done to achieve better
performances and different data-retrieval patterns</p>

<p>Think about legacy applications that have a model which can be
extensively customized by the end user, that usually
implement the <a href="http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model">EAV pattern</a>,
getting stuck into <strong>performance bottlenecks</strong>, while
a document-db like MongoDB or CouchDB would
perfectly solve the issue.</p>

<p>If you are running, for example, an e-commerce system,
you may want to have transactions and identities in a
solid and robust system like MSSQL, while your
frontend can actually run with MongoDB: once the user
purchases a product, via webservice you store it into
MSSQL.</p>

<h2>Services need</h2>

<p>One typical aspect, in SOA terms, is seeking answers
to our questions (read getting <em>responses</em> for our <em>requests</em>),
a problem which we can overcame with a simple solution:
when a service <strong>needs</strong> another one, we talk about APIs.</p>

<p>For example, your frontend might offer authentication, while
the Identity manager is a service providing identities to
multiple layers of your architecture: when the frontend needs
to authenticate a user, it will directly rely on the
Identity service, asking him to authenticate the user with the
credentials he or she submitted to the frontend.</p>

<p>APIs can be traditionally categorized into <a href="http://nordsc.com/ext/classification_of_http_based_apis.html">a few types</a>:</p>

<ul>
<li>mess: &ldquo;messy&rdquo; API don&rsquo;t follow structured rules (it cab be <em>plain-old XML over HTTP</em>
or a replication of DB writes and reads in JSON format); they can
be <strong>very useful</strong> when you need to kickstart a new, small and simple
API</li>
<li>HTTP API: services that semantically expose their domain model
in terms of resources, embracing the HTTP specification</li>
<li><a href="http://odino.org/hypermedia-services-beyond-rest-architectures/">REST</a>: hypermedia services</li>
<li>SOAP: services using strict interfaces between clients and servers,
following the SOAP spec</li>
</ul>


<p>No matter what, you will always find yourself dealing with APIs
if you decided to go for SOA: it is the simplest way to
provide <strong>data-exchange mechanisms to layers that don&rsquo;t fully
know each other&rsquo;s domain</strong>.</p>

<h2>Services listen</h2>

<p>Another <strong>very common</strong> scenario, is when services &ldquo;listen&rdquo;,
waiting for notifications sent across by other components of
the architecture: you are probably already thinking about
messaging queues and message notifications, and you are right.</p>

<p>A event-driven process can be achieved when we have tools
such as <a href="http://www.rabbitmq.com/">RabbitMQ</a> helping in gathering
and dispatching notifications to various parts of the architecture:
with Rabbit, a service can dispatch a message to a queue
and another one (or <strong>ones</strong>), through a daemon, consumes the message.</p>

<p>Thinking about what I mentioned earlier, an SMS-dispatching mechanism
could fit in this context really well: think about SMSes that are sent
once the user completes certain actions on your frontend (by gaining credits,
placing an order on your e-commerce or so on); once the user
completes an action, a notification will be sent out and
whoever needs to listen to that message will catch
and process it.</p>

<h2>So far so good</h2>

<p>In our fast and new journey towards integrating services into
our architecture, we are finding ourselves pretty well: it is
no news that we are using RabbitMQ and <a href="http://symfony.com">Symfony2</a>
for our new, isolated services, and that we already identified
a few services that can run on their own, decoupled context.</p>

<p>Thinking in SOA terms, by the way, brings out a new set of problems, like
thinking in terms of architecture, and not of application: you
don&rsquo;t deploy a new version of your application, you <strong>update a part
of the architecture</strong>; your system is decoupled, from the code to the
processes you use to handle them. And what about the complications in the
development environments? And which monitoring tool should I use to
understand that all the components are working alltogether? And&hellip;</p>

<p>There&rsquo;s room for generic problems that everyone faced and that we will
face as well, and I think it will be very interesting to share our
approach and the vision we had in our own context.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Symfony2 console: permission denied during autocompletion]]></title>
    <link href="http://odino.org/symfony2-console-permission-denied-during-autocompletion/"/>
    <updated>2013-03-11T10:11:00+04:00</updated>
    <id>http://odino.org/symfony2-console-permission-denied-during-autocompletion</id>
    <content type="html"><![CDATA[<p>Yesterday I faced a pretty cryptic issue
while using the Symfony2 console (<code>app/console</code>).</p>

<!-- more -->


<p>I guess the error is pretty common, and it&rsquo;s really
easy to fix, since the problem is that
<a href="https://github.com/hacfi/oh-my-zsh/commit/8c74d80fd6cdc7e1b48e7eb321a3e3a22674c3be">the binary is not executable</a>:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>chmod +x app/console</span></code></pre></td></tr></table></div></figure>


<p>&hellip;and you&rsquo;re done.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using Selenium and Symfony2 to help your frontend developers coding without risks]]></title>
    <link href="http://odino.org/using-behat-to-help-your-frontend-developers-coding-without-risks/"/>
    <updated>2013-03-10T11:39:00+04:00</updated>
    <id>http://odino.org/using-behat-to-help-your-frontend-developers-coding-without-risks</id>
    <content type="html"><![CDATA[<p>Since testing is one of those practices
that many consider <em>boring</em> (unless a major
catastrophe happens), you should help people
is easing their job while testing.</p>

<p>Today I am going to show the approach that
we just kickstarted, at Namshi, in order to
help designers and developers testing
frontend changes in a more automated, thus
easier, way.</p>

<!-- more -->


<p>Thanks to the <a href="https://github.com/namshi/NamshiVoyeurBundle">NamshiVoyeurBundle</a>,
it is really easy to start increasing
the efficiency of your testing department,
even if coders do not want to write
automated tests.</p>

<p>The bundle, that you can use <strong>inside a Symfony2
application</strong>, is actually very small and can be
extrapolated to be integrated in other frameworks
(like ZF2 or Cake).</p>

<p>The idea is very simple: you take some screenshots
of a website, deploy a new version, take another set of
screenshots (at the same URLs) and then compare
them, generating an image diff.</p>

<p>After you install the <code>NamshiVoyeurBundle</code> (via composer),
it is really easy to start taking screenshots;
you just have to configure a few services
and some parameters:</p>

<figure class='code'><figcaption><span>Configuring the bundle</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">parameters</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">namshi_voyeur</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="l-Scalar-Plain">browsers</span><span class="p-Indicator">:</span>
</span><span class='line'>        <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">firefox</span>
</span><span class='line'>        <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">safari</span>
</span><span class='line'>        <span class="p-Indicator">-</span> <span class="l-Scalar-Plain">chrome</span>
</span><span class='line'>      <span class="l-Scalar-Plain">urls</span><span class="p-Indicator">:</span>
</span><span class='line'>        <span class="l-Scalar-Plain">homepage</span><span class="p-Indicator">:</span>     <span class="s">&quot;/&quot;</span>
</span><span class='line'>        <span class="l-Scalar-Plain">new-arrivals</span><span class="p-Indicator">:</span> <span class="s">&quot;new-products&quot;</span>
</span><span class='line'>        <span class="l-Scalar-Plain">women</span><span class="p-Indicator">:</span>        <span class="s">&quot;women-shoes&quot;</span>
</span><span class='line'>      <span class="l-Scalar-Plain">shots_dir</span><span class="p-Indicator">:</span> <span class="s">&quot;/Users/you/Downloads/screenshots&quot;</span>
</span><span class='line'>      <span class="l-Scalar-Plain">base_url</span><span class="p-Indicator">:</span>       <span class="s">&quot;http://en-ae.namshi.com/&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain">services</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">safari</span><span class="p-Indicator">:</span>
</span><span class='line'>        <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span>  <span class="l-Scalar-Plain">Behat\Mink\Driver\Selenium2Driver</span>
</span><span class='line'>        <span class="l-Scalar-Plain">calls</span><span class="p-Indicator">:</span>
</span><span class='line'>          <span class="p-Indicator">-</span> <span class="p-Indicator">[</span><span class="nv">start</span><span class="p-Indicator">]</span>
</span><span class='line'>        <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>          <span class="l-Scalar-Plain">browser</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">safari</span>
</span><span class='line'>    <span class="l-Scalar-Plain">firefox</span><span class="p-Indicator">:</span>
</span><span class='line'>        <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span>  <span class="l-Scalar-Plain">Behat\Mink\Driver\Selenium2Driver</span>
</span><span class='line'>        <span class="l-Scalar-Plain">calls</span><span class="p-Indicator">:</span>
</span><span class='line'>          <span class="p-Indicator">-</span> <span class="p-Indicator">[</span><span class="nv">start</span><span class="p-Indicator">]</span>
</span><span class='line'>    <span class="l-Scalar-Plain">chrome</span><span class="p-Indicator">:</span>
</span><span class='line'>        <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span>  <span class="l-Scalar-Plain">Behat\Mink\Driver\Selenium2Driver</span>
</span><span class='line'>        <span class="l-Scalar-Plain">calls</span><span class="p-Indicator">:</span>
</span><span class='line'>          <span class="p-Indicator">-</span> <span class="p-Indicator">[</span><span class="nv">start</span><span class="p-Indicator">]</span>
</span><span class='line'>        <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>          <span class="l-Scalar-Plain">browser</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">chrome</span>
</span></code></pre></td></tr></table></div></figure>


<p>This configuration basically tells Voyeur that
you will be taking screenshots of three URLs:</p>

<ul>
<li><code>http://en-ae.namshi.com/</code></li>
<li><code>http://en-ae.namshi.com/new-products</code></li>
<li><code>http://en-ae.namshi.com/women-shoes</code></li>
</ul>


<p>with safari, firefox and google chrome.</p>

<p>To run the Voyeur, use the <code>cli</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>php app/console namshi:voyeur
</span></code></pre></td></tr></table></div></figure>


<p>Screenshots will be saved at <code>/Users/you/Downloads/screenshots</code>.</p>

<p>At this point, after you deployed a new version of the code,
run the Voyeur again, and you will be reay to generate the
diffs between the screenshots:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>php app/console namshi:voyeur:diff /Users/you/Downloads/screenshots/firefox/2013/03/10/1200 /Users/you/Downloads/screenshots/firefox/2013/03/10/1205
</span></code></pre></td></tr></table></div></figure>


<p>Diffs will be generated at <code>/Users/you/Downloads/screenshots/firefox/2013/03/10/1205/diff</code>.</p>

<p>That&rsquo;s it: now you can start having a look at what changed and
ask your developers to do the same, even on their local
machine, before committing any changes.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Namshi is looking for a new Lead Developer!]]></title>
    <link href="http://odino.org/namshi-is-looking-for-a-new-lead-developer/"/>
    <updated>2013-02-21T19:28:00+04:00</updated>
    <id>http://odino.org/namshi-is-looking-for-a-new-lead-developer</id>
    <content type="html"><![CDATA[<p>As some of you know, I am currently employed by
<a href="http://en-ae.namshi.com/">Namshi</a>, a
<a href="http://www.rocket-internet.de/">Rocket Internet</a>
venture, here in Dubai, since almost a year: recently,
our CTO <a href="http://halilkoklu.de/">Halil</a> decided to move forward
and start a new adventure pretty far from here
so, as part of me taking care of other things than
development, we are looking for a new Lead Developer
who can strive, with the team, towards excellence.</p>

<!-- more -->


<p>We are in the process of building new applications
and API to move towards a SOA architecture, using
cutting-edge libraries (like <a href="http://symfony.com/">Symfony2</a>), tools
(like <a href="http://behat.org/">Behat</a> and <a href="http://getcomposer.org/">Composer</a>) and practices.</p>

<p>The Job Description is still on his way but I can
post here some of the competencies that we are
looking forward to warmly welcome in the crazy Namshi
family:</p>

<ul>
<li>enterprise practices and patterns, such as automated testing, pair programming</li>
<li>HTTP webservices</li>
<li>you buzz around the PHP5.3+ ecosystem, from the Symfony2 components to tools such as Composer</li>
<li>you have Git for breakfast</li>
<li>RDBMS (MySQL, Percona)</li>
<li>can deal with pre-PHP5.3 frameworks (Zend 1 and Yii)</li>
<li>Solr and/or Memcached</li>
<li>passionate about open source and ideally contributing to it.</li>
</ul>


<p>The salary reflects the demand of skills that we are looking for, and
you will be responsible of the day-to-day development of our
applications.</p>

<p>Just to mention: the United Arab Emirated are a tax-free country,
the weather is amazing (even though for a couple months, during the summer,
its <strong>really</strong> hot) and it&rsquo;s an all-around country, meaning that if you
want, you can enjoy the glamorous side of Dubai or, if you dont feel like<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>,
you can head towards the old town and enjoy the cultural meshup made
of Indonesia, Thai and Indian food or Arab heritage.</p>

<p>I rushed this post, I know, so you can ask for more informations
in the comments or, even better, drop me an email at
<em>alex.nadalin@namshi.com</em>.</p>

<p>Cheers!</p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>Like me <a href='#fnref:1' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PHP: 'The script tried to execute a method or access a property of an incomplete object']]></title>
    <link href="http://odino.org/php-the-script-tried-to-execute-a-method-or-access-a-property-of-an-incomplete-object/"/>
    <updated>2013-01-31T13:28:00+04:00</updated>
    <id>http://odino.org/php-the-script-tried-to-execute-a-method-or-access-a-property-of-an-incomplete-object</id>
    <content type="html"><![CDATA[<p>Have you ever got this error in PHP? I bet no, never.</p>

<!-- more -->


<p>Basically, it happens when you serialize an object
of class <code>A</code> and try to unserialize it when class
<code>A</code> doesn&rsquo;t exist anymore.</p>

<h2>How (the hell) do I get there?</h2>

<p>If you are working with auto-generated proxy classes,
store objects in the session and then clear your
cache, once you retrieve an object from the session
you are going to face it. A solution is to
re-generate all the proxies before retrieving objects
from the session<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>.</p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>in an ideal world, at every deployment you clear and re-generate proxies <a href='#fnref:1' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Profiling PHP applications from the browser]]></title>
    <link href="http://odino.org/profiling-php-applications-from-the-browser/"/>
    <updated>2013-01-25T22:38:00+04:00</updated>
    <id>http://odino.org/profiling-php-applications-from-the-browser</id>
    <content type="html"><![CDATA[<p>In my <a href="http://odino.org/making-the-orientdb-odm-5-times-faster/">previous post</a> I briefly spoke about
<a href="https://github.com/jokkedk/webgrind">Webgrind</a>,
a web-based profiler for PHP:
now I&rsquo;d like to spend some more time
giving an overview on how to install
and use it, as well as <strong>what to look for
when profiling an application</strong>.</p>

<!-- more -->


<p><img class="right" src="http://odino.org/images/webgrind-call-graph-detail.png"></p>

<h2>Profiling in a few words</h2>

<p>As PHP developers, we are rarely used to
profiling: essentially, most of our
applications are not bound to extensive
CPU usage or insanely huge data-processing operations;
the scope of the language is very clear and
even though we <strong>might</strong> need to profile, once
in a while, it&rsquo;s unlikely that we will end up
having problems like
<a href="http://odino.org/book-review-data-intensive-text-processing-with-mapreduce/">optimizing MapReduce algorhitms</a>.</p>

<p>But sometimes we <strong>do</strong> need to profile,
and this will bring on the table bottlenecks of
your applications: within a session of inspection,
you will likely find optimizations that would lead to
a <code>20/30%</code> faster execution time, by just changing
your backend (PHP) code<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup>.</p>

<h2>Why Webgrind</h2>

<p>Among all the available profilers for PHP, I
choose to go with Webgrind for a bunch of reasons:</p>

<ul>
<li>nowadays, I am mostly developing on a Mac<sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup>, so
<a href="http://kcachegrind.sourceforge.net/html/Home.html">KCacheGrind</a>
wasn&rsquo;t an option</li>
<li>I didn&rsquo;t want to install <a href="https://github.com/facebook/xhprof">XHPROF</a>
as it usually takes a few minutes,
even though is probably the best profiler for PHP: facebook uses it
<strong>in production</strong>, and it&rsquo;s able to generate a lot of reports that
would make you face performance optimizations from various perspectives</li>
<li>Webgrind offers a zero-setup installation</li>
</ul>


<h2>Installation with XDebug</h2>

<p><a href="http://xdebug.org/">XDebug</a> is a must for profiling, as it&rsquo;s
the tool through which we can generate the
<a href="http://valgrind.org/docs/manual/cg-manual.html">Cachegrind</a> files, that are basically reports
on the costs of your application&rsquo;s calls.</p>

<p>To enable XDebug&rsquo;s profiling you will have to
tweak your <code>php.ini</code>&rsquo;s configuration:</p>

<figure class='code'><figcaption><span>Enabling profiling with XDebug</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>xdebug.profiler_enable <span class="o">=</span> 1
</span></code></pre></td></tr></table></div></figure>


<p>Beware that profiling each request your application
processes can be an expensive job (pages that
would usually load in 2/3 seconds can take up to
10 seconds), so you should &ndash; instead of enabling
the profiler by default &ndash; activate the <code>enable_trigger</code>
directive, which will make XDebug profile your application
only if a specific <code>GET</code> or <code>POST</code> parameter is specified
within the request:</p>

<figure class='code'><figcaption><span>Using the XDebug profiler in enable trigger mode</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>xdebug.profiler_enable <span class="o">=</span> 0
</span><span class='line'>xdebug.profiler_enable_trigger <span class="o">=</span> 1
</span></code></pre></td></tr></table></div></figure>


<p>Dont forget to restart apache once you made the changes:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>sudo apachectl restart
</span></code></pre></td></tr></table></div></figure>


<p>By visiting your application and specifying a special
<code>GET</code> parameter in the URL, you will run your first
profiled PHP response: supposing that you want to
profile the code that runs <code>http://dev.project.com</code>,
just visit <code>http://dev.project.com?XDEBUG_PROFILE=true</code></p>

<p>Once you&rsquo;re done with the XDebug configuration, it&rsquo;s
time to install Webgrind:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nb">cd</span> /path/to/your/home/projects
</span><span class='line'>
</span><span class='line'>git clone git://github.com/jokkedk/webgrind.git
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s it!</p>

<p>You can now access Webgrind at
<code>127.0.0.1/webgrind</code> or &ndash; if you prefer &ndash;
set up a virtual host for it:</p>

<figure class='code'><figcaption><span>Setting up the virtualhost at</span><a href='http://webgrind/'>link</a></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>&lt;VirtualHost *:80&gt;
</span><span class='line'>    DocumentRoot <span class="s2">&quot;/path/to/your/home/projects/webgrind&quot;</span>
</span><span class='line'>
</span><span class='line'>    ServerName webgrind
</span><span class='line'>
</span><span class='line'>    &lt;Directory <span class="s2">&quot;/path/to/your/home/projects/webgrind&quot;</span>&gt;
</span><span class='line'>        Options Indexes FollowSymLinks MultiViews
</span><span class='line'>        AllowOverride all
</span><span class='line'>        Order allow,deny
</span><span class='line'>        Allow from all
</span><span class='line'>    &lt;/Directory&gt;
</span><span class='line'>&lt;/VirtualHost&gt;
</span></code></pre></td></tr></table></div></figure>


<p>and have Webgrind running at
<code>http://webgrind/</code>.</p>

<h2>Looking at the results</h2>

<p><img class="right" src="http://odino.org/images/webgrind-select-file.png"></p>

<p>Once your application runs, XDebug will generate
the cachegrind files that Webgrind will analyze:
after each PHP response is served from your application,
you can inspect the results from the Webgrind interface,
by just selecting the first file of the list:
it might take some time for Webgrind to generate the
first report, as cachegrind files can easily size up to
100/200 megabytes (files below <code>~50MB</code> will be read in
10 seconds or so).</p>

<p>When the report is generated, you will see the results: I
strongly recommend to generate a report in <strong>milliseconds</strong>, as
it will give you a direct overview on how much time
a function takes rather than having this value as a percentage
compared to the entire application&rsquo;s run.</p>

<p><img class="center" src="http://odino.org/images/webgrind-expensive-call.png"></p>

<p>If you order results by <code>Total inclusive cost</code>, you will
exactly see which ones are the most expensive functions
of your applications: in the example, you will see that the
<code>Doctrine\ODM\OrientDB\Mapper::hydrate</code> method really
kills the performances of my application (<code>10.6</code> seconds).</p>

<p>Having this kind of report is not really useful, as
usually you need to dig deeper to understand which
exact step is making that function taking all that time:
you can investigate further by clicking on a function,
action that will open the call stack after that function
is called:</p>

<p><img class="center" src="http://odino.org/images/webgrind-call-stack.png"></p>

<p>as you see, the problems, here, lies in
<code>Doctrine\ODM\OrientDB\Mapper::createDocument</code> (<code>6.2</code> seconds)
and <code>Doctrine\ODM\OrientDB\Mapper::findClassMappingInDirectories</code>
(<code>4.3</code> seconds), so there you have the explanation why
<code>Doctrine\ODM\OrientDB\Mapper::hydrate</code> takes more than
10 seconds.</p>

<p>Then, take your time to investigate even further and make the
optimal changes in your application, run it with the
profiler enabled once more and have a look at the results:</p>

<p><img class="center" src="http://odino.org/images/webgrind-after-optimization.png"></p>

<p>As you see, after I tweaked my code,
<code>Doctrine\ODM\OrientDB\Mapper::hydrate</code> is not even the
most expensive function at all (<code>Sharah\Controller::getPartial</code> is),
and the previously performance-killer methods, which
would take <code>~6</code> and <code>~4</code> seconds, are now respectively
taking <code>~1</code> and <code>~0.1</code> seconds.</p>

<h2>The call graph</h2>

<p><img class="right" src="http://odino.org/images/webgrind-call-graph.png"></p>

<p>Another interesting feature of Webgrind<sup id='fnref:3'><a href='#fn:3' rel='footnote'>3</a></sup>
is the ability to generate a <strong>call graph</strong> to visualize
bottlenecks in the application: by having a look
at the graph you will have a top-down overview on
how much execution time (expressed in percentage)
a function will take.</p>

<p>When you look at it, you should question
every step of the graph and ask yourself is that
specific function should really take that amount
of time.</p>

<p>For example:</p>

<ul>
<li>if a controller takes <code>20%</code> of the time to run (called <code>TTR</code> from now on),
it might be that you have a design flaw, as it should be
the most expensive part of your application, calling
the models and rendering the view (which are <strong>included</strong> in
the calculated <code>TTR</code>)</li>
<li>if a model&rsquo;s method is taking <code>60% TTR</code>, there is a bad smell:
how come that just retrieving data <strong>once</strong> is taking
more than half of the <code>TTR</code>?</li>
<li>if bootstrapping the application takes <code>15% TTR</code>, then
it&rsquo;s fine, as that is usually the time a well-abstracted
framework needs to provide you a solid foundation to develop
on top of</li>
</ul>


<p>In the image above, you will see that <code>87%</code>
of the execution time is taken by the controller&rsquo;s
action (which is fine) and then equally
distributed (<code>10/20%</code>) across various other
functions that controller calls.</p>

<h2>Conclusions</h2>

<p><span class='pullquote-right' data-pullquote='sometimes the best design
is probably unpractical to perform
really well'>
This is not an extensive guide on profiling,
neither a fair comparison of PHP profilers
(as I said, I picked Webgrind, last night, more
because of the stress-free installation rather than
its actual capabilities<sup id='fnref:4'><a href='#fn:4' rel='footnote'>4</a></sup>), but I
hope it can give you a good quickstart guide
to start optimizing bottlenecks in your
applications.</p>

<p>One thing that I would really like to
point out is to <strong>stop useless optimizations</strong>:
there is no need to &ldquo;drop double quotes
in favour of single quotes&rdquo; because those
are such small optimizations that you
will <strong> never</strong> feel in your application;
create <a href="http://www.slideshare.net/arnoschn/rasmus-think-again-agile-framework-happy-php-developer-presentation-625122/73">useful benchmarks</a>
and always question your choices: running
a solid benchmark and profiling your
application properly will also
tell you that sometimes the best design
is probably unpractical to perform
really well.
</span></p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>Beware that for high-scale applications you should focus on bigger and deeper improvements: see http://odino.org/rest-better-http-cache/ <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>Shame on me, I know <a href='#fnref:2' rev='footnote'>↩</a></li><li id='fn:3'>Which is implemented in every profiler I used so far <a href='#fnref:3' rev='footnote'>↩</a></li><li id='fn:4'>If you want to seriously profile your PHP application, go for XHPROF <a href='#fnref:4' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Making the OrientDB ODM 5 times faster]]></title>
    <link href="http://odino.org/making-the-orientdb-odm-5-times-faster/"/>
    <updated>2013-01-25T17:39:00+04:00</updated>
    <id>http://odino.org/making-the-orientdb-odm-5-times-faster</id>
    <content type="html"><![CDATA[<p>Today, after heavily testing performances
on a project, I pushed some small but
precious changes to the
<a href="https://github.com/doctrine/orientdb-odm">orientdb-odm</a>.</p>

<!-- more -->


<h2>Prelude</h2>

<blockquote><p>Premature optimization is the root of all evil</p><footer><strong>Donald Knuth</strong> <cite><a href='http://c2.com/cgi/wiki?PrematureOptimization'>C2 Wiki</a></cite></footer></blockquote>


<p></p>

<p>In these days I was testing performances of a
service I am building with
<a href="https://github.com/nuvolabase/orientdb">OrientDB</a>
and the doctrine ODM that
<a href="http://odino.org/please-welcome-the-doctrine-orientdb-odm/">we built so far</a>.</p>

<p>Following one of the golden rules for software
architects, we didn&rsquo;t paid attention to performances &ndash; at first &ndash;
but rather went for a design which would allow us
to inject behaviours and easily change portions of code
(also thanks to the <a href="https://travis-ci.org/doctrine/orientdb-odm">test suite</a>),
I knew that I would have noticed huge flaws at
performance level when testing it with
<strong>production data</strong>.</p>

<p>A few days ago we committed fetchplans for repository
classes, but it wasnt enough: rather then concentrating
on which data we should fetch, I realized one major
improvement could be applied on how we <strong>map</strong> data.</p>

<h2>The golden rule</h2>

<p><span class='pullquote-right' data-pullquote='abstraction comes with a cost:
slow performances'>
When you write a OXM (object-something mapper) you will
shortly understand that a huge portion of your job
consist into abstract your design, to ease
integration of multiple components into your application:
repositories, the object manager, POXO, the data mapper,
proxy classes and so on.</p>

<p>Of course, abstraction comes with a cost:
slow performances, so one of the first things that you do is starting
to <strong>cache everything</strong>.
</span></p>

<h2>How we did it</h2>

<p>With a <a href="https://github.com/doctrine/orientdb-odm/commit/df875c946e02a9c84eee22f0c86e0a3b06bd70ec">single commit</a>
&ndash; there&rsquo;s always room for improvements &ndash; the ODM is now able
to hydrate objects <strong>5 times faster</strong>: when you hydrate similar
objects from OrientDB (for example, 2 records that share the same
attributes&#8217; values, like <code>is_published</code> or <code>country</code>), there is
no need to duplicate operations, so we added a <a href="https://github.com/doctrine/orientdb-odm/blob/df875c946e02a9c84eee22f0c86e0a3b06bd70ec/src/Doctrine/OrientDB/Util/Inflector/Cached.php">cached inflector</a>
(with an in-memory / single request cache) and did some other
improvements to the <code>Mapper</code>:</p>

<ul>
<li>cached the relations between <a href="https://github.com/doctrine/orientdb-odm/commit/df875c946e02a9c84eee22f0c86e0a3b06bd70ec#L0L297">PHP classes and OrientDB classes</a> (if 2 records of the same OrientDB class are hydrated, there is only one single <em>search</em> operation to find the PHP class that should map them)</li>
<li>cached the <a href="https://github.com/doctrine/orientdb-odm/commit/df875c946e02a9c84eee22f0c86e0a3b06bd70ec#L0L223">casting of properties</a> (if 2 objects have the same value for the <code>is_published</code> attribute, casting is done once)</li>
<li>cached <a href="https://github.com/doctrine/orientdb-odm/commit/df875c946e02a9c84eee22f0c86e0a3b06bd70ec#L0L466">properties&#8217; annotations</a> (property-level annotations are inspected once per class)</li>
</ul>


<p>There is no rocket science in what we did, but benchmarks ensure that
it&rsquo;s a <strong>huge performance improvement</strong>.</p>

<h2>By the way, we used Webgrind</h2>

<p><img class="right" src="http://odino.org/images/webgrind.png"></p>

<p>Doing almost all of my work from a Mac, I kind of
missed <a href="http://kcachegrind.sourceforge.net/html/Home.html">KCacheGrind</a>
for profiling, so I was looking for an
alternative (no, installing
<a href="https://github.com/facebook/xhprof">XHPROF</a> isn&rsquo;t an alternative at 2 in the morning)
and I found Webgrind (which is cross-platform), a web profiler
that requires zero setup:
you basically just need to provide it access from the webserver
and, by opening it with a browser, the application automatically
launches and parses the cachegrind files generated by XDebug.</p>

<p>Webgrind&rsquo;s code is a bit of a mess, but then, the
result is still pretty good &ndash; you get a good overview
of the expensiveness of your calls as well as a
call graph compiled in <a href="http://en.wikipedia.org/wiki/DOT_language">DOT</a>,
which is a de-facto standard for graph generation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Starting to play with the Doctrine OrientDB ODM]]></title>
    <link href="http://odino.org/starting-to-play-with-the-doctrine-orientdb-odm/"/>
    <updated>2013-01-20T23:00:00+04:00</updated>
    <id>http://odino.org/starting-to-play-with-the-doctrine-orientdb-odm</id>
    <content type="html"><![CDATA[<p>Since I am actively playing around with it, I
wanted to share some snippets to use the
<a href="https://github.com/doctrine/orientdb-odm">Doctrine OrientDB ODM</a>
in your PHP applications.</p>

<!-- more -->


<h2>Prelude</h2>

<p>In the last few weeks I&rsquo;ve started working,
for fun <strong>and</strong> profit, to a personal project,
nothing really exciting as of now.</p>

<p>The thing is, since I wanted to get back on some
cool piece of software, I decided to go for
OrientDB for the persistence and a
mini-framework <em>a-la</em> Symfony2 as foundation for the
PHP application &ndash; I actually considered NodeJS first,
but I need a prototype in 2 months so&hellip;</p>

<p>Point being, I&rsquo;d like to share with you my basic
approach to the OrientDB ODM.</p>

<h2>The container</h2>

<p>Given I&rsquo;ve been inspired to Symfony2,
instantiating the main ODM classes
happens in the DIC:</p>

<figure class='code'><figcaption><span>container.yml</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='yaml'><span class='line'><span class="l-Scalar-Plain">services</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">orientdb.binding.parameters</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Doctrine\OrientDB\Binding\BindingParameters</span>
</span><span class='line'>    <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="l-Scalar-Plain">host</span><span class="p-Indicator">:</span>     <span class="l-Scalar-Plain">127.0.0.1</span>
</span><span class='line'>      <span class="l-Scalar-Plain">port</span><span class="p-Indicator">:</span>     <span class="l-Scalar-Plain">2480</span>
</span><span class='line'>      <span class="l-Scalar-Plain">username</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">admin</span>
</span><span class='line'>      <span class="l-Scalar-Plain">password</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">admin</span>
</span><span class='line'>      <span class="l-Scalar-Plain">database</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">DBNAME</span>
</span><span class='line'>  <span class="l-Scalar-Plain">orientdb.binding</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Doctrine\OrientDB\Binding\HttpBinding</span>
</span><span class='line'>    <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="l-Scalar-Plain">parameters</span><span class="p-Indicator">:</span> <span class="err">@</span><span class="l-Scalar-Plain">orientdb.binding.parameters</span>
</span><span class='line'>  <span class="l-Scalar-Plain">odm</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Doctrine\ODM\OrientDB\Manager</span>
</span><span class='line'>    <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="l-Scalar-Plain">mapper</span><span class="p-Indicator">:</span> <span class="err">@</span><span class="l-Scalar-Plain">odm.mapper</span>
</span><span class='line'>      <span class="l-Scalar-Plain">binding</span><span class="p-Indicator">:</span> <span class="err">@</span><span class="l-Scalar-Plain">orientdb.binding</span>
</span><span class='line'>  <span class="l-Scalar-Plain">odm.mapper</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Doctrine\ODM\OrientDB\Mapper</span>
</span><span class='line'>    <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="l-Scalar-Plain">documentProxyDirectory</span><span class="p-Indicator">:</span> <span class="err">%</span><span class="l-Scalar-Plain">base-dir%/tmp/</span>
</span><span class='line'>      <span class="l-Scalar-Plain">annotationReader</span><span class="p-Indicator">:</span> <span class="err">@</span><span class="l-Scalar-Plain">odm.annotation-reader</span>
</span><span class='line'>    <span class="l-Scalar-Plain">calls</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="p-Indicator">-</span> <span class="p-Indicator">[</span><span class="nv">setDocumentDirectories</span><span class="p-Indicator">,</span> <span class="p-Indicator">[</span> <span class="err">%</span><span class="nv">base-dir%/src/PROJECT/Entity/</span> <span class="p-Indicator">:</span> <span class="s">&quot;PROJECT</span><span class="err">\</span><span class="s">Entity&quot;</span> <span class="p-Indicator">]</span> <span class="p-Indicator">]</span>
</span><span class='line'>  <span class="l-Scalar-Plain">odm.annotation-reader</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Doctrine\ODM\OrientDB\Mapper\Annotations\Reader</span>
</span><span class='line'>    <span class="l-Scalar-Plain">arguments</span><span class="p-Indicator">:</span>
</span><span class='line'>      <span class="l-Scalar-Plain">cacheReader</span><span class="p-Indicator">:</span> <span class="err">@</span><span class="l-Scalar-Plain">cache.array</span>
</span><span class='line'>  <span class="l-Scalar-Plain">cache.array</span><span class="p-Indicator">:</span>
</span><span class='line'>    <span class="l-Scalar-Plain">class</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Doctrine\Common\Cache\ArrayCache</span>
</span><span class='line'>
</span><span class='line'><span class="l-Scalar-Plain">parameters</span><span class="p-Indicator">:</span>
</span><span class='line'>  <span class="l-Scalar-Plain">base-dir</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">/Users/odino/Sites/PROJECT</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you see, you need:</p>

<ul>
<li>the <code>Manager</code>, which requires a <code>Mapper</code> and a connection to
OrientDB through a binding class implementing the
<code>Doctrine\OrientDB\Binding\BindingInterface</code></li>
<li>the <code>Mapper</code>, which requires a directory where it can write
proxy classes (for lazy loading), and an annotation reader
(this is not required, I&rsquo;ll explain it later), plus a source
directory to locate entities</li>
<li>the <code>HttpBinding</code>, used by the <code>Manager</code>, that does raw
queries to the OrientDB server</li>
<li>the <code>Annotations\Reader</code></li>
<li>a cache implementing the interface <code>Doctrine\Common\Cache\Cache</code>:
in dev environments it is needed since <code>ApcCache</code> is the default
one, and you would need to flush APC every time you change an
annotation in your entities (we will probably change it and put
<code>ArrayCache</code> by default, so that you will need to tweak the live
environment, not the dev one)</li>
</ul>


<h2>Autoloading</h2>

<p>The autoloading is straightforward, thanks to the <code>PSR-0</code>; the
only thing that you should keep in mind is that you will need
to specify a separate autoloader for proxy classes, since they can
be generated wherever you want (ideally, in a temporary folder,
since they should be removed every time you deploy):</p>

<figure class='code'><figcaption><span>autoload.php</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">require_once</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/../vendor/symfony/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php&#39;</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Symfony\Component\ClassLoader\UniversalClassLoader</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$loader</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">UniversalClassLoader</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">registerNamespaces</span><span class="p">(</span><span class="k">array</span><span class="p">(</span>
</span><span class='line'>    <span class="s1">&#39;Symfony&#39;</span>                       <span class="o">=&gt;</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/../vendor/symfony/symfony/src&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="s1">&#39;Doctrine\Common&#39;</span>               <span class="o">=&gt;</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/../vendor/doctrine/common/lib&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="s1">&#39;Doctrine\OrientDB&#39;</span>             <span class="o">=&gt;</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/../vendor/doctrine/orientdb-odm/src&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="s1">&#39;Doctrine\ODM\OrientDB&#39;</span>         <span class="o">=&gt;</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/../vendor/doctrine/orientdb-odm/src&#39;</span><span class="p">,</span>
</span><span class='line'>    <span class="s1">&#39;Doctrine\OrientDB\Proxy&#39;</span>       <span class="o">=&gt;</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/../tmp&#39;</span><span class="p">,</span>
</span><span class='line'><span class="p">));</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">register</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<p>You should set the autoloader for <code>Doctrine\OrientDB\Proxy</code>
accordingly to the argument <code>documentProxyDirectory</code> of the
<code>odm.mapper</code> service.</p>

<h2>Entities</h2>

<p>Following what we specified in the <code>container.yml</code>,
entities should be located in
<code>%base-dir%/src/PROJECT/Entity/</code> and follow the namespace
<code>PROJECT\Entity</code>:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">namespace</span> <span class="nx">PROJECT\Entity</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Doctrine\ODM\OrientDB\Mapper\Annotations</span> <span class="k">as</span> <span class="nx">ODM</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="sd">/**</span>
</span><span class='line'><span class="sd">* @ODM\Document(class=&quot;user&quot;)</span>
</span><span class='line'><span class="sd">*/</span>
</span><span class='line'><span class="k">class</span> <span class="nc">User</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * @ODM\Property(name=&quot;@rid&quot;, type=&quot;string&quot;)</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$rid</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * @ODM\Property(type=&quot;string&quot;)</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$email</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * @ODM\Property(type=&quot;string&quot;, notnull=&quot;false&quot;)</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$nick</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * @ODM\Property(type=&quot;linklist&quot;)</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">protected</span> <span class="nv">$addresses</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * Returns the nickname of the user, or his email if he has no nick set.</span>
</span><span class='line'><span class="sd">     * </span>
</span><span class='line'><span class="sd">     * @return string</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getNick</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">nick</span> <span class="o">?:</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getEmail</span><span class="p">();</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">setNick</span><span class="p">(</span><span class="nv">$nick</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">nick</span> <span class="o">=</span> <span class="nv">$nick</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getEmail</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">email</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">setEmail</span><span class="p">(</span><span class="nv">$email</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">email</span> <span class="o">=</span> <span class="nv">$email</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getAddresses</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addresses</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">setAddresses</span><span class="p">(</span><span class="nv">$adresses</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">addresses</span> <span class="o">=</span> <span class="nv">$addresses</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">getRid</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">rid</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">setRid</span><span class="p">(</span><span class="nv">$rid</span><span class="p">)</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">rid</span> <span class="o">=</span> <span class="nv">$rid</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you see, mapping an entity is pretty easy:
the first annotation is at class level, to define
which OrientDB classes are mapped by the entity,
then for every property that you want to be
persisted / hydrated, you define another annotation and
public getters / setters; if you want the property to be
public, you dont need getters / setters.</p>

<p>The property-level annotation has 3 parameters:</p>

<ul>
<li><strong>type</strong>: defines the type of the property in OrientDB
( <code>boolean</code>, <code>link</code>, <code>linklist</code>, <code>string</code>, <code>integer</code>, etc )</li>
<li><strong>name</strong>: the name of the attribute in the OrientDB class
(you might have a PHP property called <code>$createdAt</code> and in OrientDB
you call it <code>created_at</code>)</li>
<li><strong>notnull</strong>: defines whether the property can be <code>null</code> or not<sup id='fnref:1'><a href='#fn:1' rel='footnote'>1</a></sup></li>
</ul>


<h2>What about controllers?</h2>

<p>You can access the ODM from within
controllers of your application by
just using the container:</p>

<figure class='code'><figcaption><span>PROJECT/Controller/User.php</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">namespace</span> <span class="nx">PROJECT\Controller</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Project\Entity\User</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">UserController</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">public</span> <span class="k">function</span> <span class="nf">somethingAction</span><span class="p">()</span>
</span><span class='line'>  <span class="p">{</span>
</span><span class='line'>      <span class="nv">$user</span>       <span class="o">=</span> <span class="k">new</span> <span class="nx">User</span><span class="p">();</span>
</span><span class='line'>      <span class="nv">$manager</span>    <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getService</span><span class="p">(</span><span class="s1">&#39;odm&#39;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>      <span class="nv">$manager</span><span class="o">-&gt;...</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Repositories</h2>

<p>At this point, after boostrapping the environment and creating your first entity,
you might want to play with the repository in your controllers, to
manipulate and retrieve collections:</p>

<figure class='code'><figcaption><span>PROJECT/Controller/User.php</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$manager</span>      <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">getService</span><span class="p">(</span><span class="s1">&#39;odm&#39;</span><span class="p">);</span>
</span><span class='line'><span class="nv">$userRepository</span> <span class="o">=</span> <span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">getRepository</span><span class="p">(</span><span class="s1">&#39;PROJECT\Entity\User&#39;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>then, with the repository, you can start retrieving objects:</p>

<figure class='code'><figcaption><span>Using the repository</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// find all users</span>
</span><span class='line'><span class="nv">$userRepository</span><span class="o">-&gt;</span><span class="na">findAll</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// find one user given its RID</span>
</span><span class='line'><span class="nv">$userRepository</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nv">$rid</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// find all users with the nick &quot;overlord&quot;</span>
</span><span class='line'><span class="nv">$userRepository</span><span class="o">-&gt;</span><span class="na">findByNick</span><span class="p">(</span><span class="s2">&quot;overlord&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// find the user with the nick &quot;TheOnlyOverlord&quot;</span>
</span><span class='line'><span class="nv">$userRepository</span><span class="o">-&gt;</span><span class="na">findOneByNick</span><span class="p">(</span><span class="s2">&quot;TheOnlyOverlord&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// find jack&#39;s wife</span>
</span><span class='line'><span class="nv">$jack</span>  <span class="o">=</span> <span class="nv">$userRepository</span><span class="o">-&gt;</span><span class="na">findOneByName</span><span class="p">(</span><span class="s2">&quot;Jack&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nv">$wifey</span> <span class="o">=</span> <span class="nv">$userRepository</span><span class="o">-&gt;</span><span class="na">findOneBySpouse</span><span class="p">(</span><span class="nv">$jack</span><span class="p">);</span> <span class="c1">// spouse is an attribute of type &quot;link&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>and it&rsquo;s not over, since you can, of course, add
<strong>custom repository classes</strong>.</p>

<p>Custom repositories must be located in the entity&rsquo;s folder
and follow the naming convention <code>EntitytheymapRepository</code>:
for our <code>User</code> entity, we would need to create a <code>UserRepository</code>
class in <code>%base-dir%/src/PROJECT/Entity/</code>:</p>

<figure class='code'><figcaption><span>PROJECT\Entity\UserRepository</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">namespace</span> <span class="nx">PROJECT\Entity</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Doctrine\ODM\OrientDB\Repository</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nc">UserRepository</span> <span class="k">extends</span> <span class="nx">Repository</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>    <span class="sd">/**</span>
</span><span class='line'><span class="sd">     * Retrieves a random user.</span>
</span><span class='line'><span class="sd">     * </span>
</span><span class='line'><span class="sd">     * @return \PROJECT\Entity\User</span>
</span><span class='line'><span class="sd">     */</span>
</span><span class='line'>    <span class="k">public</span> <span class="k">function</span> <span class="nf">findRandomUser</span><span class="p">()</span>
</span><span class='line'>    <span class="p">{</span>
</span><span class='line'>        <span class="k">return</span> <span class="nb">array_rand</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">findAll</span><span class="p">());</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>so then you can call your new methods over repositories:</p>

<figure class='code'><figcaption><span>Using custom repositories</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">getRepository</span><span class="p">(</span><span class="s1">&#39;PROJECT\Entity\User&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">findRandomUser</span><span class="p">();</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Can I haz raw queries?</h2>

<p>Entities and repositories are good, but what about
adding some <code>SQL+</code><sup id='fnref:2'><a href='#fn:2' rel='footnote'>2</a></sup> to the mix?</p>

<p>That&rsquo;s very easy, thanks to the <strong>query builder</strong>
that&rsquo;s packed with the ODM:</p>

<figure class='code'><figcaption><span>Example queries</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="k">use</span> <span class="nx">Doctrine\OrientDB\Query\Query</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// instantiate a query object</span>
</span><span class='line'><span class="nv">$query</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Query</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// simple SELECT</span>
</span><span class='line'><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">))</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;nick = ?&#39;</span><span class="p">,</span> <span class="nv">$nick</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// throwing some spice into the mix</span>
</span><span class='line'><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">orWhere</span><span class="p">(</span><span class="s1">&#39;attribute = ?&#39;</span><span class="p">,</span> <span class="nv">$attribute</span><span class="p">)</span>
</span><span class='line'>    <span class="o">-&gt;</span><span class="na">orWhere</span><span class="p">(</span><span class="s1">&#39;(this IS NULL OR that IS NOT NULL)&#39;</span><span class="p">)</span>
</span><span class='line'>    <span class="o">-&gt;</span><span class="na">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</span><span class='line'>    <span class="o">-&gt;</span><span class="na">orderBy</span><span class="p">(</span><span class="o">...</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// SELECTing a single record</span>
</span><span class='line'><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$rid</span><span class="p">));</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// SELECTing two records</span>
</span><span class='line'><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nv">$rid1</span><span class="p">,</span> <span class="nv">$rid2</span><span class="p">));</span>
</span></code></pre></td></tr></table></div></figure>


<p>When you manipulate the <code>$query</code> object you are basically
creating an SQL query with an object-oriented fluent interface;
to eventually execute the query, just pass the object to
the <code>Manager</code>:</p>

<figure class='code'><figcaption><span>Executing a query</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$query</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Query</span><span class="p">();</span>
</span><span class='line'><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">from</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">))</span><span class="o">-&gt;</span><span class="na">where</span><span class="p">(</span><span class="s1">&#39;gender = ?&#39;</span><span class="p">,</span> <span class="s2">&quot;male&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$males</span> <span class="o">=</span> <span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="nv">$query</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Point being, how do you save data?</h2>

<p>Since persistence is not already handled by the <code>Manager</code>,
you will need to use raw queries for now:</p>

<figure class='code'><figcaption><span>Saving data</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='php'><span class='line'><span class="cp">&lt;?php</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$user</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
</span><span class='line'>  <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Jack&#39;</span>
</span><span class='line'><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$query</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Query</span><span class="p">();</span>
</span><span class='line'><span class="nv">$query</span><span class="o">-&gt;</span><span class="na">insert</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">into</span><span class="p">(</span><span class="s1">&#39;user&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">fields</span><span class="p">(</span><span class="nb">array_keys</span><span class="p">(</span><span class="nv">$user</span><span class="p">))</span><span class="o">-&gt;</span><span class="na">values</span><span class="p">(</span><span class="nv">$user</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="nv">$query</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>From the trenches</h2>

<p>We&rsquo;ve been very active since a couple months,
and we&rsquo;ve actually been able to roll out some major
bugfixes and improvements (more than 10 in the last
few weeks):</p>

<ul>
<li>repositories <a href="https://github.com/doctrine/orientdb-odm/issues/138">filtering by multiple criterias</a></li>
<li><a href="https://github.com/doctrine/orientdb-odm/issues/139">custom repository</a> classes</li>
<li>added ability to <a href="https://github.com/doctrine/orientdb-odm/issues/141">map timestamps</a> as DateTime objects</li>
<li>unable to <a href="https://github.com/doctrine/orientdb-odm/issues/144">update attributes if they are a collection</a></li>
<li>support for <a href="https://github.com/doctrine/orientdb-odm/commit/cbd9c3250d1fd6fc7ec1f39566b91d1f0e1531f2">INSERTing collections</a></li>
<li>proxy classes dont <a href="https://github.com/doctrine/orientdb-odm/issues/147">import signatures</a></li>
<li><code>findBy*</code> and <code>findOneBy*</code> <a href="https://github.com/doctrine/orientdb-odm/issues/149">&ldquo;magic&rdquo; methods</a></li>
<li><a href="https://github.com/doctrine/orientdb-odm/issues/150">fetchplans in <code>find*</code></a> methods of repositories</li>
<li>following <code>SQL+</code>, added <a href="https://github.com/doctrine/orientdb-odm/issues/99"><code>REBUILD INDEX</code> command</a></li>
</ul>


<p>I would not advise you to install one of the old tags,
or even the last one, which brings the namespace
changes for the incubation in the Doctrine
organization, but to install it directly from master
via composer:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="s2">&quot;doctrine/orientdb-odm&quot;</span><span class="err">:</span> <span class="s2">&quot;dev-master&quot;</span><span class="err">,</span>
</span></code></pre></td></tr></table></div></figure>


<p>as we are constantly doing bugfixes and so on
(I would day you would get an update &ndash; at least &ndash;
every week).</p>

<p>That is it, now <strong>start playing around!</strong></p>

<div class="footnotes">
<span>
Notes
</span>
    <ol>
        <li id='fn:1'>Be aware that if you are retrieving a property which is NULL in the DB and you don&#8217;t declare it as NULLable, an exception will be thrown (and there is an issue to improve the exception message https://github.com/doctrine/orientdb-odm/issues/152) <a href='#fnref:1' rev='footnote'>↩</a></li><li id='fn:2'>OrientDB&#8217;s QL is called SQL+, as it looks like SQL but has some major improvements, as it&#8217;s very developer-friendly <a href='#fnref:2' rev='footnote'>↩</a></li>
    </ol>
</div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Please welcome the Doctrine2 OrientDB ODM]]></title>
    <link href="http://odino.org/please-welcome-the-doctrine-orientdb-odm/"/>
    <updated>2012-12-28T23:12:00+04:00</updated>
    <id>http://odino.org/please-welcome-the-doctrine-orientdb-odm</id>
    <content type="html"><![CDATA[<p>It took almost 2 years from the
<a href="https://github.com/doctrine/orientdb-odm/commit/65929ec57a2e2cb1f4af034d722e17b5339b9d48">first commit</a>,
but <a href="http://www.orientdb.org/">OrientDB</a>&rsquo;s <a href="https://github.com/doctrine/orientdb-odm">PHP ODM</a>
has been finally moved to the
<a href="https://github.com/doctrine/">Doctrine</a>
organization.</p>

<!-- more -->


<h2>New daddy</h2>

<p>I&rsquo;ve blogged <a href="http://odino.org/blog/categories/orientdb/">so many times</a>
about an imminent
integration into the Doctrine ecosystem, but
due to the workload of our contributors and
some major issues we wanted to solve before this,
we were only able to seriously approach the
Doctrine team today.</p>

<p>This is a very good news, as we will be able
to take advantage of the experience of all the
doctrine contributors as well as have a bigger stage
where we can show the ODM: the biggest part of the
ODM is still pending (persistence), but
HTTP binding, query builder and object
hydration are working like a charm, and the few
bugs that we face in <a href="https://github.com/odino/sharah">real-world scenarios</a>
are solved in a matter of minutes.</p>

<h2>And now?</h2>

<p>All the namespaces have been changed, so the old
<code>Congow\Orient</code> has been replaced by
<code>Doctrine\ODM\OrientDB</code> and <code>Doctrine\OrientDB</code>:
if you were already using the library, you will need
to work on the migration a bit.</p>

<p>You may also want to have a look at the new
<a href="https://packagist.org/packages/doctrine/orientdb-odm">Packagist page</a>,
as it contains the references to the new repository.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Video introduction about NoSQL graph databases and OrientDB]]></title>
    <link href="http://odino.org/video-introduction-about-nosql-graph-databases-and-orientdb/"/>
    <updated>2012-12-08T15:56:00+04:00</updated>
    <id>http://odino.org/video-introduction-about-nosql-graph-databases-and-orientdb</id>
    <content type="html"><![CDATA[<p>During October of the last year, me and my ex-colleague David
took some time off to partecipate to the <a href="http://phpcon.pl/2011/pl/">polish PHP Conference</a>
with a talk about graph databases, OrientDB and how to integrate it
in PHP.</p>

<!-- more -->


<p>I recently found out that the videos recorded at the conference
finally made it to Vimeo: enjoy some tarzan english!</p>

<iframe src="http://player.vimeo.com/video/53451968?badge=0" width="720" height="600" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>


<p> <p><a href="http://vimeo.com/53451968">Alessandro Nadalin, David Funaro: Graph databases and PHP: time for serious stuff</a> from <a href="http://vimeo.com/user10854134">PLUG</a> on <a href="http://vimeo.com">Vimeo</a>.</p></p>
]]></content>
  </entry>
  
</feed>
