This file is indexed.

/usr/share/doc/php-doctrine-orm/html/cookbook/strategy-cookbook-introduction.html is in doctrine-orm-doc 2.4.6-1+deb8u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Strategy-Pattern &mdash; Doctrine 2 ORM 2 documentation</title>
    <link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/layout.css" type="text/css" />
    <link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script src="../_static/bootstrap/js/bootstrap.min.js"></script>

    <script type="text/javascript">
    <!--
        $(document).ready(function() {
            $("#versions").change(function() {
                var docsUrl = $(this).val();
                window.location.href = docsUrl;
            });
        });
    -->
    </script>
    <link rel="shortcut icon" href="../_static/doctrine.ico"/>
    <link rel="top" title="Doctrine 2 ORM 2 documentation" href="../index.html" />
    <link rel="next" title="Validation of Entities" href="validation-of-entities.html" />
    <link rel="prev" title="SQL-Table Prefixes" href="sql-table-prefixes.html" /> 
  </head>
  <body>
    <div id="wrapper">
      <div id="header">
        <h1 id="h1title"></h1>
        <div id="logo">
          <a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
        </div>
      </div>
      <div id="nav" class="cls">
        <div class="tl cls">
          <ul>
            <li><a target="_top" href="/">home</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/about">about</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects.html">projects</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects/orm">orm</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects/dbal">dbal</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/blog">blog</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/jira">development</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/contribute">contribute</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/community">community</a></li>
          </ul>
        </div>
      </div>
      <div id="content" class="cls">
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="validation-of-entities.html" title="Validation of Entities"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="sql-table-prefixes.html" title="SQL-Table Prefixes"
             accesskey="P">previous</a> |</li>
        <li><a href="/">Doctrine Homepage</a> &raquo;</li>
        <li><a href="../index.html">Doctrine 2 ORM 2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

        <div class="document">
            <div class="documentwrapper">
                <div class="bodywrapper">

              <div class="body" >
                
  <div class="section" id="strategy-pattern">
<h1>Strategy-Pattern<a class="headerlink" href="#strategy-pattern" title="Permalink to this headline"></a></h1>
<p>This recipe will give you a short introduction on how to design
similar entities without using expensive (i.e. slow) inheritance
but with not more than * the well-known strategy pattern * event
listeners</p>
<div class="section" id="scenario-problem">
<h2>Scenario / Problem<a class="headerlink" href="#scenario-problem" title="Permalink to this headline"></a></h2>
<p>Given a Content-Management-System, we probably want to add / edit
some so-called &#8220;blocks&#8221; and &#8220;panels&#8221;. What are they for?</p>
<ul class="simple">
<li>A block might be a registration form, some text content, a table
with information. A good example might also be a small calendar.</li>
<li>A panel is by definition a block that can itself contain blocks.
A good example for a panel might be a sidebar box: You could easily
add a small calendar into it.</li>
</ul>
<p>So, in this scenario, when building your CMS, you will surely add
lots of blocks and panels to your pages and you will find yourself
highly uncomfortable because of the following:</p>
<ul class="simple">
<li>Every existing page needs to know about the panels it contains -
therefore, you&#8217;ll have an association to your panels. But if you&#8217;ve
got several types of panels - what do you do? Add an association to
every panel-type? This wouldn&#8217;t be flexible. You might be tempted
to add an AbstractPanelEntity and an AbstractBlockEntity that use
class inheritance. Your page could then only confer to the
AbstractPanelType and Doctrine 2 would do the rest for you, i.e.
load the right entities. But - you&#8217;ll for sure have lots of panels
and blocks, and even worse, you&#8217;d have to edit the discriminator
map <em>manually</em> every time you or another developer implements a new
block / entity. This would tear down any effort of modular
programming.</li>
</ul>
<p>Therefore, we need something that&#8217;s far more flexible.</p>
</div>
<div class="section" id="solution">
<h2>Solution<a class="headerlink" href="#solution" title="Permalink to this headline"></a></h2>
<p>The solution itself is pretty easy. We will have one base class
that will be loaded via the page and that has specific behaviour -
a Block class might render the front-end and even the backend, for
example. Now, every block that you&#8217;ll write might look different or
need different data - therefore, we&#8217;ll offer an API to these
methods but internally, we use a strategy that exactly knows what
to do.</p>
<p>First of all, we need to make sure that we have an interface that
contains every needed action. Such actions would be rendering the
front-end or the backend, solving dependencies (blocks that are
supposed to be placed in the sidebar could refuse to be placed in
the middle of your page, for example).</p>
<p>Such an interface could look like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/**</span>
<span class="sd"> * This interface defines the basic actions that a block / panel needs to support.</span>
<span class="sd"> *</span>
<span class="sd"> * Every blockstrategy is *only* responsible for rendering a block and declaring some basic</span>
<span class="sd"> * support, but *not* for updating its configuration etc. For this purpose, use controllers</span>
<span class="sd"> * and models.</span>
<span class="sd"> */</span>
<span class="k">interface</span> <span class="nx">BlockStrategyInterface</span> <span class="p">{</span>
    <span class="sd">/**</span>
<span class="sd">     * This could configure your entity</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">setConfig</span><span class="p">(</span><span class="nx">Config\EntityConfig</span> <span class="nv">$config</span><span class="p">);</span>

    <span class="sd">/**</span>
<span class="sd">     * Returns the config this strategy is configured with.</span>
<span class="sd">     * @return Core\Model\Config\EntityConfig</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">getConfig</span><span class="p">();</span>

    <span class="sd">/**</span>
<span class="sd">     * Set the view object.</span>
<span class="sd">     * @param  \Zend_View_Interface $view</span>
<span class="sd">     * @return \Zend_View_Helper_Interface</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">setView</span><span class="p">(</span><span class="nx">\Zend_View_Interface</span> <span class="nv">$view</span><span class="p">);</span>

    <span class="sd">/**</span>
<span class="sd">     * @return \Zend_View_Interface</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">getView</span><span class="p">();</span>

    <span class="sd">/**</span>
<span class="sd">     * Renders this strategy. This method will be called when the user</span>
<span class="sd">     * displays the site.</span>
<span class="sd">     *</span>
<span class="sd">     * @return string</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">renderFrontend</span><span class="p">();</span>

    <span class="sd">/**</span>
<span class="sd">     * Renders the backend of this block. This method will be called when</span>
<span class="sd">     * a user tries to reconfigure this block instance.</span>
<span class="sd">     *</span>
<span class="sd">     * Most of the time, this method will return / output a simple form which in turn</span>
<span class="sd">     * calls some controllers.</span>
<span class="sd">     *</span>
<span class="sd">     * @return string</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">renderBackend</span><span class="p">();</span>

    <span class="sd">/**</span>
<span class="sd">     * Returns all possible types of panels this block can be stacked onto</span>
<span class="sd">     *</span>
<span class="sd">     * @return array</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">getRequiredPanelTypes</span><span class="p">();</span>

    <span class="sd">/**</span>
<span class="sd">     * Determines whether a Block is able to use a given type or not</span>
<span class="sd">     * @param string $typeName The typename</span>
<span class="sd">     * @return boolean</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">canUsePanelType</span><span class="p">(</span><span class="nv">$typeName</span><span class="p">);</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">setBlockEntity</span><span class="p">(</span><span class="nx">AbstractBlock</span> <span class="nv">$block</span><span class="p">);</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">getBlockEntity</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>As you can see, we have a method &#8220;setBlockEntity&#8221; which ties a potential strategy to an object of type AbstractBlock. This type will simply define the basic behaviour of our blocks and could potentially look something like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/**</span>
<span class="sd"> * This is the base class for both Panels and Blocks.</span>
<span class="sd"> * It shouldn&#39;t be extended by your own blocks - simply write a strategy!</span>
<span class="sd"> */</span>
<span class="k">abstract</span> <span class="k">class</span> <span class="nc">AbstractBlock</span> <span class="p">{</span>
    <span class="sd">/**</span>
<span class="sd">     * The id of the block item instance</span>
<span class="sd">     * This is a doctrine field, so you need to setup generation for it</span>
<span class="sd">     * @var integer</span>
<span class="sd">     */</span>
    <span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>

    <span class="c1">// Add code for relation to the parent panel, configuration objects, ....</span>

    <span class="sd">/**</span>
<span class="sd">     * This var contains the classname of the strategy</span>
<span class="sd">     * that is used for this blockitem. (This string (!) value will be persisted by Doctrine 2)</span>
<span class="sd">     *</span>
<span class="sd">     * This is a doctrine field, so make sure that you use an @column annotation or setup your</span>
<span class="sd">     * yaml or xml files correctly</span>
<span class="sd">     * @var string</span>
<span class="sd">     */</span>
    <span class="k">protected</span> <span class="nv">$strategyClassName</span><span class="p">;</span>

    <span class="sd">/**</span>
<span class="sd">     * This var contains an instance of $this-&gt;blockStrategy. Will not be persisted by Doctrine 2.</span>
<span class="sd">     *</span>
<span class="sd">     * @var BlockStrategyInterface</span>
<span class="sd">     */</span>
    <span class="k">protected</span> <span class="nv">$strategyInstance</span><span class="p">;</span>

    <span class="sd">/**</span>
<span class="sd">     * Returns the strategy that is used for this blockitem.</span>
<span class="sd">     *</span>
<span class="sd">     * The strategy itself defines how this block can be rendered etc.</span>
<span class="sd">     *</span>
<span class="sd">     * @return string</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">getStrategyClassName</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">strategyClassName</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="sd">/**</span>
<span class="sd">     * Returns the instantiated strategy</span>
<span class="sd">     *</span>
<span class="sd">     * @return BlockStrategyInterface</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">getStrategyInstance</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">strategyInstance</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="sd">/**</span>
<span class="sd">     * Sets the strategy this block / panel should work as. Make sure that you&#39;ve used</span>
<span class="sd">     * this method before persisting the block!</span>
<span class="sd">     *</span>
<span class="sd">     * @param BlockStrategyInterface $strategy</span>
<span class="sd">     */</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">setStrategy</span><span class="p">(</span><span class="nx">BlockStrategyInterface</span> <span class="nv">$strategy</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">strategyInstance</span>  <span class="o">=</span> <span class="nv">$strategy</span><span class="p">;</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">strategyClassName</span> <span class="o">=</span> <span class="nb">get_class</span><span class="p">(</span><span class="nv">$strategy</span><span class="p">);</span>
        <span class="nv">$strategy</span><span class="o">-&gt;</span><span class="na">setBlockEntity</span><span class="p">(</span><span class="nv">$this</span><span class="p">);</span>
    <span class="p">}</span>
</pre></div>
</div>
<p>Now, the important point is that $strategyClassName is a Doctrine 2
field, i.e. Doctrine will persist this value. This is only the
class name of your strategy and not an instance!</p>
<p>Finishing your strategy pattern, we hook into the Doctrine postLoad
event and check whether a block has been loaded. If so, you will
initialize it - i.e. get the strategies classname, create an
instance of it and set it via setStrategyBlock().</p>
<p>This might look like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">\Doctrine\ORM</span><span class="p">,</span>
    <span class="nx">\Doctrine\Common</span><span class="p">;</span>

<span class="sd">/**</span>
<span class="sd"> * The BlockStrategyEventListener will initialize a strategy after the</span>
<span class="sd"> * block itself was loaded.</span>
<span class="sd"> */</span>
<span class="k">class</span> <span class="nc">BlockStrategyEventListener</span> <span class="k">implements</span> <span class="nx">Common\EventSubscriber</span> <span class="p">{</span>

    <span class="k">protected</span> <span class="nv">$view</span><span class="p">;</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nx">\Zend_View_Interface</span> <span class="nv">$view</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span> <span class="o">=</span> <span class="nv">$view</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">getSubscribedEvents</span><span class="p">()</span> <span class="p">{</span>
       <span class="k">return</span> <span class="k">array</span><span class="p">(</span><span class="nx">ORM\Events</span><span class="o">::</span><span class="na">postLoad</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="k">public</span> <span class="k">function</span> <span class="nf">postLoad</span><span class="p">(</span><span class="nx">ORM\Event\LifecycleEventArgs</span> <span class="nv">$args</span><span class="p">)</span> <span class="p">{</span>
        <span class="nv">$blockItem</span> <span class="o">=</span> <span class="nv">$args</span><span class="o">-&gt;</span><span class="na">getEntity</span><span class="p">();</span>

        <span class="c1">// Both blocks and panels are instances of Block\AbstractBlock</span>
        <span class="k">if</span> <span class="p">(</span><span class="nv">$blockItem</span> <span class="nx">instanceof</span> <span class="nx">Block\AbstractBlock</span><span class="p">)</span> <span class="p">{</span>
            <span class="nv">$strategy</span>  <span class="o">=</span> <span class="nv">$blockItem</span><span class="o">-&gt;</span><span class="na">getStrategyClassName</span><span class="p">();</span>
            <span class="nv">$strategyInstance</span> <span class="o">=</span> <span class="k">new</span> <span class="nv">$strategy</span><span class="p">();</span>
            <span class="k">if</span> <span class="p">(</span><span class="k">null</span> <span class="o">!==</span> <span class="nv">$blockItem</span><span class="o">-&gt;</span><span class="na">getConfig</span><span class="p">())</span> <span class="p">{</span>
                <span class="nv">$strategyInstance</span><span class="o">-&gt;</span><span class="na">setConfig</span><span class="p">(</span><span class="nv">$blockItem</span><span class="o">-&gt;</span><span class="na">getConfig</span><span class="p">());</span>
            <span class="p">}</span>
            <span class="nv">$strategyInstance</span><span class="o">-&gt;</span><span class="na">setView</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">view</span><span class="p">);</span>
            <span class="nv">$blockItem</span><span class="o">-&gt;</span><span class="na">setStrategy</span><span class="p">(</span><span class="nv">$strategyInstance</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>In this example, even some variables are set - like a view object
or a specific configuration object.</p>
</div>
</div>


              </div>
                </div>

            </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
             
            <div id="searchbox" style="">
              <h3>Search</h3>
                <form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="text" name="q" size="18">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>
            </div>
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference internal" href="#">Strategy-Pattern</a><ul>
<li><a class="reference internal" href="#scenario-problem">Scenario / Problem</a></li>
<li><a class="reference internal" href="#solution">Solution</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="sql-table-prefixes.html"
                                  title="previous chapter">SQL-Table Prefixes</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="validation-of-entities.html"
                                  title="next chapter">Validation of Entities</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/cookbook/strategy-cookbook-introduction.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
        </div>
      </div>
          <div class="clearer"></div>
        </div>
          <div class="footer">
              &copy; Copyright 2010-12, Doctrine Project Team.
              Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
            <br/>
            <a target="_BLANK" href="http://www.servergrove.com"><img src="../_static/servergrove.jpg" /></a>      <br/><br/>
            <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
              <input type="hidden" name="cmd" value="_s-xclick" />
              <input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
              <input type="Submit" value="Donate via PayPal" />
            </form>
          </div>
      </div>

      <div id="bot-rcnr">
        <div class="tl"><!-- corner --></div>
      </div>
    </div>

  <a class="githublink" href="http://github.com/doctrine">Fork me on GitHub</a>
  </body>
</html>