3 * @see https://github.com/zendframework/zend-server for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-server/blob/master/LICENSE.md New BSD License
13 * Abstract Server implementation
15 abstract class AbstractServer
implements Server
18 * @var bool Flag; whether or not overwriting existing methods is allowed
20 protected $overwriteExistingMethods = false;
30 * Setup server description
33 public function __construct()
35 $this->table
= new Definition();
36 $this->table
->setOverwriteExistingMethods($this->overwriteExistingMethods
);
40 * Returns a list of registered methods
42 * Returns an array of method definitions.
46 public function getFunctions()
52 * Build callback for method signature
54 * @deprecated Since 2.7.0; method will have private visibility starting in 3.0.
55 * @param Reflection\AbstractFunction $reflection
56 * @return Method\Callback
58 // @codingStandardsIgnoreStart
59 protected function _buildCallback(Reflection\AbstractFunction
$reflection)
61 // @codingStandardsIgnoreEnd
62 $callback = new Method\
Callback();
63 if ($reflection instanceof Reflection\ReflectionMethod
) {
64 $callback->setType($reflection->isStatic() ?
'static' : 'instance')
65 ->setClass($reflection->getDeclaringClass()->getName())
66 ->setMethod($reflection->getName());
67 } elseif ($reflection instanceof Reflection\ReflectionFunction
) {
68 $callback->setType('function')
69 ->setFunction($reflection->getName());
75 * Build a method signature
77 * @deprecated Since 2.7.0; method will be renamed to remove underscore
79 * @param Reflection\AbstractFunction $reflection
80 * @param null|string|object $class
81 * @return Method\Definition
82 * @throws Exception\RuntimeException on duplicate entry
84 // @codingStandardsIgnoreStart
85 protected function _buildSignature(Reflection\AbstractFunction
$reflection, $class = null)
87 // @codingStandardsIgnoreEnd
88 $ns = $reflection->getNamespace();
89 $name = $reflection->getName();
90 $method = empty($ns) ?
$name : $ns . '.' . $name;
92 if (! $this->overwriteExistingMethods
&& $this->table
->hasMethod($method)) {
93 throw new Exception\
RuntimeException('Duplicate method registered: ' . $method);
96 $definition = new Method\
Definition();
97 $definition->setName($method)
98 ->setCallback($this->_buildCallback($reflection))
99 ->setMethodHelp($reflection->getDescription())
100 ->setInvokeArguments($reflection->getInvokeArguments());
102 foreach ($reflection->getPrototypes() as $proto) {
103 $prototype = new Method\
Prototype();
104 $prototype->setReturnType($this->_fixType($proto->getReturnType()));
105 foreach ($proto->getParameters() as $parameter) {
106 $param = new Method\
Parameter([
107 'type' => $this->_fixType($parameter->getType()),
108 'name' => $parameter->getName(),
109 'optional' => $parameter->isOptional(),
111 if ($parameter->isDefaultValueAvailable()) {
112 $param->setDefaultValue($parameter->getDefaultValue());
114 $prototype->addParameter($param);
116 $definition->addPrototype($prototype);
118 if (is_object($class)) {
119 $definition->setObject($class);
121 $this->table
->addMethod($definition);
128 * @deprecated Since 2.7.0; method will be renamed to remove underscore
130 * @param Method\Definition $invokable
131 * @param array $params
134 // @codingStandardsIgnoreStart
135 protected function _dispatch(Method\Definition
$invokable, array $params)
137 // @codingStandardsIgnoreEnd
138 $callback = $invokable->getCallback();
139 $type = $callback->getType();
141 if ('function' == $type) {
142 $function = $callback->getFunction();
143 return call_user_func_array($function, $params);
146 $class = $callback->getClass();
147 $method = $callback->getMethod();
149 if ('static' == $type) {
150 return call_user_func_array([$class, $method], $params);
153 $object = $invokable->getObject();
154 if (! is_object($object)) {
155 $invokeArgs = $invokable->getInvokeArguments();
156 if (! empty($invokeArgs)) {
157 $reflection = new ReflectionClass($class);
158 $object = $reflection->newInstanceArgs($invokeArgs);
160 $object = new $class;
163 return call_user_func_array([$object, $method], $params);
166 // @codingStandardsIgnoreStart
168 * Map PHP type to protocol type
170 * @deprecated Since 2.7.0; method will be renamed to remove underscore
172 * @param string $type
175 abstract protected function _fixType($type);
176 // @codingStandardsIgnoreEnd