Zend_Tool-Extending.xml 40 KB

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