Zend_Tool_Framework-WritingProviders.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.prompt">
  138. <title>Prompt the User for Input</title>
  139. <para>
  140. There are cases when the workflow of your provider requires
  141. to prompt the user for input. This can be done by requesting
  142. the client to ask for more the required input by calling:
  143. </para>
  144. <programlisting language="php"><![CDATA[
  145. class My_Component_HelloProvider
  146. extends Zend_Tool_Framework_Provider_Abstract
  147. {
  148. public function say($name = 'Ralph')
  149. {
  150. $nameResponse = $this->_registry->getClient()->promptInteractiveInput("Whats your name?");
  151. $name = $name->getContent();
  152. echo 'Hello' . $name . ', from my provider!';
  153. }
  154. }
  155. ]]>
  156. </programlisting>
  157. <para>
  158. This command throws an exception if the current client is not
  159. able to handle interactive requests. In case of the default Console Client
  160. however you will be asked to enter the name.
  161. </para>
  162. </sect3>
  163. <sect3 id="zend.tool.framework.writing-providers.advanced.pretendable">
  164. <title>Pretending to execute a Provider Action</title>
  165. <para>
  166. Another interesting feature you might wish to implement is
  167. <emphasis>pretendability</emphasis>. Pretendabilty is the ability
  168. for your provider to "pretend" as if it is doing the requested
  169. action and provider combination and give the user as much
  170. information about what it <emphasis>would</emphasis> do without
  171. actually doing it. This might be an important notion when doing
  172. heavy database or filesystem modifications that the user might not
  173. otherwise want to do.
  174. </para>
  175. <para>
  176. Pretendability is easy to implement. There are two parts to this
  177. feature: 1) marking the provider as having the ability to "pretend",
  178. and 2) checking the request to ensure the current request was indeed
  179. asked to be "pretended". This feature is demonstrated in the code
  180. sample below.
  181. </para>
  182. <programlisting language="php"><![CDATA[
  183. class My_Component_HelloProvider
  184. extends Zend_Tool_Framework_Provider_Abstract
  185. implements Zend_Tool_Framework_Provider_Pretendable
  186. {
  187. public function say($name = 'Ralph')
  188. {
  189. if ($this->_registry->getRequest()->isPretend()) {
  190. echo 'I would say hello to ' . $name . '.';
  191. } else {
  192. echo 'Hello' . $name . ', from my provider!';
  193. }
  194. }
  195. }
  196. ]]></programlisting>
  197. <para>
  198. To run the provider in pretend mode just call:
  199. </para>
  200. <programlisting language="sh"><![CDATA[
  201. % zf --pretend say hello Ralph
  202. I would say hello Ralph.
  203. ]]></programlisting>
  204. </sect3>
  205. <sect3 id="zend.tool.framework.writing-providers.advanced.verbosedebug">
  206. <title>Verbose and Debug modes</title>
  207. <para>
  208. You can also run your provider actions in "verbose" or "debug" modes.
  209. The semantics in regard to this actions have to be implemented by you
  210. in the context of your provider. You can access debug or verbose modes
  211. with:
  212. </para>
  213. <programlisting language="php"><![CDATA[
  214. class My_Component_HelloProvider
  215. implements Zend_Tool_Framework_Provider_Interface
  216. {
  217. public function say($name = 'Ralph')
  218. {
  219. if($this->_registry->getRequest()->isVerbose()) {
  220. echo "Hello::say has been called\n";
  221. }
  222. if($this->_registry->getRequest()->isDebug()) {
  223. syslog(LOG_INFO, "Hello::say has been called\n");
  224. }
  225. }
  226. }
  227. ]]>
  228. </programlisting>
  229. </sect3>
  230. <sect3 id="zend.tool.framework.writing-providers.advanced.configstorage">
  231. <title>Accessing User Config and Storage</title>
  232. <para>
  233. Using the Enviroment variable <property>ZF_CONFIG_FILE</property> or the
  234. .zf.ini in your home directory you can inject configuration parameters into
  235. any Zend Tool provider. Access to this configuration is available via the
  236. registry that is passed to your provider if you extend
  237. <classname>Zend_Tool_Framework_Provider_Abstract</classname>.
  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. $username = $this->_registry->getConfig()->username;
  246. if(!empty($username)) {
  247. echo "Hello $username!";
  248. } else {
  249. echo "Hello!";
  250. }
  251. }
  252. }
  253. ]]>
  254. </programlisting>
  255. <para>
  256. The returned configuration is of the type <classname>Zend_Tool_Framework_Client_Config</classname>
  257. but internally the <code>__get</code> and <code>__set</code> magic methods proxy to a
  258. <classname>Zend_Config</classname> of the given configuration type.
  259. </para>
  260. <para>
  261. The storage allows to save arbitrary data for later reference. This can be useful for batch
  262. processing tasks or for re-runs of your tasks. You can access the storage in a similar way
  263. like the configuration:
  264. </para>
  265. <programlisting language="php"><![CDATA[
  266. class My_Component_HelloProvider
  267. extends Zend_Tool_Framework_Provider_Abstract
  268. {
  269. public function say()
  270. {
  271. $aValue = $this->_registry->getStorage()->get("myUsername");
  272. echo "Hello $aValue!";
  273. }
  274. }
  275. ]]>
  276. </programlisting>
  277. <para>
  278. The API of the storage is very simple:
  279. </para>
  280. <programlisting language="php"><![CDATA[
  281. class Zend_Tool_Framework_Client_Storage
  282. {
  283. public function setAdapter($adapter);
  284. public function isEnabled();
  285. public function put($name, $value);
  286. public function get($name, $defaultValue=null);
  287. public function has($name);
  288. public function remove($name);
  289. public function getStreamUri($name);
  290. }
  291. ]]>
  292. </programlisting>
  293. <important>
  294. <para>
  295. When designing your providers that are config or storage aware remember to
  296. check if the required user-config or storage keys really exist for a user.
  297. You won't run into fatal errors when none of these are provided though,
  298. since empty ones are created upon request.
  299. </para>
  300. </important>
  301. </sect3>
  302. </sect2>
  303. </sect1>