3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Server
;
15 * Abstract Server implementation
17 abstract class AbstractServer
implements Server
20 * @var bool Flag; whether or not overwriting existing methods is allowed
22 protected $overwriteExistingMethods = false;
32 * Setup server description
35 public function __construct()
37 $this->table
= new Definition();
38 $this->table
->setOverwriteExistingMethods($this->overwriteExistingMethods
);
42 * Returns a list of registered methods
44 * Returns an array of method definitions.
48 public function getFunctions()
54 * Build callback for method signature
56 * @param Reflection\AbstractFunction $reflection
57 * @return Method\Callback
59 protected function _buildCallback(Reflection\AbstractFunction
$reflection)
61 $callback = new Method\
Callback();
62 if ($reflection instanceof Reflection\ReflectionMethod
) {
63 $callback->setType($reflection->isStatic() ?
'static' : 'instance')
64 ->setClass($reflection->getDeclaringClass()->getName())
65 ->setMethod($reflection->getName());
66 } elseif ($reflection instanceof Reflection\ReflectionFunction
) {
67 $callback->setType('function')
68 ->setFunction($reflection->getName());
74 * Build a method signature
76 * @param Reflection\AbstractFunction $reflection
77 * @param null|string|object $class
78 * @return Method\Definition
79 * @throws Exception\RuntimeException on duplicate entry
81 protected function _buildSignature(Reflection\AbstractFunction
$reflection, $class = null)
83 $ns = $reflection->getNamespace();
84 $name = $reflection->getName();
85 $method = empty($ns) ?
$name : $ns . '.' . $name;
87 if (!$this->overwriteExistingMethods
&& $this->table
->hasMethod($method)) {
88 throw new Exception\
RuntimeException('Duplicate method registered: ' . $method);
91 $definition = new Method\
Definition();
92 $definition->setName($method)
93 ->setCallback($this->_buildCallback($reflection))
94 ->setMethodHelp($reflection->getDescription())
95 ->setInvokeArguments($reflection->getInvokeArguments());
97 foreach ($reflection->getPrototypes() as $proto) {
98 $prototype = new Method\
Prototype();
99 $prototype->setReturnType($this->_fixType($proto->getReturnType()));
100 foreach ($proto->getParameters() as $parameter) {
101 $param = new Method\
Parameter(array(
102 'type' => $this->_fixType($parameter->getType()),
103 'name' => $parameter->getName(),
104 'optional' => $parameter->isOptional(),
106 if ($parameter->isDefaultValueAvailable()) {
107 $param->setDefaultValue($parameter->getDefaultValue());
109 $prototype->addParameter($param);
111 $definition->addPrototype($prototype);
113 if (is_object($class)) {
114 $definition->setObject($class);
116 $this->table
->addMethod($definition);
123 * @param Method\Definition $invokable
124 * @param array $params
127 protected function _dispatch(Method\Definition
$invokable, array $params)
129 $callback = $invokable->getCallback();
130 $type = $callback->getType();
132 if ('function' == $type) {
133 $function = $callback->getFunction();
134 return call_user_func_array($function, $params);
137 $class = $callback->getClass();
138 $method = $callback->getMethod();
140 if ('static' == $type) {
141 return call_user_func_array(array($class, $method), $params);
144 $object = $invokable->getObject();
145 if (!is_object($object)) {
146 $invokeArgs = $invokable->getInvokeArguments();
147 if (!empty($invokeArgs)) {
148 $reflection = new ReflectionClass($class);
149 $object = $reflection->newInstanceArgs($invokeArgs);
151 $object = new $class;
154 return call_user_func_array(array($object, $method), $params);
158 * Map PHP type to protocol type
160 * @param string $type
163 abstract protected function _fixType($type);