Zend_Tool_Framework-WritingProviders.xml 17 KB

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