Zend_Tool_Framework-WritingProviders.xml 15 KB

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