Zend_Tool_Framework-Architecture.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.tool.framework.architecture">
  4. <title>Architecture</title>
  5. <sect2 id="zend.tool.framework.architecture.registry">
  6. <title>Registry</title>
  7. <para>
  8. Because providers and manifests may come from anywhere in the
  9. <property>include_path</property>, a registry is provided to simplify access
  10. to the various pieces of the toolchain. This registry is injected
  11. into registry-aware components, which may then pull dependencies
  12. from them as necessary. Most dependencies registered with the
  13. registry will be sub-component-specific repositories.
  14. </para>
  15. <para>
  16. The interface for the registry consists of the following definition:
  17. </para>
  18. <programlisting language="php"><![CDATA[
  19. interface Zend_Tool_Framework_Registry_Interface
  20. {
  21. public function setClient(Zend_Tool_Framework_Client_Abstract $client);
  22. public function getClient();
  23. public function setLoader(Zend_Tool_Framework_Loader_Abstract $loader);
  24. public function getLoader();
  25. public function setActionRepository(
  26. Zend_Tool_Framework_Action_Repository $actionRepository
  27. );
  28. public function getActionRepository();
  29. public function setProviderRepository(
  30. Zend_Tool_Framework_Provider_Repository $providerRepository
  31. );
  32. public function getProviderRepository();
  33. public function setManifestRepository(
  34. Zend_Tool_Framework_Manifest_Repository $manifestRepository
  35. );
  36. public function getManifestRepository();
  37. public function setRequest(Zend_Tool_Framework_Client_Request $request);
  38. public function getRequest();
  39. public function setResponse(Zend_Tool_Framework_Client_Response $response);
  40. public function getResponse();
  41. }
  42. ]]></programlisting>
  43. <para>
  44. The various objects the registry manages will be discussed in their
  45. appropriate sections.
  46. </para>
  47. <para>
  48. Classes that should be registry-aware should implement
  49. <classname>Zend_Tool_Framework_Registry_EnabledInterface</classname>. This
  50. interface merely allows initialization of the registry in the target
  51. class.
  52. </para>
  53. <programlisting language="php"><![CDATA[
  54. interface Zend_Tool_Framework_Registry_EnabledInterface
  55. {
  56. public function setRegistry(
  57. Zend_Tool_Framework_Registry_Interface $registry
  58. );
  59. }
  60. ]]></programlisting>
  61. </sect2>
  62. <sect2 id="zend.tool.framework.architecture.providers">
  63. <title>Providers</title>
  64. <para>
  65. <classname>Zend_Tool_Framework_Provider</classname> represents the functional
  66. or "capability" aspect of the framework. Fundamentally,
  67. <classname>Zend_Tool_Framework_Provider</classname> will provide the
  68. interfaces necessary to produce "providers", or bits of tooling
  69. functionality that can be called and used inside the
  70. <classname>Zend_Tool_Framework</classname> toolchain. The simplistic nature of
  71. implementing this provider interface allows the developer a
  72. "one-stop-shop" of adding functionality or capabilities to
  73. <classname>Zend_Tool_Framework</classname>.
  74. </para>
  75. <para>
  76. The provider interface is an empty interface and enforces no methods
  77. (this is the Marker Interface pattern):
  78. </para>
  79. <programlisting language="php"><![CDATA[
  80. interface Zend_Tool_Framework_Provider_Interface
  81. {}
  82. ]]></programlisting>
  83. <para>
  84. Or, if you wish, you can implement the base (or abstract) Provider
  85. which will give you access to the
  86. <classname>Zend_Tool_Framework_Registry</classname>:
  87. </para>
  88. <programlisting language="php"><![CDATA[
  89. abstract class Zend_Tool_Framework_Provider_Abstract
  90. implements Zend_Tool_Framework_Provider_Interface,
  91. Zend_Tool_Registry_EnabledInterface
  92. {
  93. protected $_registry;
  94. public function setRegistry(
  95. Zend_Tool_Framework_Registry_Interface $registry
  96. );
  97. }
  98. ]]></programlisting>
  99. </sect2>
  100. <sect2 id="zend.tool.framework.architecture.loaders">
  101. <title>Loaders</title>
  102. <para>
  103. The purpose of a Loader is to find Providers and Manifest files that
  104. contain classes which implement either
  105. <classname>Zend_Tool_Framework_Provider_Interface</classname> or
  106. <classname>Zend_Tool_Framework_Manifest_Interface</classname>. Once these files are
  107. found by a loader, providers are loaded into the Provider Repository and
  108. manifest metadata is loaded into the Manifest Repository.
  109. </para>
  110. <para>
  111. To implement a loader, one must extend the following abstract class:
  112. </para>
  113. <programlisting language="php"><![CDATA[
  114. abstract class Zend_Tool_Framework_Loader_Abstract
  115. {
  116. abstract protected function _getFiles();
  117. public function load()
  118. {
  119. /** ... */
  120. }
  121. }
  122. ]]></programlisting>
  123. <para>
  124. The <methodname>_getFiles()</methodname> method should return an array of files
  125. (absolute paths). The built-in loader supplied with Zend Framework is called the
  126. IncludePath loader. By default, the Tooling framework will use an
  127. include_path based loader to find files that might include Providers
  128. or Manifest Metadata objects.
  129. <classname>Zend_Tool_Framework_Loader_IncludePathLoader</classname>, without
  130. any other options, will search for files inside the include path
  131. that end in <filename>Mainfest.php</filename>, <filename>Tool.php</filename> or
  132. <filename>Provider.php</filename>. Once found, they will be tested (by the
  133. <methodname>load()</methodname> method of the
  134. <classname>Zend_Tool_Framework_Loader_Abstract</classname>) to determine if
  135. they implement any of the supported interfaces. If they do, an
  136. instance of the found class is instantiated, and it is appended to
  137. the proper repository.
  138. </para>
  139. <programlisting language="php"><![CDATA[
  140. class Zend_Tool_Framework_Loader_IncludePathLoader
  141. extends Zend_Tool_Framework_Loader_Abstract
  142. {
  143. protected $_filterDenyDirectoryPattern = '.*(/|\\\\).svn';
  144. protected $_filterAcceptFilePattern = '.*(?:Manifest|Provider)\.php$';
  145. protected function _getFiles()
  146. {
  147. /** ... */
  148. }
  149. }
  150. ]]></programlisting>
  151. <para>
  152. As you can see, the IncludePath loader will search all include_paths
  153. for the files that match the <varname>$_filterAcceptFilePattern</varname>
  154. and <emphasis>not</emphasis> match the <varname>$_filterDenyDirectoryPattern</varname>.
  155. </para>
  156. </sect2>
  157. <sect2 id="zend.tool.framework.architecture.manifests">
  158. <title>Manifests</title>
  159. <para>
  160. In short, the Manifest shall contain specific or arbitrary metadata
  161. that is useful to any provider or client, as well as be responsible
  162. for loading any additional providers into the provider repository.
  163. </para>
  164. <para>
  165. To introduce metadata into the manifest repository, all one must do
  166. is implement the empty <classname>Zend_Tool_Framework_Manifest_Interface</classname>,
  167. and provide a <methodname>getMetadata()</methodname> method which shall return an array
  168. of objects that implement <classname>Zend_Tool_Framework_Manifest_Metadata</classname>.
  169. </para>
  170. <programlisting language="php"><![CDATA[
  171. interface Zend_Tool_Framework_Manifest_Interface
  172. {
  173. public function getMetadata();
  174. }
  175. ]]></programlisting>
  176. <para>
  177. Metadata objects are loaded (by a loader defined below) into the
  178. Manifest Repository (<classname>Zend_Tool_Framework_Manifest_Repository</classname>).
  179. Manifests will be processed after all Providers have been found to be
  180. loaded into the provider repository. This shall allow Manifests to
  181. create Metadata objects based on what is currently inside the
  182. provider repository.
  183. </para>
  184. <para>
  185. There are a few different metadata classes that can be used to
  186. describe metadata. The
  187. <classname>Zend_Tool_Framework_Manifest_Metadata</classname> is the base
  188. metadata object. As you can see by the following code
  189. snippet, the base metadata class is fairly lightweight and
  190. abstract in nature:
  191. </para>
  192. <programlisting language="php"><![CDATA[
  193. class Zend_Tool_Framework_Metadata_Basic
  194. {
  195. protected $_type = 'Global';
  196. protected $_name = null;
  197. protected $_value = null;
  198. protected $_reference = null;
  199. public function getType();
  200. public function getName();
  201. public function getValue();
  202. public function getReference();
  203. /** ... */
  204. }
  205. ]]></programlisting>
  206. <para>
  207. There are other built in metadata classes as well for describing
  208. more specialized metadata: <classname>ActionMetadata</classname> and
  209. <classname>ProviderMetadata</classname>. These classes will help you describe
  210. in more detail metadata that is specific to either actions or
  211. providers, and the reference is expected to be a reference to an
  212. action or a provider respectively. These classes are described in
  213. the following code snippet.
  214. </para>
  215. <programlisting language="php"><![CDATA[
  216. class Zend_Tool_Framework_Manifest_ActionMetadata
  217. extends Zend_Tool_Framework_Manifest_Metadata
  218. {
  219. protected $_type = 'Action';
  220. protected $_actionName = null;
  221. public function getActionName();
  222. /** ... */
  223. }
  224. class Zend_Tool_Framework_Manifest_ProviderMetadata
  225. extends Zend_Tool_Framework_Manifest_Metadata
  226. {
  227. protected $_type = 'Provider';
  228. protected $_providerName = null;
  229. protected $_actionName = null;
  230. protected $_specialtyName = null;
  231. public function getProviderName();
  232. public function getActionName();
  233. public function getSpecialtyName();
  234. /** ... */
  235. }
  236. ]]></programlisting>
  237. <para>
  238. 'Type' in these classes is used to describe the type of metadata the
  239. object is responsible for. In the cases of the
  240. <classname>ActionMetadata</classname>, the type would be 'Action', and
  241. conversely in the case of the <classname>ProviderMetadata</classname> the type
  242. is 'Provider'. These metadata types will also include additional
  243. structured information about both the "thing" they are describing as
  244. well as the object (the <methodname>getReference()</methodname>) they are
  245. referencing with this new metadata.
  246. </para>
  247. <para>
  248. In order to create your own metadata type, all one must do is extend
  249. the base <classname>Zend_Tool_Framework_Manifest_Metadata</classname> class
  250. and return these new metadata objects via a local Manifest
  251. class or object. These user based classes will live in the Manifest
  252. Repository
  253. </para>
  254. <para>
  255. Once these metadata objects are in the repository, there are then two
  256. different methods that can be used in order to search for them in
  257. the repository.
  258. </para>
  259. <programlisting language="php"><![CDATA[
  260. class Zend_Tool_Framework_Manifest_Repository
  261. {
  262. /**
  263. * To use this method to search, $searchProperties should contain the names
  264. * and values of the key/value pairs you would like to match within the
  265. * manifest.
  266. *
  267. * For Example:
  268. * $manifestRepository->findMetadatas(array(
  269. * 'action' => 'Foo',
  270. * 'name' => 'cliActionName'
  271. * ));
  272. *
  273. * Will find any metadata objects that have a key with name 'action' value
  274. * of 'Foo', AND a key named 'name' value of 'cliActionName'
  275. *
  276. * Note: to either exclude or include name/value pairs that exist in the
  277. * search criteria but do not appear in the object, pass a bool value to
  278. * $includeNonExistentProperties
  279. */
  280. public function findMetadatas(Array $searchProperties = array(),
  281. $includeNonExistentProperties = true);
  282. /**
  283. * The following will return exactly one of the matching search criteria,
  284. * regardless of how many have been returned. First one in the manifest is
  285. * what will be returned.
  286. */
  287. public function findMetadata(Array $searchProperties = array(),
  288. $includeNonExistentProperties = true)
  289. {
  290. $metadatas = $this->getMetadatas($searchProperties,
  291. $includeNonExistentProperties);
  292. return array_shift($metadatas);
  293. }
  294. }
  295. ]]></programlisting>
  296. <para>
  297. Looking at the search methods above, the signatures allow for
  298. extremely flexible searching. In order to find a metadata object,
  299. simply pass in an array of matching constraints via an array. If
  300. the data is accessible through the Property accessor (the
  301. <methodname>getSomething()</methodname> methods implemented on the metadata
  302. object), then it will be passed back to the user as a "found"
  303. metadata object.
  304. </para>
  305. </sect2>
  306. <sect2 id="zend.tool.framework.architecture.clients">
  307. <title>Clients</title>
  308. <para>
  309. Clients are the interface which bridges a user or external tool into
  310. the <classname>Zend_Tool_Framework</classname> system. Clients can come in all
  311. shapes and sizes: <acronym>RPC</acronym> endpoints, Command Line Interface, or
  312. even a web interface. <classname>Zend_Tool</classname> has implemented the command
  313. line interface as the default interface for interacting with
  314. the <classname>Zend_Tool_Framework</classname> system.
  315. </para>
  316. <para>
  317. To implement a client, one would need to extend the following
  318. abstract class:
  319. </para>
  320. <programlisting language="php"><![CDATA[
  321. abstract class Zend_Tool_Framework_Client_Abstract
  322. {
  323. /**
  324. * This method should be implemented by the client implementation to
  325. * construct and set custom loaders, request and response objects.
  326. *
  327. * (not required, but suggested)
  328. */
  329. protected function _preInit();
  330. /**
  331. * This method should be implemented by the client implementation to parse
  332. * out and set up the request objects action, provider and parameter
  333. * information.
  334. */
  335. abstract protected function _preDispatch();
  336. /**
  337. * This method should be implemented by the client implementation to take
  338. * the output of the response object and return it (in an client specific
  339. * way) back to the Tooling Client.
  340. *
  341. * (not required, but suggested)
  342. */
  343. abstract protected function _postDispatch();
  344. }
  345. ]]></programlisting>
  346. <para>
  347. As you can see, there 1 method is required to fulfill the needs of a
  348. client (two others suggested), the initialization, prehandling and post handling. For a
  349. more in depth study of how the command line client works, please see
  350. the <ulink
  351. url="http://framework.zend.com/svn/framework/standard/branches/release-1.8/library/Zend/Tool/Framework/Client/Console.php">source
  352. code</ulink>.
  353. </para>
  354. </sect2>
  355. </sect1>