Zend_XmlRpc_Server.xml 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.xmlrpc.server">
  5. <title>Zend_XmlRpc_Server(日本語)</title>
  6. <sect2 id="zend.xmlrpc.server.introduction">
  7. <title>導入</title>
  8. <para>
  9. <classname>Zend_XmlRpc_Server</classname> は、完全な機能を有した <acronym>XML-RPC</acronym> サーバです。
  10. <ulink url="http://www.xmlrpc.com/spec">
  11. www.xmlrpc.com で提示されている仕様</ulink> に準拠しています。
  12. さらに <command>system.multicall()</command> メソッドを実装しており、
  13. リクエストをまとめる (boxcarring of requests) ことができます。
  14. </para>
  15. </sect2>
  16. <sect2 id="zend.xmlrpc.server.usage">
  17. <title>基本的な使用法</title>
  18. <para>
  19. もっとも基本的な使用例は次のとおりです。
  20. </para>
  21. <programlisting language="php"><![CDATA[
  22. $server = new Zend_XmlRpc_Server();
  23. $server->setClass('My_Service_Class');
  24. echo $server->handle();
  25. ]]></programlisting>
  26. </sect2>
  27. <sect2 id="zend.xmlrpc.server.structure">
  28. <title>サーバの構造</title>
  29. <para>
  30. <classname>Zend_XmlRpc_Server</classname> はさまざまなコンポーネントで構成されています。
  31. サーバ自身からリクエスト、レスポンス、fault
  32. オブジェクトなど広範囲に広がっています。
  33. </para>
  34. <para>
  35. <classname>Zend_XmlRpc_Server</classname> を起動するには、
  36. まずサーバにひとつ以上のクラスか関数をアタッチする必要があります。
  37. アタッチするには <methodname>setClass()</methodname> メソッドおよび
  38. <methodname>addFunction()</methodname> メソッドを使用します。
  39. </para>
  40. <para>
  41. 起動させたら、次に <classname>Zend_XmlRpc_Request</classname> オブジェクトを
  42. <methodname>Zend_XmlRpc_Server::handle()</methodname> に渡します。
  43. もし渡さなかった場合は、<classname>Zend_XmlRpc_Request_Http</classname>
  44. のインスタンスを作成して <filename>php://input</filename>
  45. からの入力を受け取ります。
  46. </para>
  47. <para>
  48. <methodname>Zend_XmlRpc_Server::handle()</methodname> は、
  49. リクエストメソッドに応じて適切なハンドラに処理を振り分けます。
  50. そして、
  51. <classname>Zend_XmlRpc_Response</classname> を継承したオブジェクトか
  52. <classname>Zend_XmlRpc_Server_Fault</classname> オブジェクトを返します。
  53. これらのオブジェクトはどちらも <methodname>__toString()</methodname>
  54. メソッドを実装しており、妥当な <acronym>XML-RPC</acronym> <acronym>XML</acronym> レスポンスを直接出力できます。
  55. </para>
  56. </sect2>
  57. <sect2 id="zend.xmlrpc.server.anatomy">
  58. <title>Web サービスの解剖図</title>
  59. <sect3 id="zend.xmlrpc.server.anatomy.general">
  60. <title>一般的な考慮点</title>
  61. <!-- TODO : to be translation -->
  62. <para>
  63. For maximum performance it is recommended to use a simple
  64. bootstrap file for the server component. Using
  65. <classname>Zend_XmlRpc_Server</classname> inside a
  66. <link linkend="zend.controller"><classname>Zend_Controller</classname></link>
  67. is strongly discouraged to avoid the overhead.
  68. </para>
  69. <para>
  70. Services change over time and while webservices are generally
  71. less change intense as code-native <acronym>APIs</acronym>, it
  72. is recommended to version your service. Do so to lay grounds to
  73. provide compatibility for clients using older versions of your
  74. service and manage your service lifecycle including deprecation
  75. timeframes.To do so just include a version number into your
  76. <acronym>URI</acronym>. It is also recommended to include the
  77. remote protocol name in the <acronym>URI</acronym> to allow easy
  78. integration of upcoming remoting technologies.
  79. http://myservice.ws/<emphasis>1.0/XMLRPC/</emphasis>.
  80. </para>
  81. </sect3>
  82. <sect3 id="zend.xmlrpc.server.anatomy.expose">
  83. <title>What to expose?</title>
  84. <para>
  85. Most of the time it is not sensible to expose business objects
  86. directly. Business objects are usually small and under heavy
  87. change, because change is cheap in this layer of your
  88. application. Once deployed and adopted, web services are hard to
  89. change. Another concern is <acronym>I/O</acronym> and latency:
  90. the best webservice calls are those not happening. Therefore
  91. service calls need to be more coarse-grained than usual business
  92. logic is. Often an additional layer in front of your business
  93. objects makes sense. This layer is sometimes referred to as <ulink
  94. url="http://martinfowler.com/eaaCatalog/remoteFacade.html">Remote
  95. Facade</ulink>.
  96. Such a service layer adds a coarse grained interface on top of
  97. your business logic and groups verbose operations into smaller
  98. ones.
  99. </para>
  100. </sect3>
  101. </sect2>
  102. <sect2 id="zend.xmlrpc.server.conventions">
  103. <title>規約</title>
  104. <para>
  105. <classname>Zend_XmlRpc_Server</classname> では、開発者が関数やクラスメソッドを
  106. <acronym>XML-RPC</acronym> メソッドとしてアタッチできるようになっています。
  107. アタッチされるメソッドの情報は <classname>Zend_Server_Reflection</classname>
  108. を使用して取得し、関数やメソッドのコメントブロックから
  109. メソッドのヘルプ文とシグネチャを取得します。
  110. </para>
  111. <para>
  112. <acronym>XML-RPC</acronym> の型は必ずしも <acronym>PHP</acronym> の型と一対一対応しているわけではありません。
  113. しかし、@param や @return の行をもとに、できるだけ適切な型を推測しようとします。
  114. <acronym>XML-RPC</acronym> の型の中には、直接対応する <acronym>PHP</acronym> の型がないものもありますが、
  115. その場合は PHPDoc の中で <acronym>XML-RPC</acronym> の型のヒントを指定します。
  116. たとえば次のような型が該当します。
  117. </para>
  118. <itemizedlist>
  119. <listitem>
  120. <para>
  121. <emphasis><property>dateTime.iso8601</property></emphasis> ...
  122. '<command>YYYYMMDDTHH:mm:ss</command>' 形式の文字列
  123. </para>
  124. </listitem>
  125. <listitem><para><emphasis>base64</emphasis> ... base64 エンコードされたデータ</para></listitem>
  126. <listitem><para><emphasis>struct</emphasis> ... 任意の連想配列</para></listitem>
  127. </itemizedlist>
  128. <para>
  129. ヒントを指定するには、次のようにします。
  130. </para>
  131. <programlisting language="php"><![CDATA[
  132. /**
  133. * これはサンプル関数です
  134. *
  135. * @param base64 $val1 Base64 エンコードされたデータ
  136. * @param dateTime.iso8601 $val2 ISO 日付
  137. * @param struct $val3 連想配列
  138. * @return struct
  139. */
  140. function myFunc($val1, $val2, $val3)
  141. {
  142. }
  143. ]]></programlisting>
  144. <para>
  145. PhpDocumentor はパラメータや返り値の型を検証しません。
  146. そのため、これが <acronym>API</acronym> ドキュメントに影響を及ぼすことはありません。
  147. しかし、このヒントは必須です。メソッドがコールされた際に、
  148. この情報をもとにサーバで検証を行うからです。
  149. </para>
  150. <para>
  151. パラメータや返り値で複数の型を指定してもかまいません。
  152. <acronym>XML-RPC</acronym> の仕様では、system.methodSignature は
  153. すべてのメソッドシグネチャ
  154. (すなわちパラメータと返り値の組み合わせ) の配列を返すことになっています。
  155. 複数指定する方法は、通常の PhpDocumentor の場合と同様に
  156. '|' 演算子を使用します。
  157. </para>
  158. <programlisting language="php"><![CDATA[
  159. /**
  160. * This is a sample function
  161. *
  162. * @param string|base64 $val1 文字列あるいは base64 エンコードされたデータ
  163. * @param string|dateTime.iso8601 $val2 文字列あるいは ISO 日付
  164. * @param array|struct $val3 Normal 数値添字配列あるいは連想配列
  165. * @return boolean|struct
  166. */
  167. function myFunc($val1, $val2, $val3)
  168. {
  169. }
  170. ]]></programlisting>
  171. <note>
  172. <para>
  173. 複数のシグネチャを定義すると、それを利用する開発者を混乱させてしまいます。
  174. 物事を簡単にするために、 <acronym>XML-RPC</acronym> サービスのメソッドは単純なシグネチャだけを持つべきでしょう。
  175. </para>
  176. </note>
  177. </sect2>
  178. <sect2 id="zend.xmlrpc.server.namespaces">
  179. <title>名前空間の活用</title>
  180. <para>
  181. <acronym>XML-RPC</acronym> には名前空間の概念があります。基本的に、これは
  182. 複数の <acronym>XML-RPC</acronym> メソッドをドット区切りの名前空間でまとめるものです。
  183. これにより、さまざまなクラスで提供されるメソッド名の衝突を避けることができます。
  184. 例として、<acronym>XML-RPC</acronym> サーバは 'system'
  185. 名前空間でこれらのメソッドを提供することが期待されています。
  186. </para>
  187. <itemizedlist>
  188. <listitem><para>system.listMethods</para></listitem>
  189. <listitem><para>system.methodHelp</para></listitem>
  190. <listitem><para>system.methodSignature</para></listitem>
  191. </itemizedlist>
  192. <para>
  193. 内部的には、これらは
  194. <classname>Zend_XmlRpc_Server</classname> の同名のメソッドに対応しています。
  195. </para>
  196. <para>
  197. 自分が提供するメソッドに名前空間を追加したい場合は、
  198. 関数やクラスをアタッチする際のメソッドで名前空間を指定します。
  199. </para>
  200. <programlisting language="php"><![CDATA[
  201. // My_Service_Class のパブリックメソッドは、すべて
  202. // myservice.メソッド名 でアクセスできるようになります
  203. $server->setClass('My_Service_Class', 'myservice');
  204. // 関数 'somefunc' は funcs.somefunc としてアクセスするようにします
  205. $server->addFunction('somefunc', 'funcs');
  206. ]]></programlisting>
  207. </sect2>
  208. <sect2 id="zend.xmlrpc.server.request">
  209. <title>独自のリクエストオブジェクト</title>
  210. <para>
  211. ほとんどの場合は、
  212. <classname>Zend_XmlRpc_Server</classname> や <classname>Zend_XmlRpc_Request_Http</classname>
  213. に含まれるデフォルトのリクエスト型を使用するでしょう。
  214. しかし、<acronym>XML-RPC</acronym> を <acronym>CLI</acronym> や
  215. <acronym>GUI</acronym> 環境などで動かしたい場合もあるでしょうし、
  216. リクエストの内容をログに記録したい場合もあるでしょう。
  217. そのような場合には、<classname>Zend_XmlRpc_Request</classname>
  218. を継承した独自のリクエストオブジェクトを作成します。
  219. 注意すべき点は、<methodname>getMethod()</methodname> メソッドと <methodname>getParams()</methodname>
  220. メソッドを必ず実装しなければならないということです。
  221. これらは、<acronym>XML-RPC</acronym> サーバがリクエストを処理する際に必要となります。
  222. </para>
  223. </sect2>
  224. <sect2 id="zend.xmlrpc.server.response">
  225. <title>独自のレスポンス</title>
  226. <para>
  227. リクエストオブジェクトと同様、<classname>Zend_XmlRpc_Server</classname>
  228. は独自のレスポンスオブジェクトを返すこともできます。
  229. デフォルトでは <classname>Zend_XmlRpc_Response_Http</classname> オブジェクトが返されます。
  230. これは、<acronym>XML-RPC</acronym> で使用される適切な Content-Type <acronym>HTTP</acronym>
  231. ヘッダを送信します。独自のオブジェクトを使用する場面としては、
  232. レスポンスをログに記録したり、
  233. あるいはレスポンスを標準出力に返したりといったことが考えられます。
  234. </para>
  235. <para>
  236. 独自のレスポンスクラスを使用するには、<methodname>handle()</methodname> をコールする前に
  237. <methodname>Zend_XmlRpc_Server::setResponseClass()</methodname> を使用します。
  238. </para>
  239. </sect2>
  240. <sect2 id="zend.xmlrpc.server.fault">
  241. <title>Fault による例外の処理</title>
  242. <para>
  243. <classname>Zend_XmlRpc_Server</classname> は、配送先のメソッドで発生した例外を捕捉します。
  244. 例外を捕捉した場合は、<acronym>XML-RPC</acronym> の fault レスポンスを生成します。
  245. しかし、デフォルトでは、例外メッセージとコードは fault
  246. レスポンスで用いられません。これは、
  247. あなたのコードを守るための判断によるものです。
  248. たいていの例外は、コードや環境に関する情報を必要以上にさらけ出してしまいます
  249. (わかりやすい例だと、データベースの抽象化レイヤの例外を想像してみてください)。
  250. </para>
  251. <para>
  252. しかし、例外クラスをホワイトリストに登録することで、
  253. fault レスポンス内で例外を使用することもできます。
  254. そうするには、
  255. <methodname>Zend_XmlRpc_Server_Fault::attachFaultException()</methodname>
  256. を使用して例外クラスをホワイトリストに渡します。
  257. </para>
  258. <programlisting language="php"><![CDATA[
  259. Zend_XmlRpc_Server_Fault::attachFaultException('My_Project_Exception');
  260. ]]></programlisting>
  261. <para>
  262. 他のプロジェクトの例外を継承した例外クラスを利用するのなら、
  263. 一連のクラス群を一度にホワイトリストに登録することもできます。
  264. <classname>Zend_XmlRpc_Server_Exceptions</classname> は常にホワイトリストに登録されており、
  265. 固有の内部エラー (メソッドが未定義であるなど) を報告できます。
  266. </para>
  267. <para>
  268. ホワイトリストに登録されていない例外が発生した場合は、
  269. コード '404'、メッセージ 'Unknown error' の falut
  270. レスポンスを生成します。
  271. </para>
  272. </sect2>
  273. <sect2 id="zend.xmlrpc.server.caching">
  274. <title>リクエスト間でのサーバ定義のキャッシュ</title>
  275. <para>
  276. たくさんのクラスを <acronym>XML-RPC</acronym> サーバインスタンスにアタッチすると、
  277. リソースを大量に消費してしまいます。各クラスを調べるために
  278. リフレクション <acronym>API</acronym> を (<classname>Zend_Server_Reflection</classname> 経由で) 使用する必要があり、
  279. 使用できるすべてのメソッドのシグネチャをサーバクラスに提供します。
  280. </para>
  281. <para>
  282. 使用するリソースの量を軽減するために、<classname>Zend_XmlRpc_Server_Cache</classname>
  283. を用いてリクエスト間でサーバ定義をキャッシュできます。
  284. <methodname>__autoload()</methodname> と組み合わせることで、これはパフォーマンスを劇的に向上させます。
  285. </para>
  286. <para>
  287. 使用例は次のようになります。
  288. </para>
  289. <programlisting language="php"><![CDATA[
  290. function __autoload($class)
  291. {
  292. Zend_Loader::loadClass($class);
  293. }
  294. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  295. $server = new Zend_XmlRpc_Server();
  296. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  297. require_once 'My/Services/Glue.php';
  298. require_once 'My/Services/Paste.php';
  299. require_once 'My/Services/Tape.php';
  300. $server->setClass('My_Services_Glue', 'glue'); // glue. 名前空間
  301. $server->setClass('My_Services_Paste', 'paste'); // paste. 名前空間
  302. $server->setClass('My_Services_Tape', 'tape'); // tape. 名前空間
  303. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  304. }
  305. echo $server->handle();
  306. ]]></programlisting>
  307. <para>
  308. この例では、スクリプトと同じディレクトリにある <property>xmlrpc.cache</property>
  309. からサーバの定義を取得しようとします。取得できなかった場合は、
  310. 必要なサービスクラスを読み込み、
  311. それをサーバのインスタンスにアタッチし、
  312. そしてその定義を新しいキャッシュファイルに記録します。
  313. </para>
  314. </sect2>
  315. <sect2 id="zend.xmlrpc.server.use">
  316. <title>使用例</title>
  317. <para>
  318. 以下のいくつかの使用例で、開発者が使用できるオプションを説明します。
  319. 各使用例は、それまでに紹介した例に追加していく形式になります。
  320. </para>
  321. <example id="zend.xmlrpc.server.use.attach-function">
  322. <title>基本的な使用法</title>
  323. <para>
  324. 次の例は関数を <acronym>XML-RPC</acronym> メソッドとしてアタッチし、
  325. 受け取ったコールを処理します。
  326. </para>
  327. <programlisting language="php"><![CDATA[
  328. /**
  329. * 値の MD5 合計を返します
  330. *
  331. * @param string $value md5sum を計算する値
  332. * @return string 値の MD5 合計
  333. */
  334. function md5Value($value)
  335. {
  336. return md5($value);
  337. }
  338. $server = new Zend_XmlRpc_Server();
  339. $server->addFunction('md5Value');
  340. echo $server->handle();
  341. ]]></programlisting>
  342. </example>
  343. <example id="zend.xmlrpc.server.use.attach-class">
  344. <title>クラスのアタッチ</title>
  345. <para>
  346. 次の例は、クラスのパブリックメソッドを
  347. <acronym>XML-RPC</acronym> メソッドとしてアタッチします。
  348. </para>
  349. <programlisting language="php"><![CDATA[
  350. require_once 'Services/Comb.php';
  351. $server = new Zend_XmlRpc_Server();
  352. $server->setClass('Services_Comb');
  353. echo $server->handle();
  354. ]]></programlisting>
  355. </example>
  356. <example id="zend.xmlrpc.server.use.attach-class-with-arguments">
  357. <title>引数にクラスを添付</title>
  358. <!-- TODO : to be translated -->
  359. <para>
  360. The following example illustrates how to attach a class' public
  361. methods and passing arguments to its methods. This can be used to specify certain
  362. defaults when registering service classes.
  363. </para>
  364. <programlisting language="php"><![CDATA[
  365. class Services_PricingService
  366. {
  367. /**
  368. * Calculate current price of product with $productId
  369. *
  370. * @param ProductRepository $productRepository
  371. * @param PurchaseRepository $purchaseRepository
  372. * @param integer $productId
  373. */
  374. public function calculate(ProductRepository $productRepository,
  375. PurchaseRepository $purchaseRepository,
  376. $productId)
  377. {
  378. ...
  379. }
  380. }
  381. $server = new Zend_XmlRpc_Server();
  382. $server->setClass('Services_PricingService',
  383. 'pricing',
  384. new ProductRepository(),
  385. new PurchaseRepository());
  386. ]]></programlisting>
  387. <para>
  388. The arguments passed at <methodname>setClass()</methodname> at server construction
  389. time are injected into the method call <command>pricing.calculate()</command> on
  390. remote invokation. In the example above, only the argument <varname>$purchaseId</varname>
  391. is expected from the client.
  392. </para>
  393. </example>
  394. <example id="zend.xmlrpc.server.use.attach-class-with-arguments-constructor">
  395. <title>Passing arguments only to constructor</title>
  396. <para>
  397. <classname>Zend_XmlRpc_Server</classname> allows to restrict argument passing to
  398. constructors only. This can be used for constructor dependency injection.
  399. To limit injection to constructors, call <methodname>sendArgumentsToAllMethods</methodname>
  400. and pass <constant>FALSE</constant> as an argument. This disables the default behavior of all arguments
  401. being injected into the remote method. In the example below the instance of
  402. <classname>ProductRepository</classname> and <classname>PurchaseRepository</classname> is only
  403. injected into the constructor of <classname>Services_PricingService2</classname>.
  404. </para>
  405. <programlisting language="php"><![CDATA[
  406. class Services_PricingService2
  407. {
  408. /**
  409. * @param ProductRepository $productRepository
  410. * @param PurchaseRepository $purchaseRepository
  411. */
  412. public function __construct(ProductRepository $productRepository,
  413. PurchaseRepository $purchaseRepository)
  414. {
  415. ...
  416. }
  417. /**
  418. * Calculate current price of product with $productId
  419. *
  420. * @param integer $productId
  421. * @return double
  422. */
  423. public function calculate($productId)
  424. {
  425. ...
  426. }
  427. }
  428. $server = new Zend_XmlRpc_Server();
  429. $server->sendArgumentsToAllMethods(false);
  430. $server->setClass('Services_PricingService2',
  431. 'pricing',
  432. new ProductRepository(),
  433. new PurchaseRepository());
  434. ]]></programlisting>
  435. </example>
  436. <example id="zend.xmlrpc.server.use.attach-instance">
  437. <title>Attaching a class instance</title>
  438. <para>
  439. <methodname>setClass()</methodname> allows to register a previously instantiated
  440. object at the server. Just pass an instance instead of the class name. Obviously
  441. passing arguments to the constructor is not possible with pre-instantiated
  442. objects.
  443. </para>
  444. </example>
  445. <example id="zend.xmlrpc.server.use.attach-several-classes-namespaces">
  446. <title>名前空間を用いた複数のクラスのアタッチ</title>
  447. <para>
  448. 次の例は、複数のクラスをそれぞれの名前空間でアタッチします。
  449. </para>
  450. <programlisting language="php"><![CDATA[
  451. require_once 'Services/Comb.php';
  452. require_once 'Services/Brush.php';
  453. require_once 'Services/Pick.php';
  454. $server = new Zend_XmlRpc_Server();
  455. $server->setClass('Services_Comb', 'comb'); // メソッドをコールするには comb.* とします
  456. $server->setClass('Services_Brush', 'brush'); // メソッドをコールするには brush.* とします
  457. $server->setClass('Services_Pick', 'pick'); // メソッドをコールするには pick.* とします
  458. echo $server->handle();
  459. ]]></programlisting>
  460. </example>
  461. <example id="zend.xmlrpc.server.use.exceptions-faults">
  462. <title>fault レスポンス用に使用する例外の指定</title>
  463. <para>
  464. 次の例は、<classname>Services_Exception</classname> の派生クラスに対して
  465. そのコードとメッセージを falut レスポンスで報告させるようにします。
  466. </para>
  467. <programlisting language="php"><![CDATA[
  468. require_once 'Services/Exception.php';
  469. require_once 'Services/Comb.php';
  470. require_once 'Services/Brush.php';
  471. require_once 'Services/Pick.php';
  472. // Services_Exceptions を fault レスポンスで報告させるようにします
  473. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  474. $server = new Zend_XmlRpc_Server();
  475. $server->setClass('Services_Comb', 'comb'); // メソッドをコールするには comb.* とします
  476. $server->setClass('Services_Brush', 'brush'); // メソッドをコールするには brush.* とします
  477. $server->setClass('Services_Pick', 'pick'); // メソッドをコールするには pick.* とします
  478. echo $server->handle();
  479. ]]></programlisting>
  480. </example>
  481. <example id="zend.xmlrpc.server.use.custom-request-object">
  482. <title>独自のリクエスト及びレスポンスオブジェクトの利用</title>
  483. <!-- TODO : to be translated -->
  484. <para>
  485. Some use cases require to utilize a custom request object.
  486. For example, <acronym>XML/RPC</acronym> is not bound to
  487. <acronym>HTTP</acronym> as a transfer protocol. It is possible to use
  488. other transfer protocols like <acronym>SSH</acronym> or telnet to send
  489. the request and response data over the wire. Another use case is
  490. authentication and authorization. In case of a different transfer
  491. protocol, one need to change the implementation to read request data.
  492. </para>
  493. <para>
  494. 次の例は、独自のリクエストオブジェクトを作成し、
  495. それをサーバに渡して処理します。
  496. </para>
  497. <programlisting language="php"><![CDATA[
  498. require_once 'Services/Request.php';
  499. require_once 'Services/Exception.php';
  500. require_once 'Services/Comb.php';
  501. require_once 'Services/Brush.php';
  502. require_once 'Services/Pick.php';
  503. // Services_Exceptions を fault レスポンスで報告させるようにします
  504. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  505. $server = new Zend_XmlRpc_Server();
  506. $server->setClass('Services_Comb', 'comb'); // メソッドをコールするには comb.* とします
  507. $server->setClass('Services_Brush', 'brush'); // メソッドをコールするには brush.* とします
  508. $server->setClass('Services_Pick', 'pick'); // メソッドをコールするには pick.* とします
  509. // リクエストオブジェクトを作成します
  510. $request = new Services_Request();
  511. echo $server->handle($request);
  512. ]]></programlisting>
  513. </example>
  514. <example id="zend.xmlrpc.server.use.custom-response-object">
  515. <title>独自のレスポンスクラスの指定</title>
  516. <para>
  517. 次の例は、独自のレスポンスクラスを作成し、
  518. それをレスポンスとして返します。
  519. </para>
  520. <programlisting language="php"><![CDATA[
  521. require_once 'Services/Request.php';
  522. require_once 'Services/Response.php';
  523. require_once 'Services/Exception.php';
  524. require_once 'Services/Comb.php';
  525. require_once 'Services/Brush.php';
  526. require_once 'Services/Pick.php';
  527. // Services_Exceptions を fault レスポンスで報告させるようにします
  528. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  529. $server = new Zend_XmlRpc_Server();
  530. $server->setClass('Services_Comb', 'comb'); // メソッドをコールするには comb.* とします
  531. $server->setClass('Services_Brush', 'brush'); // メソッドをコールするには brush.* とします
  532. $server->setClass('Services_Pick', 'pick'); // メソッドをコールするには pick.* とします
  533. // リクエストオブジェクトを作成します
  534. $request = new Services_Request();
  535. // 独自のレスポンスを使用します
  536. $server->setResponseClass('Services_Response');
  537. echo $server->handle($request);
  538. ]]></programlisting>
  539. </example>
  540. </sect2>
  541. <sect2 id="zend.xmlrpc.server.performance">
  542. <title>パフォーマンスの最適化</title>
  543. <example id="zend.xmlrpc.server.performance.caching">
  544. <title>リクエスト間でのサーバ定義のキャッシュ</title>
  545. <para>
  546. 次の例は、リクエスト間でサーバ定義をキャッシュします。
  547. </para>
  548. <programlisting language="php"><![CDATA[
  549. // キャッシュファイルを指定します
  550. $cacheFile = dirname(__FILE__) . '/xmlrpc.cache';
  551. // Services_Exceptions を fault レスポンスで報告させるようにします
  552. Zend_XmlRpc_Server_Fault::attachFaultException('Services_Exception');
  553. $server = new Zend_XmlRpc_Server();
  554. // サーバ定義をキャッシュから取得しようとします
  555. if (!Zend_XmlRpc_Server_Cache::get($cacheFile, $server)) {
  556. $server->setClass('Services_Comb', 'comb'); // メソッドをコールするには comb.* とします
  557. $server->setClass('Services_Brush', 'brush'); // メソッドをコールするには brush.* とします
  558. $server->setClass('Services_Pick', 'pick'); // メソッドをコールするには pick.* とします
  559. // キャッシュに保存します
  560. Zend_XmlRpc_Server_Cache::save($cacheFile, $server);
  561. }
  562. // リクエストオブジェクトを作成します
  563. $request = new Services_Request();
  564. // 独自のレスポンスを使用します
  565. $server->setResponseClass('Services_Response');
  566. echo $server->handle($request);
  567. ]]></programlisting>
  568. </example>
  569. <note>
  570. <para>
  571. サーバーのキャッシュファイルは、ドキュメントルートの外部に配置されるべきです。
  572. </para>
  573. </note>
  574. <example id="zend.xmlrpc.server.performance.xmlgen">
  575. <title>XML 生成を最適化</title>
  576. <!-- TODO : to be translated -->
  577. <para>
  578. <classname>Zend_XmlRpc_Server</classname> uses
  579. <classname>DOMDocument</classname> of <acronym>PHP</acronym>
  580. extension <emphasis>ext/dom</emphasis> to generate it's
  581. <acronym>XML</acronym> output. While <emphasis>ext/dom</emphasis> is
  582. available on a lot of hosts it is not exactly the fastest.
  583. Benchmarks have shown, that <classname>XmlWriter</classname>
  584. from <emphasis>ext/xmlwriter</emphasis> performs better.
  585. </para>
  586. <para>
  587. If <emphasis>ext/xmlwriter</emphasis> is available on your host, you can
  588. select a the <classname>XmlWriter</classname>-based generator
  589. to leaverage the performance differences.
  590. </para>
  591. <programlisting language="php"><![CDATA[
  592. require_once 'Zend/XmlRpc/Server.php';
  593. require_once 'Zend/XmlRpc/Generator/XmlWriter.php';
  594. Zend_XmlRpc_Value::setGenerator(new Zend_XmlRpc_Generator_XmlWriter());
  595. $server = new Zend_XmlRpc_Server();
  596. ...
  597. ]]></programlisting>
  598. </example>
  599. <note>
  600. <title>Benchmark your application</title>
  601. <para>
  602. Performance is determined by a lot of parameters and
  603. benchmarks only apply for the specific test case. Differences
  604. come from <acronym>PHP</acronym> version, installed extensions, webserver and
  605. operating system just to name a few. Please make sure to
  606. benchmark your application on your own and decide which
  607. generator to use based on <emphasis>your</emphasis> numbers.
  608. </para>
  609. </note>
  610. <note>
  611. <title>Benchmark your client</title>
  612. <para>
  613. This optimization makes sense for the client side too. Just
  614. select the alternate <acronym>XML</acronym> generator before
  615. doing any work with <classname>Zend_XmlRpc_Client</classname>.
  616. </para>
  617. </note>
  618. </sect2>
  619. </sect1>
  620. <!--
  621. vim:se ts=4 sw=4 et:
  622. -->