Zend_Tool_Framework-WritingProviders.xml 17 KB

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