Zend_Tool_Framework-WritingProviders.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.tool.framework.writing-providers">
  4. <title>Creating Providers to use with Zend_Tool_Framework</title>
  5. <para>
  6. In general, a provider, on its own, is nothing more than the shell for a
  7. developer to bundle up some capabilities they wish to dispatch with the
  8. command line (or other) clients. It is an analogue to what a
  9. "controller" is inside of your MVC application.
  10. </para>
  11. <sect2 id="zend.tool.framework.writing-providers.basic">
  12. <title>Basic Instructions for Creating Providers</title>
  13. <para>
  14. As an example, if a developer wants to add the capability of showing
  15. the version of a datafile that his 3rd party component is working
  16. from, there is only one class the developer would need to implement.
  17. Assuming the component is called <code>My_Component</code>, he would
  18. create a class named <code>My_Component_HelloProvider</code> in a
  19. file named <code>HelloProvider.php</code> somewhere on the
  20. <code>include_path</code>. This class would implement
  21. <classname>Zend_Tool_Framework_Provider_Interface</classname>, and the body of
  22. this file would only have to look like the following:
  23. </para>
  24. <programlisting language="php"><![CDATA[
  25. class My_Component_HelloProvider
  26. implements Zend_Tool_Framework_Provider_Interface
  27. {
  28. public function say()
  29. {
  30. echo 'Hello from my provider!';
  31. }
  32. }
  33. ]]></programlisting>
  34. <para>
  35. Given that code above, and assuming the developer wishes to access
  36. this functionality through the console client, the call would look
  37. like this:
  38. </para>
  39. <programlisting language="sh"><![CDATA[
  40. % zf say hello
  41. Hello from my provider!
  42. ]]></programlisting>
  43. </sect2>
  44. <sect2 id="zend.tool.framework.writing-providers.advanced">
  45. <title>Advanced Development Information</title>
  46. <para>
  47. The above "Hello World" example is great for simple commands, but
  48. what about something more advanced? As your scripting and tooling
  49. needs grow, you might find that you need the ability to accept
  50. variables. Much like function signatures have parameters, your
  51. tooling requests can also accept parameters.
  52. </para>
  53. <para>
  54. Just as each tooling request can be isolated to a method within a
  55. class, the parameters of a tooling request can also be isolated in a
  56. very well known place. Parameters of the action methods of a
  57. provider can include the same parameters you want your client to
  58. utilize when calling that provider and action combination. For
  59. example, if you wanted to accept a name in the above example, you
  60. would probably do this in OO code:
  61. </para>
  62. <programlisting language="php"><![CDATA[
  63. class My_Component_HelloProvider
  64. implements Zend_Tool_Framework_Provider_Interface
  65. {
  66. public function say($name = 'Ralph')
  67. {
  68. echo 'Hello' . $name . ', from my provider!';
  69. }
  70. }
  71. ]]></programlisting>
  72. <para>
  73. The above example can then be called via the command line <code>zf
  74. say hello Joe</code>. "Joe" will be supplied to the provider as
  75. a parameter of the method call. Also note, as you see that the
  76. parameter is optional, that means it is also optional on the command
  77. line, so that <code>zf say hello</code> will still work, and default
  78. to the name "Ralph".
  79. </para>
  80. <para>
  81. Another interesting feature you might wish to implement is
  82. <emphasis>pretendability</emphasis>. Pretendabilty is the ability
  83. for your provider to "pretend" as if it is doing the requested
  84. action and provider combination and give the user as much
  85. information about what it <emphasis>would</emphasis> do without
  86. actually doing it. This might be an important notion when doing
  87. heavy database or filesystem modifications that the user might not
  88. otherwise want to do.
  89. </para>
  90. <para>
  91. Pretendability is easy to implement. There are two parts to this
  92. feature: 1) marking the provider as having the ability to "pretend",
  93. and 2) checking the request to ensure the current request was indeed
  94. asked to be "pretended". This feature is demonstrated in the code
  95. sample below.
  96. </para>
  97. <programlisting language="php"><![CDATA[
  98. class My_Component_HelloProvider
  99. extends Zend_Tool_Framework_Provider_Abstract
  100. implements Zend_Tool_Framework_Provider_Pretendable
  101. {
  102. public function say($name = 'Ralph')
  103. {
  104. if ($this->_registry->getRequest()->isPretend()) {
  105. echo 'I would say hello to ' . $name . '.';
  106. } else {
  107. echo 'Hello' . $name . ', from my provider!';
  108. }
  109. }
  110. }
  111. ]]></programlisting>
  112. </sect2>
  113. </sect1>