Zend_Tool_Framework-Architecture.xml 16 KB

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