File: Zend_Dojo-BuildLayers.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (430 lines) | stat: -rw-r--r-- 15,664 bytes parent folder | download | duplicates (2)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.dojo.build-layers">
    <title>Zend_Dojo build layer support</title>

    <sect2 id="zend.dojo.build-layers.introduction">
        <title>Introduction</title>

        <para>
            Dojo build layers provide a clean path from development to
            production when using Dojo for your UI layer. In development, you
            can have load-on-demand, rapid application prototyping; a build
            layer takes all Dojo dependencies and compiles them to a single
            file, optionally stripping whitespace and comments, and performing
            code heuristics to allow further minification of variable names.
            Additionally, it can do <acronym>CSS</acronym> minification.
        </para>

        <para>
            In order to create a build layer, you would traditionally create a
            JavaScript file that has <command>dojo.require</command> statements for
            each dependency, and optionally some additional code that might run
            when the script is loaded. As an example:
        </para>

        <programlisting language="javascript"><![CDATA[
dojo.provide("custom.main");

dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.form.Form");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
]]></programlisting>

        <para>
            This script is generally referred to as a "layer" script.
        </para>

        <para>
            Then, in your application's layout, you'd instruct Dojo to load this
            module:
        </para>

        <programlisting language="html"><![CDATA[
<html>
<head>
    <script type="text/javascript" src="/js/dojo/dojo.js"></script>
    <script type="text/javascript">
        dojo.registerModulePath("custom", "../custom/");
        dojo.require("custom.main");
    </script>
]]></programlisting>

        <para>
            If you use <classname>Zend_Dojo</classname> to do this, you'd do the
            following:
        </para>

        <programlisting language="php"><![CDATA[
$view->dojo()->registerModulePath('custom', '../custom/')
             ->requireModule('custom.main');
]]></programlisting>

        <para>
            But since <classname>Zend_Dojo</classname> aggregates your various
            <command>dojo.require</command> statements, how do you create your layer
            script? You could open each page and view the generated
            <command>dojo.require</command> statements, and cut and paste them into a
            layer script file manually.
        </para>

        <para>
            However, a better solution exists: since
            <classname>Zend_Dojo</classname> aggregates this information
            already, you can simply pull that information and build your layer
            file. This is the purpose of
            <classname>Zend_Dojo_BuildLayer</classname>.
        </para>
    </sect2>

    <sect2 id="zend.dojo.build-layers.usage">
        <title>Generating Custom Module Layers with Zend_Dojo_BuildLayer</title>

        <para>
            At its simplest, you simply instantiate
            <classname>Zend_Dojo_BuildLayer</classname>, feed it the view object
            and the name of your custom module layer, and have it generate the
            content of the layer file; it is up to you to then write it to disk.
        </para>

        <para>
            As an example, let's say you wanted to create the module layer
            "<filename>custom.main</filename>". Assuming you follow the recommended project
            directory structure, and that you are storing your JavaScript files under
            <filename>public/js/</filename>, you could do the following:
        </para>

        <programlisting language="php"><![CDATA[
$build = new Zend_Dojo_BuildLayer(array(
    'view'      => $view,
    'layerName' => 'custom.main',
));

$layerContents = $build->generateLayerScript();
$filename      = APPLICATION_PATH . '/../public/js/custom/main.js';
if (!file_exists(dirname($filename))) {
    mkdir(dirname($filename));
}
file_put_contents($filename, $layerContents);
]]></programlisting>

        <para>
            When should you do the above? For it to work correctly, you need to
            do it after all view scripts and the layout have been rendered, to
            ensure that the <methodname>dojo()</methodname> helper is fully populated. One
            easy way to do so is using a front controller plugin, with a
            <methodname>dispatchLoopShutdown()</methodname> hook:
        </para>

        <programlisting language="php"><![CDATA[
class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
{
    public $layerScript = APPLICATION_PATH . '/../public/js/custom/main.js';
    protected $_build;

    public function dispatchLoopShutdown()
    {
        if (!file_exists($this->layerScript)) {
            $this->generateDojoLayer();
        }
    }

    public function getBuild()
    {
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
            'ViewRenderer'
        );
        $viewRenderer->initView();
        if (null === $this->_build) {
            $this->_build = new Zend_Dojo_BuildLayer(array(
                'view'      => $viewRenderer->view,
                'layerName' => 'custom.main',
            ));
        }
        return $this->_build;
    }

    public function generateDojoLayer()
    {
        $build = $this->getBuild();
        $layerContents = $build->generateLayerScript();
        if (!file_exists(dirname($this->layerScript))) {
            mkdir(dirname($this->layerScript));
        }
        file_put_contents($this->layerScript, $layerContents);
    }
}
]]></programlisting>

        <note>
            <title>Do not generate the layer on every page</title>

            <para>
                It's tempting to generate the layer script on each and every
                page. However, this is resource intensive, as it must write to
                the disk on each page. Additionally, since the mtime of the file
                will keep changing, you will get no benefits of client-side
                caching. Write the file once.
            </para>
        </note>

        <sect3 id="zend.dojo.build-layers.usage.options">
            <title>BuildLayer options</title>

            <para>
                The above functionality will suffice for most situations. For
                those needing more customization, a variety of options may be
                invoked.
            </para>

            <sect4 id="zend.dojo.build-layers.usage.options.view">
                <title>Setting the view object</title>

                <para>
                    While the view object may be passed during instantiation,
                    you may also pass it in to an instance via the
                    <methodname>setView()</methodname> method:
                </para>

                <programlisting language="php"><![CDATA[
$build->setView($view);
]]></programlisting>
            </sect4>

            <sect4 id="zend.dojo.build-layers.usage.options.layername">
                <title>Setting the layer name</title>

                <para>
                    While the layer name may be passed during instantiation,
                    you may also pass it in to an instance via the
                    <methodname>setLayerName()</methodname> method:
                </para>

                <programlisting language="php"><![CDATA[
$build->setLayerName("custom.main");
]]></programlisting>
            </sect4>

            <sect4 id="zend.dojo.build-layers.usage.options.onload">
                <title>Including onLoad events in the generated layer</title>

                <para>
                    <command>dojo.addOnLoad</command> is a useful utility for
                    specifying actions that should trigger when the <acronym>DOM</acronym> has
                    finished loading. The <methodname>dojo()</methodname> view helper can
                    create these statements via its
                    <methodname>addOnLoad()</methodname> and
                    <methodname>onLoadCapture()</methodname> methods. In some
                    cases, it makes sense to push these into your layer file
                    instead of rendering them via your view scripts.
                </para>

                <para>
                    By default, these are not rendered; to enable them, pass the
                    <property>consumeOnLoad</property> configuration key during
                    instantiation:
                </para>

                <programlisting language="php"><![CDATA[
$build = new Zend_Dojo_BuildLayer(array(
    'view'          => $view,
    'layerName'     => 'custom.main',
    'consumeOnLoad' => true,
));
]]></programlisting>

                <para>
                    Alternately, you can use the
                    <methodname>setConsumeOnLoad()</methodname> method after
                    instantiation:
                </para>

                <programlisting language="php"><![CDATA[
$build->setConsumeOnLoad(true);
]]></programlisting>
            </sect4>

            <sect4 id="zend.dojo.build-layers.usage.options.javascript">
                <title>Including captured JavaScript in the generated layer</title>

                <para>
                    The <methodname>dojo()</methodname> view helper includes methods for
                    capturing arbitrary JavaScript to include in the
                    &lt;script&gt; tag containing the various
                    <command>dojo.require</command> and <command>dojo.addOnLoad</command>
                    statements. This can be useful when creating default data
                    stores or globally scoped objects used throughout your
                    application.
                </para>

                <para>
                    By default, these are not rendered; to enable them, pass the
                    <property>consumeJavascript</property> configuration key during
                    instantiation:
                </para>

                <programlisting language="php"><![CDATA[
$build = new Zend_Dojo_BuildLayer(array(
    'view'              => $view,
    'layerName'         => 'custom.main',
    'consumeJavascript' => true,
));
]]></programlisting>

                <para>
                    Alternately, you can use the
                    <methodname>setConsumeJavascript()</methodname> method after
                    instantiation:
                </para>

                <programlisting language="php"><![CDATA[
$build->setConsumeJavascript(true);
]]></programlisting>
            </sect4>
        </sect3>
    </sect2>

    <sect2 id="zend.dojo.build-layers.profiles">
        <title>Generating Build Profiles with Zend_Dojo_BuildLayer</title>

        <para>
            One of the chief benefits of a Dojo module layer is that it
            facilitates the creation of a custom build.
            <classname>Zend_Dojo_BuildLayer</classname> has functionality for
            generate build profiles.
        </para>

        <para>
            The simplest use case is to utilize the
            <methodname>generateBuildProfile()</methodname> method and send the
            output to a file:
        </para>

        <programlisting language="php"><![CDATA[
$build = new Zend_Dojo_BuildLayer(array(
    'view'      => $view,
    'layerName' => 'custom.main',
));

$profile   = $build->generateBuildProfile();
$filename  = APPLICATION_PATH . '/../misc/scripts/custom.profile.js';
file_put_contents($filename, $profile);
]]></programlisting>

        <para>
            Just like generating layers, you may want to automate this via a
            <methodname>dispatchLoopShutdown()</methodname> plugin hook; you
            could even simply modify the one shown for generating layers to read
            as follows:
        </para>

        <programlisting language="php"><![CDATA[
class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
{
    public $layerScript  = APPLICATION_PATH
                         . '/../public/js/custom/main.js';
    public $buildProfile = APPLICATION_PATH
                         . '/../misc/scripts/custom.profile.js';
    protected $_build;

    public function dispatchLoopShutdown()
    {
        if (!file_exists($this->layerScript)) {
            $this->generateDojoLayer();
        }
        if (!file_exists($this->buildProfile)) {
            $this->generateBuildProfile();
        }
    }

    public function generateDojoLayer() { /* ... */ }

    public function generateBuildProfile()
    {
        $profile = $this->getBuild()->generateBuildProfile();
        file_put_contents($this->buildProfile, $profile);
    }

}
]]></programlisting>

        <para>
            As noted, with module layers, you should only create the file once.
        </para>

        <sect3 id="zend.dojo.build-layers.profiles.options">
            <title>Build Profile options</title>

            <para>
                The above functionality will suffice for most situations. The
                only way to customize build profile generation is to provide
                additional build profile options to utilize.
            </para>

            <para>
                As an example, you may want to specify what type of
                optimizations should be performed, whether or not to optimize
                <acronym>CSS</acronym> files in the layer, whether or not to copy tests into the
                build, etc. For a listing of available options, you should read
                the <ulink url="http://docs.dojocampus.org/build/index">Dojo
                    Build documentation</ulink> and <ulink
                    url="http://dojotoolkit.org/reference-guide/dojo/index.html#package-system">accompanying
                documentation</ulink>.
            </para>

            <para>
                Setting these options is trivial: use the
                <methodname>addProfileOption()</methodname>,
                <methodname>addProfileOptions()</methodname>, or
                <methodname>setProfileOptions()</methodname> methods. The first
                method adds a single key and value option pair, the second will add
                several, and the third will overwrite any options with the list
                of key and value pairs provided.
            </para>

            <para>
                By default, the following options are set:
            </para>

            <programlisting language="javascript"><![CDATA[
{
    action:        "release",
    optimize:      "shrinksafe",
    layerOptimize: "shrinksafe",
    copyTests:     false,
    loader:        "default",
    cssOptimize:   "comments"
}
]]></programlisting>

            <para>
                You can pass in whatever key and value pairs you want; the Dojo
                build script will ignore those it does not understand.
            </para>

            <para>
                As an example of setting options:
            </para>

            <programlisting language="php"><![CDATA[
// A single option:
$build->addProfileOption('version', 'zend-1.3.1');

// Several options:
$build->addProfileOptions(array(
    'loader'   => 'xdomain',
    'optimize' => 'packer',
));

// Or overwrite options:
$build->setProfileOptions(array(
    'version'  => 'custom-1.3.1',
    'loader'   => 'shrinksafe',
    'optimize' => 'shrinksafe',
));
]]></programlisting>
        </sect3>
    </sect2>
</sect1>