2
0

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