Zend_Tool-Extending.xml 41 KB

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