<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>melp.nl</title>
	<atom:link href="http://melp.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://melp.nl</link>
	<description>A geek&#039;s outlet on open source, web development and related stuff</description>
	<lastBuildDate>Sun, 13 May 2012 17:55:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>PHPUnit: Closures as dataProviders help with repetitive code</title>
		<link>http://melp.nl/2012/05/phpunit-closures-as-dataproviders-help-with-repetitive-code/</link>
		<comments>http://melp.nl/2012/05/phpunit-closures-as-dataproviders-help-with-repetitive-code/#comments</comments>
		<pubDate>Sun, 13 May 2012 17:20:58 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[FP]]></category>
		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=694</guid>
		<description><![CDATA[Since PHP5.3 we have closures. The concept behind closures is unbelievably powerful, and even though PHP has struggled with the concept of typing, callables and whatnot, the main concept of closures remains: passing logic in stead of data, or even: as if it were data. In my previous post I argued that functional and declarative [...]]]></description>
			<content:encoded><![CDATA[<p>Since PHP5.3 we have closures. The concept behind closures is unbelievably powerful, and even though PHP has struggled with the concept of typing, callables and whatnot, the main concept of closures remains: passing logic in stead of data, or even: as if it were data. In my <a href="http://melp.nl/2012/01/declarative-is-the-new-black/">previous post</a> I argued that functional and declarative programming will prevail over pure OO on the long run. This post proves another example of why.</p>

<p><span id="more-694"></span></p>

<p>One simple example of proving closures&#8217; power is in the concept of PHPUnit&#8217;s dataProviders. A data provider provides data to a test, in which a test consumes the data and asserts that the code is ok. As data can also be closures, you can also alter the execution of the tests appropriate to the data you want to test, without actually altering the test. I&#8217;ll show you an example <sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>.</p>

<p>Consider the following class:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FooBuilder <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> add<span style="color: #009900;">&#40;</span><span style="color: #000088;">$property</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> remove<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <a href="http://www.php.net/unset"><span style="color: #990000;">unset</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$property</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> build<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">properties</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>

<p>Though there is no obvious use case for this class, the pattern is simple: we have some builder with methods to create something, and the builder provides a <a href="http://martinfowler.com/bliki/FluentInterface.html">fluent interface</a> to do so. This is also very common for query builders, including jQuery. With such a pattern, there is a meriad of possibilities to use this code, with at least as much edge conditions. The order in which the methods are executed might influence the way it is implemented, what would happen if properties are added, removed and then removed again, etc etc.</p>

<p>These edge cases won&#8217;t be displayed by simple use of code coverage, and introducing separate tests for each of these conditions would be cumbersome, having to introduce separate test names for each of the cases.</p>

<p>So, here&#8217;s where closures come in handy. We&#8217;ll simply call the test testBuildingWithFluentInterfaceWillResultInExpectedArray:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FooBuilderTest <span style="color: #000000; font-weight: bold;">extends</span> \PHPUnit_Framework_TestCase <span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @dataProvider builderCases
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> testBuildingWithFluentInterfaceWillResultInExpectedArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$expected</span><span style="color: #339933;">,</span> <span style="color: #000088;">$builderImpl</span><span style="color: #339933;">,</span> <span style="color: #000088;">$description</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$builder</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FooBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$expectedArray</span><span style="color: #339933;">,</span> <span style="color: #000088;">$builderImpl</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$builder</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$description</span> failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> builderCases<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
            <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
                <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>FooBuilder <span style="color: #000088;">$builder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$builder</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;Initial construction&quot;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #666666; font-style: italic;">/* .... */</span>
            <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
                <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'c'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'x'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'y'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'z'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>FooBuilder <span style="color: #000088;">$builder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$builder</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'b'</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'z'</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'c'</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;Adding different properties multiple times in a different order&quot;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #666666; font-style: italic;">/* .... */</span>
            <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
                <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>FooBuilder <span style="color: #000088;">$builder</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$builder</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'y'</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'z'</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #339933;">-&gt;</span><span style="color: #004000;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'x'</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;Adding different properties multiple times in a different order&quot;</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre>

<p>This way, you´ll have a clear view of all the test cases and expected output, without having to only provide data through your data provider, but logic too. Clear separation of use case and consumer logic makes the test very clear and easily extendable.</p>

<p>Hope this offers more food for creativity in your tests <img src='http://melp.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>The example is available as a <a href="https://gist.github.com/2689365">GIST</a>&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2012/05/phpunit-closures-as-dataproviders-help-with-repetitive-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Declarative is the new black.</title>
		<link>http://melp.nl/2012/01/declarative-is-the-new-black/</link>
		<comments>http://melp.nl/2012/01/declarative-is-the-new-black/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 23:34:06 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[FP]]></category>
		<category><![CDATA[languages]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=662</guid>
		<description><![CDATA[The paradigm shift in the purest sense of the expression. The old paradigm makes way for new ones. And declarative is the new one. Well&#8230;, not new new &#8230; The &#8220;old&#8221; paradigm: Procedural Procedural programming is a form of imperative programming in which you define &#8220;procedures&#8221; or &#8220;subroutines&#8221;1 that encapsulate a certain set of operations, [...]]]></description>
			<content:encoded><![CDATA[<p>The paradigm shift in the purest sense of the expression. The old paradigm makes way for new ones. And declarative is the new one. Well&#8230;, not <em>new</em> <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)">new</a> &#8230;
<span id="more-662"></span></p>

<h2>The &#8220;old&#8221; paradigm: Procedural</h2>

<p>Procedural programming is a form of imperative programming in which you define &#8220;procedures&#8221; or &#8220;subroutines&#8221;<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> that encapsulate a certain set of operations, and accept parameters or &#8220;arguments&#8221; to that set of operations. A very simple example of this, is a procedure that will return the sum of two arguments. In C:</p>

<pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> sum<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>

<p>We declare a procedure that accepts two integer arguments <code>a</code> and <code>b</code> and return an integer sum of the arguments. Simple, efficient, clear. If we want the sum to be present in a third variable, we simply <em>execute</em> the procedure:</p>

<pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> sum<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> c<span style="color: #339933;">;</span>
    c <span style="color: #339933;">=</span> sum<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;c now contains %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* Will output: &quot;c now contains 30&quot; */</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>

<h3>Procedural is <em>imperative</em></h3>

<p>The procedural style of programming is imperative. Imperative means that you program the execution flow. You decide what pieces of code gets executed by using control structures, loops, procedures, etcetera, and the <em>state</em> of the variables (the &#8220;context&#8221; or &#8220;environment&#8221;) ultimately define the execution flow. This is very intuitive, since you can read most imperative languages as &#8220;if something happens, you do this and that&#8221;. More or less like an instruction video for driving. <q>If someone comes from the right, yield.</q></p>

<h2>The &#8220;newer&#8221; paradigm: Object Oriented</h2>

<p>Another, still imperative, paradigm is Object Oriented programming. Though it is not strictly different from procedural programming, it introduces a new concept: binding data and methods to a single object, that represent one single previously initialized state of the object. This is called instantiation and means that multiple objects of one type can exist in memory simultaneously. Each of these objects have their own methods that act only on the state of that particular object.</p>

<p>There are two concepts that are new in OO style programming, first of which is encapsulation, where the outside world does not need to know what is going on within some object&#8217;s internals. Second is polymorphism, which means that an object can explicitly express that it is capable of some sort of functionality and may implement it in a very different way than another object which exposes the same behaviour. Huh? Bear with me. Here&#8217;s an example.</p>

<pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AreaCalculator <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">float</span> total <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> add<span style="color: #009900;">&#40;</span>HasArea obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        total <span style="color: #339933;">+=</span> obj.<span style="color: #006633;">getArea</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> subtract<span style="color: #009900;">&#40;</span>HasArea obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        total <span style="color: #339933;">-=</span> obj.<span style="color: #006633;">getArea</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> HasArea <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arectangle+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Rectangle</span></a> <span style="color: #000000; font-weight: bold;">implements</span> HasArea <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> _side1<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> _side2<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arectangle+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Rectangle</span></a><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> side1, <span style="color: #000066; font-weight: bold;">double</span> side2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _side1 <span style="color: #339933;">=</span> side1<span style="color: #339933;">;</span>
        _side2 <span style="color: #339933;">=</span> side2<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> _side1 <span style="color: #339933;">*</span> _side2<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Circle <span style="color: #000000; font-weight: bold;">implements</span> HasArea <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">double</span> _radius<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Circle<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">double</span> radius<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _radius <span style="color: #339933;">=</span> radius<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> getArea<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">PI</span> <span style="color: #339933;">*</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Amath+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Math</span></a>.<span style="color: #006633;">PI</span> <span style="color: #339933;">*</span> _radius<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Example <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        AreaCalculator calc <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AreaCalculator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// our yard is 10 by 4.2 meters</span>
        calc.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Arectangle+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Rectangle</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10.0</span>, <span style="color: #cc66cc;">4.2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// But it has a pool, 3 meter in diameter</span>
        calc.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Circle<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The square metrage we need to buy grass seed for is &quot;</span> <span style="color: #339933;">+</span> calc.<span style="color: #006633;">total</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#125;</span></pre>

<p>The result:</p>

<pre><code>The square metrage we need to buy grass seed for is 27.195593
</code></pre>

<p>The example shows that two different objects have some common characteristic. In this case, they both know how they should produce their own area. The implementation is bound to the meaning of the class itself, since the area of a circle is calculated differently from the area of a rectangle. So, they both provide their own implementation to do so. Since the two different implementations are tightly related, it does make sense that we defined a common interface for it, so the actual consumer of the objects (the calculator) does not need to know they are totally different kinds of objects. All it needs to know is that the object is capable of producing it&#8217;s own area. That&#8217;s it. Well, if you know OO you know this already, but you might not have known that this phenomenon is called <em>polymorphism</em>. The fact that we hide the properties <code>_side1</code>, <code>_side2</code> and <code>_radius</code> from the outside world is <em>encapsulation</em>. Especially polymorphism is something that is hard to achieve with a purely procedural language.</p>

<p>It strikes to see that OO versus procedural, however still imperative, is much more descriptive. It describes natural things in a natural way. A pear is a fruit, an apple is a fruit, they both have some specific characteristics, but they share a lot of common properties and probably operations, such as <code>feed()</code> or <code>ferment()</code>. The programming style still is imperative as it follows the execution flow, but much of the organisation of the code revolves around typing and classifying, so it is more descriptive even though not yet strictly declarative.</p>

<p>It would also appear that OO moves focus from processing and decision-making to behaviour. Describing behaviour is typically part of the object model, because behaviour defines how objects interact and how they depend on each other. This is curious: the data no longer plays the main role in the design of the program, the behaviour does. Let&#8217;s stash that thought.</p>

<h1>Declarative programming</h1>

<p>The main difference between declarative and imperative programming, is that declarative means that you express what something <em>is</em>, rather than what something should <em>do</em>. A common example of declarative programming is build scripts. I&#8217;ll use a Makefile for this example.</p>

<p>As we define a task within a Makefile, we can specify what tasks it is dependent on. As this simply declares what the task does, without actually doing any decision-making within the program this is typically a declarative approach. The tasks get declared as what set of executions the represent. The decisions that are needed to make for execution of the tasks is made by the platform:</p>

<pre class="makefile" style="font-family:monospace;"># building the app depends on mysum.c (the example above)
# if mysum alters, the app should alter too
mysum: mysum.c
    gcc mysum.c -o mysum</pre>

<p>The approach is declarative. We do no checks, we just tell what needs to be done to generate the app. See the entire Makefile for the above examples on <a href="https://github.com/drm/declarative-is-the-new-black/blob/master/Makefile">the github repo</a></p>

<p>Another relatively simple example is Haskell. Without explaining too much about functional programming, the main idea of Haskell is that you declare anything you need. Declaring things does not mean executing them. For example:</p>

<pre class="haskell" style="font-family:monospace;">l <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:repeat"><span style="font-weight: bold;">repeat</span></a> <span style="color: red;">5</span></pre>

<p>What happens here is that we declare a list that is made up of an infinite list of 5s. So, how come nothing happens? If the list is infinite, how can it exist? It exists because it is declared. It only gets <em>meaning</em> as soon as it is used in context, which means that the meaning is defined by <em>evaluating</em> the list in it&#8217;s context. When (or even <em>if</em>) this happens. The problem is, imperative programming leads to the assumption that everything that is actually declared, will be evaluated <em>as soon as execution flow enters</em>. This is different in declarative languages, simply because they do not have execution flow. Or, at least, the execution flow is irrelevant. Evaluation is delayed until the point it is required. That is called lazy evaluation. And we like lazy, because lazy means CPU cycles and memory is only consumed as needed.</p>

<h2>The best of both worlds</h2>

<p>Languages now tend to shift to a mixture of (imperative) Object Oriented and (declarative) functional. We see that in Python, Ruby, Erlang and maybe even the best example of them all: Scala. What&#8217;s even more surprising is that in PHP world, which isn&#8217;t the fastest cat of the bunch, the seed is actually planted. Symfony2 introduces declarative paradigms with an <em>inversion of control</em> service container. That is declarative. You simply declare what service depends on another. PHP code is generated to have these dependencies resolve gracefully and lazily and we no longer need to think about execution flow for this. The services and their dependencies are only <em>declared</em>, and the (imperative) code for initializing the services is generated under water. Consequentally, requesting the services from the container will have the services be initialized lazily.</p>

<p>Another fine example is NodeJS. Since the entire model is built around one simple event loop, all behaviour is essentially defined declaratively.</p>

<h2>So, what&#8217;s next?</h2>

<p>Learning imperative languages will become obsolete, just as learning assembly has. You will need to learn how to define behaviour and dependencies, but no longer what needs to be done to get the entire thing rolling. As languages get more and more declarative capabilities and functional programming paradigm features, the imperative idea will only be a resort where the awesomest go.</p>

<p>Or&#8230;. will it? Compiling code introduces overhead. There is no compiler that is able to optimize all and every situation as well as an advanced programmer could. This holds true for, say, a C to Assembly compiler, and would most certainly be the case for compiling declarative approaches. Because ultimately, the little pieces of hardware that get the work done, are not that professed in languages. They only do ones and zeros, and have no concept of meaning. This means that bringing the code from the keyboard to the processor&#8217;s registry will become further and further apart. And if that&#8217;s the case, there&#8217;s only two ways to go. Either software will become even less efficient, or the hardware needs to get smarter. Pick one.</p>

<p><em>NB: The code in this post is on <a href="https://github.com/drm/declarative-is-the-new-black">github</a></em></p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>I&#8217;m purposefully calling these &#8220;procedures&#8221; and not &#8220;functions&#8221;, as functions might confuse with functions in functional programming.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2012/01/declarative-is-the-new-black/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Symfony2 + PHPCR + Doctrine2 + Jackalope recipe</title>
		<link>http://melp.nl/2011/07/symfony2-phpcr-doctrine2-jackalope-recipe/</link>
		<comments>http://melp.nl/2011/07/symfony2-phpcr-doctrine2-jackalope-recipe/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 21:19:03 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[content repository]]></category>
		<category><![CDATA[doctrine2]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[symfony2]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=644</guid>
		<description><![CDATA[Lately I&#8217;ve followed some developments in the Symfony2 corner of the PHP community with great interest. One of the most enticing developments is the usage of a Content Repository as a backend for your CMS. There is some work being done on the Symfony CMF, combining Symfony2, Doctrine2, PHPCR and Jackalope into a set of [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve followed some developments in the Symfony2 corner of the PHP community with great interest. One of the most enticing developments is the usage of a Content Repository as a backend for your CMS. There is some work being done on the <a href="https://github.com/symfony-cmf/">Symfony CMF</a>, combining <a href="http://symfony.com">Symfony2</a>, <a href="http://doctrine-project.org/">Doctrine2</a>, <a href="https://github.com/phpcr">PHPCR</a> and <a href="https://github.com/jackalope/jackalope">Jackalope</a> into a set of tools for building CMS&#8217;es based on a Content Repository backend. I didn&#8217;t get anything of the CMF to run yet, so I decided to dive in to tying these separate techniques together myself, and get a little proof-of-concept working. <a href="http://github.com/drm/MelpPagesBundle">Here&#8217;s the code</a>, and here&#8217;s the recipe: <span id="more-644"></span></p>

<p>I do not intend to convince you to use a content repository. I think the matters speak for themselves. If you know all the limitations classical setups using relational databases have, you&#8217;ll probably see a future in NoSQL databases, as they are affectionately called. MongoDB and CouchDB never convinced me the way the JCR specification did. Google the specs and read them yourself and know what I mean.</p>

<h2>1. Get a CR backend running</h2>

<p>I chose <a href="http://jackrabbit.apache.org">Apache Jackrabbit</a>, a Java Content Repository (JCR), to do this. As pointed out on the <a href="https://github.com/jackalope">Jackalope</a> website, you&#8217;ll need a patched version of Jackrabbit to get things compatible with PHPCR. Actually, it is just version 2.2 with a backport patch of the 2.3 branch applied to it <sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, so don&#8217;t fear using it. I&#8217;m told it is pretty stable.</p>

<ol>
<li><a href="http://patched-jackrabbit.s3-website-eu-west-1.amazonaws.com/">Get the patched Jackrabbit here</a>.</li>
<li><p>Run the the server</p>

<pre class="shell" style="font-family:monospace;">java -jar jackrabbit-standalone-*.jar</pre></li>
</ol>

<p>This will run jackrabbit at port 8080. Visit your jackrabbit front end at <a href="http://localhost:8080/">http://localhost:8080/</a>. You&#8217;ll be asked a username and password, but you can leave them blank. By default, there is no access control. If you see a page about jackrabbit, it works.</p>

<h2>2. Install Symfony</h2>

<p>I&#8217;m assuming you know how to do this. If you don&#8217;t, follow <a href="http://symfony.com/doc/current/quick_tour/">the instructions at symfony.com</a>. The short version is:</p>

<ol>
<li><a href="http://symfony.com/download">Download Symfony 2.0 latest version</a></li>
<li>Extract the archive in your document root, say <code>/var/www/</code></li>
<li>Visit <a href="http://localhost/Symfony/web/config.php">/Symfony/web/config.php</a> and follow the instructions</li>
<li>Visit <a href="http://localhost/Symfony/web/app_dev.php">/Symfony/web/app_dev.php</a> to determine if the installation worked</li>
</ol>

<h2>3. Install PHPCR, Jackalope, DoctrinePHPCR ODM and the DoctrinePHPCRBundle</h2>

<p>Go to your <code>vendor</code> dir and clone the PHPCR libraries from github:</p>

<pre class="shell" style="font-family:monospace;">cd /var/www/Symfony/vendor
git clone git://github.com/phpcr/phpcr.git
git clone git://github.com/jackalope/jackalope.git
git clone git://github.com/doctrine/phpcd-odm.git
cd bundles/Symfony/Bundle
git clone git://github.com/symfony-cmf/DoctrinePHPCRBundle.git</pre>

<h2>4. Configure the autoloader</h2>

<p>Open <code>app/autoload.php</code> and configure the namespaces for the downloaded libraries:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000088;">$loader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registerNamespaces</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #666666; font-style: italic;">/* ... */</span>
    <span style="color: #0000ff;">'Jackalope'</span>             <span style="color: #339933;">=&gt;</span> __DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/../vendor/jackalope/src'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'PHPCR'</span>                 <span style="color: #339933;">=&gt;</span> __DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/../vendor/phpcr/src'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'Doctrine\\ODM\\PHPCR'</span>  <span style="color: #339933;">=&gt;</span> __DIR__<span style="color: #339933;">.</span><span style="color: #0000ff;">'/../vendor/phpcr-odm/lib/'</span><span style="color: #339933;">,</span>
    <span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre>

<h2>5. Add the DoctrinePHPCRBundle to your AppKernel</h2>

<p>Open <code>app/AppKernel.php</code> and add the bundle to the <code>registerBundles()</code> call:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerBundles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$bundles</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #666666; font-style: italic;">/* ... */</span>
        <span style="color: #000000; font-weight: bold;">new</span> Symfony\Bundle\DoctrinePHPCRBundle\DoctrinePHPCRBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #009900;">&#125;</span></pre>

<h2>6. Configure the ODM default service</h2>

<p>Add the following section to your <code>app/config/config.yml</code> file:</p>

<pre class="yaml" style="font-family:monospace;">doctrine_phpcr:
    session:
        backend:
            url: http://localhost:8080/server/
        workspace: default
        username: ''
        password: ''
    odm:
        auto_mapping: true</pre>

<h2>7. Set up the Doctrine system types</h2>

<p>To have Jackrabbit understand some type internals Doctrine uses for document mapping, we&#8217;ll need to make the content repository aware of these types. There is a console command for that, to make things easy:</p>

<pre class="shell" style="font-family:monospace;">app/console doctrine:phpcr:register-system-node-types</pre>

<p>If you skip this step, persisting documents with a Doctrine DocumentManager will fail with a Jackrabbit error message complaining about <code>phpcr</code> being an unknown namespace.</p>

<h2>8. Set up a PagesBundle</h2>

<p>You should, of course, change the class and namespace names to whatever you wish. Here&#8217;s what I used:</p>

<p>In <code>src/Melp</code>, I created a <a href="http://github.com/drm/MelpPagesBundle">PagesBundle</a>. This bundle will hold my routes for editing, creating, deleting and showing pages from the content repository. It will also hold the Document model classes I will be using for mapping documents to objects:</p>

<pre class="shell" style="font-family:monospace;">cd /var/www/Symfony/src
mkdir -p Melp/PagesBundle/{Controller,Document,Resources{,/views}}</pre>

<p>This creates the bundle&#8217;s directory structure. There is also a console command for it, which can scaffold the directory structure and some classes for you:</p>

<pre class="shell" style="font-family:monospace;">cd /var/www/Symfony
app/console generate:bundle</pre>

<p>The MelpPagesBundle class will be in <code>/var/www/Symfony/src/Melp/PagesBundle/MelpPagesBundle.php</code>, and contains the following code:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">namespace</span> Melp\PagesBundle<span style="color: #339933;">;</span>
&nbsp;
use Symfony\Component\HttpKernel\Bundle\Bundle<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MelpPagesBundle <span style="color: #000000; font-weight: bold;">extends</span> Bundle <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre>

<p>Add the bundle instance to the AppKernel class&#8217;s bundle initialization routine:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> registerBundles<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$bundles</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #666666; font-style: italic;">/* ... */</span>
        <span style="color: #000000; font-weight: bold;">new</span> Melp\Bundle\PagesBundle\MelpPagesBundle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* ... */</span>
<span style="color: #009900;">&#125;</span></pre>

<h2>9. Set up the controller, routes and view scripts</h2>

<p>I&#8217;ll use Annotations for the controller set up. I was a bit reluctant at first to use annotations for such configuration, but I find it much easier and clear to use annotations than the other configuration options. The only thing we&#8217;ll need is to let the router configuration know the controller exists. We&#8217;ll do that in app/config/routing.yml:</p>

<pre class="yml" style="font-family:monospace;">_pages:
    resource:  &quot;@MelpPagesBundle/Controller/PageController.php&quot;
    type: annotation</pre>

<p>I will also use Twig view scripts. You can of course use PHP view scripts or whatever you prefer, but Twig has had my heart for some time already. The <code>@Template</code> annotation will tell Symfony to render the view resources using the twig template engine as is configured in the default Symfony2 distribution you downloaded. <sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup></p>

<p>The controller class will then look like this:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">namespace</span> Melp\PagesBundle\Controller<span style="color: #339933;">;</span>
&nbsp;
use Symfony\Bundle\FrameworkBundle\Controller\Controller<span style="color: #339933;">;</span>
use Symfony\Component\Routing\Annotation\Route<span style="color: #339933;">;</span>
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> PageController <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @Route(&quot;/&quot;)
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">redirect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">generateUrl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'view'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'path'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'home'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @Route(&quot;/{path}&quot;, requirements={&quot;path&quot; = &quot;(?!(edit|delete|create)).+&quot;}, name=&quot;view&quot;)
     * @Template
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> viewAction<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @Route(&quot;/edit/{path}&quot;, requirements={&quot;path&quot; = &quot;.*&quot;}, name=&quot;edit&quot;)
     * @Template
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> editAction<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @Route(&quot;/create/{path}&quot;, requirements={&quot;path&quot; = &quot;.*&quot;}, name=&quot;create&quot;)
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> createAction<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @Route(&quot;/delete/{path}&quot;, requirements={&quot;path&quot; = &quot;.*&quot;}, name=&quot;delete&quot;)
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> deleteAction<span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>

<p>Since I want to have the routes reflect the path in the content repository, I use a wildcard pattern for the paths, so they may contain slashes. You can use PCRE in your routes, which I used to exclude the <code>/edit</code>, <code>/create</code> and <code>/delete</code> routes from the default <code>viewAction()</code> to avoid conflicts.</p>

<p>These actions will implement CRUD actions for nodes inside the content repository.</p>

<h2>10. Create the document model class</h2>

<p>To have Doctrine manage my model class, I will need to attach Doctrine annotations to it, so Doctrine will know how to read and write model data from and to the content repository backend. I will use a simple Page class, with <code>title</code> and <code>content</code> properties, wich we will later implement the CRUD for, as layed out in the Controller above.</p>

<p>In <code>src/Melp/PagesBundle/Document</code>, create the model reflecting the &#8216;Page&#8217; data structure we&#8217;ll be using to edit and show pages <sup id="fnref:3"><a href="#fn:3" rel="footnote">3</a></sup>.</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">namespace</span> Melp\PagesBundle\Document<span style="color: #339933;">;</span>
&nbsp;
use Doctrine\ODM\PHPCR\Mapping\Annotations <span style="color: #b1b100;">as</span> PHPCRODM<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * @PHPCRODM\Document(alias=&quot;page&quot;)
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Page
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/** @PHPCRODM\Id */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$path</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** @PHPCRODM\String */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** @PHPCRODM\String */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>

<h2>11. Create a Pages facade and register it as a service</h2>

<p>I created a facade for controlling how pages are persisted and added some logic to that for placing pages in a configured root:</p>

<pre class="yaml" style="font-family:monospace;">services:
    pages:
        class: Melp\PagesBundle\Service\PagesFacade
        arguments:
            - @doctrine_phpcr.odm.default_document_manager
            - 'Melp\PagesBundle\Document\Page'
            - &quot;/pages/&quot;
            - &quot;home&quot;</pre>

<p>The facade exposes the following methods:</p>

<pre><code>public function __construct(\Doctrine\ODM\PHPCR\DocumentManager $dm, $documentClass, $root, $default)
{
}


function createDefault($title = "Homepage")
{
}


function removePath($path)
{
}


function findPath($path)
{
}


function remove($node)
{
}


function persist($node)
{
}
</code></pre>

<p>This saves some clutter in the controller code. The Facade dispatches to the document manager and the document repository for finding and persisting pages.</p>

<h2>12. Create a PageType form class</h2>

<p>Following a best practice not to initialize your forms inside your controller, I created a PageType class in <code>src/Melp/PagesBundle/Form/Type/PageType.php</code>:</p>

<pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">namespace</span> Melp\PagesBundle\Form\Type<span style="color: #339933;">;</span>
&nbsp;
use \Symfony\Component\Form\AbstractType<span style="color: #339933;">;</span>
use \Symfony\Component\Form\FormBuilder<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> PageType <span style="color: #000000; font-weight: bold;">extends</span> AbstractType
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> buildForm<span style="color: #009900;">&#40;</span>FormBuilder <span style="color: #000088;">$builder</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a> <span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$builder</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'required'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'content'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'textarea'</span><span style="color: #009900;">&#41;</span>
        <span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>

<p>This class will be used inside the controller to build the forms for creating new pages and editing existing ones.</p>

<h2>13. Implement the CRUD Controller</h2>

<p>I chose to implement a very, very basic CRUD for the document class. <a href="https://github.com/drm/MelpPagesBundle/blob/master/Controller/PageController.php">The code</a> speaks for itself. I used <a href="https://github.com/drm/MelpPagesBundle/tree/master/Resources/views/Page">twig templates</a> to render the output of the edit, create and view actions, and rely heavy on flash messages and redirects for feedback to the user.</p>

<h2>Summarizing</h2>

<p>Much, much easier than an RDBMS based hierarchical system of pages, much more convenient and easier to control, and crazily scalable. I think there is much future in this kind of set up. So get comfortable with it while I&#8217;ll do the same, and I hope to get even deeper into the subject some next blog.</p>

<p>I hope this post helped you some further if you got stuck, and is of some inspiration if you want to get started. All tips and comments are appreciated.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p><a href="https://github.com/jackalope/jackalope/wiki/Running-a-jackrabbit-server">https://github.com/jackalope/jackalope/wiki/Running-a-jackrabbit-server</a>&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:2">
<p>See <code>app/config/config.yml</code>, the <code>framework.templating</code> section&#160;<a href="#fnref:2" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:3">
<p>A small introduction is given to this in <a href="[https://github.com/doctrine/phpcr-odm">github.com/doctrine/phpcr-odm</a>&#160;<a href="#fnref:3" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/07/symfony2-phpcr-doctrine2-jackalope-recipe/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Failure in a bash script usually means failure of the script</title>
		<link>http://melp.nl/2011/07/failure-in-a-bash-script-usually-means-failure-of-the-script/</link>
		<comments>http://melp.nl/2011/07/failure-in-a-bash-script-usually-means-failure-of-the-script/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 18:49:50 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=632</guid>
		<description><![CDATA[So, when you write a bash script that does a certain amount of tasks for you, but you don&#8217;t want the script to keep running after some command inside the script failed, simply add a line to the script. set -e Quoting the man page: -e errexit Exit immediately if a simple command exits with [...]]]></description>
			<content:encoded><![CDATA[<p>So, when you write a bash script that does a certain amount of tasks for you, but you don&#8217;t want the script to keep running after some command inside the script failed, simply add a line to the script. <span id="more-632"></span></p>

<pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> <span style="color: #660033;">-e</span></pre>

<p>Quoting the man page:</p>

<blockquote>
  <p><strong><em>-e errexit</em></strong></p>
  
  <p>Exit immediately if a simple command exits with a non-zero, unless the command that fails is part of an until or loop, part of an if statement, part of a &amp;&amp; or || list, if the command&#8217;s return is being inverted using !.</p>
</blockquote>

<p>If you want to reset to normal behaviour, reverse it using:</p>

<pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">set</span> +e</pre>

<p>Very useful, it&#8217;ll save you lot of tedious if&#8217;s and error checking.</p>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/07/failure-in-a-bash-script-usually-means-failure-of-the-script/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apply low priority to low priority processes</title>
		<link>http://melp.nl/2011/07/apply-low-priority-to-low-priority-processes/</link>
		<comments>http://melp.nl/2011/07/apply-low-priority-to-low-priority-processes/#comments</comments>
		<pubDate>Tue, 19 Jul 2011 19:02:59 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=626</guid>
		<description><![CDATA[My transmission client was hogging my machine. Then I realized I never really use process priorities for CPU and/or IO, which is actually a pretty bad thing, considering some processes just are there to get some job done, but don&#8217;t need priority at all. Then I realized I also never use it for backup and [...]]]></description>
			<content:encoded><![CDATA[<p>My transmission client was hogging my machine. Then I realized I never really use process priorities for CPU and/or IO, which is actually a pretty bad thing, considering some processes just are there to get some job done, but don&#8217;t need priority <em>at all</em>. Then I realized I also never use it for backup and such, which could be a problem for the server I&#8217;m running the backup on. <span id="more-626"></span></p>

<p>I already knew of the <code>nice</code> program, with which you can alter the CPU scheduling prioritization of processes. <code>nice</code> usually prefixes a command to start it up with altered &#8216;niceness&#8217;:</p>

<pre class="shell" style="font-family:monospace;">$ nice -n 19 rsync -a ~ /net/share/backup</pre>

<p><code>renice</code> is used to alter an already running process&#8217;s niceness:</p>

<pre class="shell" style="font-family:monospace;">$ renice -n 19 -p $PID # where, of course $PID is de pid of the process you're altering</pre>

<p>Pretty useful, and also pretty common knowledge, as far as I know. But then it dawned on me that CPU priority isn&#8217;t at all what&#8217;s useful here. You want I/O priority. So, a little googling lead me to <code>ionice</code> &#8211; how inventive a name &#8211; with which you can alter process I/O prioritization. Awesome!</p>

<pre class="shell" style="font-family:monospace;">$ ionice -c 2 -n 4 -p 1234 # Give process 1234 &quot;best effort&quot; priority 4</pre>

<p>You should read the man pages of <code>nice</code> and <code>ionice</code> to find out what the parameters mean (just because it&#8217;s a good habit to read man pages, and not blogs <img src='http://melp.nl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ), but the basic usage is as follows:</p>

<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># start a process 'some command with arguments' with a niceness of 19. </span>
<span style="color: #666666; font-style: italic;"># 19 means lowest priority, -19 means highest</span>
<span style="color: #c20cb9; font-weight: bold;">nice</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">19</span> some <span style="color: #7a0874; font-weight: bold;">command</span> with arguments 
&nbsp;
<span style="color: #666666; font-style: italic;"># The process with pid 1234 gets a new niceness of 10, provided you are either root,</span>
<span style="color: #666666; font-style: italic;"># or you are the owner of the process</span>
renice <span style="color: #660033;">-n</span> <span style="color: #000000;">10</span> <span style="color: #660033;">-p</span> <span style="color: #000000;">1234</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The process with pid 1234 gets &quot;Idle&quot; class priority, which means it only gets I/O access</span>
<span style="color: #666666; font-style: italic;"># when no other process needs it.</span>
ionice <span style="color: #660033;">-p</span> <span style="color: #000000;">1234</span> <span style="color: #660033;">-c</span> <span style="color: #000000;">3</span></pre>

<p>Use <code>top</code> and <code>iotop</code> to monitor the usage of CPU and I/O usage respectively. From now on, I&#8217;ll be <em>nice</em> to the kernel and not let him figure out all the prioritization. Wow that&#8217;s cheesy <img src='http://melp.nl/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/07/apply-low-priority-to-low-priority-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automate sudo nano; something I should&#8217;ve done a loooong time ago</title>
		<link>http://melp.nl/2011/07/automate-sudo-nano-something-i-shouldve-done-a-loooong-time-ago/</link>
		<comments>http://melp.nl/2011/07/automate-sudo-nano-something-i-shouldve-done-a-loooong-time-ago/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 18:59:15 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=616</guid>
		<description><![CDATA[Remember those countless times you&#8217;ve edited a file with nano, didn&#8217;t notice that you weren&#8217;t root at the time, and carefully made your configuration changes and saved the file, only to find out that you weren&#8217;t root and you have no rights to modify the file? I do. And it sucks. Big time. And if [...]]]></description>
			<content:encoded><![CDATA[<p>Remember those countless times you&#8217;ve edited a file with nano, didn&#8217;t notice that you weren&#8217;t root at the time, and carefully made your configuration changes and saved the file, only to find out that you weren&#8217;t root and you have no rights to modify the file? <span id="more-616"></span></p>

<p>I do. And it sucks. Big time. And if bash wasn&#8217;t so awesome in helping you tune your shell into what you want for yourself, I probably would&#8217;ve found some configuration in nano to warn me properly. But, bash <em>is</em> awesome, and after all I like tuning my stuff into <strong>my</strong> stuff. So here&#8217;s my solution:</p>

<p>Edit <code>~/.bashrc</code> and add the following lines</p>

<cite class="gist">(GIST: <a href="https://gist.github.com/1087457" target="_blank">1087457</a>)</cite><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #c20cb9; font-weight: bold;">nano</span><span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> 
    <span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">nano</span></span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> <span style="color: #c20cb9; font-weight: bold;">nano</span><span style="color: #000000; font-weight: bold;">`</span>; 
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-w</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-w</span> <span style="color: #ff0000;">&quot;<span style="color: #780078;">`dirname $1`</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">then</span> 
        <span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;$1 is not editable by you. sudo [y/N]? &quot;</span> y
        <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$y</span>&quot;</span> == <span style="color: #ff0000;">&quot;y&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">||</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$y</span>&quot;</span> == <span style="color: #ff0000;">&quot;Y&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #007800;">$nano</span> $<span style="color: #000000; font-weight: bold;">@</span>
    <span style="color: #000000; font-weight: bold;">else</span> 
        <span style="color: #007800;">$nano</span> $<span style="color: #000000; font-weight: bold;">@</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre>

<p><code>source</code> your file, or log out and log back in again.</p>

<pre class="shell" style="font-family:monospace;">$ . ~/.bashrc</pre>

<p>And you&#8217;re done! You&#8217;ll get a nice prompt asking you if you want to sudo to change the file if you do not have privileges to edit the file. Check, done, and done. Gotta love that unix environment.</p>

<pre class="shell" style="font-family:monospace;">$ nano /etc/apt/sources.list
/etc/apt/sources.list is not editable by you. sudo [y/N]?</pre>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/07/automate-sudo-nano-something-i-shouldve-done-a-loooong-time-ago/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Password-less authentication SSH</title>
		<link>http://melp.nl/2011/07/password-less-authentication-ssh/</link>
		<comments>http://melp.nl/2011/07/password-less-authentication-ssh/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 13:52:25 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=588</guid>
		<description><![CDATA[I use this a lot, and you should too. It saves lots of time, but you should also be aware that password-less authentication (and it&#8217;s ease) imposes a security risk on your behalf. You should be very cautious when you&#8217;re connecting to the remote machines (whether they are testing, staging or production environments, in ascending [...]]]></description>
			<content:encoded><![CDATA[<p>I use this a lot, and you should too. It saves lots of time, but you should also be aware that password-less authentication (and it&#8217;s ease) imposes a security risk on your behalf. You should be very cautious when you&#8217;re connecting to the remote machines (whether they are testing, staging or production environments, in ascending order of danger) beacuse there is no longer a password threshold reminding you that you&#8217;re about to do something that you might not have intended for that machine. Nevertheless, it is a timesaver and it simplifies authentication to machines you&#8217;ll often connect to.
<span id="more-588"></span></p>

<p>This is how to do it. First, check if you have an rsa key pair in your homedir:</p>

<pre class="shell" style="font-family:monospace;">$ ls ~/.ssh/id_rsa*</pre>

<p>You should have an <code>id_rsa</code> and an <code>id_rsa.pub</code> file there. If not, you need to generate them, using the following command</p>

<pre class="shell" style="font-family:monospace;">$ ssh-keygen -t rsa</pre>

<p>If you wish, you can add a passphrase for accessing the keys. This is an extra security, which you don&#8217;t really need if you don&#8217;t hand out your private key to anyone. However, if you feel quirky about entering systems without passwords, you can enter the passphrase anyway. In that case, you will need to enter that password in stead of the password on the remote machine when you want to connect.</p>

<p>Use the <code>ssh-copy-id</code> script to copy your public key to the remote machine&#8217;s &#8220;~/.ssh/authorized_keys&#8221; file. You can do this by hand also, but the script makes it a little bit easier for you:</p>

<pre class="shell" style="font-family:monospace;">$ ssh-copy-id remoteuser@remotehost</pre>

<p>After you entered the remote password as prompted, the machine&#8217;s door will be open to you. In terms of security, a good rule of thumb is not to use password-less authentication unless the firewall restricts ssh access to your ssh client&#8217;s machine, or to your office network. Be sensible, but don&#8217;t learn every password you&#8217;ll get handed over by heart, it&#8217;s a waste of time and brainpower.</p>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/07/password-less-authentication-ssh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash control structures have redirection too</title>
		<link>http://melp.nl/2011/07/bash-control-structures-have-redirection-too/</link>
		<comments>http://melp.nl/2011/07/bash-control-structures-have-redirection-too/#comments</comments>
		<pubDate>Sat, 02 Jul 2011 15:08:38 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=607</guid>
		<description><![CDATA[I found out a useful thing today. You can redirect output of bash control structures as well. Typically useful for for loops, which would need a buffer of some kind otherwise to have it&#8217;s output sorted, for example. Check out this not-particularly-useful example for i in 3 2 1; do echo $i done &#124; sort [...]]]></description>
			<content:encoded><![CDATA[<p>I found out a useful thing today. You can redirect output of bash control structures as well. Typically useful for <code>for</code> loops, which would need a buffer of some kind otherwise to have it&#8217;s output sorted, for example.</p>

<p><span id="more-607"></span></p>

<p>Check out this not-particularly-useful example</p>

<pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">3</span> <span style="color: #000000;">2</span> <span style="color: #000000;">1</span>; <span style="color: #000000; font-weight: bold;">do</span> 
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span>
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-n</span></pre>

<p>Result:</p>

<pre class="shell" style="font-family:monospace;">1
2
3</pre>

<p>What I&#8217;ve proven here is that the output of the for loop goes through the sort pipe, before ending up on your screen. Pretty useful if you&#8217;re doing some processing inside a for loop, but still want to make use of sorting without having to end up using temporary files.</p>

<p>A more useful example (something I tried today) was listing a bunch of SVN repositories and the times they were last changed (in days):</p>

<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;">now</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> +<span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">*/</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #007800;">mtime</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">date</span> <span style="color: #660033;">--date</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$(svnlook date $i)</span>&quot;</span> +<span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #007800;">age</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">$now</span> - <span style="color: #007800;">$mtime</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">24</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000;">3600</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #007800;">$age</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{printf &quot;%-40s%d\n&quot;, $1, $2}'</span> 
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sort</span> <span style="color: #660033;">-k</span> <span style="color: #000000;">2</span> <span style="color: #660033;">-n</span></pre>

<p>The <code>-k</code> option directs <code>sort</code> to use the second column in the <code>for</code>-loop&#8217;s output. Very useful, imho <img src='http://melp.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/07/bash-control-structures-have-redirection-too/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binding keyboard volume buttons to mixer control</title>
		<link>http://melp.nl/2011/06/binding-keyboard-volume-buttons-to-mixer-control/</link>
		<comments>http://melp.nl/2011/06/binding-keyboard-volume-buttons-to-mixer-control/#comments</comments>
		<pubDate>Sat, 25 Jun 2011 13:42:52 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[openbox]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=590</guid>
		<description><![CDATA[Here&#8217;s how to bind your specific buttons (such as + and &#8211; controls for volume and the mute button) to controlling your mixer in openbox. Open your ~/.config/openbox/rc.xml in your favorite editor. Also open a terminal window. In the terminal window start xev to find out what keys you are pressing: $ xev xev is [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to bind your specific buttons (such as + and &#8211; controls for volume and the mute button) to controlling your mixer in openbox. Open your <code>~/.config/openbox/rc.xml</code> in your favorite editor. Also open a terminal window. In the terminal window start <code>xev</code> to find out what keys you are pressing:
<span id="more-590"></span></p>

<pre class="shell" style="font-family:monospace;">$ xev</pre>

<p><code>xev</code> is the X Event monitor, which shows you all events that X receives and dispatches. You should be aware that it will output any event that occurs, so the trick is to know what you&#8217;re looking for. We&#8217;re looking for Key events, so it&#8217;s probably easiest grepping the key events from the result:</p>

<pre class="shell" style="font-family:monospace;">$ xev | egrep '^KeyPress' -A5</pre>

<p>Now press the keys you wish to know the keycode of. Openbox&#8217;s rc.xml will need the name part of the <code>keysym</code>. For example pressing your Escape button would yield something like:</p>

<pre><code>KeyPress event, serial 39, synthetic NO, window 0x1800001,
root 0x15a, subw 0x0, time 3512708, (-289,-351), root:(1862,445),
state 0x0, keycode 9 (keysym 0xff1b, Escape), same_screen YES,
XLookupString gives 1 bytes: (1b)
XmbLookupString gives 1 bytes: (1b)
XFilterEvent returns: False
</code></pre>

<p>The part you&#8217;re looking for is:</p>

<blockquote>
  <p>state 0&#215;0, keycode 9 (<strong>keysym 0xff1b, Escape</strong>), same_screen YES,</p>
</blockquote>

<p>Now, do the same for the +, &#8211; and Mute buttons on your keyboard. Press them and find out what code X dispatches. You&#8217;ll find <code>XF86AudioRaiseVolume</code>, <code>XF86AudioLowerVolume</code> and <code>XF86AudioMute</code> are the codes you&#8217;re looking for.</p>

<p>Now, we need a program to influence mixer control. The easiest is to use <code>amixer</code>, which is the alsa command line tool to control the entire mixer. To change the volume, you can pass the mixer control name and some commands to the <code>amixer set</code> command. Here&#8217;s my config:</p>

<pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;keybind</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;XF86AudioRaiseVolume&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Execute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;command<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>amixer -q set Master 2+ unmute<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/command<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/keybind<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;keybind</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;XF86AudioLowerVolume&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Execute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;command<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>amixer -q set Master 2- unmute<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/command<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/keybind<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;keybind</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;XF86AudioMute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Execute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;command<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>amixer -q set Master toggle<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/command<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/keybind<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>Of course, this isn&#8217;t really rocket science, but since now you know how to find out what key code you are pressing, you can think of all kinds of exciting things to do with all those special buttons at the top of your keyboard. Go crazy <img src='http://melp.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/06/binding-keyboard-volume-buttons-to-mixer-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Raising windows in stead of opening new ones</title>
		<link>http://melp.nl/2011/06/raising-windows-in-stead-of-opening-new-ones/</link>
		<comments>http://melp.nl/2011/06/raising-windows-in-stead-of-opening-new-ones/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 20:08:19 +0000</pubDate>
		<dc:creator>drm</dc:creator>
				<category><![CDATA[Linux & BSD]]></category>
		<category><![CDATA[openbox]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://melp.nl/?p=583</guid>
		<description><![CDATA[This is a pretty useful utility script for EWMH-compliant window managers, such as openbox. Since I use my terminal a lot, I don&#8217;t want to get stuck with 500 terminal windows at the end of the day because of all the terminal shortcuts (W+T) I used. So, let&#8217;s add a modifier to the shortcut to [...]]]></description>
			<content:encoded><![CDATA[<p>This is a pretty useful utility script for EWMH-compliant window managers, such as openbox. Since I use my terminal a lot, I don&#8217;t want to get stuck with 500 terminal windows at the end of the day because of all the terminal shortcuts (W+T) I used. So, let&#8217;s add a modifier to the shortcut to open new ones, and use the following script to open the first you opened.
<span id="more-583"></span></p>

<pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #007800;"><span style="color: #c20cb9; font-weight: bold;">id</span></span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span>wmctrl <span style="color: #660033;">-lx</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $1}'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; 
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$id</span>&quot;</span> == <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">shift</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> $<span style="color: #000000; font-weight: bold;">@</span> <span style="color: #000000; font-weight: bold;">||</span> wmctrl <span style="color: #660033;">-ia</span> <span style="color: #007800;">$id</span>;</pre>

<p>With this nifty script, you can raise an existing window or spawn the program newly with the following syntax:</p>

<pre><code>reraise gedit --whatever-argument-you-wish-to-pass
</code></pre>

<p>If you put this in your openbox rc.xml as a shortcut, you can easily assign shortcuts behaving as &#8220;only open this program anew if the window isn&#8217;t there yet.&#8221; Pretty useful for terminals, editors and browsers.</p>

<p>Simple usage example would by a local MySQL terminal:</p>

<pre><code>reraise.sh MySQL 'xfce4-terminal --title=MySQL -x mysql'
</code></pre>

<p>Very useful, if you, like me, hate to search for windows and prefer manhandling your array of shortcuts. Of course, you will need to install <code>wmctrl</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://melp.nl/2011/06/raising-windows-in-stead-of-opening-new-ones/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

