composer package updates
[openemr.git] / vendor / zendframework / zend-server / src / AbstractServer.php
blobd0c4540b2639c510d96e9dff3459023bdb758848
1 <?php
2 /**
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
6 */
8 namespace Zend\Server;
10 use ReflectionClass;
12 /**
13 * Abstract Server implementation
15 abstract class AbstractServer implements Server
17 /**
18 * @var bool Flag; whether or not overwriting existing methods is allowed
20 protected $overwriteExistingMethods = false;
22 /**
23 * @var Definition
25 protected $table;
27 /**
28 * Constructor
30 * Setup server description
33 public function __construct()
35 $this->table = new Definition();
36 $this->table->setOverwriteExistingMethods($this->overwriteExistingMethods);
39 /**
40 * Returns a list of registered methods
42 * Returns an array of method definitions.
44 * @return Definition
46 public function getFunctions()
48 return $this->table;
51 /**
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());
71 return $callback;
74 /**
75 * Build a method signature
77 * @deprecated Since 2.7.0; method will be renamed to remove underscore
78 * prefix in 3.0.
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);
122 return $definition;
126 * Dispatch method
128 * @deprecated Since 2.7.0; method will be renamed to remove underscore
129 * prefix in 3.0.
130 * @param Method\Definition $invokable
131 * @param array $params
132 * @return mixed
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);
159 } else {
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
171 * prefix in 3.0.
172 * @param string $type
173 * @return string
175 abstract protected function _fixType($type);
176 // @codingStandardsIgnoreEnd