This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Zend_Module-Intro.xml
518 lines (457 loc) · 19.8 KB
/
Zend_Module-Intro.xml
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<section
xmlns="http://docbook.org/ns/docbook" version="5.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:id="zend.module.intro">
<info><title>Introduction to the Module System</title></info>
<para>
Zend Framework 2.0 introduces a new and powerful approach to modules.
This new module system is designed with flexibility, simplicity, and
re-usability in mind. A module may contain just about anything: PHP
code, including MVC functionality; library code; view scripts; and/or
or public assets such as images, CSS, and JavaScript. The possibilities
are endless.
</para>
<para>
The module system in Zend Framework 2 is made of up two main
components:
</para>
<itemizedlist>
<listitem>
<para>
<classname>Zend\Module\Manager</classname> is responsible for
most module-related tasks such as instantiating modules'
<classname>Module</classname> objects, merging module
configuration, keeping track of loaded modules, etc.
</para>
</listitem>
<listitem>
<para>
<classname>Zend\Loader\ModuleAutoloader</classname> is a
specialized autoloader that is responsible for locating and
on-demand loading of modules' <classname>Module</classname>
classes from a variety of sources.
</para>
</listitem>
</itemizedlist>
<note>
<para>
The name of a module in Zend Framework 2 is simply a <link
xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="http://php.net/namespaces"><acronym>PHP</acronym>
namespace</link> and must follow all of the same rules.
</para>
</note>
<para>
The recommended structure of a typical MVC-oriented module is as
follows:
</para>
<literallayout>
module_root/
Module.php
autoload_classmap.php
autoload_function.php
autoload_register.php
configs/
module.config.php
public/
images/
css/
js/
src/
<module_namespace>/
<code files>
tests/
phpunit.xml
bootstrap.php
<module_namespace>/
<test code files>
views/
<dir-named-after-a-controller/
<.phtml files>
</literallayout>
<section xml:id="zend.module.intro.the-module-class">
<info><title>The Module Class</title></info>
<para>
The <classname>Module</classname> class is single most important
part of any Zend Framework 2 module. When initializing a module,
<classname>Zend\Module\Manager</classname> only cares that a
<classname>Module</classname> class exists under that module's
namespace.
</para>
<example
xml:id="zend.module.intro.example.minimal-module">
<info><title>A Minimal Module</title></info>
<para>
As an example, provided the module name "MyModule",
<classname>Zend\Module\Manager</classname> will simply expect
the class <classname>MyModule\Module</classname> to be
available. It is the responsibility of
<classname>Zend\Loader\ModuleAutoloader</classname> to find and
include the <classname>MyModule\Module</classname> class if it
is not already available.
</para>
<para>
A module named "MyModule" module might start out looking
something like this:
</para>
<literallayout>
MyModule/
Module.php
</literallayout>
<para>
Within <filename>Module.php</filename>, you define your
<classname>MyModule\Module</classname> class:
</para>
<programlisting language="php"><![CDATA[
namespace MyModule;
class Module
{
}
]]></programlisting>
<para>
Though it will not serve any purpose, this "MyModule" module
now has everything it needs to be considered a valid module and
be loaded by the module system!
</para>
</example>
<para>
This <classname>Module</classname> class serves as the single entry
point for <classname>Zend\Module\Manager</classname> to detect and
interact with a module. From within this simple, yet powerful
class, modules can override or provide additional application
configuration, perform initialization tasks such as registering
autoloader(s) and event listeners, declare dependencies, and much
more.
</para>
<para>
Typically, you'll want to take advantage of a couple of special
methods in your module's <classname>Module</classname> class. The
following methods are called by
<classname>Zend\Module\Manager</classname> on
<emphasis>every</emphasis> enabled module for
<emphasis>every</emphasis> application request:
</para>
<itemizedlist>
<listitem>
<para>
The <methodname>init()</methodname> method should be used
for performing <emphasis>lightweight</emphasis> tasks such
as registering an autoloader or event listeners.
<classname>Zend\Module\Manager</classname> passes itself as
the only parameter to the <methodname>init()</methodname>
method.
</para>
</listitem>
<listitem>
<para>
The <methodname>getConfig()</methodname> method allows
modules to inject their own configuration into the global
application configuration. The application environment
string will be passed as the only parameter to the
<methodname>getConfig()</methodname> method, allowing you
to return the appropriate configuration contengent upon the
application environment. The
<methodname>getConfig()</methodname> method is expected to
always return an instance of
<classname>Zend\Config\Config</classname>. All returned
configurations will be merged by
<classname>Zend\Module\Manager</classname> and resulting
merged configuration will be utilized by the application.
</para>
</listitem>
</itemizedlist>
<example
xml:id="zend.module.intro.example.typical-module-class">
<info><title>A Typical Module Class</title></info>
<para>
The following example shows a more typical usage of the
<classname>Module</classname> class:
</para>
<programlisting language="php"><![CDATA[
namespace MyModule;
use Zend\Config\Config,
Zend\Loader\AutoloaderFactory,
Zend\Module\Manager;
class Module
{
public function init(Manager $moduleManager = null)
{
$this->initAutoloader();
}
public function getConfig($env = null)
{
$config = new Config(include __DIR__ . '/configs/module.config.php');
if ($env && isset($config->{$env})) {
return $config->{$env};
}
return $config;
}
public function initAutoloader()
{
AutoloaderFactory::factory(array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
));
}
}
]]></programlisting>
</example>
<note>
<para>
While modules can exist without a Module class, it would not be
detected by the module autoloader or manager. An alternative
method of utilizing such a module would have to be provided by
the developer. For the purposes of this documentation, we will
not go into such use-cases.
</para>
</note>
</section>
<section xml:id="zend.module.intro.the-init.post-event">
<info><title>The "init.post" Event</title></info>
<para>
It is not safe for a module to assume that any other modules have
already been loaded at the time <methodname>init()</methodname>
method is called. If your module needs to perform some action(s)
after all other modules have been loaded, the module manager's
"init.post" event makes this easy.
</para>
<example
xml:id="zend.module.intro.example.init.post-event">
<info><title>Sample Usage of "init.post" Event</title></info>
<programlisting language="php"><![CDATA[
use Zend\Module\Manager as ModuleManager;
class Module
{
public function init(ModuleManager $moduleManger)
{
$events = $moduleManager->events();
$events->attach('init.post', array($this, 'postInit'));
}
public function postInit($e)
{
// This method is called once all modules are loaded.
$moduleManager = $e->getTarget();
$loadedModules = $moduleManager->getLoadedModules();
$config = $moduleManager->getMergedConfig();
}
}
]]></programlisting>
</example>
</section>
<section xml:id="zend.module.intro.module-loading">
<info><title>Module Loading</title></info>
<para>
Typically, modules will live under a <filename>modules/</filename>
directory under your application's root directory. To make such a
configuration work, we need to configure and register an instance
of <classname>Zend\Loader\ModuleAutoloader</classname>. The module
autoloader takes an array of paths which it should search when
trying to load modules.
</para>
<example
xml:id="zend.module.intro.example.module-loading-single-path">
<info><title>Registering a Single Module Path</title></info>
<para>
The following example will search for modules in
<filename>/path/to/myzf2project/modules</filename>.
</para>
<programlisting language="php"><![CDATA[
// Assuming we're in /path/to/myzf2project/public/index.php
$moduleLoader = new \Zend\Loader\ModuleAutoloader(array(
realpath(__DIR__ . '/../modules')
));
$moduleLoader->register();
]]></programlisting>
</example>
<para>
Of course, multiple module paths can be registered as well,
allowing multiple applications to share a single copy of a module.
</para>
<example
xml:id="zend.module.intro.example.module-loading-multiple-paths">
<info><title>Registering Multiple Module Paths</title></info>
<para>
In this example, two paths are registered with the module
autoloader: an application-local modules directory and a
system-wide modules directory.
</para>
<programlisting language="php"><![CDATA[
// Assuming we're in /path/to/myzf2project/public/index.php
$moduleLoader = new \Zend\Loader\ModuleAutoloader(array(
realpath(__DIR__ . '/../modules'),
'/path/to/shared/modules'
));
$moduleLoader->register();
]]></programlisting>
</example>
<note>
<para>
Module paths behave very similar to the PHP include path, and
are searched in the order they are defined. If you have modules
with the same name in more than one registered module path, the
module autoloader will return the first one it finds.
</para>
</note>
</section>
<section xml:id="zend.module.intro.non-standard-module-paths">
<info><title>Non-Standard / Explicit Module Paths</title></info>
<para>
Sometimes you may want to specify exactly where a module is instead
of letting <classname>Zend\Loader\ModuleAutoloader</classname> try
to find it in the registered paths.
</para>
<example
xml:id="zend.module.intro.example.module-loading-nonstandard-paths">
<info><title>Registering a Non-Standard / Explicit Module Path</title></info>
<para>
In this example, the autoloader will first check for
<classname>MyModule\Module</classname> in
<filename>/path/to/mymoduledir/Module.php</filename>. If it's
not found, then it will fall back to searching any other
registered module paths.
</para>
<programlisting language="php"><![CDATA[
$moduleLoader = new \Zend\Loader\ModuleAutoloader(array(
realpath(__DIR__ . '/../modules'),
'MyModule' => '/path/to/mymoduledir'
));
$moduleLoader->register();
]]></programlisting>
</example>
<para>
This same method works if you provide the path to a phar archive.
</para>
</section>
<section xml:id="zend.module.intro.the-autoload-files">
<info><title>The autoload_*.php Files</title></info>
<para>
The three <filename>autoload_*.php</filename> files are not
required, but recommended. They provide the following:
</para>
<itemizedlist>
<listitem>
<para>
<filename>autoload_filemap.php</filename> should return an
array classmap of class name/filename pairs (with the
filenames resolved via the <varname>__DIR__</varname> magic
constant).
</para>
</listitem>
<listitem>
<para>
<filename>autoload_function.php</filename> should return a
PHP callback that can be passed to
<methodname>spl_autoload_register()</methodname>.
Typically, this callback should utilize the map returned by
<filename>autoload_filemap.php</filename>.
</para>
</listitem>
<listitem>
<para>
<filename>autoload_register.php</filename> should register
a PHP callback (typically that returned by
<filename>autoload_function.php</filename> with
<methodname>spl_autoload_register()</methodname>.
</para>
</listitem>
</itemizedlist>
<para>
The purpose of these three files is to provide reasonable default
mechanisms for autoloading the classes contained in the module,
thus providing a trivial way to consume the module without
requiring <classname>Zend\Module</classname> (e.g., for use outside
a ZF2 application).
</para>
</section>
<section xml:id="zend.module.intro.packagine-modules-with-phar">
<info><title>Packaging Modules with Phar</title></info>
<para>
If you prefer, you may easily package your module as a <link
xmlns:xlink="http://www.w3.org/1999/xlink"
xlink:href="http://php.net/phar">phar archive</link>. Supported
phar extensions are: .phar, .phar.gz, .phar.bz2, .phar.tar,
.phar.tar.gz, .phar.tar.bz2, .phar.zip, .tar, .tar.gz, .tar.bz2,
and .zip. The module autoloader is able to discover modules with
these extensions.
</para>
<para>
The easiest way to package your module is to simply tar the module
directory. You can then replace the <filename>MyModule/</filename>
directory with <filename>MyModule.tar</filename>, and it should
still be autoloaded without any additional changes!
</para>
<note>
<para>
If possible, avoid using any type of compression (bz2, gz, zip)
on your phar archives, as it introduces unnecessary CPU
overhead to each request.
</para>
</note>
</section>
<section xml:id="zend.module.intro.best-practices-for-modules">
<info><title>Best Practices for Modules</title></info>
<itemizedlist>
<listitem>
<para>
Keep the <methodname>init()</methodname> method
lightweight. Be conservative with the actions you perform
in the <methodname>init()</methodname> method of your
<classname>Module</classname> class. This method is ran for
<emphasis>every</emphasis> page request, and should not
perform anything heavy. As a rule of thumb, registering
autoloaders and event listeners are appropriate tasks to
perform in the <methodname>init()</methodname> method. Such
lightweight tasks will generally not have a measurable
impact on the performance of your application, even with
many modules enabled. It is considered bad practice to
utilize the <methodname>init()</methodname> method for
setting up or configuring instances of application
resources such as a database connection, application
logger, or mailer. Tasks such as these are better served
through the new dependency injection capabilities of Zend
Framework 2.
</para>
</listitem>
<listitem>
<para>
Do not perform writes within a module. You should
<emphasis>never</emphasis> code your module to perform or
expect any writes within the module's directory. Once
installed, the files within a module's directory should
always match the distribution verbatim. Any user-provided
configuration should be performed via overrides in the
Application module. Any other required filesystem writes
should be performed in some writeable path that is outside
of the module's directory. There are two primary advantages
to following this rule: First, any modules which attempt to
write within themselves will not be compatible with phar
packaging. Second, by keeping the module in sync with the
upstream distribution, updates via mechanisms such as Git
will be simple and trouble-free. Of course, the Application
module is a special exception to this rule, as there is
typically no upstream distribution for this module, and
it's unlikely you would want to run this package from
within a phar archive.
</para>
</listitem>
<listitem>
<para>
Utilize a vendor prefix for module names. To avoid module
naming conflicts, you are encouraged to prefix your module
namespace with a vendor prefix. As an example, the
(incomplete) developer tools module distributed by Zend is
named "ZendDeveloperTools" instead of simply
"DeveloperTools".
</para>
</listitem>
</itemizedlist>
</section>
</section>