PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/Zend/Json/Server/Smd.php

http://firephp.googlecode.com/
PHP | 479 lines | 223 code | 47 blank | 209 comment | 15 complexity | b5d20c3c56dbc7e5b37a775a74cd24c6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Json
  17. * @subpackage Server
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Json
  24. * @subpackage Server
  25. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Json_Server_Smd
  29. {
  30. const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
  31. const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
  32. const SMD_VERSION = '2.0';
  33. /**
  34. * Content type
  35. * @var string
  36. */
  37. protected $_contentType = 'application/json';
  38. /**
  39. * Content type regex
  40. * @var string
  41. */
  42. protected $_contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
  43. /**
  44. * Service description
  45. * @var string
  46. */
  47. protected $_description;
  48. /**
  49. * Generate Dojo-compatible SMD
  50. * @var bool
  51. */
  52. protected $_dojoCompatible = false;
  53. /**
  54. * Current envelope
  55. * @var string
  56. */
  57. protected $_envelope = self::ENV_JSONRPC_1;
  58. /**
  59. * Allowed envelope types
  60. * @var array
  61. */
  62. protected $_envelopeTypes = array(
  63. self::ENV_JSONRPC_1,
  64. self::ENV_JSONRPC_2,
  65. );
  66. /**
  67. * Service id
  68. * @var string
  69. */
  70. protected $_id;
  71. /**
  72. * Services offerred
  73. * @var array
  74. */
  75. protected $_services = array();
  76. /**
  77. * Service target
  78. * @var string
  79. */
  80. protected $_target;
  81. /**
  82. * Global transport
  83. * @var string
  84. */
  85. protected $_transport = 'POST';
  86. /**
  87. * Allowed transport types
  88. * @var array
  89. */
  90. protected $_transportTypes = array('POST');
  91. /**
  92. * Set object state via options
  93. *
  94. * @param array $options
  95. * @return Zend_Json_Server_Smd
  96. */
  97. public function setOptions(array $options)
  98. {
  99. $methods = get_class_methods($this);
  100. foreach ($options as $key => $value) {
  101. $method = 'set' . ucfirst($key);
  102. if (in_array($method, $methods)) {
  103. $this->$method($value);
  104. }
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Set transport
  110. *
  111. * @param string $transport
  112. * @return Zend_Json_Server_Smd
  113. */
  114. public function setTransport($transport)
  115. {
  116. if (!in_array($transport, $this->_transportTypes)) {
  117. require_once 'Zend/Json/Server/Exception.php';
  118. throw new Zend_Json_Server_Exception(sprintf('Invalid transport "%s" specified', $transport));
  119. }
  120. $this->_transport = $transport;
  121. return $this;
  122. }
  123. /**
  124. * Get transport
  125. *
  126. * @return string
  127. */
  128. public function getTransport()
  129. {
  130. return $this->_transport;
  131. }
  132. /**
  133. * Set envelope
  134. *
  135. * @param string $envelopeType
  136. * @return Zend_Json_Server_Smd
  137. */
  138. public function setEnvelope($envelopeType)
  139. {
  140. if (!in_array($envelopeType, $this->_envelopeTypes)) {
  141. require_once 'Zend/Json/Server/Exception.php';
  142. throw new Zend_Json_Server_Exception(sprintf('Invalid envelope type "%s"', $envelopeType));
  143. }
  144. $this->_envelope = $envelopeType;
  145. return $this;
  146. }
  147. /**
  148. * Retrieve envelope
  149. *
  150. * @return string
  151. */
  152. public function getEnvelope()
  153. {
  154. return $this->_envelope;
  155. }
  156. // Content-Type of response; default to application/json
  157. /**
  158. * Set content type
  159. *
  160. * @param string $type
  161. * @return Zend_Json_Server_Smd
  162. */
  163. public function setContentType($type)
  164. {
  165. if (!preg_match($this->_contentTypeRegex, $type)) {
  166. require_once 'Zend/Json/Server/Exception.php';
  167. throw new Zend_Json_Server_Exception(sprintf('Invalid content type "%s" specified', $type));
  168. }
  169. $this->_contentType = $type;
  170. return $this;
  171. }
  172. /**
  173. * Retrieve content type
  174. *
  175. * @return string
  176. */
  177. public function getContentType()
  178. {
  179. return $this->_contentType;
  180. }
  181. /**
  182. * Set service target
  183. *
  184. * @param string $target
  185. * @return Zend_Json_Server_Smd
  186. */
  187. public function setTarget($target)
  188. {
  189. $this->_target = (string) $target;
  190. return $this;
  191. }
  192. /**
  193. * Retrieve service target
  194. *
  195. * @return string
  196. */
  197. public function getTarget()
  198. {
  199. return $this->_target;
  200. }
  201. /**
  202. * Set service ID
  203. *
  204. * @param string $Id
  205. * @return Zend_Json_Server_Smd
  206. */
  207. public function setId($id)
  208. {
  209. $this->_id = (string) $id;
  210. return $this->_id;
  211. }
  212. /**
  213. * Get service id
  214. *
  215. * @return string
  216. */
  217. public function getId()
  218. {
  219. return $this->_id;
  220. }
  221. /**
  222. * Set service description
  223. *
  224. * @param string $description
  225. * @return Zend_Json_Server_Smd
  226. */
  227. public function setDescription($description)
  228. {
  229. $this->_description = (string) $description;
  230. return $this->_description;
  231. }
  232. /**
  233. * Get service description
  234. *
  235. * @return string
  236. */
  237. public function getDescription()
  238. {
  239. return $this->_description;
  240. }
  241. /**
  242. * Indicate whether or not to generate Dojo-compatible SMD
  243. *
  244. * @param bool $flag
  245. * @return Zend_Json_Server_Smd
  246. */
  247. public function setDojoCompatible($flag)
  248. {
  249. $this->_dojoCompatible = (bool) $flag;
  250. return $this;
  251. }
  252. /**
  253. * Is this a Dojo compatible SMD?
  254. *
  255. * @return bool
  256. */
  257. public function isDojoCompatible()
  258. {
  259. return $this->_dojoCompatible;
  260. }
  261. /**
  262. * Add Service
  263. *
  264. * @param Zend_Json_Server_Smd_Service|array $service
  265. * @return void
  266. */
  267. public function addService($service)
  268. {
  269. require_once 'Zend/Json/Server/Smd/Service.php';
  270. if ($service instanceof Zend_Json_Server_Smd_Service) {
  271. $name = $service->getName();
  272. } elseif (is_array($service)) {
  273. $service = new Zend_Json_Server_Smd_Service($service);
  274. $name = $service->getName();
  275. } else {
  276. require_once 'Zend/Json/Server/Exception.php';
  277. throw new Zend_Json_Server_Exception('Invalid service passed to addService()');
  278. }
  279. if (array_key_exists($name, $this->_services)) {
  280. require_once 'Zend/Json/Server/Exception.php';
  281. throw new Zend_Json_Server_Exception('Attempt to register a service already registered detected');
  282. }
  283. $this->_services[$name] = $service;
  284. return $this;
  285. }
  286. /**
  287. * Add many services
  288. *
  289. * @param array $services
  290. * @return Zend_Json_Server_Smd
  291. */
  292. public function addServices(array $services)
  293. {
  294. foreach ($services as $service) {
  295. $this->addService($service);
  296. }
  297. return $this;
  298. }
  299. /**
  300. * Overwrite existing services with new ones
  301. *
  302. * @param array $services
  303. * @return Zend_Json_Server_Smd
  304. */
  305. public function setServices(array $services)
  306. {
  307. $this->_services = array();
  308. return $this->addServices($services);
  309. }
  310. /**
  311. * Get service object
  312. *
  313. * @param string $name
  314. * @return false|Zend_Json_Server_Smd_Service
  315. */
  316. public function getService($name)
  317. {
  318. if (array_key_exists($name, $this->_services)) {
  319. return $this->_services[$name];
  320. }
  321. return false;
  322. }
  323. /**
  324. * Return services
  325. *
  326. * @return array
  327. */
  328. public function getServices()
  329. {
  330. return $this->_services;
  331. }
  332. /**
  333. * Remove service
  334. *
  335. * @param string $name
  336. * @return boolean
  337. */
  338. public function removeService($name)
  339. {
  340. if (array_key_exists($name, $this->_services)) {
  341. unset($this->_services[$name]);
  342. return true;
  343. }
  344. return false;
  345. }
  346. /**
  347. * Cast to array
  348. *
  349. * @return array
  350. */
  351. public function toArray()
  352. {
  353. if ($this->isDojoCompatible()) {
  354. return $this->toDojoArray();
  355. }
  356. $transport = $this->getTransport();
  357. $envelope = $this->getEnvelope();
  358. $contentType = $this->getContentType();
  359. $SMDVersion = self::SMD_VERSION;
  360. $service = compact('transport', 'envelope', 'contentType', 'SMDVersion');
  361. if (null !== ($target = $this->getTarget())) {
  362. $service['target'] = $target;
  363. }
  364. if (null !== ($id = $this->getId())) {
  365. $service['id'] = $id;
  366. }
  367. $services = $this->getServices();
  368. if (!empty($services)) {
  369. $service['services'] = array();
  370. foreach ($services as $name => $svc) {
  371. $svc->setEnvelope($envelope);
  372. $service['services'][$name] = $svc->toArray();
  373. }
  374. $service['methods'] = $service['services'];
  375. }
  376. return $service;
  377. }
  378. /**
  379. * Export to DOJO-compatible SMD array
  380. *
  381. * @return array
  382. */
  383. public function toDojoArray()
  384. {
  385. $SMDVersion = '.1';
  386. $serviceType = 'JSON-RPC';
  387. $service = compact('SMDVersion', 'serviceType');
  388. $target = $this->getTarget();
  389. $services = $this->getServices();
  390. if (!empty($services)) {
  391. $service['methods'] = array();
  392. foreach ($services as $name => $svc) {
  393. $method = array(
  394. 'name' => $name,
  395. 'serviceURL' => $target,
  396. );
  397. $params = array();
  398. foreach ($svc->getParams() as $param) {
  399. $paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
  400. $params[] = array(
  401. 'name' => $paramName,
  402. 'type' => $param['type'],
  403. );
  404. }
  405. if (!empty($params)) {
  406. $method['parameters'] = $params;
  407. }
  408. $service['methods'][] = $method;
  409. }
  410. }
  411. return $service;
  412. }
  413. /**
  414. * Cast to JSON
  415. *
  416. * @return string
  417. */
  418. public function toJson()
  419. {
  420. require_once 'Zend/Json.php';
  421. return Zend_Json::encode($this->toArray());
  422. }
  423. /**
  424. * Cast to string (JSON)
  425. *
  426. * @return string
  427. */
  428. public function __toString()
  429. {
  430. return $this->toJson();
  431. }
  432. }