Zend_Tool_Framework-WritingProviders.xml 5.1 KB

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