Zend_Tool_Framework-WritingProviders.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 <code>getProviders()</code> 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. ]]>
  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 <code>echo</code>
  124. or a similiar output mechanism. Rewritting our hello provider with this knowledge it looks like:
  125. </para>
  126. <programlisting language="php"><![CDATA[
  127. class My_Component_HelloProvider
  128. extends Zend_Tool_Framework_Provider_Abstract
  129. {
  130. public function say()
  131. {
  132. $this->_registry->getResponse()->appendContent("Hello from my provider!");
  133. }
  134. }
  135. ]]>
  136. </programlisting>
  137. <para>
  138. As you can see one has to extend the <classname>Zend_Tool_Framework_Provider_Abstract</classname>
  139. to gain access to the Registry which holds the <classname>Zend_Tool_Framework_Client_Response</classname>
  140. instance.
  141. </para>
  142. </sect2>
  143. <sect2 id="zend.tool.framework.writing-providers.advanced">
  144. <title>Advanced Development Information</title>
  145. <sect3 id="zend.tool.framework.writing-providers.advanced.variables">
  146. <title>Passing Variables to a Provider</title>
  147. <para>
  148. The above "Hello World" example is great for simple commands, but
  149. what about something more advanced? As your scripting and tooling
  150. needs grow, you might find that you need the ability to accept
  151. variables. Much like function signatures have parameters, your
  152. tooling requests can also accept parameters.
  153. </para>
  154. <para>
  155. Just as each tooling request can be isolated to a method within a
  156. class, the parameters of a tooling request can also be isolated in a
  157. very well known place. Parameters of the action methods of a
  158. provider can include the same parameters you want your client to
  159. utilize when calling that provider and action combination. For
  160. example, if you wanted to accept a name in the above example, you
  161. would probably do this in OO code:
  162. </para>
  163. <programlisting language="php"><![CDATA[
  164. class My_Component_HelloProvider
  165. implements Zend_Tool_Framework_Provider_Interface
  166. {
  167. public function say($name = 'Ralph')
  168. {
  169. echo 'Hello' . $name . ', from my provider!';
  170. }
  171. }
  172. ]]></programlisting>
  173. <para>
  174. The above example can then be called via the command line
  175. <command>zf say hello Joe</command>. "Joe" will be supplied to the provider as
  176. a parameter of the method call. Also note, as you see that the
  177. parameter is optional, that means it is also optional on the command
  178. line, so that <command>zf say hello</command> will still work, and default
  179. to the name "Ralph".
  180. </para>
  181. </sect3>
  182. <sect3 id="zend.tool.framework.writing-providers.advanced.prompt">
  183. <title>Prompt the User for Input</title>
  184. <para>
  185. There are cases when the workflow of your provider requires
  186. to prompt the user for input. This can be done by requesting
  187. the client to ask for more the required input by calling:
  188. </para>
  189. <programlisting language="php"><![CDATA[
  190. class My_Component_HelloProvider
  191. extends Zend_Tool_Framework_Provider_Abstract
  192. {
  193. public function say($name = 'Ralph')
  194. {
  195. $nameResponse = $this->_registry->getClient()->promptInteractiveInput("Whats your name?");
  196. $name = $name->getContent();
  197. echo 'Hello' . $name . ', from my provider!';
  198. }
  199. }
  200. ]]>
  201. </programlisting>
  202. <para>
  203. This command throws an exception if the current client is not
  204. able to handle interactive requests. In case of the default Console Client
  205. however you will be asked to enter the name.
  206. </para>
  207. </sect3>
  208. <sect3 id="zend.tool.framework.writing-providers.advanced.pretendable">
  209. <title>Pretending to execute a Provider Action</title>
  210. <para>
  211. Another interesting feature you might wish to implement is
  212. <emphasis>pretendability</emphasis>. Pretendabilty is the ability
  213. for your provider to "pretend" as if it is doing the requested
  214. action and provider combination and give the user as much
  215. information about what it <emphasis>would</emphasis> do without
  216. actually doing it. This might be an important notion when doing
  217. heavy database or filesystem modifications that the user might not
  218. otherwise want to do.
  219. </para>
  220. <para>
  221. Pretendability is easy to implement. There are two parts to this
  222. feature: 1) marking the provider as having the ability to "pretend",
  223. and 2) checking the request to ensure the current request was indeed
  224. asked to be "pretended". This feature is demonstrated in the code
  225. sample below.
  226. </para>
  227. <programlisting language="php"><![CDATA[
  228. class My_Component_HelloProvider
  229. extends Zend_Tool_Framework_Provider_Abstract
  230. implements Zend_Tool_Framework_Provider_Pretendable
  231. {
  232. public function say($name = 'Ralph')
  233. {
  234. if ($this->_registry->getRequest()->isPretend()) {
  235. echo 'I would say hello to ' . $name . '.';
  236. } else {
  237. echo 'Hello' . $name . ', from my provider!';
  238. }
  239. }
  240. }
  241. ]]></programlisting>
  242. <para>
  243. To run the provider in pretend mode just call:
  244. </para>
  245. <programlisting language="sh"><![CDATA[
  246. % zf --pretend say hello Ralph
  247. I would say hello Ralph.
  248. ]]></programlisting>
  249. </sect3>
  250. <sect3 id="zend.tool.framework.writing-providers.advanced.verbosedebug">
  251. <title>Verbose and Debug modes</title>
  252. <para>
  253. You can also run your provider actions in "verbose" or "debug" modes.
  254. The semantics in regard to this actions have to be implemented by you
  255. in the context of your provider. You can access debug or verbose modes
  256. with:
  257. </para>
  258. <programlisting language="php"><![CDATA[
  259. class My_Component_HelloProvider
  260. implements Zend_Tool_Framework_Provider_Interface
  261. {
  262. public function say($name = 'Ralph')
  263. {
  264. if($this->_registry->getRequest()->isVerbose()) {
  265. echo "Hello::say has been called\n";
  266. }
  267. if($this->_registry->getRequest()->isDebug()) {
  268. syslog(LOG_INFO, "Hello::say has been called\n");
  269. }
  270. }
  271. }
  272. ]]>
  273. </programlisting>
  274. </sect3>
  275. <sect3 id="zend.tool.framework.writing-providers.advanced.configstorage">
  276. <title>Accessing User Config and Storage</title>
  277. <para>
  278. Using the Enviroment variable <property>ZF_CONFIG_FILE</property> or the
  279. .zf.ini in your home directory you can inject configuration parameters into
  280. any Zend Tool provider. Access to this configuration is available via the
  281. registry that is passed to your provider if you extend
  282. <classname>Zend_Tool_Framework_Provider_Abstract</classname>.
  283. </para>
  284. <programlisting language="php"><![CDATA[
  285. class My_Component_HelloProvider
  286. extends Zend_Tool_Framework_Provider_Abstract
  287. {
  288. public function say()
  289. {
  290. $username = $this->_registry->getConfig()->username;
  291. if(!empty($username)) {
  292. echo "Hello $username!";
  293. } else {
  294. echo "Hello!";
  295. }
  296. }
  297. }
  298. ]]>
  299. </programlisting>
  300. <para>
  301. The returned configuration is of the type <classname>Zend_Tool_Framework_Client_Config</classname>
  302. but internally the <code>__get</code> and <code>__set</code> magic methods proxy to a
  303. <classname>Zend_Config</classname> of the given configuration type.
  304. </para>
  305. <para>
  306. The storage allows to save arbitrary data for later reference. This can be useful for batch
  307. processing tasks or for re-runs of your tasks. You can access the storage in a similar way
  308. like the configuration:
  309. </para>
  310. <programlisting language="php"><![CDATA[
  311. class My_Component_HelloProvider
  312. extends Zend_Tool_Framework_Provider_Abstract
  313. {
  314. public function say()
  315. {
  316. $aValue = $this->_registry->getStorage()->get("myUsername");
  317. echo "Hello $aValue!";
  318. }
  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. ]]>
  337. </programlisting>
  338. <important>
  339. <para>
  340. When designing your providers that are config or storage aware remember to
  341. check if the required user-config or storage keys really exist for a user.
  342. You won't run into fatal errors when none of these are provided though,
  343. since empty ones are created upon request.
  344. </para>
  345. </important>
  346. </sect3>
  347. </sect2>
  348. </sect1>