/** * Add method to definition * * @param array|\Zend\Server\Method\Definition $method * @param null|string $name * @return \Zend\Server\Definition * @throws \Zend\Server\Exception\InvalidArgumentException if duplicate or invalid method provided */ public function addMethod($method, $name = null) { if (is_array($method)) { $method = new Method\Definition($method); } elseif (!$method instanceof Method\Definition) { throw new Exception\InvalidArgumentException('Invalid method provided'); } if (is_numeric($name)) { $name = null; } if (null !== $name) { $method->setName($name); } else { $name = $method->getName(); } if (null === $name) { throw new Exception\InvalidArgumentException('No method name provided'); } if (!$this->overwriteExistingMethods && array_key_exists($name, $this->methods)) { throw new Exception\InvalidArgumentException(sprintf('Method by name of "%s" already exists', $name)); } $this->methods[$name] = $method; return $this; }
/** * Get method return type * * @param Method\Definition $method * @return string|array */ protected function _getReturnType(Method\Definition $method) { $return = array(); foreach ($method->getPrototypes() as $prototype) { $return[] = $prototype->getReturnType(); } if (1 == count($return)) { return $return[0]; } return $return; }
public function testPassingOptionsToConstructorShouldSetObjectState() { $options = array('name' => 'foo.bar', 'callback' => array('function' => 'foo', 'type' => 'function'), 'prototypes' => array(array('returnType' => 'struct', 'parameters' => array('string', 'array'))), 'methodHelp' => 'foo bar', 'object' => new \stdClass(), 'invokeArguments' => array('foo', array('bar', 'baz'))); $definition = new Method\Definition($options); $test = $definition->toArray(); $this->assertEquals($options['name'], $test['name']); $this->assertEquals($options['callback'], $test['callback']); $this->assertEquals($options['prototypes'], $test['prototypes']); $this->assertEquals($options['methodHelp'], $test['methodHelp']); $this->assertEquals($options['object'], $test['object']); $this->assertEquals($options['invokeArguments'], $test['invokeArguments']); }
/** * Dispatch method * * @param Method\Definition $invocable * @param array $params * @return mixed */ protected function _dispatch(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); } $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(); } } return call_user_func_array(array($object, $method), $params); }
/** * (non-PHPdoc). * * @see \Zend\Server\AbstractServer::_dispatch() */ public function _dispatch(\Zend\Server\Method\Definition $invocable, array $params) { return call_user_func_array(array($this->container->get($invocable->getNameSm()), $invocable->getCallback()->getMethod()), $params); }
/** * Serialize to array. * * @return array */ public function toArray() { return parent::toArray() + array('nameSm' => $this->getNameSm()); }