Zend_Application-TheoryOfOperation.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.application.theory-of-operation">
  4. <title>Theory of Operation</title>
  5. <para>
  6. Getting an <acronym>MVC</acronym> application configured and ready to dispatch has
  7. required an increasing amount of code as more features become
  8. available: setting up the database, configuring your view and view
  9. helpers, configuring your layouts, registering plugins, registering
  10. action helpers, and more.
  11. </para>
  12. <para>
  13. Additionally, you will often want to reuse the same code to
  14. bootstrap your tests, a cronjob, or a service script. While it's
  15. possible to simply include your bootstrap script, oftentimes there
  16. are initializations that are environment specific – you may not need
  17. the <acronym>MVC</acronym> for a cronjob, or just the DB layer for a service script.
  18. </para>
  19. <para>
  20. <classname>Zend_Application</classname> aims to make this easier and to
  21. promote reuse by encapsulating bootstrapping into <acronym>OOP</acronym> paradigms.
  22. </para>
  23. <para>
  24. <classname>Zend_Application</classname> is broken into three realms:
  25. </para>
  26. <itemizedlist>
  27. <listitem>
  28. <para>
  29. <classname>Zend_Application</classname>: loads the <acronym>PHP</acronym>
  30. environment, including include_paths and autoloading, and instantiates
  31. the requested bootstrap class.
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. <classname>Zend_Application_Bootstrap</classname>: provides
  37. interfaces for bootstrap classes.
  38. <classname>Zend_Application_Bootstrap_Bootstrap</classname> provides common
  39. functionality for most bootstrapping needs, including
  40. dependency checking algorithms and the ability to load
  41. bootstrap resources on demand.
  42. </para>
  43. </listitem>
  44. <listitem>
  45. <para>
  46. <classname>Zend_Application_Resource</classname> provides an
  47. interface for standard bootstrapping resources that can be
  48. loaded on demand by a bootstrap instance, as well as several
  49. default resource implementations.
  50. </para>
  51. </listitem>
  52. </itemizedlist>
  53. <para>
  54. Developers create a bootstrap class for their application, extending
  55. <classname>Zend_Application_Bootstrap_Bootstrap</classname> or implementing (minimally)
  56. <classname>Zend_Application_Bootstrap_Bootstrapper</classname>. The entry point
  57. (e.g., <filename>public/index.php</filename>) will load
  58. <classname>Zend_Application</classname>, and instantiate it by passing:
  59. </para>
  60. <itemizedlist>
  61. <listitem>
  62. <para>
  63. The current environment
  64. </para>
  65. </listitem>
  66. <listitem>
  67. <para>
  68. Options for bootstrapping
  69. </para>
  70. </listitem>
  71. </itemizedlist>
  72. <para>
  73. The bootstrap options include the path to the file containing the
  74. bootstrap class and optionally:
  75. </para>
  76. <itemizedlist>
  77. <listitem>
  78. <para>
  79. Any extra include_paths to set
  80. </para>
  81. </listitem>
  82. <listitem>
  83. <para>
  84. Any additional autoloader namespaces to register
  85. </para>
  86. </listitem>
  87. <listitem>
  88. <para>
  89. Any <filename>php.ini</filename> settings to initialize
  90. </para>
  91. </listitem>
  92. <listitem>
  93. <para>
  94. The class name for the bootstrap class (if not "Bootstrap")
  95. </para>
  96. </listitem>
  97. <listitem>
  98. <para>
  99. Resource prefix to path pairs to use
  100. </para>
  101. </listitem>
  102. <listitem>
  103. <para>
  104. Any resources to use (by class name or short name)
  105. </para>
  106. </listitem>
  107. <listitem>
  108. <para>
  109. Additional path to a configuration file to load
  110. </para>
  111. </listitem>
  112. <listitem>
  113. <para>
  114. Additional configuration options
  115. </para>
  116. </listitem>
  117. </itemizedlist>
  118. <para>
  119. Options may be an array, a <classname>Zend_Config</classname> object, or the path
  120. to a configuration file.
  121. </para>
  122. <sect2 id="zend.application.theory-of-operation.bootstrap">
  123. <title>Bootstrapping</title>
  124. <para>
  125. <classname>Zend_Application</classname>'s second area of responsibility is
  126. executing the application bootstrap. Bootstraps minimally need to
  127. implement <classname>Zend_Application_Bootstrap_Bootstrapper</classname>,
  128. which defines the following <acronym>API</acronym>:
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. interface Zend_Application_Bootstrap_Bootstrapper
  132. {
  133. public function __construct($application);
  134. public function setOptions(array $options);
  135. public function getApplication();
  136. public function getEnvironment();
  137. public function getClassResources();
  138. public function getClassResourceNames();
  139. public function bootstrap($resource = null);
  140. public function run();
  141. }
  142. ]]></programlisting>
  143. <para>
  144. This <acronym>API</acronym> allows the bootstrap to accept the environment and
  145. configuration from the application object, report the resources its
  146. responsible for bootstrapping, and then bootstrap and run the
  147. application.
  148. </para>
  149. <para>
  150. You can implement this interface on your own, extend
  151. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>, or use
  152. <classname>Zend_Application_Bootstrap_Bootstrap</classname>.
  153. </para>
  154. <para>
  155. Besides this functionality, there are a number of other areas of
  156. concern you should familiarize yourself with.
  157. </para>
  158. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-methods">
  159. <title>Resource Methods</title>
  160. <para>
  161. The <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  162. implementation provides a simple convention for defining class
  163. resource methods. Any protected method beginning with a name
  164. prefixed with <emphasis>_init</emphasis> will be considered a resource
  165. method.
  166. </para>
  167. <para>
  168. To bootstrap a single resource method, use the
  169. <methodname>bootstrap()</methodname> method, and pass it the name of the
  170. resource. The name will be the method name minus the
  171. <emphasis>_init</emphasis> prefix.
  172. </para>
  173. <para>
  174. To bootstrap several resource methods, pass an array of names.
  175. Too bootstrap all resource methods, pass nothing.
  176. </para>
  177. <para>
  178. Take the following bootstrap class:
  179. </para>
  180. <programlisting language="php"><![CDATA[
  181. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  182. {
  183. protected function _initFoo()
  184. {
  185. // ...
  186. }
  187. protected function _initBar()
  188. {
  189. // ...
  190. }
  191. protected function _initBaz()
  192. {
  193. // ...
  194. }
  195. }
  196. ]]></programlisting>
  197. <para>
  198. To bootstrap just the <methodname>_initFoo()</methodname> method, do the
  199. following:
  200. </para>
  201. <programlisting language="php"><![CDATA[
  202. $bootstrap->bootstrap('foo');
  203. ]]></programlisting>
  204. <para>
  205. To bootstrap the <methodname>_initFoo()</methodname> and
  206. <methodname>_initBar()</methodname> methods, do the following:
  207. </para>
  208. <programlisting language="php"><![CDATA[
  209. $bootstrap->bootstrap(array('foo', 'bar'));
  210. ]]></programlisting>
  211. <para>
  212. To bootstrap all resource methods, call <methodname>bootstrap()</methodname>
  213. with no arguments:
  214. </para>
  215. <programlisting language="php"><![CDATA[
  216. $bootstrap->bootstrap();
  217. ]]></programlisting>
  218. </sect3>
  219. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-plugins">
  220. <title>Bootstraps that use resource plugins</title>
  221. <para>
  222. To make your bootstraps more re-usable, we have provided the
  223. ability to push your resources into resource plugin classes.
  224. This allows you to mix and match resources simply via
  225. configuration. We will cover <link
  226. linkend="zend.application.theory-of-operation.resources">how
  227. to create resources</link> later; in
  228. this section we will show you how to utilize them only.
  229. </para>
  230. <para>
  231. If your bootstrap should be capable of using resource plugins,
  232. you will need to implement an additional interface,
  233. <classname>Zend_Application_Bootstrap_ResourceBootstrapper</classname>.
  234. This interface defines an <acronym>API</acronym> for locating, registering, and
  235. loading resource plugins:
  236. </para>
  237. <programlisting language="php"><![CDATA[
  238. interface Zend_Application_Bootstrap_ResourceBootstrapper
  239. {
  240. public function registerPluginResource($resource, $options = null);
  241. public function unregisterPluginResource($resource);
  242. public function hasPluginResource($resource);
  243. public function getPluginResource($resource);
  244. public function getPluginResources();
  245. public function getPluginResourceNames();
  246. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
  247. public function getPluginLoader();
  248. }
  249. ]]></programlisting>
  250. <para>
  251. Resource plugins basically provide the ability to create
  252. resource intializers that can be re-used between applications.
  253. This allows you to keep your actual bootstrap relatively clean,
  254. and to introduce new resources without needing to touch your
  255. bootstrap itself.
  256. </para>
  257. <para>
  258. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname> (and
  259. <classname>Zend_Application_Bootstrap_Bootstrap</classname> by extension)
  260. implement this interface as well, allowing you to utilize
  261. resource plugins.
  262. </para>
  263. <para>
  264. To utilize resource plugins, you must specify them in the
  265. options passed to the application object and/or bootstrap. These
  266. options may come from a configuration file, or be passed in
  267. manually. Options will be of key to options pairs, with the key
  268. representing the resource name. The resource name will be the
  269. segment following the class prefix. For example, the resources
  270. shipped with Zend Framework have the class prefix
  271. "<classname>Zend_Application_Resource_</classname>"; anything following this would
  272. be the name of the resource. As an example,
  273. </para>
  274. <programlisting language="php"><![CDATA[
  275. $application = new Zend_Application(APPLICATION_ENV, array(
  276. 'resources' => array(
  277. 'FrontController' => array(
  278. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  279. ),
  280. ),
  281. ));
  282. ]]></programlisting>
  283. <para>
  284. This indicates that the "FrontController" resource should be
  285. used, with the options specified.
  286. </para>
  287. <para>
  288. If you begin writing your own resource plugins, or utilize
  289. third-party resource plugins, you will need to tell your
  290. bootstrap where to look for them. Internally, the bootstrap
  291. utilizes <classname>Zend_Loader_PluginLoader</classname>, so you will only
  292. need to indicate the common class prefix an path pairs.
  293. </para>
  294. <para>
  295. As an example, let's assume you have custom resource plugins in
  296. <filename>APPLICATION_PATH/resources/</filename> and that they share the
  297. common class prefix of <classname>My_Resource</classname>. You would then
  298. pass that information to the application object as follows:
  299. </para>
  300. <programlisting language="php"><![CDATA[
  301. $application = new Zend_Application(APPLICATION_ENV, array(
  302. 'pluginPaths' => array(
  303. 'My_Resource' => APPLICATION_PATH . '/resources/',
  304. ),
  305. 'resources' => array(
  306. 'FrontController' => array(
  307. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  308. ),
  309. ),
  310. ));
  311. ]]></programlisting>
  312. <para>
  313. You would now be able to use resources from that directory.
  314. </para>
  315. <para>
  316. Just like resource methods, you use the <methodname>bootstrap()</methodname>
  317. method to execute resource plugins. Just like with resource
  318. methods, you can specify either a single resource plugin,
  319. multiple plugins (via an array), or all plugins. Additionally,
  320. you can mix and match to execute resource methods as well.
  321. </para>
  322. <programlisting language="php"><![CDATA[
  323. // Execute one:
  324. $bootstrap->bootstrap('FrontController');
  325. // Execute several:
  326. $bootstrap->bootstrap(array('FrontController', 'Foo'));
  327. // Execute all resource methods and plugins:
  328. $bootstrap->bootstrap();
  329. ]]></programlisting>
  330. </sect3>
  331. <sect3 id="zend.application.theory-of-operation.bootstrap.registry">
  332. <title>Resource Registry</title>
  333. <para>
  334. Many, if not all, of your resource methods or plugins will
  335. initialize objects, and in many cases, these objects will be
  336. needed elsewhere in your application. How can you access them?
  337. </para>
  338. <para>
  339. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  340. provides a local registry for these objects. To store your
  341. objects in them, you simply return them from your resources.
  342. </para>
  343. <para>
  344. For maximum flexibility, this registry is referred to as a
  345. "container" internally; its only requirements are that it is an
  346. object. Resources are then registered as properties named after
  347. the resource name. By default, an instance of
  348. <classname>Zend_Registry</classname> is used, but you may also specify any
  349. other object you wish. The methods <methodname>setContainer()</methodname>
  350. and <methodname>getContainer()</methodname> may be used to manipulate the
  351. container itself. <methodname>getResource($resource)</methodname> can be
  352. used to fetch a given resource from the container, and
  353. <methodname>hasResource($resource)</methodname> to check if the resource has
  354. actually been registered.
  355. </para>
  356. <para>
  357. As an example, consider a basic view resource:
  358. </para>
  359. <programlisting language="php"><![CDATA[
  360. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  361. {
  362. protected function _initView()
  363. {
  364. $view = new Zend_View();
  365. // more initialization...
  366. return $view;
  367. }
  368. }
  369. ]]></programlisting>
  370. <para>
  371. You can then check for it and/or fetch it as follows:
  372. </para>
  373. <programlisting language="php"><![CDATA[
  374. // Using the has/getResource() pair:
  375. if ($bootstrap->hasResource('view')) {
  376. $view = $bootstrap->getResource('view');
  377. }
  378. // Via the container:
  379. $container = $bootstrap->getContainer();
  380. if (isset($container->view)) {
  381. $view = $container->view;
  382. }
  383. ]]></programlisting>
  384. <para>
  385. Please note that the registry and also the container is not global. This
  386. means that you need access to the bootstrap in order to fetch
  387. resources. <classname>Zend_Application_Bootstrap_Bootstrap</classname>
  388. provides some convenience for this: during its
  389. <methodname>run()</methodname> execution, it registers itself as the front
  390. controller parameter "bootstrap", which allows you to fetch it
  391. from the router, dispatcher, plugins, and action controllers.
  392. </para>
  393. <para>
  394. As an example, if you wanted access to the view resource from
  395. above within your action controller, you could do the following:
  396. </para>
  397. <programlisting language="php"><![CDATA[
  398. class FooController extends Zend_Controller_Action
  399. {
  400. public function init()
  401. {
  402. $bootstrap = $this->getInvokeArg('bootstrap');
  403. $view = $bootstrap->getResource('view');
  404. // ...
  405. }
  406. }
  407. ]]></programlisting>
  408. </sect3>
  409. <sect3 id="zend.application.theory-of-operation.bootstrap.dependency-tracking">
  410. <title>Dependency Tracking</title>
  411. <para>
  412. In addition to executing resource methods and plugins, it's
  413. necessary to ensure that these are executed once and once
  414. only; these are meant to bootstrap an application, and
  415. executing multiple times can lead to resource overhead.
  416. </para>
  417. <para>
  418. At the same time, some resources may depend on other
  419. resources being executed. To solve these two issues,
  420. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  421. provides a simple, effective mechanism for dependency
  422. tracking.
  423. </para>
  424. <para>
  425. As noted previously, all resources -- whether methods or plugins
  426. -- are bootstrapped by calling <methodname>bootstrap($resource)</methodname>,
  427. where <varname>$resource</varname> is the name of a resource, an array
  428. of resources, or, left empty, indicates all resources should be
  429. run.
  430. </para>
  431. <para>
  432. If a resource depends on another resource, it should call
  433. <methodname>bootstrap()</methodname> within its code to ensure that resource
  434. has been executed. Subsequent calls to it will then be ignored.
  435. </para>
  436. <para>
  437. In a resource method, such a call would look like this:
  438. </para>
  439. <programlisting language="php"><![CDATA[
  440. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  441. {
  442. protected function _initRequest()
  443. {
  444. // Ensure the front controller is initialized
  445. $this->bootstrap('FrontController');
  446. // Retrieve the front controller from the bootstrap registry
  447. $front = $this->getResource('FrontController');
  448. $request = new Zend_Controller_Request_Http();
  449. $request->setBaseUrl('/foo');
  450. $front->setRequest($request);
  451. // Ensure the request is stored in the bootstrap registry
  452. return $request;
  453. }
  454. }
  455. ]]></programlisting>
  456. </sect3>
  457. </sect2>
  458. <sect2 id="zend.application.theory-of-operation.resources">
  459. <title>Resource Plugins</title>
  460. <para>
  461. <link
  462. linkend="zend.application.theory-of-operation.bootstrap.resource-plugins">As noted
  463. previously</link>, a good way to create re-usable bootstrap resources and to
  464. offload much of your coding to discrete classes is to utilize resource
  465. plugins. While Zend Framework ships with a number of standard
  466. resource plugins, the intention is that developers should write
  467. their own to encapsulate their own initialization needs.
  468. </para>
  469. <para>
  470. Resource plugins need only implement
  471. <classname>Zend_Application_Resource_Resource</classname>, or, more simply
  472. still, extend
  473. <classname>Zend_Application_Resource_ResourceAbstract</classname>. The basic
  474. interface is simply this:
  475. </para>
  476. <programlisting language="php"><![CDATA[
  477. interface Zend_Application_Resource_Resource
  478. {
  479. public function __construct($options = null);
  480. public function setBootstrap(
  481. Zend_Application_Bootstrap_Bootstrapper $bootstrap
  482. );
  483. public function getBootstrap();
  484. public function setOptions(array $options);
  485. public function getOptions();
  486. public function init();
  487. }
  488. ]]></programlisting>
  489. <para>
  490. The interface defines simply that a resource plugin should accept options
  491. to the constructor, have mechanisms for setting and retrieving
  492. options, have mechanisms for setting and retrieving the bootstrap
  493. object, and an initialization method.
  494. </para>
  495. <para>
  496. As an example, let's assume you have a common view intialization you
  497. use in your applications. You have a common doctype, <acronym>CSS</acronym> and
  498. JavaScript, and you want to be able to pass in a base document title
  499. via configuration. Such a resource plugin might look like this:
  500. </para>
  501. <programlisting language="php"><![CDATA[
  502. class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
  503. {
  504. protected $_view;
  505. public function init()
  506. {
  507. // Return view so bootstrap will store it in the registry
  508. return $this->getView();
  509. }
  510. public function getView()
  511. {
  512. if (null === $this->_view) {
  513. $options = $this->getOptions();
  514. $title = '';
  515. if (array_key_exists('title', $options)) {
  516. $title = $options['title'];
  517. unset($options['title']);
  518. }
  519. $view = new Zend_View($options);
  520. $view->doctype('XHTML1_STRICT');
  521. $view->headTitle($title);
  522. $view->headLink()->appendStylesheet('/css/site.css');
  523. $view->headScript()->appendfile('/js/analytics.js');
  524. $viewRenderer =
  525. Zend_Controller_Action_HelperBroker::getStaticHelper(
  526. 'ViewRenderer'
  527. );
  528. $viewRenderer->setView($view);
  529. $this->_view = $view;
  530. }
  531. return $this->_view;
  532. }
  533. }
  534. ]]></programlisting>
  535. <para>
  536. As long as you register the prefix path for this resource plugin,
  537. you can then use it in your application. Even better, because it
  538. uses the plugin loader, you are effectively overriding the shipped
  539. "View" resource plugin, ensuring that your own is used instead.
  540. </para>
  541. </sect2>
  542. </sect1>