Zend_Tool-Extending.xml 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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:
  256. </para>
  257. <programlisting language="sh"><![CDATA[
  258. ln -s /usr/local/share/ZendFramework/bin/zf.sh /usr/local/bin/zf
  259. # OR (for example)
  260. ln -s /home/username/lib/ZendFramework/bin/zf.sh /home/username/bin/zf
  261. ]]></programlisting>
  262. <para>
  263. This will create a link which you should be able to access globally
  264. on the command line.
  265. </para>
  266. </sect4>
  267. <sect4 id="zend.tool.extending.zend-tool-framework.cli-client.setup-windows">
  268. <title>Setting up the CLI tool on Windows</title>
  269. <para>
  270. The most common setup in the Windows Win32 environment, is to copy
  271. the <filename>zf.bat</filename> and <filename>zf.php</filename> into the same
  272. directory as your <acronym>PHP</acronym> binary. This can generally be found in
  273. one of the following places:
  274. </para>
  275. <programlisting language="text"><![CDATA[
  276. C:\PHP
  277. C:\Program Files\ZendServer\bin\
  278. C:\WAMP\PHP\bin
  279. ]]></programlisting>
  280. <para>
  281. You should be able to run <filename>php.exe</filename> on the command line.
  282. If you are not able to, first check the documentation that came with
  283. your <acronym>PHP</acronym> distribution, or ensure that the path to
  284. <filename>php.exe</filename> is in your
  285. Windows <constant>PATH</constant> environment variable.
  286. </para>
  287. <para>
  288. The next order of business is to ensure that Zend Framework
  289. library is set up correctly inside of the system <acronym>PHP</acronym>
  290. <property>include_path</property>. To find out where your
  291. <property>include_path</property> is located, you can type
  292. <command>php -i</command> and look for the <property>include_path</property>
  293. variable, or more succinctly execute
  294. <command>php -i | grep include_path</command> if you have Cygwin setup with
  295. grep available. Once you have found where your
  296. <property>include_path</property> is located (this will generally be
  297. something like <filename>C:\PHP\pear</filename>,
  298. <filename>C:\PHP\share</filename>,
  299. <filename>C:\Program%20Files\ZendServer\share</filename> or similar), ensure
  300. that the contents of the library/ directory are put inside your
  301. <property>include_path</property> specified directory.
  302. </para>
  303. <para>
  304. Once you have done those two things, you should be able to issue a
  305. command and get back the proper response like this:
  306. </para>
  307. <para>
  308. <inlinegraphic scale="100" align="center" valign="middle"
  309. fileref="figures/zend.tool.framework.cliversionwin32.png" format="PNG" />
  310. </para>
  311. <para>
  312. If you do not see this type of output, go back and check your setup
  313. to ensure you have all of the necessary pieces in the proper place.
  314. </para>
  315. <para>
  316. There are a couple of alternative setups you might want to employ
  317. depending on your server's configuration, your level of access, or
  318. for other reasons.
  319. </para>
  320. <para>
  321. <emphasis>Alternative Setup</emphasis> involves keeping the Zend
  322. Framework download together as is, and altering both your system
  323. <constant>PATH</constant> as well as the <filename>php.ini</filename> file.
  324. In your user's environment, make sure to add
  325. <filename>C:\Path\To\ZendFramework\bin</filename>, so that your
  326. <filename>zf.bat</filename> file is executable. Also, alter the
  327. <filename>php.ini</filename> file to ensure that
  328. <filename>C:\Path\To\ZendFramework\library</filename> is in your
  329. <property>include_path</property>.
  330. </para>
  331. </sect4>
  332. <sect4 id="zend.tool.extending.zend-tool-framework.cli-client.setup-othernotes">
  333. <title>Other Setup Considerations</title>
  334. <para>
  335. If for some reason you do not want Zend Framework library inside
  336. your <property>include_path</property>, there is another option. There are
  337. two special environment variables that <filename>zf.php</filename> will
  338. utilize to determine the location of your Zend Framework
  339. installation.
  340. </para>
  341. <para>
  342. The first is <constant>ZEND_TOOL_INCLUDE_PATH_PREPEND</constant>, which will
  343. prepend the value of this environment variable to the system
  344. (<filename>php.ini</filename>) <property>include_path</property> before loading
  345. the client.
  346. </para>
  347. <para>
  348. Alternatively, you might want to use
  349. <constant>ZEND_TOOL_INCLUDE_PATH</constant> to completely
  350. <emphasis>replace</emphasis> the system <property>include_path</property>
  351. for one that makes sense specifically for the <command>zf</command>
  352. command line tool.
  353. </para>
  354. </sect4>
  355. </sect3>
  356. <sect3 id="zend.tool.extending.zend-tool-framework.providers-and-manifests">
  357. <title>Creating Providers</title>
  358. <para>
  359. In general, a provider, on its own, is nothing more than the shell for a
  360. developer to bundle up some capabilities they wish to dispatch with the
  361. command line (or other) clients. It is an analogue to what a
  362. "controller" is inside of your <acronym>MVC</acronym> application.
  363. </para>
  364. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.loading">
  365. <title>How Zend_Tool finds your Providers</title>
  366. <para>
  367. By default <classname>Zend_Tool</classname> uses the BasicLoader to find all
  368. the providers that you can run. It recursivly iterates all
  369. include path directories and opens all files that end
  370. with "Manifest.php" or "Provider.php". All classes in these
  371. files are inspected if they implement either
  372. <classname>Zend_Tool_Framework_Provider_Interface</classname>
  373. or <classname>Zend_Tool_Framework_Manifest_ProviderManifestable</classname>.
  374. Instances of the provider interface make up for the real functionality
  375. and all their public methods are accessible as provider actions.
  376. The ProviderManifestable interface however requires the implementation of a
  377. method <methodname>getProviders()</methodname> which returns an array of
  378. instantiated provider interface instances.
  379. </para>
  380. <para>
  381. The following naming rules apply on how you can access the providers
  382. that were found by the IncludePathLoader:
  383. </para>
  384. <itemizedlist>
  385. <listitem>
  386. <para>
  387. The last part of your classname split by underscore is used
  388. for the provider name, e.g. "My_Provider_Hello" leads to your
  389. provider being accessible by the name "hello".
  390. </para>
  391. </listitem>
  392. <listitem>
  393. <para>
  394. If your provider has a method <methodname>getName()</methodname>
  395. it will be used instead of the previous method to determine
  396. the name.
  397. </para>
  398. </listitem>
  399. <listitem>
  400. <para>
  401. If your provider has "Provider" as prefix, e.g. it is called
  402. <classname>My_HelloProvider</classname> it will be stripped
  403. from the name so that the provider will be called "hello".
  404. </para>
  405. </listitem>
  406. </itemizedlist>
  407. <note>
  408. <para>The IncludePathLoader does not follow symlinks, that means
  409. you cannot link provider functionality into your include paths,
  410. they have to be physically present in the include paths.</para>
  411. </note>
  412. <example
  413. id="zend.tool.extending.zend-tool-framework.providers-and-manifests.loading.example">
  414. <title>Exposing Your Providers with a Manifest</title>
  415. <para>
  416. You can expose your providers to <classname>Zend_Tool</classname> by
  417. offering a manifest with a special filename ending with "Manifest.php".
  418. A Provider Manifest is an implementation of the
  419. <interface>Zend_Tool_Framework_Manifest_ProviderManifestable</interface>
  420. and requires the <methodname>getProviders()</methodname> method to return
  421. an array of instantiated providers. In anticipation of our first
  422. own provider <classname>My_Component_HelloProvider</classname>
  423. we will create the following manifest:
  424. </para>
  425. <programlisting language="php"><![CDATA[
  426. class My_Component_Manifest
  427. implements Zend_Tool_Framework_Manifest_ProviderManifestable
  428. {
  429. public function getProviders()
  430. {
  431. return array(
  432. new My_Component_HelloProvider()
  433. );
  434. }
  435. }
  436. ]]></programlisting>
  437. </example>
  438. </sect4>
  439. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.basic">
  440. <title>Basic Instructions for Creating Providers</title>
  441. <para>
  442. As an example, if a developer wants to add the capability of showing
  443. the version of a datafile that his 3rd party component is working
  444. from, there is only one class the developer would need to implement.
  445. Assuming the component is called <classname>My_Component</classname>, he would
  446. create a class named <classname>My_Component_HelloProvider</classname> in a
  447. file named <filename>HelloProvider.php</filename> somewhere on the
  448. <property>include_path</property>. This class would implement
  449. <classname>Zend_Tool_Framework_Provider_Interface</classname>, and the body of
  450. this file would only have to look like the following:
  451. </para>
  452. <programlisting language="php"><![CDATA[
  453. class My_Component_HelloProvider
  454. implements Zend_Tool_Framework_Provider_Interface
  455. {
  456. public function say()
  457. {
  458. echo 'Hello from my provider!';
  459. }
  460. }
  461. ]]></programlisting>
  462. <para>
  463. Given that code above, and assuming the developer wishes to access
  464. this functionality through the console client, the call would look
  465. like this:
  466. </para>
  467. <programlisting language="sh"><![CDATA[
  468. % zf say hello
  469. Hello from my provider!
  470. ]]></programlisting>
  471. </sect4>
  472. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.response">
  473. <title>The response object</title>
  474. <para>
  475. As discussed in the architecture section <classname>Zend_Tool</classname> allows
  476. to hook different clients for using your <classname>Zend_Tool</classname>
  477. providers. To keep compliant with different clients you should use the response
  478. object to return messages from your providers instead of using
  479. <methodname>echo()</methodname> or a similiar output mechanism. Rewritting our
  480. hello provider with this knowledge it looks like:
  481. </para>
  482. <programlisting language="php"><![CDATA[
  483. class My_Component_HelloProvider
  484. extends Zend_Tool_Framework_Provider_Abstract
  485. {
  486. public function say()
  487. {
  488. $this->_registry
  489. ->getResponse()
  490. ->appendContent("Hello from my provider!");
  491. }
  492. }
  493. ]]></programlisting>
  494. <para>
  495. As you can see one has to extend the
  496. <classname>Zend_Tool_Framework_Provider_Abstract</classname> to gain access to
  497. the Registry which holds the
  498. <classname>Zend_Tool_Framework_Client_Response</classname> instance.
  499. </para>
  500. </sect4>
  501. <sect4 id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced">
  502. <title>Advanced Development Information</title>
  503. <sect5
  504. id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.variables">
  505. <title>Passing Variables to a Provider</title>
  506. <para>
  507. The above "Hello World" example is great for simple commands, but
  508. what about something more advanced? As your scripting and tooling
  509. needs grow, you might find that you need the ability to accept
  510. variables. Much like function signatures have parameters, your
  511. tooling requests can also accept parameters.
  512. </para>
  513. <para>
  514. Just as each tooling request can be isolated to a method within a
  515. class, the parameters of a tooling request can also be isolated in a
  516. very well known place. Parameters of the action methods of a
  517. provider can include the same parameters you want your client to
  518. utilize when calling that provider and action combination. For
  519. example, if you wanted to accept a name in the above example, you
  520. would probably do this in OO code:
  521. </para>
  522. <programlisting language="php"><![CDATA[
  523. class My_Component_HelloProvider
  524. implements Zend_Tool_Framework_Provider_Interface
  525. {
  526. public function say($name = 'Ralph')
  527. {
  528. echo 'Hello' . $name . ', from my provider!';
  529. }
  530. }
  531. ]]></programlisting>
  532. <para>
  533. The above example can then be called via the command line
  534. <command>zf say hello Joe</command>. "Joe" will be supplied to the provider
  535. as a parameter of the method call. Also note, as you see that the
  536. parameter is optional, that means it is also optional on the command
  537. line, so that <command>zf say hello</command> will still work, and default
  538. to the name "Ralph".
  539. </para>
  540. </sect5>
  541. <sect5
  542. id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.prompt">
  543. <title>Prompt the User for Input</title>
  544. <para>
  545. There are cases when the workflow of your provider requires
  546. to prompt the user for input. This can be done by requesting
  547. the client to ask for more the required input by calling:
  548. </para>
  549. <programlisting language="php"><![CDATA[
  550. class My_Component_HelloProvider
  551. extends Zend_Tool_Framework_Provider_Abstract
  552. {
  553. public function say($name = 'Ralph')
  554. {
  555. $nameResponse = $this->_registry
  556. ->getClient()
  557. ->promptInteractiveInput("Whats your name?");
  558. $name = $nameResponse->getContent();
  559. echo 'Hello' . $name . ', from my provider!';
  560. }
  561. }
  562. ]]></programlisting>
  563. <para>
  564. This command throws an exception if the current client is not
  565. able to handle interactive requests. In case of the default Console Client
  566. however you will be asked to enter the name.
  567. </para>
  568. </sect5>
  569. <sect5
  570. id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.pretendable">
  571. <title>Pretending to execute a Provider Action</title>
  572. <para>
  573. Another interesting feature you might wish to implement is
  574. <emphasis>pretendability</emphasis>. Pretendabilty is the ability
  575. for your provider to "pretend" as if it is doing the requested
  576. action and provider combination and give the user as much
  577. information about what it <emphasis>would</emphasis> do without
  578. actually doing it. This might be an important notion when doing
  579. heavy database or filesystem modifications that the user might not
  580. otherwise want to do.
  581. </para>
  582. <para>
  583. Pretendability is easy to implement. There are two parts to this
  584. feature: 1) marking the provider as having the ability to "pretend",
  585. and 2) checking the request to ensure the current request was indeed
  586. asked to be "pretended". This feature is demonstrated in the code
  587. sample below.
  588. </para>
  589. <programlisting language="php"><![CDATA[
  590. class My_Component_HelloProvider
  591. extends Zend_Tool_Framework_Provider_Abstract
  592. implements Zend_Tool_Framework_Provider_Pretendable
  593. {
  594. public function say($name = 'Ralph')
  595. {
  596. if ($this->_registry->getRequest()->isPretend()) {
  597. echo 'I would say hello to ' . $name . '.';
  598. } else {
  599. echo 'Hello' . $name . ', from my provider!';
  600. }
  601. }
  602. }
  603. ]]></programlisting>
  604. <para>
  605. To run the provider in pretend mode just call:
  606. </para>
  607. <programlisting language="sh"><![CDATA[
  608. % zf --pretend say hello Ralph
  609. I would say hello Ralph.
  610. ]]></programlisting>
  611. </sect5>
  612. <sect5
  613. id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.verbosedebug">
  614. <title>Verbose and Debug modes</title>
  615. <para>
  616. You can also run your provider actions in "verbose" or "debug" modes.
  617. The semantics in regard to this actions have to be implemented by you
  618. in the context of your provider. You can access debug or verbose modes
  619. with:
  620. </para>
  621. <programlisting language="php"><![CDATA[
  622. class My_Component_HelloProvider
  623. implements Zend_Tool_Framework_Provider_Interface
  624. {
  625. public function say($name = 'Ralph')
  626. {
  627. if($this->_registry->getRequest()->isVerbose()) {
  628. echo "Hello::say has been called\n";
  629. }
  630. if($this->_registry->getRequest()->isDebug()) {
  631. syslog(LOG_INFO, "Hello::say has been called\n");
  632. }
  633. }
  634. }
  635. ]]></programlisting>
  636. </sect5>
  637. <sect5
  638. id="zend.tool.extending.zend-tool-framework.providers-and-manifests.advanced.configstorage">
  639. <title>Accessing User Config and Storage</title>
  640. <para>
  641. Using the Enviroment variable <property>ZF_CONFIG_FILE</property> or the
  642. .zf.ini in your home directory you can inject configuration parameters into
  643. any <classname>Zend_Tool</classname> provider. Access to this configuration
  644. is available via the registry that is passed to your provider if you extend
  645. <classname>Zend_Tool_Framework_Provider_Abstract</classname>.
  646. </para>
  647. <programlisting language="php"><![CDATA[
  648. class My_Component_HelloProvider
  649. extends Zend_Tool_Framework_Provider_Abstract
  650. {
  651. public function say()
  652. {
  653. $username = $this->_registry->getConfig()->username;
  654. if(!empty($username)) {
  655. echo "Hello $username!";
  656. } else {
  657. echo "Hello!";
  658. }
  659. }
  660. }
  661. ]]></programlisting>
  662. <para>
  663. The returned configuration is of the type
  664. <classname>Zend_Tool_Framework_Client_Config</classname> but internally the
  665. <methodname>__get()</methodname> and <methodname>__set()</methodname> magic
  666. methods proxy to a <classname>Zend_Config</classname> of the given
  667. configuration type.
  668. </para>
  669. <para>
  670. The storage allows to save arbitrary data for later reference. This can be
  671. useful for batch processing tasks or for re-runs of your tasks. You can
  672. access the storage in a similar way like the configuration:
  673. </para>
  674. <programlisting language="php"><![CDATA[
  675. class My_Component_HelloProvider
  676. extends Zend_Tool_Framework_Provider_Abstract
  677. {
  678. public function say()
  679. {
  680. $aValue = $this->_registry->getStorage()->get("myUsername");
  681. echo "Hello $aValue!";
  682. }
  683. }
  684. ]]></programlisting>
  685. <para>
  686. The <acronym>API</acronym> of the storage is very simple:
  687. </para>
  688. <programlisting language="php"><![CDATA[
  689. class Zend_Tool_Framework_Client_Storage
  690. {
  691. public function setAdapter($adapter);
  692. public function isEnabled();
  693. public function put($name, $value);
  694. public function get($name, $defaultValue=null);
  695. public function has($name);
  696. public function remove($name);
  697. public function getStreamUri($name);
  698. }
  699. ]]></programlisting>
  700. <important>
  701. <para>
  702. When designing your providers that are config or storage aware remember
  703. to check if the required user-config or storage keys really exist for a
  704. user. You won't run into fatal errors when none of these are provided
  705. though, since empty ones are created upon request.
  706. </para>
  707. </important>
  708. </sect5>
  709. </sect4>
  710. </sect3>
  711. </sect2>
  712. <sect2 id="zend.tool.extending.zend-tool-project">
  713. <title>Zend_Tool_Project Extensions</title>
  714. <para>
  715. <classname>Zend_Tool_Project</classname> exposes a rich set of functionality and
  716. capabilities that make the task of creating new providers, specficially those targetting
  717. project easier and more manageable.
  718. </para>
  719. <sect3 id="zend.tool.extending.zend-tool-project.architecture">
  720. <title>Overall Architecture</title>
  721. <para>
  722. This same concept applies to Zend Framework projects. In Zend Framework projects,
  723. you have controllers, actions, views, models, databases and so on and so forth. In
  724. terms of <classname>Zend_Tool</classname>, we need a way to track these types of
  725. resources - thus <classname>Zend_Tool_Project</classname>.
  726. </para>
  727. <para>
  728. <classname>Zend_Tool_Project</classname> is capable of tracking project resources
  729. throughout the development of a project. So, for example, if in one command you
  730. created a controller, and in the next command you wish to create an action within
  731. that controller, <classname>Zend_Tool_Project</classname> is gonna have to
  732. <emphasis>know</emphasis> about the controller file you created so that you can (in
  733. the next action), be able to append that action to it. This is what keeps our
  734. projects up to date and <emphasis>stateful</emphasis>.
  735. </para>
  736. <para>
  737. Another important point to understand about projects is that typically, resources
  738. are organized in a hierarchical fashion. With that in mind,
  739. <classname>Zend_Tool_Project</classname> is capable of serializing the current
  740. project into a internal representation that allows it to keep track of not only
  741. <emphasis>what</emphasis> resources are part of a project at any given time, but
  742. also <emphasis>where</emphasis> they are in relation to one another.
  743. </para>
  744. </sect3>
  745. <sect3 id="zend.tool.extending.zend-tool-project.providers">
  746. <title>Creating Providers</title>
  747. <para>
  748. Project specific providers are created in the same fashion as plain framework
  749. providers, with one exception: project providers must extend the
  750. <classname>Zend_Tool_Project_Provider_Abstract</classname>. This class comes with
  751. some significant functionality that helps developers load existing project, obtian
  752. the profile object, and be able to search the profile, then later store any changes
  753. to the current project profile.
  754. </para>
  755. <programlisting language="php"><![CDATA[
  756. class My_Component_HelloProvider
  757. extends Zend_Tool_Project_Provider_Abstract
  758. {
  759. public function say()
  760. {
  761. $profile = $this->_loadExistingProfile();
  762. /* ... do project stuff here */
  763. $this->_storeProfile();
  764. }
  765. }
  766. ]]></programlisting>
  767. </sect3>
  768. <!--
  769. <sect3 id="zend.tool.extending.zend-tool-project.resources-and-contexts">
  770. <title>Creating Resources &amp; Contexts</title>
  771. </sect3>
  772. -->
  773. </sect2>
  774. </sect1>