/library/Zend/Server/AbstractServer.php

https://github.com/jtai/zf2 · PHP · 219 lines · 110 code · 19 blank · 90 comment · 12 complexity · 11fa99d61b3ede585765bf5aee8a4060 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Server
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Server;
  24. use ReflectionClass;
  25. /**
  26. * Abstract Server implementation
  27. *
  28. * @category Zend
  29. * @package Zend_Server
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class AbstractServer implements Server
  34. {
  35. /**
  36. * @deprecated
  37. * @var array List of PHP magic methods (lowercased)
  38. */
  39. protected static $magic_methods = array(
  40. '__call',
  41. '__clone',
  42. '__construct',
  43. '__destruct',
  44. '__get',
  45. '__isset',
  46. '__set',
  47. '__set_state',
  48. '__sleep',
  49. '__tostring',
  50. '__unset',
  51. '__wakeup',
  52. );
  53. /**
  54. * @var bool Flag; whether or not overwriting existing methods is allowed
  55. */
  56. protected $overwriteExistingMethods = false;
  57. /**
  58. * @var Definition
  59. */
  60. protected $table;
  61. /**
  62. * Constructor
  63. *
  64. * Setup server description
  65. *
  66. * @return void
  67. */
  68. public function __construct()
  69. {
  70. $this->table = new Definition();
  71. $this->table->setOverwriteExistingMethods($this->overwriteExistingMethods);
  72. }
  73. /**
  74. * Returns a list of registered methods
  75. *
  76. * Returns an array of method definitions.
  77. *
  78. * @return Definition
  79. */
  80. public function getFunctions()
  81. {
  82. return $this->table;
  83. }
  84. /**
  85. * Lowercase a string
  86. *
  87. * Lowercase's a string by reference
  88. *
  89. * @deprecated
  90. * @param string $string value
  91. * @param string $key
  92. * @return string Lower cased string
  93. */
  94. public static function lowerCase(&$value, &$key)
  95. {
  96. trigger_error(__CLASS__ . '::' . __METHOD__ . '() is deprecated and will be removed in a future version', E_USER_NOTICE);
  97. return $value = strtolower($value);
  98. }
  99. /**
  100. * Build callback for method signature
  101. *
  102. * @param Reflection\AbstractFunction $reflection
  103. * @return Method\Callback
  104. */
  105. protected function _buildCallback(Reflection\AbstractFunction $reflection)
  106. {
  107. $callback = new Method\Callback();
  108. if ($reflection instanceof Reflection\ReflectionMethod) {
  109. $callback->setType($reflection->isStatic() ? 'static' : 'instance')
  110. ->setClass($reflection->getDeclaringClass()->getName())
  111. ->setMethod($reflection->getName());
  112. } elseif ($reflection instanceof Reflection\ReflectionFunction) {
  113. $callback->setType('function')
  114. ->setFunction($reflection->getName());
  115. }
  116. return $callback;
  117. }
  118. /**
  119. * Build a method signature
  120. *
  121. * @param Reflection\AbstractFunction $reflection
  122. * @param null|string|object $class
  123. * @return Method\Definition
  124. * @throws Exception on duplicate entry
  125. */
  126. protected function _buildSignature(Reflection\AbstractFunction $reflection, $class = null)
  127. {
  128. $ns = $reflection->getNamespace();
  129. $name = $reflection->getName();
  130. $method = empty($ns) ? $name : $ns . '.' . $name;
  131. if (!$this->overwriteExistingMethods && $this->table->hasMethod($method)) {
  132. throw new Exception\RuntimeException('Duplicate method registered: ' . $method);
  133. }
  134. $definition = new Method\Definition();
  135. $definition->setName($method)
  136. ->setCallback($this->_buildCallback($reflection))
  137. ->setMethodHelp($reflection->getDescription())
  138. ->setInvokeArguments($reflection->getInvokeArguments());
  139. foreach ($reflection->getPrototypes() as $proto) {
  140. $prototype = new Method\Prototype();
  141. $prototype->setReturnType($this->_fixType($proto->getReturnType()));
  142. foreach ($proto->getParameters() as $parameter) {
  143. $param = new Method\Parameter(array(
  144. 'type' => $this->_fixType($parameter->getType()),
  145. 'name' => $parameter->getName(),
  146. 'optional' => $parameter->isOptional(),
  147. ));
  148. if ($parameter->isDefaultValueAvailable()) {
  149. $param->setDefaultValue($parameter->getDefaultValue());
  150. }
  151. $prototype->addParameter($param);
  152. }
  153. $definition->addPrototype($prototype);
  154. }
  155. if (is_object($class)) {
  156. $definition->setObject($class);
  157. }
  158. $this->table->addMethod($definition);
  159. return $definition;
  160. }
  161. /**
  162. * Dispatch method
  163. *
  164. * @param Method\Definition $invocable
  165. * @param array $params
  166. * @return mixed
  167. */
  168. protected function _dispatch(Method\Definition $invocable, array $params)
  169. {
  170. $callback = $invocable->getCallback();
  171. $type = $callback->getType();
  172. if ('function' == $type) {
  173. $function = $callback->getFunction();
  174. return call_user_func_array($function, $params);
  175. }
  176. $class = $callback->getClass();
  177. $method = $callback->getMethod();
  178. if ('static' == $type) {
  179. return call_user_func_array(array($class, $method), $params);
  180. }
  181. $object = $invocable->getObject();
  182. if (!is_object($object)) {
  183. $invokeArgs = $invocable->getInvokeArguments();
  184. if (!empty($invokeArgs)) {
  185. $reflection = new ReflectionClass($class);
  186. $object = $reflection->newInstanceArgs($invokeArgs);
  187. } else {
  188. $object = new $class;
  189. }
  190. }
  191. return call_user_func_array(array($object, $method), $params);
  192. }
  193. /**
  194. * Map PHP type to protocol type
  195. *
  196. * @param string $type
  197. * @return string
  198. */
  199. abstract protected function _fixType($type);
  200. }