Zend_Tool_Framework-WritingProviders.xml 12 KB

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