Zend_Tool_Framework-WritingProviders.xml 15 KB

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