Zend_Application-TheoryOfOperation.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 17397 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.application.theory-of-operation">
  5. <title>Működési elv</title>
  6. <para>
  7. Egy <acronym>MVC</acronym> alkalmazás beállítása és felkészítése az indulásra egyre növekvő
  8. mennyiségű kódot igényelt, ahogy egyre több és több lehetőség állt rendelkezésre: beállítani
  9. az adatbázist, a nézetet és a nézet segédeket, az elrendezéseket, bejegyezni a
  10. bővítményeket, művelet segédeket és a többi.
  11. </para>
  12. <para>
  13. Ezen felül gyakoran igény lehet ugyanazon kód használata a tesztek, ütemezett feladatok vagy
  14. egy webszolgáltatás rendszertöltéséhez. Miközben lehetséges egyszerűen beilleszteni a
  15. rendszertöltő állományt, gyakori, hogy bizonyos beállítások környezetre jellemzők – nem
  16. szükséges az <acronym>MVC</acronym> egy ütemezett feladathoz, vagy az adatbázis réteg egy
  17. szolgáltatáshoz.
  18. </para>
  19. <para>
  20. A <classname>Zend_Application</classname> célja ennek leegyszerűsítése és az újrahasznosítás
  21. elősegítése a rendszertöltés <acronym>OOP</acronym> mintákba zárásával.
  22. </para>
  23. <para>
  24. A <classname>Zend_Application</classname> három részre bontható:
  25. </para>
  26. <itemizedlist>
  27. <listitem>
  28. <para>
  29. <classname>Zend_Application</classname>: betölti a <acronym>PHP</acronym>
  30. környezetet, beleértve az include_path-t és az automatikus betöltést, illetve
  31. példányosítja a kért rendszertöltő osztályt.
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. <classname>Zend_Application_Bootstrap</classname>: felületeket nyújt a rendszertöltő
  37. osztályoknak. A <classname>Zend_Application_Bootstrap_Bootstrap</classname> a
  38. legtöbb rendszertöltővel szemben támasztható igényt kielégítő funkcionalitást nyújt,
  39. beleértve a függőség-ellenőrző algoritmusokat és a rendszertöltő erőforrások igény
  40. szerinti betöltését.
  41. </para>
  42. </listitem>
  43. <listitem>
  44. <para>
  45. A <classname>Zend_Application_Resource</classname> szabványos, egy rendszertöltő
  46. példány által igény szerint betölthető erőforrásokhoz nyújt felületet, csakúgy, mint
  47. több kész megvalósítást.
  48. </para>
  49. </listitem>
  50. </itemizedlist>
  51. <para>
  52. A fejlesztők a <classname>Zend_Application_Bootstrap_Bootstrap</classname> kiterjesztésével
  53. vagy (legkevesebb) a <classname>Zend_Application_Bootstrap_Bootstrapper</classname>
  54. megvalósításával hozhatnak létre rendszertöltőt alkalmazásukhoz. A belépési pont
  55. (<filename>public/index.php</filename>, pl.) betölti és példányosítja a
  56. <classname>Zend_Application</classname>-t
  57. </para>
  58. <itemizedlist>
  59. <listitem>
  60. <para>
  61. a pillanatnyi környezet és
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. a rendszertöltés beállításainak
  67. </para>
  68. </listitem>
  69. </itemizedlist>
  70. <para>
  71. átadásával.
  72. </para>
  73. <para>
  74. Utóbbiak magukban foglalják a rendszertöltő osztályt tartalmazó állomány elérési útját,
  75. illetve igény szerint:
  76. </para>
  77. <itemizedlist>
  78. <listitem>
  79. <para>
  80. további include_path összetevőket;
  81. </para>
  82. </listitem>
  83. <listitem>
  84. <para>
  85. további automatikus betöltéshez bejegyzendő névtereket;
  86. </para>
  87. </listitem>
  88. <listitem>
  89. <para>
  90. beállítandó <filename>php.ini</filename> értékeket;
  91. </para>
  92. </listitem>
  93. <listitem>
  94. <para>
  95. a rendszertöltő osztály nevét (ha nem „Bootstrap”)
  96. </para>
  97. </listitem>
  98. <listitem>
  99. <para>
  100. erőforrás előtagat a az elérési utakhoz;
  101. </para>
  102. </listitem>
  103. <listitem>
  104. <para>
  105. használandó erőforrásokat (az osztály neve vagy rövid név szerint);
  106. </para>
  107. </listitem>
  108. <listitem>
  109. <para>
  110. további elérési utakat konfigurációs állományok betöltéséhez, illetve
  111. </para>
  112. </listitem>
  113. <listitem>
  114. <para>
  115. további konfigurációs beállításokat.
  116. </para>
  117. </listitem>
  118. </itemizedlist>
  119. <para>
  120. A beállítások érkezhetnek egy tömbben, egy <classname>Zend_Config</classname> objektumban
  121. vagy egy konfigurációs állomány elérési útja képében.
  122. </para>
  123. <sect2 id="zend.application.theory-of-operation.bootstrap">
  124. <title>Bootstrapping</title>
  125. <para>
  126. <classname>Zend_Application</classname>'s second area of responsibility is
  127. executing the application bootstrap. Bootstraps minimally need to
  128. implement <classname>Zend_Application_Bootstrap_Bootstrapper</classname>,
  129. which defines the following <acronym>API</acronym>:
  130. </para>
  131. <programlisting language="php"><![CDATA[
  132. interface Zend_Application_Bootstrap_Bootstrapper
  133. {
  134. public function __construct($application);
  135. public function setOptions(array $options);
  136. public function getApplication();
  137. public function getEnvironment();
  138. public function getClassResources();
  139. public function getClassResourceNames();
  140. public function bootstrap($resource = null);
  141. public function run();
  142. }
  143. ]]></programlisting>
  144. <para>
  145. This <acronym>API</acronym> allows the bootstrap to accept the environment and
  146. configuration from the application object, report the resources its
  147. responsible for bootstrapping, and then bootstrap and run the
  148. application.
  149. </para>
  150. <para>
  151. You can implement this interface on your own, extend
  152. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>, or use
  153. <classname>Zend_Application_Bootstrap_Bootstrap</classname>.
  154. </para>
  155. <para>
  156. Besides this functionality, there are a number of other areas of
  157. concern you should familiarize yourself with.
  158. </para>
  159. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-methods">
  160. <title>Resource Methods</title>
  161. <para>
  162. The <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  163. implementation provides a simple convention for defining class
  164. resource methods. Any protected method beginning with a name
  165. prefixed with <emphasis>_init</emphasis> will be considered a resource
  166. method.
  167. </para>
  168. <para>
  169. To bootstrap a single resource method, use the
  170. <methodname>bootstrap()</methodname> method, and pass it the name of the
  171. resource. The name will be the method name minus the
  172. <emphasis>_init</emphasis> prefix.
  173. </para>
  174. <para>
  175. To bootstrap several resource methods, pass an array of names.
  176. Too bootstrap all resource methods, pass nothing.
  177. </para>
  178. <para>
  179. Take the following bootstrap class:
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  183. {
  184. protected function _initFoo()
  185. {
  186. // ...
  187. }
  188. protected function _initBar()
  189. {
  190. // ...
  191. }
  192. protected function _initBaz()
  193. {
  194. // ...
  195. }
  196. }
  197. ]]></programlisting>
  198. <para>
  199. To bootstrap just the <methodname>_initFoo()</methodname> method, do the
  200. following:
  201. </para>
  202. <programlisting language="php"><![CDATA[
  203. $bootstrap->bootstrap('foo');
  204. ]]></programlisting>
  205. <para>
  206. To bootstrap the <methodname>_initFoo()</methodname> and
  207. <methodname>_initBar()</methodname> methods, do the following:
  208. </para>
  209. <programlisting language="php"><![CDATA[
  210. $bootstrap->bootstrap(array('foo', 'bar'));
  211. ]]></programlisting>
  212. <para>
  213. To bootstrap all resource methods, call <methodname>bootstrap()</methodname>
  214. with no arguments:
  215. </para>
  216. <programlisting language="php"><![CDATA[
  217. $bootstrap->bootstrap();
  218. ]]></programlisting>
  219. </sect3>
  220. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-plugins">
  221. <title>Bootstraps that use resource plugins</title>
  222. <para>
  223. To make your bootstraps more re-usable, we have provided the
  224. ability to push your resources into resource plugin classes.
  225. This allows you to mix and match resources simply via
  226. configuration. We will cover <link
  227. linkend="zend.application.theory-of-operation.resources">how
  228. to create resources</link> later; in
  229. this section we will show you how to utilize them only.
  230. </para>
  231. <para>
  232. If your bootstrap should be capable of using resource plugins,
  233. you will need to implement an additional interface,
  234. <classname>Zend_Application_Bootstrap_ResourceBootstrapper</classname>.
  235. This interface defines an <acronym>API</acronym> for locating, registering, and
  236. loading resource plugins:
  237. </para>
  238. <programlisting language="php"><![CDATA[
  239. interface Zend_Application_Bootstrap_ResourceBootstrapper
  240. {
  241. public function registerPluginResource($resource, $options = null);
  242. public function unregisterPluginResource($resource);
  243. public function hasPluginResource($resource);
  244. public function getPluginResource($resource);
  245. public function getPluginResources();
  246. public function getPluginResourceNames();
  247. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader);
  248. public function getPluginLoader();
  249. }
  250. ]]></programlisting>
  251. <para>
  252. Resource plugins basically provide the ability to create
  253. resource intializers that can be re-used between applications.
  254. This allows you to keep your actual bootstrap relatively clean,
  255. and to introduce new resources without needing to touch your
  256. bootstrap itself.
  257. </para>
  258. <para>
  259. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname> (and
  260. <classname>Zend_Application_Bootstrap_Bootstrap</classname> by extension)
  261. implement this interface as well, allowing you to utilize
  262. resource plugins.
  263. </para>
  264. <para>
  265. To utilize resource plugins, you must specify them in the
  266. options passed to the application object and/or bootstrap. These
  267. options may come from a configuration file, or be passed in
  268. manually. Options will be of key to options pairs, with the key
  269. representing the resource name. The resource name will be the
  270. segment following the class prefix. For example, the resources
  271. shipped with Zend Framework have the class prefix
  272. "<classname>Zend_Application_Resource_</classname>"; anything following this would
  273. be the name of the resource. As an example,
  274. </para>
  275. <programlisting language="php"><![CDATA[
  276. $application = new Zend_Application(APPLICATION_ENV, array(
  277. 'resources' => array(
  278. 'FrontController' => array(
  279. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  280. ),
  281. ),
  282. ));
  283. ]]></programlisting>
  284. <para>
  285. This indicates that the "FrontController" resource should be
  286. used, with the options specified.
  287. </para>
  288. <para>
  289. If you begin writing your own resource plugins, or utilize
  290. third-party resource plugins, you will need to tell your
  291. bootstrap where to look for them. Internally, the bootstrap
  292. utilizes <classname>Zend_Loader_PluginLoader</classname>, so you will only
  293. need to indicate the common class prefix an path pairs.
  294. </para>
  295. <para>
  296. As an example, let's assume you have custom resource plugins in
  297. <filename>APPLICATION_PATH/resources/</filename> and that they share the
  298. common class prefix of <classname>My_Resource</classname>. You would then
  299. pass that information to the application object as follows:
  300. </para>
  301. <programlisting language="php"><![CDATA[
  302. $application = new Zend_Application(APPLICATION_ENV, array(
  303. 'pluginPaths' => array(
  304. 'My_Resource' => APPLICATION_PATH . '/resources/',
  305. ),
  306. 'resources' => array(
  307. 'FrontController' => array(
  308. 'controllerDirectory' => APPLICATION_PATH . '/controllers',
  309. ),
  310. ),
  311. ));
  312. ]]></programlisting>
  313. <para>
  314. You would now be able to use resources from that directory.
  315. </para>
  316. <para>
  317. Just like resource methods, you use the <methodname>bootstrap()</methodname>
  318. method to execute resource plugins. Just like with resource
  319. methods, you can specify either a single resource plugin,
  320. multiple plugins (via an array), or all plugins. Additionally,
  321. you can mix and match to execute resource methods as well.
  322. </para>
  323. <programlisting language="php"><![CDATA[
  324. // Execute one:
  325. $bootstrap->bootstrap('FrontController');
  326. // Execute several:
  327. $bootstrap->bootstrap(array('FrontController', 'Foo'));
  328. // Execute all resource methods and plugins:
  329. $bootstrap->bootstrap();
  330. ]]></programlisting>
  331. </sect3>
  332. <sect3 id="zend.application.theory-of-operation.bootstrap.registry">
  333. <title>Resource Registry</title>
  334. <para>
  335. Many, if not all, of your resource methods or plugins will
  336. initialize objects, and in many cases, these objects will be
  337. needed elsewhere in your application. How can you access them?
  338. </para>
  339. <para>
  340. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  341. provides a local registry for these objects. To store your
  342. objects in them, you simply return them from your resources.
  343. </para>
  344. <para>
  345. For maximum flexibility, this registry is referred to as a
  346. "container" internally; its only requirements are that it is an
  347. object. Resources are then registered as properties named after
  348. the resource name. By default, an instance of
  349. <classname>Zend_Registry</classname> is used, but you may also specify any
  350. other object you wish. The methods <methodname>setContainer()</methodname>
  351. and <methodname>getContainer()</methodname> may be used to manipulate the
  352. container itself. <methodname>getResource($resource)</methodname> can be
  353. used to fetch a given resource from the container, and
  354. <methodname>hasResource($resource)</methodname> to check if the resource has
  355. actually been registered.
  356. </para>
  357. <para>
  358. As an example, consider a basic view resource:
  359. </para>
  360. <programlisting language="php"><![CDATA[
  361. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  362. {
  363. protected function _initView()
  364. {
  365. $view = new Zend_View();
  366. // more initialization...
  367. return $view;
  368. }
  369. }
  370. ]]></programlisting>
  371. <para>
  372. You can then check for it and/or fetch it as follows:
  373. </para>
  374. <programlisting language="php"><![CDATA[
  375. // Using the has/getResource() pair:
  376. if ($bootstrap->hasResource('view')) {
  377. $view = $bootstrap->getResource('view');
  378. }
  379. // Via the container:
  380. $container = $bootstrap->getContainer();
  381. if (isset($container->view)) {
  382. $view = $container->view;
  383. }
  384. ]]></programlisting>
  385. <para>
  386. Please note that the registry and also the container is not global. This
  387. means that you need access to the bootstrap in order to fetch
  388. resources. <classname>Zend_Application_Bootstrap_Bootstrap</classname>
  389. provides some convenience for this: during its
  390. <methodname>run()</methodname> execution, it registers itself as the front
  391. controller parameter "bootstrap", which allows you to fetch it
  392. from the router, dispatcher, plugins, and action controllers.
  393. </para>
  394. <para>
  395. As an example, if you wanted access to the view resource from
  396. above within your action controller, you could do the following:
  397. </para>
  398. <programlisting language="php"><![CDATA[
  399. class FooController extends Zend_Controller_Action
  400. {
  401. public function init()
  402. {
  403. $bootstrap = $this->getInvokeArg('bootstrap');
  404. $view = $bootstrap->getResource('view');
  405. // ...
  406. }
  407. }
  408. ]]></programlisting>
  409. </sect3>
  410. <sect3 id="zend.application.theory-of-operation.bootstrap.dependency-tracking">
  411. <title>Dependency Tracking</title>
  412. <para>
  413. In addition to executing resource methods and plugins, it's
  414. necessary to ensure that these are executed once and once
  415. only; these are meant to bootstrap an application, and
  416. executing multiple times can lead to resource overhead.
  417. </para>
  418. <para>
  419. At the same time, some resources may depend on other
  420. resources being executed. To solve these two issues,
  421. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname>
  422. provides a simple, effective mechanism for dependency
  423. tracking.
  424. </para>
  425. <para>
  426. As noted previously, all resources -- whether methods or plugins
  427. -- are bootstrapped by calling <methodname>bootstrap($resource)</methodname>,
  428. where <varname>$resource</varname> is the name of a resource, an array
  429. of resources, or, left empty, indicates all resources should be
  430. run.
  431. </para>
  432. <para>
  433. If a resource depends on another resource, it should call
  434. <methodname>bootstrap()</methodname> within its code to ensure that resource
  435. has been executed. Subsequent calls to it will then be ignored.
  436. </para>
  437. <para>
  438. In a resource method, such a call would look like this:
  439. </para>
  440. <programlisting language="php"><![CDATA[
  441. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  442. {
  443. protected function _initRequest()
  444. {
  445. // Ensure the front controller is initialized
  446. $this->bootstrap('FrontController');
  447. // Retrieve the front controller from the bootstrap registry
  448. $front = $this->getResource('FrontController');
  449. $request = new Zend_Controller_Request_Http();
  450. $request->setBaseUrl('/foo');
  451. $front->setRequest($request);
  452. // Ensure the request is stored in the bootstrap registry
  453. return $request;
  454. }
  455. }
  456. ]]></programlisting>
  457. </sect3>
  458. </sect2>
  459. <sect2 id="zend.application.theory-of-operation.resources">
  460. <title>Resource Plugins</title>
  461. <para>
  462. <link
  463. linkend="zend.application.theory-of-operation.bootstrap.resource-plugins">As noted
  464. previously</link>, a good way to create re-usable bootstrap resources and to
  465. offload much of your coding to discrete classes is to utilize resource
  466. plugins. While Zend Framework ships with a number of standard
  467. resource plugins, the intention is that developers should write
  468. their own to encapsulate their own initialization needs.
  469. </para>
  470. <para>
  471. Resources need only implement
  472. <classname>Zend_Application_Resource_Resource</classname>, or, more simply
  473. still, extend
  474. <classname>Zend_Application_Resource_ResourceAbstract</classname>. The basic
  475. interface is simply this:
  476. </para>
  477. <programlisting language="php"><![CDATA[
  478. interface Zend_Application_Resource_Resource
  479. {
  480. public function __construct($options = null);
  481. public function setBootstrap(
  482. Zend_Application_Bootstrap_Bootstrapper $bootstrap
  483. );
  484. public function getBootstrap();
  485. public function setOptions(array $options);
  486. public function getOptions();
  487. public function init();
  488. }
  489. ]]></programlisting>
  490. <para>
  491. The interface defines simply that a resource should accept options
  492. to the constructor, have mechanisms for setting and retrieving
  493. options, have mechanisms for setting and retrieving the bootstrap
  494. object, and an initialization method.
  495. </para>
  496. <para>
  497. As an example, let's assume you have a common view intialization you
  498. use in your applications. You have a common doctype, <acronym>CSS</acronym> and
  499. JavaScript, and you want to be able to pass in a base document title
  500. via configuration. Such a resource might look like this:
  501. </para>
  502. <programlisting language="php"><![CDATA[
  503. class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
  504. {
  505. protected $_view;
  506. public function init()
  507. {
  508. // Return view so bootstrap will store it in the registry
  509. return $this->getView();
  510. }
  511. public function getView()
  512. {
  513. if (null === $this->_view) {
  514. $options = $this->getOptions();
  515. $title = '';
  516. if (array_key_exists('title', $options)) {
  517. $title = $options['title'];
  518. unset($options['title']);
  519. }
  520. $view = new Zend_View($options);
  521. $view->doctype('XHTML1_STRICT');
  522. $view->headTitle($title);
  523. $view->headLink()->appendStylesheet('/css/site.css');
  524. $view->headScript()->appendfile('/js/analytics.js');
  525. $viewRenderer =
  526. Zend_Controller_Action_HelperBroker::getStaticHelper(
  527. 'ViewRenderer'
  528. );
  529. $viewRenderer->setView($view);
  530. $this->_view = $view;
  531. }
  532. return $this->_view;
  533. }
  534. }
  535. ]]></programlisting>
  536. <para>
  537. As long as you register the prefix path for this resource plugin,
  538. you can then use it in your application. Even better, because it
  539. uses the plugin loader, you are effectively overriding the shipped
  540. "View" resource plugin, ensuring that your own is used instead.
  541. </para>
  542. </sect2>
  543. </sect1>