Zend_Tool_Framework-WritingProviders.xml 17 KB

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