/** * Add method to definition * * @param array|Zend_Server_Method_Definition $method * @param null|string $name * @return Zend_Server_Definition * @throws Zend_Server_Exception if duplicate or invalid method provided */ public function addMethod($method, $name = null) { if (is_array($method)) { $method = new Zend_Server_Method_Definition($method); } elseif (!$method instanceof Zend_Server_Method_Definition) { throw new Zend_Server_Exception('Invalid method provided'); } if (is_numeric($name)) { $name = null; } if (null !== $name) { $method->setName($name); } else { $name = $method->getName(); } if (null === $name) { throw new Zend_Server_Exception('No method name provided'); } if (!$this->_overwriteExistingMethods && array_key_exists($name, $this->_methods)) { throw new Zend_Server_Exception(sprintf('Method by name of "%s" already exists', $name)); } $this->_methods[$name] = $method; return $this; }
/** * Add service method to service map * * @param Zend_Server_Reflection_Function $method * @return void */ protected function _addMethodServiceMap(Zend_Server_Method_Definition $method) { $serviceInfo = array('name' => $method->getName(), 'return' => $this->_getReturnType($method)); $params = $this->_getParams($method); $serviceInfo['params'] = $params; $serviceMap = $this->getServiceMap(); if (false !== $serviceMap->getService($serviceInfo['name'])) { $serviceMap->removeService($serviceInfo['name']); } $serviceMap->addService($serviceInfo); }
/** * Dispatch method * * @param Zend_Server_Method_Definition $invocable * @param array $params * @return mixed */ protected function _dispatch(Zend_Server_Method_Definition $invocable, array $params) { $callback = $invocable->getCallback(); $type = $callback->getType(); if ('function' == $type) { $function = $callback->getFunction(); return call_user_func_array($function, $params); } $class = $callback->getClass(); $method = $callback->getMethod(); if ('static' == $type) { return call_user_func_array(array($class, $method), $params); } if (!isset($this->_instances[$invocable->getName()])) { $object = $invocable->getObject(); if (!is_object($object)) { $invokeArgs = $invocable->getInvokeArguments(); if (!empty($invokeArgs)) { $reflection = new ReflectionClass($class); $object = $reflection->newInstanceArgs($invokeArgs); } else { $object = new $class(); } } $this->_instances[$invocable->getName()] = $object; } $object = $this->_instances[$invocable->getName()]; return call_user_func_array(array($object, $method), $params); }