Zend_Application-TheoryOfOperation.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 15334 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.application.theory-of-operation">
  5. <title>Theorie der Funktionsweise</title>
  6. <para>
  7. Um eine MVC Anwendung zu konfigurieren und bereit zur Auslieferung zu bekommen wird ein
  8. immer größerer Anteil an Code benötigt sobald mehr Features verfügbar sind: Setzen der
  9. Datenbank, Konfiguration der View und View Helfer, Konfiguration der Layouts, Registrierung
  10. der Plugins, Registrierung der Aktion Helfer, und andere.
  11. </para>
  12. <para>
  13. Zusätzlich will man oft den gleichen Code wiederverwenden um Tests zu bootstrappen, einen
  14. Cronjob, oder ein Service Skript zu haben. Wärend es sehr einfach ist sein Bootstrap Skript
  15. einzubauen, gibt es oft Initialisierungen die abhängig sind von der Umgebung - man könnte
  16. kein MVC für einen Cronjob benötigen, oder nur den DB Layer für ein Service Skript.
  17. </para>
  18. <para>
  19. <classname>Zend_Application</classname> zielt darauf ab das alles einfacher zu machen und bietet
  20. Wiederwervendbarkeit durch die Kapselung vom Bootstrapping in OOP Paradigmen.
  21. </para>
  22. <para>
  23. <classname>Zend_Application</classname> ist in drei Bereiche geteilt:
  24. </para>
  25. <itemizedlist>
  26. <listitem>
  27. <para>
  28. <classname>Zend_Application</classname>: Lädt die PHP Umgebung, inklusive Inklude-Pfade und
  29. Autoloading, und instanziiert die benötigte Bootstrap Klasse.
  30. </para>
  31. </listitem>
  32. <listitem>
  33. <para>
  34. <classname>Zend_Application_Bootstrap</classname>: Bietet Interfaces für Bootstrap Klassen.
  35. <classname>Zend_Application_Bootstrap_Bootstrap</classname> bietet übliche Funktionalitäten für
  36. die meisten Notwendigkeiten des Bootstrappings, inklusive Angorithmen um
  37. Abhängigkeiten zu prüfen und die Möglichkeit Bootstrap Ressourcen bei Bedarf zu
  38. laden.
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. <classname>Zend_Application_Resource</classname> bietet ein Interface für Standard
  44. Bootstrap Ressourcen die bei Bedarf von einer Bootstrap Instanz geladen werden
  45. können, sowie verschiedene standardmäßige Ressource Implementationen.
  46. </para>
  47. </listitem>
  48. </itemizedlist>
  49. <para>
  50. Entwickler erstellen eine Bootstrap Klasse für Ihre Anwendung und erweitern
  51. <classname>Zend_Application_Bootstrap_Bootstrap</classname> oder implementieren (mindestens)
  52. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>. Der Einstiegspunkt
  53. (z.B. <filename>public/index.php</filename>) lädt <classname>Zend_Application</classname>
  54. und initialisiert Sie, indem folgendes übergeben wird:
  55. </para>
  56. <itemizedlist>
  57. <listitem>
  58. <para>
  59. Die aktuelle Umgebung
  60. </para>
  61. </listitem>
  62. <listitem>
  63. <para>
  64. Optionen für das Bootstrapping
  65. </para>
  66. </listitem>
  67. </itemizedlist>
  68. <para>
  69. Die Bootstrap Optionen enthalten den Pfad zur Datei die die Bootstrap Klasse enthält und
  70. optional:
  71. </para>
  72. <itemizedlist>
  73. <listitem>
  74. <para>
  75. Jeden include_path der extra zu setzen ist
  76. </para>
  77. </listitem>
  78. <listitem>
  79. <para>
  80. Jeden Autoloader Namespace der zusätzlich zu registrieren ist
  81. </para>
  82. </listitem>
  83. <listitem>
  84. <para>
  85. Jede php.ini Einstellung die zu initialisieren ist
  86. </para>
  87. </listitem>
  88. <listitem>
  89. <para>
  90. Den Klassennamen für die Bootstrap Klasse (wenn diese nicht "Bootstrap" ist)
  91. </para>
  92. </listitem>
  93. <listitem>
  94. <para>
  95. Ressourcen Präfix/Pfad Paare die zu verwenden sind
  96. </para>
  97. </listitem>
  98. <listitem>
  99. <para>
  100. Jede Ressource die zu verwenden ist (durch Name der Klasse oder Kurzname)
  101. </para>
  102. </listitem>
  103. <listitem>
  104. <para>
  105. Zusätzliche Pfade zu einer Konfigurationsdatei die zu laden ist
  106. </para>
  107. </listitem>
  108. <listitem>
  109. <para>
  110. Zusätzliche Konfigurations Optionen
  111. </para>
  112. </listitem>
  113. </itemizedlist>
  114. <para>
  115. Optionen können, ein Array, ein <classname>Zend_Config</classname> Objekt, oder der Pfad zu
  116. einer Konfigurationsdatei sein.
  117. </para>
  118. <sect2 id="zend.application.theory-of-operation.bootstrap">
  119. <title>Bootstrapping</title>
  120. <para>
  121. <classname>Zend_Application</classname>'s zweites Feld der Arbeit ist die Ausführung des
  122. Bootstraps der Anwendung. Bootstraps müssen mindestens
  123. <classname>Zend_Application_Bootstrap_Bootstrapper</classname> implementieren, welches die
  124. folgende API definiert:
  125. </para>
  126. <programlisting role="php"><![CDATA[
  127. interface Zend_Application_Bootstrap_Bootstrapper
  128. {
  129. public function __construct($application);
  130. public function setOptions(array $options);
  131. public function getApplication();
  132. public function getEnvironment();
  133. public function getClassResources();
  134. public function getClassResourceNames();
  135. public function bootstrap($resource = null);
  136. public function run();
  137. }
  138. ]]></programlisting>
  139. <para>
  140. This API allows the bootstrap to accept the environment and
  141. configuration from the application object, report the resources its
  142. responsible for bootstrapping, and then bootstrap and run the
  143. application.
  144. </para>
  145. <para>
  146. You can implement this interface on your own, extend
  147. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>, or use
  148. <classname>Zend_Application_Bootstrap_Bootstrap</classname>.
  149. </para>
  150. <para>
  151. Besides this functionality, there are a number of other areas of
  152. concern you should familiarize yourself with.
  153. </para>
  154. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-methods">
  155. <title>Resource Methods</title>
  156. <para>
  157. The <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  158. implementation provides a simple convention for defining class
  159. resource methods. Any protected method beginning with a name
  160. prefixed with <code>_init</code> will be considered a resource
  161. method.
  162. </para>
  163. <para>
  164. To bootstrap a single resource method, use the
  165. <code>bootstrap()</code> method, and pass it the name of the
  166. resource. The name will be the method name minus the
  167. <code>_init</code> prefix.
  168. </para>
  169. <para>
  170. To bootstrap several resource methods, pass an array of names.
  171. Too bootstrap all resource methods, pass nothing.
  172. </para>
  173. <para>
  174. Take the following bootstrap class:
  175. </para>
  176. <programlisting role="php"><![CDATA[
  177. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  178. {
  179. protected function _initFoo()
  180. {
  181. // ...
  182. }
  183. protected function _initBar()
  184. {
  185. // ...
  186. }
  187. protected function _initBaz()
  188. {
  189. // ...
  190. }
  191. }
  192. ]]></programlisting>
  193. <para>
  194. $o bootstrap just the <code>_initFoo()</code> method, do the
  195. following:
  196. </para>
  197. <programlisting role="php"><![CDATA[
  198. $bootstrap->bootstrap('foo');
  199. ]]></programlisting>
  200. <para>
  201. To bootstrap the <code>_initFoo()</code> and
  202. <code>_initBar()</code> methods, do the following:
  203. </para>
  204. <programlisting role="php"><![CDATA[
  205. $bootstrap->bootstrap(array('foo', 'bar));
  206. ]]></programlisting>
  207. <para>
  208. To bootstrap all resource methods, call <code>bootstrap()</code>
  209. with no arguments:
  210. </para>
  211. <programlisting role="php"><![CDATA[
  212. $bootstrap->bootstrap();
  213. ]]></programlisting>
  214. </sect3>
  215. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-plugins">
  216. <title>Bootstraps that use resource plugins</title>
  217. <para>
  218. To make your bootstraps more re-usable, we have provided the
  219. ability to push your resources into resource plugin classes.
  220. This allows you to mix and match resources simply via
  221. configuration. We will cover <link
  222. linkend="zend.application.theory-of-operation.resources">how
  223. to create resources</link> later; in
  224. this section we will show you how to utilize them only.
  225. </para>
  226. <para>
  227. If your bootstrap should be capable of using resource plugins,
  228. you will need to implement an additional interface,
  229. <classname>Zend_Application_Bootstrap_ResourceBootstrapper</classname>.
  230. This interface defines an API for locating, registering, and
  231. loading resource plugins:
  232. </para>
  233. <programlisting role="php"><![CDATA[
  234. interface Zend_Application_Bootstrap_ResourceBootstrapper
  235. {
  236. public function registerPluginResource($resource, $options = null);
  237. public function unregisterPluginResource($resource);
  238. public function hasPluginResource($resource);
  239. public function getPluginResource($resource);
  240. public function getPluginResources();
  241. public function getPluginResourceNames();
  242. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
  243. public function getPluginLoader();
  244. }
  245. ]]></programlisting>
  246. <para>
  247. Resource plugins basically provide the ability to create
  248. resource intializers that can be re-used between applications.
  249. This allows you to keep your actual bootstrap relatively clean,
  250. and to introduce new resources without needing to touch your
  251. bootstrap itself.
  252. </para>
  253. <para>
  254. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname> (and
  255. <classname>Zend_Application_Bootstrap_Bootstrap</classname> by extension)
  256. implement this interface as well, allowing you to utilize
  257. resource plugins.
  258. </para>
  259. <para>
  260. To utilize resource plugins, you must specify them in the
  261. options passed to the application object and/or bootstrap. These
  262. options may come from a configuration file, or be passed in
  263. manually. Options will be of key/options pairs, with the key
  264. representing the resource name. The resource name will be the
  265. segment following the class prefix. For example, the resources
  266. shipped with Zend Framework have the class prefix
  267. "Zend_Application_Resource_"; anything following this would be
  268. the name of the resource. As an example,
  269. </para>
  270. <programlisting role="php"><![CDATA[
  271. $application = new Zend_Application(APPLICATION_ENV, array(
  272. 'resources' => array(
  273. 'FrontController' => array(
  274. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  275. ),
  276. ),
  277. ));
  278. ]]></programlisting>
  279. <para>
  280. This indicates that the "FrontController" resource should be
  281. used, with the options specified.
  282. </para>
  283. <para>
  284. If you begin writing your own resource plugins, or utilize
  285. third-party resource plugins, you will need to tell your
  286. bootstrap where to look for them. Internally, the bootstrap
  287. utilizes <classname>Zend_Loader_PluginLoader</classname>, so you will only
  288. need to indicate the common class prefix an path pairs.
  289. </para>
  290. <para>
  291. As an example, let's assume you have custom resource plugins in
  292. <code>APPLICATION_PATH/resources/</code> and that they share the
  293. common class prefix of <code>My_Resource</code>. You would then
  294. pass that information to the application object as follows:
  295. </para>
  296. <programlisting role="php"><![CDATA[
  297. $application = new Zend_Application(APPLICATION_ENV, array(
  298. 'pluginPaths' => array(
  299. 'My_Resource' => APPLICATION_PATH . '/resources/',
  300. ),
  301. 'resources' => array(
  302. 'FrontController' => array(
  303. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  304. ),
  305. ),
  306. ));
  307. ]]></programlisting>
  308. <para>
  309. You would now be able to use resources from that directory.
  310. </para>
  311. <para>
  312. Just like resource methods, you use the <code>bootstrap()</code>
  313. method to execute resource plugins. Just like with resource
  314. methods, you can specify either a single resource plugin,
  315. multiple plugins (via an array), or all plugins. Additionally,
  316. you can mix and match to execute resource methods as well.
  317. </para>
  318. <programlisting role="php"><![CDATA[
  319. // Execute one:
  320. $bootstrap->bootstrap('FrontController');
  321. // Execute several:
  322. $bootstrap->bootstrap(array('FrontController', 'Foo'));
  323. // Execute all resource methods and plugins:
  324. $bootstrap->bootstrap();
  325. ]]></programlisting>
  326. </sect3>
  327. <sect3 id="zend.application.theory-of-operation.bootstrap.registry">
  328. <title>Resource Registry</title>
  329. <para>
  330. Many, if not all, of your resource methods or plugins will
  331. initialize objects, and in many cases, these objects will be
  332. needed elsewhere in your application. How can you access them?
  333. </para>
  334. <para>
  335. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  336. provides a local registry for these objects. To store your
  337. objects in them, you simply return them from your resources.
  338. </para>
  339. <para>
  340. For maximum flexibility, this registry is referred to as a
  341. "container" internally; its only requirements are that it is an
  342. object. Resources are then registered as properties named after
  343. the resource name. By default, an instance of
  344. <classname>Zend_Registry</classname> is used, but you may also specify any
  345. other object you wish. The methods <code>setContainer()</code>
  346. and <code>getContainer()</code> may be used to manipulate the
  347. container itself. <code>getResource($resource)</code> can be
  348. used to fetch a given resource from the container, and
  349. <code>hasResource($resource)</code> to check if the resource has
  350. actually been registered.
  351. </para>
  352. <para>
  353. As an example, consider a basic view resource:
  354. </para>
  355. <programlisting role="php"><![CDATA[
  356. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  357. {
  358. protected function _initView()
  359. {
  360. $view = new Zend_View();
  361. // more initialization...
  362. return $view;
  363. }
  364. }
  365. ]]></programlisting>
  366. <para>
  367. You can then check for it and/or fetch it as follows:
  368. </para>
  369. <programlisting role="php"><![CDATA[
  370. // Using the has/getResource() pair:
  371. if ($bootstrap->hasResource('view')) {
  372. $view = $bootstrap->getResource('view');
  373. }
  374. // Via the container:
  375. $container = $bootstrap->getContainer();
  376. if (isset($container->view)) {
  377. $view = $container->view;
  378. }
  379. ]]></programlisting>
  380. <para>
  381. Please note that the registry/container is not global. This
  382. means that you need access to the bootstrap in order to fetch
  383. resources. <classname>Zend_Application_Bootstrap_Bootstrap</classname>
  384. provides some convenience for this: during its
  385. <code>run()</code> execution, it registers itself as the front
  386. controller parameter "bootstrap", which allows you to fetch it
  387. from the router, dispatcher, plugins, and action controllers.
  388. </para>
  389. <para>
  390. As an example, if you wanted access to the view resource from
  391. above within your action controller, you could do the following:
  392. </para>
  393. <programlisting role="php"><![CDATA[
  394. class FooController extends Zend_Controller_Action
  395. {
  396. public function init()
  397. {
  398. $bootstrap = $this->getInvokeArg('bootstrap');
  399. $view = $bootstrap->getResource('view');
  400. // ...
  401. }
  402. }
  403. ]]></programlisting>
  404. </sect3>
  405. <sect3 id="zend.application.theory-of-operation.bootstrap.dependency-tracking">
  406. <title>Dependency Tracking</title>
  407. <para>
  408. In addition to executing resource methods and plugins, it's
  409. necessary to ensure that these are executed once and once
  410. only; these are meant to bootstrap an application, and
  411. executing multiple times can lead to resource overhead.
  412. </para>
  413. <para>
  414. At the same time, some resources may depend on other
  415. resources being executed. To solve these two issues,
  416. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  417. provides a simple, effective mechanism for dependency
  418. tracking.
  419. </para>
  420. <para>
  421. As noted previously, all resources -- whether methods or plugins
  422. -- are bootstrapped by calling <code>bootstrap($resource)</code>,
  423. where <code>$resource</code> is the name of a resource, an array
  424. of resources, or, left empty, indicates all resources should be
  425. run.
  426. </para>
  427. <para>
  428. If a resource depends on another resource, it should call
  429. <code>bootstrap()</code> within its code to ensure that resource
  430. has been executed. Subsequent calls to it will then be ignored.
  431. </para>
  432. <para>
  433. In a resource method, such a call would look like this:
  434. </para>
  435. <programlisting role="php"><![CDATA[
  436. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  437. {
  438. protected function _initRequest()
  439. {
  440. // Ensure the front controller is initialized
  441. $this->bootstrap('FrontController');
  442. // Retrieve the front controller from the bootstrap registry
  443. $front = $this->getResource('FrontController');
  444. $request = new Zend_Controller_Request_Http();
  445. $request->setBaseUrl('/foo');
  446. $front->setRequest($request);
  447. // Ensure the request is stored in the bootstrap registry
  448. return $request;
  449. }
  450. }
  451. ]]></programlisting>
  452. </sect3>
  453. </sect2>
  454. <sect2 id="zend.application.theory-of-operation.resources">
  455. <title>Resource Plugins</title>
  456. <para>
  457. <link linkend="zend.application.theory-of-operation.bootstrap.resource-plugins">As noted previously</link>,
  458. a good way to create re-usable bootstrap resources and to offload
  459. much of your coding to discrete classes is to utilize resource
  460. plugins. While Zend Framework ships with a number of standard
  461. resource plugins, the intention is that developers should write
  462. their own to encapsulate their own intialization needs.
  463. </para>
  464. <para>
  465. Resources need only implement
  466. <classname>Zend_Application_Resource_Resource</classname>, or, more simply
  467. still, extend
  468. <classname>Zend_Application_Resource_ResourceAbstract</classname>. The basic
  469. interface is simply this:
  470. </para>
  471. <programlisting role="php"><![CDATA[
  472. interface Zend_Application_Resource_Resource
  473. {
  474. public function __construct($options = null);
  475. public function setBootstrap(
  476. Zend_Application_Bootstrap_Bootstrapper $bootstrap
  477. );
  478. public function getBootstrap();
  479. public function setOptions(array $options);
  480. public function getOptions();
  481. public function init();
  482. }
  483. ]]></programlisting>
  484. <para>
  485. The interface defines simply that a resource should accept options
  486. to the constructor, have mechanisms for setting and retrieving
  487. options, have mechanisms for setting and retrieving the bootstrap
  488. object, and an initialization method.
  489. </para>
  490. <para>
  491. As an example, let's assume you have a common view intialization you
  492. use in your applications. You have a common doctype, CSS and
  493. JavaScript, and you want to be able to pass in a base document title
  494. via configuration. Such a resource might look like this:
  495. </para>
  496. <programlisting role="php"><![CDATA[
  497. class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
  498. {
  499. protected $_view;
  500. public function init()
  501. {
  502. // Return view so bootstrap will store it in the registry
  503. return $this->getView();
  504. }
  505. public function getView()
  506. {
  507. if (null === $this->_view) {
  508. $options = $this->getOptions();
  509. $title = '';
  510. if (array_key_exists('title', $options)) {
  511. $title = $options['title'];
  512. unset($options['title']);
  513. }
  514. $view = new Zend_View($options);
  515. $view->doctype('XHTML1_STRICT');
  516. $view->headTitle($title);
  517. $view->headLink()->appendStylesheet('/css/site.css');
  518. $view->headScript()->appendfile('/js/analytics.js');
  519. $viewRenderer =
  520. Zend_Controller_Action_HelperBroker::getStaticHelper(
  521. 'ViewRenderer',
  522. );
  523. $viewRenderer->setView($view);
  524. $this->_view = $view;
  525. }
  526. return $this->_view;
  527. }
  528. }
  529. ]]></programlisting>
  530. <para>
  531. As long as you register the prefix path for this resource plugin,
  532. you can then use it in your application. Even better, because it
  533. uses the plugin loader, you are effectively overriding the shipped
  534. "View" resource plugin, ensuring that your own is used instead.
  535. </para>
  536. </sect2>
  537. </sect1>