Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Server.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Server/Abstract.php';
26 
34 {
38  const VERSION_1 = '1.0';
39  const VERSION_2 = '2.0';
46  protected $_autoEmitResponse = true;
47 
51  protected $_overwriteExistingMethods = true;
52 
57  protected $_request;
58 
63  protected $_response;
64 
69  protected $_serviceMap;
70 
75  protected $_smdMethods;
76 
80  protected $_table;
81 
89  public function addFunction($function, $namespace = '')
90  {
91  if (!is_string($function) && (!is_array($function) || (2 > count($function)))) {
92  #require_once 'Zend/Json/Server/Exception.php';
93  throw new Zend_Json_Server_Exception('Unable to attach function; invalid');
94  }
95 
96  if (!is_callable($function)) {
97  #require_once 'Zend/Json/Server/Exception.php';
98  throw new Zend_Json_Server_Exception('Unable to attach function; does not exist');
99  }
100 
101  $argv = null;
102  if (2 < func_num_args()) {
103  $argv = func_get_args();
104  $argv = array_slice($argv, 2);
105  }
106 
107  #require_once 'Zend/Server/Reflection.php';
108  if (is_string($function)) {
109  $method = Zend_Server_Reflection::reflectFunction($function, $argv, $namespace);
110  } else {
111  $class = array_shift($function);
112  $action = array_shift($function);
113  $reflection = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);
114  $methods = $reflection->getMethods();
115  $found = false;
116  foreach ($methods as $method) {
117  if ($action == $method->getName()) {
118  $found = true;
119  break;
120  }
121  }
122  if (!$found) {
123  $this->fault('Method not found', -32601);
124  return $this;
125  }
126  }
127 
128  $definition = $this->_buildSignature($method);
129  $this->_addMethodServiceMap($definition);
130 
131  return $this;
132  }
133 
142  public function setClass($class, $namespace = '', $argv = null)
143  {
144  $argv = null;
145  if (3 < func_num_args()) {
146  $argv = func_get_args();
147  $argv = array_slice($argv, 3);
148  }
149 
150  #require_once 'Zend/Server/Reflection.php';
151  $reflection = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);
152 
153  foreach ($reflection->getMethods() as $method) {
154  $definition = $this->_buildSignature($method, $class);
155  $this->_addMethodServiceMap($definition);
156  }
157  return $this;
158  }
159 
167  public function fault($fault = null, $code = 404, $data = null)
168  {
169  #require_once 'Zend/Json/Server/Error.php';
170  $error = new Zend_Json_Server_Error($fault, $code, $data);
171  $this->getResponse()->setError($error);
172  return $error;
173  }
174 
181  public function handle($request = false)
182  {
183  if ((false !== $request) && (!$request instanceof Zend_Json_Server_Request)) {
184  #require_once 'Zend/Json/Server/Exception.php';
185  throw new Zend_Json_Server_Exception('Invalid request type provided; cannot handle');
186  } elseif ($request) {
187  $this->setRequest($request);
188  }
189 
190  // Handle request
191  $this->_handle();
192 
193  // Get response
194  $response = $this->_getReadyResponse();
195 
196  // Emit response?
197  if ($this->autoEmitResponse()) {
198  echo $response;
199  return;
200  }
201 
202  // or return it?
203  return $response;
204  }
205 
212  public function loadFunctions($definition)
213  {
214  if (!is_array($definition) && (!$definition instanceof Zend_Server_Definition)) {
215  #require_once 'Zend/Json/Server/Exception.php';
216  throw new Zend_Json_Server_Exception('Invalid definition provided to loadFunctions()');
217  }
218 
219  foreach ($definition as $key => $method) {
220  $this->_table->addMethod($method, $key);
222  }
223  }
224 
225  public function setPersistence($mode)
226  {
227  }
228 
236  {
237  $this->_request = $request;
238  return $this;
239  }
240 
246  public function getRequest()
247  {
248  if (null === ($request = $this->_request)) {
249  #require_once 'Zend/Json/Server/Request/Http.php';
251  }
252  return $this->_request;
253  }
254 
262  {
263  $this->_response = $response;
264  return $this;
265  }
266 
272  public function getResponse()
273  {
274  if (null === ($response = $this->_response)) {
275  #require_once 'Zend/Json/Server/Response/Http.php';
277  }
278  return $this->_response;
279  }
280 
287  public function setAutoEmitResponse($flag)
288  {
289  $this->_autoEmitResponse = (bool) $flag;
290  return $this;
291  }
292 
298  public function autoEmitResponse()
299  {
301  }
302 
303  // overloading for SMD metadata
311  public function __call($method, $args)
312  {
313  if (preg_match('/^(set|get)/', $method, $matches)) {
314  if (in_array($method, $this->_getSmdMethods())) {
315  if ('set' == $matches[1]) {
316  $value = array_shift($args);
317  $this->getServiceMap()->$method($value);
318  return $this;
319  } else {
320  return $this->getServiceMap()->$method();
321  }
322  }
323  }
324  return null;
325  }
326 
332  public function getServiceMap()
333  {
334  if (null === $this->_serviceMap) {
335  #require_once 'Zend/Json/Server/Smd.php';
336  $this->_serviceMap = new Zend_Json_Server_Smd();
337  }
338  return $this->_serviceMap;
339  }
340 
348  {
349  $serviceInfo = array(
350  'name' => $method->getName(),
351  'return' => $this->_getReturnType($method),
352  );
353  $params = $this->_getParams($method);
354  $serviceInfo['params'] = $params;
355  $serviceMap = $this->getServiceMap();
356  if (false !== $serviceMap->getService($serviceInfo['name'])) {
357  $serviceMap->removeService($serviceInfo['name']);
358  }
359  $serviceMap->addService($serviceInfo);
360  }
361 
368  protected function _fixType($type)
369  {
370  return $type;
371  }
372 
380  protected function _getDefaultParams(array $args, array $params)
381  {
382  $defaultParams = array_slice($params, count($args));
383  foreach ($defaultParams as $param) {
384  $value = null;
385  if (array_key_exists('default', $param)) {
386  $value = $param['default'];
387  }
388  array_push($args, $value);
389  }
390  return $args;
391  }
392 
400  {
401  $params = array();
402  foreach ($method->getPrototypes() as $prototype) {
403  foreach ($prototype->getParameterObjects() as $key => $parameter) {
404  if (!isset($params[$key])) {
405  $params[$key] = array(
406  'type' => $parameter->getType(),
407  'name' => $parameter->getName(),
408  'optional' => $parameter->isOptional(),
409  );
410  if (null !== ($default = $parameter->getDefaultValue())) {
411  $params[$key]['default'] = $default;
412  }
413  $description = $parameter->getDescription();
414  if (!empty($description)) {
415  $params[$key]['description'] = $description;
416  }
417  continue;
418  }
419  $newType = $parameter->getType();
420  if (!is_array($params[$key]['type'])) {
421  if ($params[$key]['type'] == $newType) {
422  continue;
423  }
424  $params[$key]['type'] = (array) $params[$key]['type'];
425  } elseif (in_array($newType, $params[$key]['type'])) {
426  continue;
427  }
428  array_push($params[$key]['type'], $parameter->getType());
429  }
430  }
431  return $params;
432  }
433 
439  protected function _getReadyResponse()
440  {
441  $request = $this->getRequest();
442  $response = $this->getResponse();
443 
444  $response->setServiceMap($this->getServiceMap());
445  if (null !== ($id = $request->getId())) {
446  $response->setId($id);
447  }
448  if (null !== ($version = $request->getVersion())) {
449  $response->setVersion($version);
450  }
451 
452  return $response;
453  }
454 
462  {
463  $return = array();
464  foreach ($method->getPrototypes() as $prototype) {
465  $return[] = $prototype->getReturnType();
466  }
467  if (1 == count($return)) {
468  return $return[0];
469  }
470  return $return;
471  }
472 
478  protected function _getSmdMethods()
479  {
480  if (null === $this->_smdMethods) {
481  $this->_smdMethods = array();
482  #require_once 'Zend/Json/Server/Smd.php';
483  $methods = get_class_methods('Zend_Json_Server_Smd');
484  foreach ($methods as $key => $method) {
485  if (!preg_match('/^(set|get)/', $method)) {
486  continue;
487  }
488  if (strstr($method, 'Service')) {
489  continue;
490  }
491  $this->_smdMethods[] = $method;
492  }
493  }
494  return $this->_smdMethods;
495  }
496 
502  protected function _handle()
503  {
504  $request = $this->getRequest();
505 
506  if (!$request->isMethodError() && (null === $request->getMethod())) {
507  return $this->fault('Invalid Request', -32600);
508  }
509 
510  if ($request->isMethodError()) {
511  return $this->fault('Invalid Request', -32600);
512  }
513 
514  $method = $request->getMethod();
515  if (!$this->_table->hasMethod($method)) {
516  return $this->fault('Method not found', -32601);
517  }
518 
519  $params = $request->getParams();
520  $invocable = $this->_table->getMethod($method);
521  $serviceMap = $this->getServiceMap();
522  $service = $serviceMap->getService($method);
523  $serviceParams = $service->getParams();
524 
525  if (count($params) < count($serviceParams)) {
526  $params = $this->_getDefaultParams($params, $serviceParams);
527  }
528 
529  //Make sure named parameters are passed in correct order
530  if ( is_string( key( $params ) ) ) {
531 
532  $callback = $invocable->getCallback();
533  if ('function' == $callback->getType()) {
534  $reflection = new ReflectionFunction( $callback->getFunction() );
535  $refParams = $reflection->getParameters();
536  } else {
537 
538  $reflection = new ReflectionMethod(
539  $callback->getClass(),
540  $callback->getMethod()
541  );
542  $refParams = $reflection->getParameters();
543  }
544 
545  $orderedParams = array();
546  foreach( $reflection->getParameters() as $refParam ) {
547  if( isset( $params[ $refParam->getName() ] ) ) {
548  $orderedParams[ $refParam->getName() ] = $params[ $refParam->getName() ];
549  } elseif( $refParam->isOptional() ) {
550  $orderedParams[ $refParam->getName() ] = $refParam->getDefaultValue();
551  } else {
552  throw new Zend_Server_Exception(
553  'Method ' . $request->getMethod() . ' is missing required parameter: ' . $refParam->getName()
554  );
555  }
556  }
557  $params = $orderedParams;
558  }
559 
560  try {
561  $result = $this->_dispatch($invocable, $params);
562  } catch (Exception $e) {
563  return $this->fault($e->getMessage(), $e->getCode(), $e);
564  }
565 
566  $this->getResponse()->setResult($result);
567  }
568 }
$response
Definition: 404.php:11
_dispatch(Zend_Server_Method_Definition $invocable, array $params)
Definition: Abstract.php:205
__call($method, $args)
Definition: Server.php:311
setRequest(Zend_Json_Server_Request $request)
Definition: Server.php:235
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_getParams(Zend_Server_Method_Definition $method)
Definition: Server.php:399
$id
Definition: fieldset.phtml:14
setResponse(Zend_Json_Server_Response $response)
Definition: Server.php:261
_buildSignature(Zend_Server_Reflection_Function_Abstract $reflection, $class=null)
Definition: Abstract.php:158
addFunction($function, $namespace='')
Definition: Server.php:89
$type
Definition: item.phtml:13
static reflectFunction($function, $argv=false, $namespace='')
Definition: Reflection.php:96
$methods
Definition: billing.phtml:71
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
setPersistence($mode)
Definition: Server.php:225
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
setAutoEmitResponse($flag)
Definition: Server.php:287
loadFunctions($definition)
Definition: Server.php:212
_fixType($type)
Definition: Server.php:368
handle($request=false)
Definition: Server.php:181
$method
Definition: info.phtml:13
const VERSION_1
Definition: Server.php:38
_getReturnType(Zend_Server_Method_Definition $method)
Definition: Server.php:461
static reflectClass($class, $argv=false, $namespace='')
Definition: Reflection.php:60
setClass($class, $namespace='', $argv=null)
Definition: Server.php:142
$_overwriteExistingMethods
Definition: Server.php:51
fault($fault=null, $code=404, $data=null)
Definition: Server.php:167
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
const VERSION_2
Definition: Server.php:39
_addMethodServiceMap(Zend_Server_Method_Definition $method)
Definition: Server.php:347
$code
Definition: info.phtml:12
_getDefaultParams(array $args, array $params)
Definition: Server.php:380