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

/lib/Zend/Json/Server/Smd.php

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