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