Zend_Tool-Extending.xml 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19778 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.tool.extending">
  5. <title>Extending Zend_Tool</title>
  6. <sect2 id="zend.tool.extending.overview">
  7. <title>Overview of Zend_Tool</title>
  8. <para>
  9. <classname>Zend_Tool_Framework</classname> is a framework for exposing common
  10. functionalities such as the creation of project scaffolds, code
  11. generation, search index generation, and much more. Functionality may be
  12. written and exposed via <acronym>PHP</acronym> classes dropped into the
  13. <acronym>PHP</acronym> <property>include_path</property>, providing incredible
  14. flexibility of implementation. The functionality may then be consumed by writing
  15. implementation and/or protocol-specific clients -- such as console
  16. clients, <acronym>XML-RPC</acronym>, <acronym>SOAP</acronym>, and much more.
  17. </para>
  18. <para>
  19. <classname>Zend_Tool_Project</classname> builds on and extends the capabilities of
  20. <classname>Zend_Tool_Framework</classname> to that of managing a "project". In general, a
  21. "project" is a planned endeavor or an initiative. In the computer world, projects generally
  22. are a collection of resources. These resources can be files, directories, databases,
  23. schemas, images, styles, and more.
  24. </para>
  25. </sect2>
  26. <sect2 id="zend.tool.extending.zend-tool-framework">
  27. <title>Zend_Tool_Framework Extensions</title>
  28. <sect3 id="zend.tool.extending.zend-tool-framework.architecture">
  29. <title>Overall Architecture</title>
  30. <para>
  31. <classname>Zend_Tool_Framework</classname> provides the following:
  32. </para>
  33. <itemizedlist>
  34. <listitem>
  35. <para>
  36. <emphasis>Common interfaces and abstracts</emphasis> that allow
  37. developers to create functionality and capabilities that are
  38. dispatchable by tooling clients.
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. <emphasis>Base client functionality</emphasis> and a concrete
  44. console implementation that connect external tools and
  45. interfaces to the <classname>Zend_Tool_Framework</classname>. The Console
  46. client may be used in <acronym>CLI</acronym> environments such as unix shells and
  47. the Windows console.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <emphasis>"Provider" and "Manifest" interfaces</emphasis> that
  53. can be utilized by the tooling system. "Providers" represent the
  54. functional aspect of the framework, and define the actions that
  55. tooling clients may call. "Manifests" act as metadata registries
  56. that provide additional context for the various defined
  57. providers.
  58. </para>
  59. </listitem>
  60. <listitem>
  61. <para>
  62. <emphasis>An introspective loading system</emphasis> that will
  63. scan the environment for providers and determine what is
  64. required to dispatch them.
  65. </para>
  66. </listitem>
  67. <listitem>
  68. <para>
  69. <emphasis>A standard set of system providers</emphasis> that
  70. allow the system to report what the full capabilities of the
  71. system are as well as provide useful feedback. This also
  72. includes a comprehensive "Help System".
  73. </para>
  74. </listitem>
  75. </itemizedlist>
  76. <para>
  77. Definitions that you should be aware of through this manual with respect
  78. to <classname>Zend_Tool_Framework</classname> include:
  79. </para>
  80. <itemizedlist>
  81. <listitem><para>
  82. <classname>Zend_Tool_Framework</classname> - The framework which exposes
  83. tooling capabilities.
  84. </para></listitem>
  85. <listitem><para>
  86. <emphasis>Tooling Client</emphasis> - A developer tool that connects
  87. to and consumes <classname>Zend_Tool_Framework</classname>.
  88. </para></listitem>
  89. <listitem><para>
  90. <emphasis>Client</emphasis> - The subsystem of
  91. <classname>Zend_Tool_Framework</classname> that exposes an interface such that
  92. tooling clients can connect, query and execute commands.
  93. </para></listitem>
  94. <listitem><para>
  95. <emphasis>Console Client / Command Line Interface /
  96. <filename>zf.php</filename></emphasis> - The tooling client for the command line.
  97. </para></listitem>
  98. <listitem><para>
  99. <emphasis>Provider</emphasis> - A subsystem and a collection of
  100. built-in functionality that the framework exports.
  101. </para></listitem>
  102. <listitem><para>
  103. <emphasis>Manifest</emphasis> - A subsystem for defining,
  104. organizing, and disseminating provider requirement data.
  105. </para></listitem>
  106. <listitem><para>
  107. <classname>Zend_Tool_Project</classname> Provider - A set of providers
  108. specifically for creating and maintaining Zend Framework-based
  109. projects.
  110. </para></listitem>
  111. </itemizedlist>
  112. </sect3>
  113. <sect3 id="zend.tool.extending.zend-tool-framework.cli-client">
  114. <title>Understanding the CLI Client</title>
  115. <para>
  116. The <acronym>CLI</acronym>, or command line tool (internally known as the console tool),
  117. is currently the primary interface for dispatching <classname>Zend_Tool</classname>
  118. requests. With the <acronym>CLI</acronym> tool, developers can issue tooling requests
  119. inside the "command line windows", also commonly known as a "terminal"
  120. window. This environment is predominant in the *nix environment, but
  121. also has a common implementation in windows with the
  122. <filename>cmd.exe</filename>, console2 and also with the Cygwin project.
  123. </para>
  124. <sect4 id="zend.tool.extending.zend-tool-framework.cli-client.setup-general">
  125. <title>Setting up the CLI tool</title>
  126. <para>
  127. To issue tooling requests via the command line client, you first
  128. need to set up the client so that your system can handle the "zf"
  129. command. The command line client, for all intents and purposes, is
  130. the <filename>.sh</filename> or <filename>.bat</filename> file that is provided with
  131. your Zend Framework distribution. In trunk, it can be found here:
  132. <ulink
  133. url="http://framework.zend.com/svn/framework/standard/trunk/bin/">http://framework.zend.com/svn/framework/standard/trunk/bin/</ulink>.
  134. </para>
  135. <para>
  136. As you can see, there are 3 files in the <filename>/bin/</filename>
  137. directory: a <filename>zf.php</filename>, <filename>zf.sh</filename>, and
  138. <filename>zf.bat</filename>. The <filename>zf.sh</filename> and the
  139. <filename>zf.bat</filename> are the operating system specific client
  140. wrappers: <filename>zf.sh</filename> for the *nix environment, and
  141. <filename>zf.bat</filename> for the Win32 environment. These client wrappers are
  142. responsible for finding the proper <filename>php.exe</filename>, finding the
  143. <filename>zf.php</filename>, and passing on the client request. The
  144. <filename>zf.php</filename> is the responsible for handling understanding
  145. your environment, constructing the proper include_path, and passing
  146. what is provided on the command line to the proper library component
  147. for dispatching.
  148. </para>
  149. <para>
  150. Ultimately, you want to ensure two things to make everything work
  151. regardless of the operating system you are on:
  152. </para>
  153. <orderedlist>
  154. <listitem>
  155. <para>
  156. <filename>zf.sh/zf.bat</filename> is reachable from your system
  157. path. This is the ability to call <command>zf</command> from
  158. anywhere on your command line, regardless of what your
  159. current working directory is.
  160. </para>
  161. </listitem>
  162. <listitem>
  163. <para>
  164. <filename>ZendFramework/library</filename> is in your
  165. <property>include_path</property>.
  166. </para>
  167. </listitem>
  168. </orderedlist>
  169. <note>
  170. <para>
  171. Note: while the above are the most ideal
  172. requirements, you can simply download Zend Framework and expect it
  173. to work as <filename>./path/to/zf.php</filename> some command.
  174. </para>
  175. </note>
  176. </sect4>
  177. <sect4 id="zend.tool.extending.zend-tool-framework.cli-client.setup-starnix">
  178. <title>Setting up the CLI tool on Unix-like Systems</title>
  179. <para>
  180. The most common setup in the *nix environment, is to copy the
  181. <filename>zf.sh</filename> and <filename>zf.php</filename> into the same directory
  182. as your <acronym>PHP</acronym> binary. This can generally be found in one of the
  183. following places:
  184. </para>
  185. <programlisting language="text"><![CDATA[
  186. /usr/bin
  187. /usr/local/bin
  188. /usr/local/ZendServer/bin/
  189. /Applications/ZendServer/bin/
  190. ]]></programlisting>
  191. <para>
  192. To find out the location of your <acronym>PHP</acronym> binary, you can execute 'which
  193. php' on the command line. This will return the location of the <acronym>PHP</acronym>
  194. binary you will be using to run <acronym>PHP</acronym> scripts in this environment.
  195. </para>
  196. <para>
  197. The next order of business is to ensure that Zend Framework
  198. library is set up correctly inside of the system <acronym>PHP</acronym>
  199. <property>include_path</property>. To find out where your
  200. <property>include_path</property> is located, you can execute <command>php -i</command>
  201. and look for the <property>include_path</property> variable, or more succinctly,
  202. execute <command>php -i | grep include_path</command>. Once you have found where
  203. your <property>include_path</property> is located (this will generally be
  204. something like <filename>/usr/lib/php</filename>, <filename>/usr/share/php</filename>,
  205. <filename>/usr/local/lib/php</filename>, or similar), ensure that the contents of the
  206. <filename>/library/</filename> directory are put
  207. inside your <property>include_path</property> specified directory.
  208. </para>
  209. <para>
  210. Once you have done those two things, you should be able to issue a
  211. command and get back the proper response like this:
  212. </para>
  213. <para>
  214. <inlinegraphic scale="100" align="center" valign="middle"
  215. fileref="figures/zend.tool.framework.cliversionunix.png" format="PNG" />
  216. </para>
  217. <para>
  218. If you do not see this type of output, go back and check your setup
  219. to ensure you have all of the necessary pieces in the proper place.
  220. </para>
  221. <para>
  222. There are a couple of alternative setups you might want to employ
  223. depending on your servers configuration, your level of access, or
  224. for other reasons.
  225. </para>
  226. <para>
  227. <emphasis>Alternative Setup</emphasis> involves keeping the Zend
  228. Framework download together as is, and creating a link from a <constant>PATH</constant>
  229. location to the <filename>zf.sh</filename>. What this means is you can
  230. place the contents of the ZendFramework download into a location
  231. such as <filename>/usr/local/share/ZendFramework</filename>, or more locally
  232. like <filename>/home/username/lib/ZendFramework</filename>, and creating a
  233. symbolic link to the <filename>zf.sh</filename>.
  234. </para>
  235. <para>
  236. Assuming you want to put the link inside <filename>/usr/local/bin</filename>
  237. (this could also work for placing the link inside
  238. <filename>/home/username/bin/</filename> for example) you would issue a
  239. command similar to this: </para>
  240. <programlisting language="sh"><![CDATA[
  241. ln -s /usr/local/share/ZendFramework/bin/zf.sh /usr/local/bin/zf
  242. # OR (for example)
  243. ln -s /home/username/lib/ZendFramework/bin/zf.sh /home/username/bin/zf
  244. ]]></programlisting>
  245. <para>
  246. This will create a link which you should be able to access globally
  247. on the command line.
  248. </para>
  249. </sect4>
  250. <sect4 id="zend.tool.extending.zend-tool-framework.cli-client.setup-windows">
  251. <title>Setting up the CLI tool on Windows</title>
  252. <para>
  253. The most common setup in the Windows Win32 environment, is to copy
  254. the <filename>zf.bat</filename> and <filename>zf.php</filename> into the same
  255. directory as your <acronym>PHP</acronym> binary. This can generally be found in one of
  256. the following places:
  257. </para>
  258. <programlisting language="text"><![CDATA[
  259. C:\PHP
  260. C:\Program Files\ZendServer\bin\
  261. C:\WAMP\PHP\bin
  262. ]]></programlisting>
  263. <para>
  264. You should be able to run <filename>php.exe</filename> on the command line.
  265. If you are not able to, first check the documentation that came with
  266. your <acronym>PHP</acronym> distribution, or ensure that the path to
  267. <filename>php.exe</filename> is in your
  268. Windows <constant>PATH</constant> environment variable.
  269. </para>
  270. <para>
  271. The next order of business is to ensure that Zend Framework
  272. library is set up correctly inside of the system <acronym>PHP</acronym>
  273. <property>include_path</property>. To find out where your
  274. <property>include_path</property> is located, you can type <command>php -i</command> and
  275. look for the <property>include_path</property> variable, or more succinctly
  276. execute <command>php -i | grep include_path</command> if you have Cygwin setup with
  277. grep available. Once you have found where your
  278. <property>include_path</property> is located (this will generally be
  279. something like <filename>C:\PHP\pear</filename>, <filename>C:\PHP\share</filename>,
  280. <filename>C:\Program%20Files\ZendServer\share</filename> or similar), ensure
  281. that the contents of the library/ directory are put inside your
  282. <property>include_path</property> specified directory.
  283. </para>
  284. <para>
  285. Once you have done those two things, you should be able to issue a
  286. command and get back the proper response like this:
  287. </para>
  288. <para>
  289. <inlinegraphic scale="100" align="center" valign="middle"
  290. fileref="figures/zend.tool.framework.cliversionwin32.png" format="PNG" />
  291. </para>
  292. <para>
  293. If you do not see this type of output, go back and check your setup
  294. to ensure you have all of the necessary pieces in the proper place.
  295. </para>
  296. <para>
  297. There are a couple of alternative setups you might want to employ
  298. depending on your server's configuration, your level of access, or
  299. for other reasons.
  300. </para>
  301. <para>
  302. <emphasis>Alternative Setup</emphasis> involves keeping the Zend
  303. Framework download together as is, and altering both your system
  304. <constant>PATH</constant> as well as the <filename>php.ini</filename> file.
  305. In your user's environment, make sure to add
  306. <filename>C:\Path\To\ZendFramework\bin</filename>, so that your
  307. <filename>zf.bat</filename> file is executable. Also, alter the
  308. <filename>php.ini</filename> file to ensure that
  309. <filename>C:\Path\To\ZendFramework\library</filename> is in your
  310. <property>include_path</property>.
  311. </para>
  312. </sect4>
  313. <sect4 id="zend.tool.extending.zend-tool-framework.cli-client.setup-othernotes">
  314. <title>Other Setup Considerations</title>
  315. <para>
  316. If for some reason you do not want Zend Framework library inside
  317. your <property>include_path</property>, there is another option. There are
  318. two special environment variables that <filename>zf.php</filename> will
  319. utilize to determine the location of your Zend Framework
  320. installation.
  321. </para>
  322. <para>
  323. The first is <constant>ZEND_TOOL_INCLUDE_PATH_PREPEND</constant>, which will
  324. prepend the value of this environment variable to the system
  325. (<filename>php.ini</filename>) <property>include_path</property> before loading the
  326. client.
  327. </para>
  328. <para>
  329. Alternatively, you might want to use
  330. <constant>ZEND_TOOL_INCLUDE_PATH</constant> to completely
  331. <emphasis>replace</emphasis> the system <property>include_path</property>
  332. for one that makes sense specifically for the <command>zf</command>
  333. command line tool.
  334. </para>
  335. </sect4>
  336. </sect3>
  337. <sect3 id="zend.tool.extending.zend-tool-framework.providers-and-manifests">
  338. <title>Creating Providers</title>
  339. <para>
  340. In general, a provider, on its own, is nothing more than the shell for a
  341. developer to bundle up some capabilities they wish to dispatch with the
  342. command line (or other) clients. It is an analogue to what a
  343. "controller" is inside of your <acronym>MVC</acronym> application.
  344. </para>
  345. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.loading">
  346. <title>How Zend Tool finds your Providers</title>
  347. <para>
  348. By default Zend Tool uses the BasicLoader to find all
  349. the providers that you can run. It recursivly iterates all
  350. include path directories and opens all files that end
  351. with "Manifest.php" or "Provider.php". All classes in these
  352. files are inspected if they implement either
  353. <classname>Zend_Tool_Framework_Provider_Interface</classname>
  354. or <classname>Zend_Tool_Framework_Manifest_ProviderManifestable</classname>.
  355. Instances of the provider interface make up for the real functionality
  356. and all their public methods are accessible as provider actions.
  357. The ProviderManifestable interface however requires the implementation of a method
  358. <methodname>getProviders()</methodname> which returns an array of
  359. instantiated provider interface instances.
  360. </para>
  361. <para>
  362. The following naming rules apply on how you can access the providers
  363. that were found by the IncludePathLoader:
  364. </para>
  365. <itemizedlist>
  366. <listitem>
  367. <para>
  368. The last part of your classname split by underscore is used
  369. for the provider name, e.g. "My_Provider_Hello" leads to your
  370. provider being accessible by the name "hello".
  371. </para>
  372. </listitem>
  373. <listitem>
  374. <para>
  375. If your provider has a method <methodname>getName()</methodname>
  376. it will be used instead of the previous method to determine
  377. the name.
  378. </para>
  379. </listitem>
  380. <listitem>
  381. <para>
  382. If your provider has "Provider" as prefix, e.g. it is called
  383. <classname>My_HelloProvider</classname> it will be stripped
  384. from the name so that the provider will be called "hello".
  385. </para>
  386. </listitem>
  387. </itemizedlist>
  388. <note>
  389. <para>The IncludePathLoader does not follow symlinks, that means
  390. you cannot link provider functionality into your include paths,
  391. they have to be physically present in the include paths.</para>
  392. </note>
  393. <example id="zend.tool.extending.zend-tool-framework.providers-and-manifests.loading.example">
  394. <title>Exposing Your Providers with a Manifest</title>
  395. <para>
  396. You can expose your providers to Zend Tool by offering a manifest
  397. with a special filename ending with "Manifest.php".
  398. A Provider Manifest is an implementation of the
  399. <interface>Zend_Tool_Framework_Manifest_ProviderManifestable</interface>
  400. and requires the <methodname>getProviders()</methodname> method to return
  401. an array of instantiated providers. In anticipation of our first
  402. own provider <classname>My_Component_HelloProvider</classname>
  403. we will create the following manifest:
  404. </para>
  405. <programlisting language="php"><![CDATA[
  406. class My_Component_Manifest
  407. implements Zend_Tool_Framework_Manifest_ProviderManifestable
  408. {
  409. public function getProviders()
  410. {
  411. return array(
  412. new My_Component_HelloProvider()
  413. );
  414. }
  415. }
  416. ]]></programlisting>
  417. </example>
  418. </sect4>
  419. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.basic">
  420. <title>Basic Instructions for Creating Providers</title>
  421. <para>
  422. As an example, if a developer wants to add the capability of showing
  423. the version of a datafile that his 3rd party component is working
  424. from, there is only one class the developer would need to implement.
  425. Assuming the component is called <classname>My_Component</classname>, he would
  426. create a class named <classname>My_Component_HelloProvider</classname> in a
  427. file named <filename>HelloProvider.php</filename> somewhere on the
  428. <property>include_path</property>. This class would implement
  429. <classname>Zend_Tool_Framework_Provider_Interface</classname>, and the body of
  430. this file would only have to look like the following:
  431. </para>
  432. <programlisting language="php"><![CDATA[
  433. class My_Component_HelloProvider
  434. implements Zend_Tool_Framework_Provider_Interface
  435. {
  436. public function say()
  437. {
  438. echo 'Hello from my provider!';
  439. }
  440. }
  441. ]]></programlisting>
  442. <para>
  443. Given that code above, and assuming the developer wishes to access
  444. this functionality through the console client, the call would look
  445. like this:
  446. </para>
  447. <programlisting language="sh"><![CDATA[
  448. % zf say hello
  449. Hello from my provider!
  450. ]]></programlisting>
  451. </sect4>
  452. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.response">
  453. <title>The response object</title>
  454. <para>
  455. As discussed in the architecture section Zend Tool allows to hook different clients for
  456. using your Zend Tool providers. To keep compliant with different clients you should
  457. use the response object to return messages from your providers instead of using
  458. <methodname>echo()</methodname> or a similiar output mechanism. Rewritting our hello
  459. provider with this knowledge it looks like:
  460. </para>
  461. <programlisting language="php"><![CDATA[
  462. class My_Component_HelloProvider
  463. extends Zend_Tool_Framework_Provider_Abstract
  464. {
  465. public function say()
  466. {
  467. $this->_registry->getResponse
  468. ->appendContent("Hello from my provider!");
  469. }
  470. }
  471. ]]></programlisting>
  472. <para>
  473. As you can see one has to extend the <classname>Zend_Tool_Framework_Provider_Abstract</classname>
  474. to gain access to the Registry which holds the <classname>Zend_Tool_Framework_Client_Response</classname>
  475. instance.
  476. </para>
  477. </sect4>
  478. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced">
  479. <title>Advanced Development Information</title>
  480. <sect5 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.variables">
  481. <title>Passing Variables to a Provider</title>
  482. <para>
  483. The above "Hello World" example is great for simple commands, but
  484. what about something more advanced? As your scripting and tooling
  485. needs grow, you might find that you need the ability to accept
  486. variables. Much like function signatures have parameters, your
  487. tooling requests can also accept parameters.
  488. </para>
  489. <para>
  490. Just as each tooling request can be isolated to a method within a
  491. class, the parameters of a tooling request can also be isolated in a
  492. very well known place. Parameters of the action methods of a
  493. provider can include the same parameters you want your client to
  494. utilize when calling that provider and action combination. For
  495. example, if you wanted to accept a name in the above example, you
  496. would probably do this in OO code:
  497. </para>
  498. <programlisting language="php"><![CDATA[
  499. class My_Component_HelloProvider
  500. implements Zend_Tool_Framework_Provider_Interface
  501. {
  502. public function say($name = 'Ralph')
  503. {
  504. echo 'Hello' . $name . ', from my provider!';
  505. }
  506. }
  507. ]]></programlisting>
  508. <para>
  509. The above example can then be called via the command line
  510. <command>zf say hello Joe</command>. "Joe" will be supplied to the provider as
  511. a parameter of the method call. Also note, as you see that the
  512. parameter is optional, that means it is also optional on the command
  513. line, so that <command>zf say hello</command> will still work, and default
  514. to the name "Ralph".
  515. </para>
  516. </sect5>
  517. <sect5 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.prompt">
  518. <title>Prompt the User for Input</title>
  519. <para>
  520. There are cases when the workflow of your provider requires
  521. to prompt the user for input. This can be done by requesting
  522. the client to ask for more the required input by calling:
  523. </para>
  524. <programlisting language="php"><![CDATA[
  525. class My_Component_HelloProvider
  526. extends Zend_Tool_Framework_Provider_Abstract
  527. {
  528. public function say($name = 'Ralph')
  529. {
  530. $nameResponse = $this->_registry
  531. ->getClient()
  532. ->promptInteractiveInput("Whats your name?");
  533. $name = $name->getContent();
  534. echo 'Hello' . $name . ', from my provider!';
  535. }
  536. }
  537. ]]></programlisting>
  538. <para>
  539. This command throws an exception if the current client is not
  540. able to handle interactive requests. In case of the default Console Client
  541. however you will be asked to enter the name.
  542. </para>
  543. </sect5>
  544. <sect5 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.pretendable">
  545. <title>Pretending to execute a Provider Action</title>
  546. <para>
  547. Another interesting feature you might wish to implement is
  548. <emphasis>pretendability</emphasis>. Pretendabilty is the ability
  549. for your provider to "pretend" as if it is doing the requested
  550. action and provider combination and give the user as much
  551. information about what it <emphasis>would</emphasis> do without
  552. actually doing it. This might be an important notion when doing
  553. heavy database or filesystem modifications that the user might not
  554. otherwise want to do.
  555. </para>
  556. <para>
  557. Pretendability is easy to implement. There are two parts to this
  558. feature: 1) marking the provider as having the ability to "pretend",
  559. and 2) checking the request to ensure the current request was indeed
  560. asked to be "pretended". This feature is demonstrated in the code
  561. sample below.
  562. </para>
  563. <programlisting language="php"><![CDATA[
  564. class My_Component_HelloProvider
  565. extends Zend_Tool_Framework_Provider_Abstract
  566. implements Zend_Tool_Framework_Provider_Pretendable
  567. {
  568. public function say($name = 'Ralph')
  569. {
  570. if ($this->_registry->getRequest()->isPretend()) {
  571. echo 'I would say hello to ' . $name . '.';
  572. } else {
  573. echo 'Hello' . $name . ', from my provider!';
  574. }
  575. }
  576. }
  577. ]]></programlisting>
  578. <para>
  579. To run the provider in pretend mode just call:
  580. </para>
  581. <programlisting language="sh"><![CDATA[
  582. % zf --pretend say hello Ralph
  583. I would say hello Ralph.
  584. ]]></programlisting>
  585. </sect5>
  586. <sect5 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.verbosedebug">
  587. <title>Verbose and Debug modes</title>
  588. <para>
  589. You can also run your provider actions in "verbose" or "debug" modes.
  590. The semantics in regard to this actions have to be implemented by you
  591. in the context of your provider. You can access debug or verbose modes
  592. with:
  593. </para>
  594. <programlisting language="php"><![CDATA[
  595. class My_Component_HelloProvider
  596. implements Zend_Tool_Framework_Provider_Interface
  597. {
  598. public function say($name = 'Ralph')
  599. {
  600. if($this->_registry->getRequest()->isVerbose()) {
  601. echo "Hello::say has been called\n";
  602. }
  603. if($this->_registry->getRequest()->isDebug()) {
  604. syslog(LOG_INFO, "Hello::say has been called\n");
  605. }
  606. }
  607. }
  608. ]]></programlisting>
  609. </sect5>
  610. <sect5 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.configstorage">
  611. <title>Accessing User Config and Storage</title>
  612. <para>
  613. Using the Enviroment variable <property>ZF_CONFIG_FILE</property> or the
  614. .zf.ini in your home directory you can inject configuration parameters into
  615. any Zend Tool provider. Access to this configuration is available via the
  616. registry that is passed to your provider if you extend
  617. <classname>Zend_Tool_Framework_Provider_Abstract</classname>.
  618. </para>
  619. <programlisting language="php"><![CDATA[
  620. class My_Component_HelloProvider
  621. extends Zend_Tool_Framework_Provider_Abstract
  622. {
  623. public function say()
  624. {
  625. $username = $this->_registry->getConfig()->username;
  626. if(!empty($username)) {
  627. echo "Hello $username!";
  628. } else {
  629. echo "Hello!";
  630. }
  631. }
  632. }
  633. ]]></programlisting>
  634. <para>
  635. The returned configuration is of the type
  636. <classname>Zend_Tool_Framework_Client_Config</classname> but internally the
  637. <methodname>__get()</methodname> and <methodname>__set()</methodname> magic methods
  638. proxy to a <classname>Zend_Config</classname> of the given configuration type.
  639. </para>
  640. <para>
  641. The storage allows to save arbitrary data for later reference. This can be useful for batch
  642. processing tasks or for re-runs of your tasks. You can access the storage in a similar way
  643. like the configuration:
  644. </para>
  645. <programlisting language="php"><![CDATA[
  646. class My_Component_HelloProvider
  647. extends Zend_Tool_Framework_Provider_Abstract
  648. {
  649. public function say()
  650. {
  651. $aValue = $this->_registry->getStorage()->get("myUsername");
  652. echo "Hello $aValue!";
  653. }
  654. }
  655. ]]></programlisting>
  656. <para>
  657. The API of the storage is very simple:
  658. </para>
  659. <programlisting language="php"><![CDATA[
  660. class Zend_Tool_Framework_Client_Storage
  661. {
  662. public function setAdapter($adapter);
  663. public function isEnabled();
  664. public function put($name, $value);
  665. public function get($name, $defaultValue=null);
  666. public function has($name);
  667. public function remove($name);
  668. public function getStreamUri($name);
  669. }
  670. ]]></programlisting>
  671. <important>
  672. <para>
  673. When designing your providers that are config or storage aware remember to
  674. check if the required user-config or storage keys really exist for a user.
  675. You won't run into fatal errors when none of these are provided though,
  676. since empty ones are created upon request.
  677. </para>
  678. </important>
  679. </sect5>
  680. </sect4>
  681. </sect3>
  682. </sect2>
  683. <sect2 id="zend.tool.extending.zend-tool-project">
  684. <title>Zend_Tool_Project Extensions</title>
  685. <para>
  686. Zend_Tool_Project exposes a rich set of functionality and capabilities that make the task
  687. of creating new providers, specficially those targetting project easier and more manageable.
  688. </para>
  689. <sect3 id="zend.tool.extending.zend-tool-project.architecture">
  690. <title>Overall Architecture</title>
  691. <para>
  692. This same concept applies to Zend Framework projects. In Zend Framework projects, you have
  693. controllers, actions, views, models, databases and so on and so forth. In terms of
  694. <classname>Zend_Tool</classname>, we need a way to track these types of resources - thus
  695. <classname>Zend_Tool_Project</classname>.
  696. </para>
  697. <para>
  698. <classname>Zend_Tool_Project</classname> is capable of tracking project resources throughout
  699. the development of a project. So, for example, if in one command you created a controller,
  700. and in the next command you wish to create an action within that controller,
  701. <classname>Zend_Tool_Project</classname> is gonna have to <emphasis>know</emphasis> about
  702. the controller file you created so that you can (in the next action), be able to append that
  703. action to it. This is what keeps our projects up to date and <emphasis>stateful</emphasis>.
  704. </para>
  705. <para>
  706. Another important point to understand about projects is that typically, resources are
  707. organized in a hierarchical fashion. With that in mind,
  708. <classname>Zend_Tool_Project</classname> is capable of serializing the current project into
  709. a internal representation that allows it to keep track of not only <emphasis>what</emphasis>
  710. resources are part of a project at any given time, but also <emphasis>where</emphasis> they
  711. are in relation to one another.
  712. </para>
  713. </sect3>
  714. <sect3 id="zend.tool.extending.zend-tool-project.providers">
  715. <title>Creating Providers</title>
  716. <para>
  717. Project specific providers are created in the same fashion as plain framework providers, with
  718. one exception: project providers must extend the <code>Zend_Tool_Project_Provider_Abstract</code>.
  719. This class comes with some significant functionality that helps developers load existing project,
  720. obtian the profile object, and be able to search the profile, then later store any changes to the
  721. current project profile.
  722. </para>
  723. <programlisting language="php"><![CDATA[
  724. class My_Component_HelloProvider
  725. extends Zend_Tool_Project_Provider_Abstract
  726. {
  727. public function say()
  728. {
  729. $profile = $this->_loadExistingProfile();
  730. /* ... do project stuff here */
  731. $this->_storeProfile();
  732. }
  733. }
  734. ]]></programlisting>
  735. </sect3>
  736. <!--
  737. <sect3 id="zend.tool.extending.zend-tool-project.resources-and-contexts">
  738. <title>Creating Resources &amp; Contexts</title>
  739. </sect3>
  740. -->
  741. </sect2>
  742. </sect1>