2
0

Zend_Application-TheoryOfOperation.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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>Rendszertöltés</title>
  125. <para>
  126. A <classname>Zend_Application</classname> felelősségi körébe tartozik az alkalmazás
  127. rendszertöltőjének végrehajtása is. Egy rendszertöltőnek legkevesebb meg kell
  128. valósítania <classname>Zend_Application_Bootstrap_Bootstrapper</classname>-t, mely
  129. a következő <acronym>API</acronym>-t határozza meg:
  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. Ez az <acronym>API</acronym> lehetővé teszi a rendszertöltőnek a környezet és
  146. beállítások fogadását az alkalmazás objektumtól, hogy jelentsen az erőforrásokról,
  147. melyek indításáért felelős, majd betöltse és futtassa az alkalmazást.
  148. </para>
  149. <para>
  150. A felület megvalósítható önállóan, lehetőség van a
  151. <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname> kiterjesztésére
  152. vagy a <classname>Zend_Application_Bootstrap_Bootstrap</classname> használatára.
  153. </para>
  154. <para>
  155. Ezek mellett egy sor más dolog is van, mellyel érdemes megismerkedni.
  156. </para>
  157. <sect3 id="zend.application.theory-of-operation.bootstrap.resource-methods">
  158. <title>Erőforrás tagfüggvények</title>
  159. <para>
  160. A <classname>Zend_Application_Bootstrap_BootstrapAbstract</classname> megvalósítás
  161. egy egyszerű egyezményt nyújt erőforrás tagfüggvények meghatározására. Minden védett
  162. tagfüggvény, mely az <emphasis>_init</emphasis> előtaggal kezdődik, erőforrás
  163. tagfüggvénynek számít.
  164. </para>
  165. <para>
  166. Egy adott erőforrás tagfüggvény betöltéséhez a <methodname>bootstrap()</methodname>
  167. metódus használatos az erőforrás nevének megadásával. A név a tagfüggvény neve az
  168. <emphasis>_init</emphasis> előtag elhagyásával.
  169. </para>
  170. <para>
  171. Több tagfüggvény betöltéséhez egy tömböt kell átadni, az összeséhez pedig semmit.
  172. </para>
  173. <para>
  174. Vegyük a következő rendszertöltő osztályt:
  175. </para>
  176. <programlisting language="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. Az <methodname>_initFoo()</methodname> tagfüggvény betöltéséhez a következő a
  195. teendő:
  196. </para>
  197. <programlisting language="php"><![CDATA[
  198. $bootstrap->bootstrap('foo');
  199. ]]></programlisting>
  200. <para>
  201. Az <methodname>_initFoo()</methodname> és az <methodname>_initBar()</methodname>
  202. betöltéséhez a következő:
  203. </para>
  204. <programlisting language="php"><![CDATA[
  205. $bootstrap->bootstrap(array('foo', 'bar'));
  206. ]]></programlisting>
  207. <para>
  208. Az öszes betöltéséhez a <methodname>bootstrap()</methodname> argumentumok nélkül
  209. hívandó:
  210. </para>
  211. <programlisting language="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 <acronym>API</acronym> for locating, registering, and
  231. loading resource plugins:
  232. </para>
  233. <programlisting language="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 to 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. "<classname>Zend_Application_Resource_</classname>"; anything following this would
  268. be the name of the resource. As an example,
  269. </para>
  270. <programlisting language="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. <filename>APPLICATION_PATH/resources/</filename> and that they share the
  293. common class prefix of <classname>My_Resource</classname>. You would then
  294. pass that information to the application object as follows:
  295. </para>
  296. <programlisting language="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 <methodname>bootstrap()</methodname>
  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 language="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 <methodname>setContainer()</methodname>
  346. and <methodname>getContainer()</methodname> may be used to manipulate the
  347. container itself. <methodname>getResource($resource)</methodname> can be
  348. used to fetch a given resource from the container, and
  349. <methodname>hasResource($resource)</methodname> 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 language="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 language="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 and also the 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. <methodname>run()</methodname> 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 language="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 <methodname>bootstrap($resource)</methodname>,
  423. where <varname>$resource</varname> 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. <methodname>bootstrap()</methodname> 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 language="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
  458. linkend="zend.application.theory-of-operation.bootstrap.resource-plugins">As noted
  459. previously</link>, a good way to create re-usable bootstrap resources and to
  460. offload much of your coding to discrete classes is to utilize resource
  461. plugins. While Zend Framework ships with a number of standard
  462. resource plugins, the intention is that developers should write
  463. their own to encapsulate their own initialization needs.
  464. </para>
  465. <para>
  466. Resources need only implement
  467. <classname>Zend_Application_Resource_Resource</classname>, or, more simply
  468. still, extend
  469. <classname>Zend_Application_Resource_ResourceAbstract</classname>. The basic
  470. interface is simply this:
  471. </para>
  472. <programlisting language="php"><![CDATA[
  473. interface Zend_Application_Resource_Resource
  474. {
  475. public function __construct($options = null);
  476. public function setBootstrap(
  477. Zend_Application_Bootstrap_Bootstrapper $bootstrap
  478. );
  479. public function getBootstrap();
  480. public function setOptions(array $options);
  481. public function getOptions();
  482. public function init();
  483. }
  484. ]]></programlisting>
  485. <para>
  486. The interface defines simply that a resource should accept options
  487. to the constructor, have mechanisms for setting and retrieving
  488. options, have mechanisms for setting and retrieving the bootstrap
  489. object, and an initialization method.
  490. </para>
  491. <para>
  492. As an example, let's assume you have a common view intialization you
  493. use in your applications. You have a common doctype, <acronym>CSS</acronym> and
  494. JavaScript, and you want to be able to pass in a base document title
  495. via configuration. Such a resource might look like this:
  496. </para>
  497. <programlisting language="php"><![CDATA[
  498. class My_Resource_View extends Zend_Application_Resource_ResourceAbstract
  499. {
  500. protected $_view;
  501. public function init()
  502. {
  503. // Return view so bootstrap will store it in the registry
  504. return $this->getView();
  505. }
  506. public function getView()
  507. {
  508. if (null === $this->_view) {
  509. $options = $this->getOptions();
  510. $title = '';
  511. if (array_key_exists('title', $options)) {
  512. $title = $options['title'];
  513. unset($options['title']);
  514. }
  515. $view = new Zend_View($options);
  516. $view->doctype('XHTML1_STRICT');
  517. $view->headTitle($title);
  518. $view->headLink()->appendStylesheet('/css/site.css');
  519. $view->headScript()->appendfile('/js/analytics.js');
  520. $viewRenderer =
  521. Zend_Controller_Action_HelperBroker::getStaticHelper(
  522. 'ViewRenderer'
  523. );
  524. $viewRenderer->setView($view);
  525. $this->_view = $view;
  526. }
  527. return $this->_view;
  528. }
  529. }
  530. ]]></programlisting>
  531. <para>
  532. As long as you register the prefix path for this resource plugin,
  533. you can then use it in your application. Even better, because it
  534. uses the plugin loader, you are effectively overriding the shipped
  535. "View" resource plugin, ensuring that your own is used instead.
  536. </para>
  537. </sect2>
  538. </sect1>