Zend_Tool_Framework-WritingProviders.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19568 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.tool.framework.writing-providers">
  5. <title>Creando Proveedores para usar con Zend_Tool_Framework</title>
  6. <para>
  7. In general, a provider, on its own, is nothing more than the shell for a
  8. developer to bundle up some capabilities they wish to dispatch with the
  9. command line (or other) clients. It is an analogue to what a
  10. "controller" is inside of your <acronym>MVC</acronym> application.
  11. </para>
  12. <sect2 id="zend.tool.framework.writing-providers.loading">
  13. <title>How Zend Tool finds your Providers</title>
  14. <para>
  15. By default Zend Tool uses the IncludePathLoader to find all
  16. the providers that you can run. It recursivly iterates all
  17. include path directories and opens all files that end
  18. with "Manifest.php" or "Provider.php". All classes in these
  19. files are inspected if they implement either
  20. <classname>Zend_Tool_Framework_Provider_Interface</classname>
  21. or <classname>Zend_Tool_Framework_Manifest_ProviderManifestable</classname>.
  22. Instances of the provider interface make up for the real functionality
  23. and all their public methods are accessible as provider actions.
  24. The ProviderManifestable interface however requires the implementation of a method
  25. <methodname>getProviders()</methodname> which returns an array of
  26. instantiated provider interface instances.
  27. </para>
  28. <para>
  29. The following naming rules apply on how you can access the providers
  30. that were found by the IncludePathLoader:
  31. </para>
  32. <itemizedlist>
  33. <listitem>
  34. <para>
  35. The last part of your classname split by underscore is used
  36. for the provider name, e.g. "My_Provider_Hello" leads to your
  37. provider being accessible by the name "hello".
  38. </para>
  39. </listitem>
  40. <listitem>
  41. <para>
  42. If your provider has a method <methodname>getName()</methodname>
  43. it will be used instead of the previous method to determine
  44. the name.
  45. </para>
  46. </listitem>
  47. <listitem>
  48. <para>
  49. If your provider has "Provider" as prefix, e.g. it is called
  50. <classname>My_HelloProvider</classname> it will be stripped
  51. from the name so that the provider will be called "hello".
  52. </para>
  53. </listitem>
  54. </itemizedlist>
  55. <note>
  56. <para>The IncludePathLoader does not follow symlinks, that means
  57. you cannot link provider functionality into your include paths,
  58. they have to be physically present in the include paths.</para>
  59. </note>
  60. <example id="zend.tool.framework.writing-providers.loading.example">
  61. <title>Exposing Your Providers with a Manifest</title>
  62. <para>
  63. You can expose your providers to Zend Tool by offering a manifest
  64. with a special filename ending with "Manifest.php".
  65. A Provider Manifest is an implementation of the
  66. <interface>Zend_Tool_Framework_Manifest_ProviderManifestable</interface>
  67. and requires the <methodname>getProviders()</methodname> method to return
  68. an array of instantiated providers. In anticipation of our first
  69. own provider <classname>My_Component_HelloProvider</classname>
  70. we will create the following manifest:
  71. </para>
  72. <programlisting language="php"><![CDATA[
  73. class My_Component_Manifest
  74. implements Zend_Tool_Framework_Manifest_ProviderManifestable
  75. {
  76. public function getProviders()
  77. {
  78. return array(
  79. new My_Component_HelloProvider()
  80. );
  81. }
  82. }
  83. ]]></programlisting>
  84. </example>
  85. </sect2>
  86. <sect2 id="zend.tool.framework.writing-providers.basic">
  87. <title>Basic Instructions for Creating Providers</title>
  88. <para>
  89. As an example, if a developer wants to add the capability of showing
  90. the version of a datafile that his 3rd party component is working
  91. from, there is only one class the developer would need to implement.
  92. Assuming the component is called <classname>My_Component</classname>, he would
  93. create a class named <classname>My_Component_HelloProvider</classname> in a
  94. file named <filename>HelloProvider.php</filename> somewhere on the
  95. <property>include_path</property>. This class would implement
  96. <classname>Zend_Tool_Framework_Provider_Interface</classname>, and the body of
  97. this file would only have to look like the following:
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. class My_Component_HelloProvider
  101. implements Zend_Tool_Framework_Provider_Interface
  102. {
  103. public function say()
  104. {
  105. echo 'Hello from my provider!';
  106. }
  107. }
  108. ]]></programlisting>
  109. <para>
  110. Given that code above, and assuming the developer wishes to access
  111. this functionality through the console client, the call would look
  112. like this:
  113. </para>
  114. <programlisting language="sh"><![CDATA[
  115. % zf say hello
  116. Hello from my provider!
  117. ]]></programlisting>
  118. </sect2>
  119. <sect2 id="zend.tool.framework.writing-providers.response">
  120. <title>The response object</title>
  121. <para>
  122. As discussed in the architecture section Zend Tool allows to hook different clients for
  123. using your Zend Tool providers. To keep compliant with different clients you should
  124. use the response object to return messages from your providers instead of using
  125. <methodname>echo()</methodname> or a similiar output mechanism. Rewritting our hello
  126. provider with this knowledge it looks like:
  127. </para>
  128. <programlisting language="php"><![CDATA[
  129. class My_Component_HelloProvider
  130. extends Zend_Tool_Framework_Provider_Abstract
  131. {
  132. public function say()
  133. {
  134. $this->_registry->getResponse
  135. ->appendContent("Hello from my provider!");
  136. }
  137. }
  138. ]]></programlisting>
  139. <para>
  140. As you can see one has to extend the <classname>Zend_Tool_Framework_Provider_Abstract</classname>
  141. to gain access to the Registry which holds the <classname>Zend_Tool_Framework_Client_Response</classname>
  142. instance.
  143. </para>
  144. </sect2>
  145. <sect2 id="zend.tool.framework.writing-providers.advanced">
  146. <title>Advanced Development Information</title>
  147. <sect3 id="zend.tool.framework.writing-providers.advanced.variables">
  148. <title>Passing Variables to a Provider</title>
  149. <para>
  150. The above "Hello World" example is great for simple commands, but
  151. what about something more advanced? As your scripting and tooling
  152. needs grow, you might find that you need the ability to accept
  153. variables. Much like function signatures have parameters, your
  154. tooling requests can also accept parameters.
  155. </para>
  156. <para>
  157. Just as each tooling request can be isolated to a method within a
  158. class, the parameters of a tooling request can also be isolated in a
  159. very well known place. Parameters of the action methods of a
  160. provider can include the same parameters you want your client to
  161. utilize when calling that provider and action combination. For
  162. example, if you wanted to accept a name in the above example, you
  163. would probably do this in OO code:
  164. </para>
  165. <programlisting language="php"><![CDATA[
  166. class My_Component_HelloProvider
  167. implements Zend_Tool_Framework_Provider_Interface
  168. {
  169. public function say($name = 'Ralph')
  170. {
  171. echo 'Hello' . $name . ', from my provider!';
  172. }
  173. }
  174. ]]></programlisting>
  175. <para>
  176. The above example can then be called via the command line
  177. <command>zf say hello Joe</command>. "Joe" will be supplied to the provider as
  178. a parameter of the method call. Also note, as you see that the
  179. parameter is optional, that means it is also optional on the command
  180. line, so that <command>zf say hello</command> will still work, and default
  181. to the name "Ralph".
  182. </para>
  183. </sect3>
  184. <sect3 id="zend.tool.framework.writing-providers.advanced.prompt">
  185. <title>Prompt the User for Input</title>
  186. <para>
  187. There are cases when the workflow of your provider requires
  188. to prompt the user for input. This can be done by requesting
  189. the client to ask for more the required input by calling:
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. class My_Component_HelloProvider
  193. extends Zend_Tool_Framework_Provider_Abstract
  194. {
  195. public function say($name = 'Ralph')
  196. {
  197. $nameResponse = $this->_registry
  198. ->getClient()
  199. ->promptInteractiveInput("Whats your name?");
  200. $name = $name->getContent();
  201. echo 'Hello' . $name . ', from my provider!';
  202. }
  203. }
  204. ]]></programlisting>
  205. <para>
  206. This command throws an exception if the current client is not
  207. able to handle interactive requests. In case of the default Console Client
  208. however you will be asked to enter the name.
  209. </para>
  210. </sect3>
  211. <sect3 id="zend.tool.framework.writing-providers.advanced.pretendable">
  212. <title>Pretending to execute a Provider Action</title>
  213. <para>
  214. Another interesting feature you might wish to implement is
  215. <emphasis>pretendability</emphasis>. Pretendabilty is the ability
  216. for your provider to "pretend" as if it is doing the requested
  217. action and provider combination and give the user as much
  218. information about what it <emphasis>would</emphasis> do without
  219. actually doing it. This might be an important notion when doing
  220. heavy database or filesystem modifications that the user might not
  221. otherwise want to do.
  222. </para>
  223. <para>
  224. Pretendability is easy to implement. There are two parts to this
  225. feature: 1) marking the provider as having the ability to "pretend",
  226. and 2) checking the request to ensure the current request was indeed
  227. asked to be "pretended". This feature is demonstrated in the code
  228. sample below.
  229. </para>
  230. <programlisting language="php"><![CDATA[
  231. class My_Component_HelloProvider
  232. extends Zend_Tool_Framework_Provider_Abstract
  233. implements Zend_Tool_Framework_Provider_Pretendable
  234. {
  235. public function say($name = 'Ralph')
  236. {
  237. if ($this->_registry->getRequest()->isPretend()) {
  238. echo 'I would say hello to ' . $name . '.';
  239. } else {
  240. echo 'Hello' . $name . ', from my provider!';
  241. }
  242. }
  243. }
  244. ]]></programlisting>
  245. <para>
  246. To run the provider in pretend mode just call:
  247. </para>
  248. <programlisting language="sh"><![CDATA[
  249. % zf --pretend say hello Ralph
  250. I would say hello Ralph.
  251. ]]></programlisting>
  252. </sect3>
  253. <sect3 id="zend.tool.framework.writing-providers.advanced.verbosedebug">
  254. <title>Verbose and Debug modes</title>
  255. <para>
  256. You can also run your provider actions in "verbose" or "debug" modes.
  257. The semantics in regard to this actions have to be implemented by you
  258. in the context of your provider. You can access debug or verbose modes
  259. with:
  260. </para>
  261. <programlisting language="php"><![CDATA[
  262. class My_Component_HelloProvider
  263. implements Zend_Tool_Framework_Provider_Interface
  264. {
  265. public function say($name = 'Ralph')
  266. {
  267. if($this->_registry->getRequest()->isVerbose()) {
  268. echo "Hello::say has been called\n";
  269. }
  270. if($this->_registry->getRequest()->isDebug()) {
  271. syslog(LOG_INFO, "Hello::say has been called\n");
  272. }
  273. }
  274. }
  275. ]]></programlisting>
  276. </sect3>
  277. <sect3 id="zend.tool.framework.writing-providers.advanced.configstorage">
  278. <title>Accessing User Config and Storage</title>
  279. <para>
  280. Using the Enviroment variable <property>ZF_CONFIG_FILE</property> or the
  281. .zf.ini in your home directory you can inject configuration parameters into
  282. any Zend Tool provider. Access to this configuration is available via the
  283. registry that is passed to your provider if you extend
  284. <classname>Zend_Tool_Framework_Provider_Abstract</classname>.
  285. </para>
  286. <programlisting language="php"><![CDATA[
  287. class My_Component_HelloProvider
  288. extends Zend_Tool_Framework_Provider_Abstract
  289. {
  290. public function say()
  291. {
  292. $username = $this->_registry->getConfig()->username;
  293. if(!empty($username)) {
  294. echo "Hello $username!";
  295. } else {
  296. echo "Hello!";
  297. }
  298. }
  299. }
  300. ]]></programlisting>
  301. <para>
  302. The returned configuration is of the type
  303. <classname>Zend_Tool_Framework_Client_Config</classname> but internally the
  304. <methodname>__get()</methodname> and <methodname>__set()</methodname> magic methods
  305. proxy to a <classname>Zend_Config</classname> of the given configuration type.
  306. </para>
  307. <para>
  308. The storage allows to save arbitrary data for later reference. This can be useful for batch
  309. processing tasks or for re-runs of your tasks. You can access the storage in a similar way
  310. like the configuration:
  311. </para>
  312. <programlisting language="php"><![CDATA[
  313. class My_Component_HelloProvider
  314. extends Zend_Tool_Framework_Provider_Abstract
  315. {
  316. public function say()
  317. {
  318. $aValue = $this->_registry->getStorage()->get("myUsername");
  319. echo "Hello $aValue!";
  320. }
  321. }
  322. ]]></programlisting>
  323. <para>
  324. The API of the storage is very simple:
  325. </para>
  326. <programlisting language="php"><![CDATA[
  327. class Zend_Tool_Framework_Client_Storage
  328. {
  329. public function setAdapter($adapter);
  330. public function isEnabled();
  331. public function put($name, $value);
  332. public function get($name, $defaultValue=null);
  333. public function has($name);
  334. public function remove($name);
  335. public function getStreamUri($name);
  336. }
  337. ]]></programlisting>
  338. <important>
  339. <para>
  340. When designing your providers that are config or storage aware remember to
  341. check if the required user-config or storage keys really exist for a user.
  342. You won't run into fatal errors when none of these are provided though,
  343. since empty ones are created upon request.
  344. </para>
  345. </important>
  346. </sect3>
  347. </sect2>
  348. </sect1>