Zend_Tool_Framework-WritingProviders.xml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 18033 -->
  4. <sect1 id="zend.tool.framework.writing-providers">
  5. <title>Zend_Tool_Frameworkを利用してプロバイダを作成する</title>
  6. <para>
  7. 一般的に、プロバイダそれ自体は、
  8. コマンドラインでクライアント(またはそれ以外)をディスパッチすることを求めるいくらかの能力をバンドルするための、
  9. 開発者のためのシェル以外の何物でもありません。
  10. <acronym>MVC</acronym>アプリケーションの中での「コントローラ」と似ています。
  11. </para>
  12. <sect2 id="zend.tool.framework.writing-providers.manifest">
  13. <!-- TODO:to be translated -->
  14. <title>Exposing Your Providers with a Manifest</title>
  15. <para>
  16. Before writing your own providers you need to create a manifest, which
  17. returns instances of your providers to allow Zend Tool to detect
  18. them. A Provider Manifest is an implementation of the
  19. <interface>Zend_Tool_Framework_Manifest_ProviderManifestable</interface>
  20. and requires the <code>getProviders()</code> method to return
  21. an array of instantiated providers. In anticipation of our first
  22. own provider <classname>My_Component_HelloProvider</classname>
  23. we will create the following manifest:
  24. </para>
  25. <programlisting language="php"><![CDATA[
  26. class My_Component_Manifest implements Zend_Tool_Framework_Manifest_ProviderManifestable
  27. {
  28. public function getProviders()
  29. {
  30. return array(
  31. new My_Component_HelloProvider()
  32. );
  33. }
  34. }
  35. ]]>
  36. </programlisting>
  37. <para>
  38. A provider manifest class always has to end with the Term "Manifest" otherwise
  39. Zend_Tool cannot autodetect it while searching through your include path.
  40. </para>
  41. </sect2>
  42. <sect2 id="zend.tool.framework.writing-providers.basic">
  43. <title>プロバイダを作成するための基本命令</title>
  44. <para>
  45. 例えば、サード・パーティのコンポーネントが働かせる
  46. データファイルのバージョンを示す能力を開発者が加えたければ、
  47. 開発者が実装する必要があるクラスが1つだけあります。
  48. もしコンポーネントが<classname>My_Component</classname>と呼ばれるなら、
  49. <property>include_path</property>上のどこかに<filename>HelloProvider.php</filename>という名前のファイルで
  50. <classname>My_Component_HelloProvider</classname>という名のクラスを作成するでしょう。
  51. このクラスは<classname>Zend_Tool_Framework_Provider_Interface</classname>を実装します、
  52. そして、このファイルの本体は以下のように見えなければならないだけです:
  53. </para>
  54. <programlisting language="php"><![CDATA[
  55. class My_Component_HelloProvider
  56. implements Zend_Tool_Framework_Provider_Interface
  57. {
  58. public function say()
  59. {
  60. echo 'Hello from my provider!';
  61. }
  62. }
  63. ]]></programlisting>
  64. <para>
  65. 上記のコードが与えられて、
  66. コンソール・クライアントを通じて開発者がこの機能にアクセスしたけれ、
  67. 呼び出しはこのように見えるでしょう:
  68. </para>
  69. <programlisting language="sh"><![CDATA[
  70. % zf say hello
  71. Hello from my provider!
  72. ]]></programlisting>
  73. </sect2>
  74. <sect2 id="zend.tool.framework.writing-providers.response">
  75. <title>レスポンスオブジェクト</title>
  76. <!-- TODO: to be translated -->
  77. <para>
  78. As discussed in the architecture section Zend Tool allows to hook different clients for
  79. using your Zend Tool providers. To keep compliant with different clients you should
  80. use the response object to return messages from your providers instead of using <code>echo</code>
  81. or a similiar output mechanism. Rewritting our hello provider with this knowledge it looks like:
  82. </para>
  83. <programlisting language="php"><![CDATA[
  84. class My_Component_HelloProvider
  85. extends Zend_Tool_Framework_Provider_Abstract
  86. {
  87. public function say()
  88. {
  89. $this->_registry->getResponse()->appendContent("Hello from my provider!");
  90. }
  91. }
  92. ]]>
  93. </programlisting>
  94. <!-- TODO: to be translated -->
  95. <para>
  96. As you can see one has to extend the <classname>Zend_Tool_Framework_Provider_Abstract</classname>
  97. to gain access to the Registry which holds the <classname>Zend_Tool_Framework_Client_Response</classname>
  98. instance.
  99. </para>
  100. </sect2>
  101. <sect2 id="zend.tool.framework.writing-providers.advanced">
  102. <title>先進の開発情報</title>
  103. <sect3 id="zend.tool.framework.writing-providers.advanced.variables">
  104. <title>プロバイダに変数を渡す</title>
  105. <para>
  106. 上記の例の "Hello World" は、単純なコマンドとして有名です、
  107. しかし、より進んだ何かについてはどうでしょうか?
  108. スクリプトを書くこととツーリングの必要性が高まるにつれ、
  109. 変数を扱う能力を必要とすると気付くかもしれません。
  110. だいたいのファンクション・シグニチャにはパラメータがあるように、
  111. ツーリング・リクエストはパラメータを受け入れることもできます。
  112. </para>
  113. <para>
  114. 各々のツーリング・リクエストがクラス内でメソッドに分離されることができると、
  115. ツーリング・リクエストのパラメータはきわめて周知の立場で分離されることもできます。
  116. プロバイダのアクション・メソッドのパラメータは、
  117. クライアントがそのプロバイダとアクションの組合せを呼ぶとき、
  118. 利用することを望む同じパラメータを含むことができます。
  119. たとえば、あなたが上記の例で名前を扱いたいならば、
  120. あなたは多分オブジェクト指向コードでこうするでしょう:
  121. </para>
  122. <programlisting language="php"><![CDATA[
  123. class My_Component_HelloProvider
  124. implements Zend_Tool_Framework_Provider_Interface
  125. {
  126. public function say($name = 'Ralph')
  127. {
  128. echo 'Hello' . $name . ', from my provider!';
  129. }
  130. }
  131. ]]></programlisting>
  132. <para>
  133. それから上記の例は、コマンドライン<command>zf say hello Joe</command>によって呼ぶことができます。
  134. "Joe" は、メソッド呼び出しのパラメータとして、プロバイダに供給されます。
  135. また注意すべきこととして、
  136. パラメータが任意だとあなたがわかるように、
  137. <command>zf say hello</command>がさらに機能して、名前 "Ralph" にデフォルト設定するように、
  138. コマンドライン上で選択できることを意味します。
  139. </para>
  140. </sect3>
  141. <sect3 id="zend.tool.framework.writing-providers.advanced.prompt">
  142. <!-- TODO: to be translated -->
  143. <title>Prompt the User for Input</title>
  144. <para>
  145. There are cases when the workflow of your provider requires
  146. to prompt the user for input. This can be done by requesting
  147. the client to ask for more the required input by calling:
  148. </para>
  149. <programlisting language="php"><![CDATA[
  150. class My_Component_HelloProvider
  151. extends Zend_Tool_Framework_Provider_Abstract
  152. {
  153. public function say($name = 'Ralph')
  154. {
  155. $nameResponse = $this->_registry->getClient()->promptInteractiveInput("Whats your name?");
  156. $name = $name->getContent();
  157. echo 'Hello' . $name . ', from my provider!';
  158. }
  159. }
  160. ]]>
  161. </programlisting>
  162. <!-- TODO: to be translated -->
  163. <para>
  164. This command throws an exception if the current client is not
  165. able to handle interactive requests. In case of the default Console Client
  166. however you will be asked to enter the name.
  167. </para>
  168. </sect3>
  169. <sect3 id="zend.tool.framework.writing-providers.advanced.pretendable">
  170. <title>プロバイダ・アクションを実行するための擬態</title>
  171. <para>
  172. あなたが実装したいかもしれないもう一つの面白い特徴は、<emphasis>擬態性</emphasis>です。
  173. 擬態性は、
  174. まるでそれがリクエストされたアクションとプロバイダの組み合わせを実行しているように擬態して、
  175. 実際にそれを実行せずに、それが実行するで<emphasis>あろう</emphasis>ことについて沢山の情報をユーザーに与えることを
  176. プロバイダでできるようにします。
  177. これ以外の場合にはユーザーが実行したくないかもしれない重いデータベースや、
  178. ファイルシステム修正をするときに重要な小道具であるかもしれません。
  179. </para>
  180. <para>
  181. 擬態性は簡単に実装できます。
  182. このフィーチャーには2つの要素があります:
  183. 1) プロバイダが「擬態する」能力を持つことを示します。
  184. 2) 現在のリクエストが本当に、
  185. 「擬態する」よう要求されたことを確実にするために、リクエストをチェックします。
  186. このフィーチャーは下記のコードサンプルで示されます。
  187. </para>
  188. <programlisting language="php"><![CDATA[
  189. class My_Component_HelloProvider
  190. extends Zend_Tool_Framework_Provider_Abstract
  191. implements Zend_Tool_Framework_Provider_Pretendable
  192. {
  193. public function say($name = 'Ralph')
  194. {
  195. if ($this->_registry->getRequest()->isPretend()) {
  196. echo 'I would say hello to ' . $name . '.';
  197. } else {
  198. echo 'Hello' . $name . ', from my provider!';
  199. }
  200. }
  201. }
  202. ]]></programlisting>
  203. <para>
  204. 擬態モードでプロバイダを実行してちょっと呼び出し
  205. </para>
  206. <programlisting language="sh"><![CDATA[
  207. % zf --pretend say hello Ralph
  208. I would say hello Ralph.
  209. ]]></programlisting>
  210. </sect3>
  211. <sect3 id="zend.tool.framework.writing-providers.advanced.verbosedebug">
  212. <title>冗長及びデバッグモード</title>
  213. <!-- TODO: to be translated -->
  214. <para>
  215. You can also run your provider actions in "verbose" or "debug" modes.
  216. The semantics in regard to this actions have to be implemented by you
  217. in the context of your provider. You can access debug or verbose modes
  218. with:
  219. </para>
  220. <programlisting language="php"><![CDATA[
  221. class My_Component_HelloProvider
  222. implements Zend_Tool_Framework_Provider_Interface
  223. {
  224. public function say($name = 'Ralph')
  225. {
  226. if($this->_registry->getRequest()->isVerbose()) {
  227. echo "Hello::say has been called\n";
  228. }
  229. if($this->_registry->getRequest()->isDebug()) {
  230. syslog(LOG_INFO, "Hello::say has been called\n");
  231. }
  232. }
  233. }
  234. ]]>
  235. </programlisting>
  236. </sect3>
  237. <sect3 id="zend.tool.framework.writing-providers.advanced.configstorage">
  238. <title>ユーザーの構成及びストレージにアクセス</title>
  239. <!-- TODO: to be translated -->
  240. <para>
  241. Using the Enviroment variable <property>ZF_CONFIG_FILE</property> or the
  242. .zf.ini in your home directory you can inject configuration parameters into
  243. any Zend Tool provider. Access to this configuration is available via the
  244. registry that is passed to your provider if you extend
  245. <classname>Zend_Tool_Framework_Provider_Abstract</classname>.
  246. </para>
  247. <programlisting language="php"><![CDATA[
  248. class My_Component_HelloProvider
  249. extends Zend_Tool_Framework_Provider_Abstract
  250. {
  251. public function say()
  252. {
  253. $username = $this->_registry->getConfig()->username;
  254. if(!empty($username)) {
  255. echo "Hello $username!";
  256. } else {
  257. echo "Hello!";
  258. }
  259. }
  260. }
  261. ]]>
  262. </programlisting>
  263. <!-- TODO: to be translated -->
  264. <para>
  265. The returned configuration is of the type <classname>Zend_Tool_Framework_Client_Config</classname>
  266. but internally the <code>__get</code> and <code>__set</code> magic methods proxy to a
  267. <classname>Zend_Config</classname> of the given configuration type.
  268. </para>
  269. <para>
  270. The storage allows to save arbitrary data for later reference. This can be useful for batch
  271. processing tasks or for re-runs of your tasks. You can access the storage in a similar way
  272. like the configuration:
  273. </para>
  274. <programlisting language="php"><![CDATA[
  275. class My_Component_HelloProvider
  276. extends Zend_Tool_Framework_Provider_Abstract
  277. {
  278. public function say()
  279. {
  280. $aValue = $this->_registry->getStorage()->get("myUsername");
  281. echo "Hello $aValue!";
  282. }
  283. }
  284. ]]>
  285. </programlisting>
  286. <para>
  287. ストレージ API はとても簡単です。
  288. </para>
  289. <programlisting language="php"><![CDATA[
  290. class Zend_Tool_Framework_Client_Storage
  291. {
  292. public function setAdapter($adapter);
  293. public function isEnabled();
  294. public function put($name, $value);
  295. public function get($name, $defaultValue=null);
  296. public function has($name);
  297. public function remove($name);
  298. public function getStreamUri($name);
  299. }
  300. ]]>
  301. </programlisting>
  302. <!-- TODO: to be translated -->
  303. <important>
  304. <para>
  305. When designing your providers that are config or storage aware remember to
  306. check if the required user-config or storage keys really exist for a user.
  307. You won't run into fatal errors when none of these are provided though,
  308. since empty ones are created upon request.
  309. </para>
  310. </important>
  311. </sect3>
  312. </sect2>
  313. </sect1>