/library/Zend/Server/AbstractServer.php

https://github.com/zucchi/zf2 · PHP · 168 lines · 91 code · 17 blank · 60 comment · 12 complexity · 59c59f8d5555c53a583922fce5982c7d MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Server
  9. */
  10. namespace Zend\Server;
  11. use ReflectionClass;
  12. /**
  13. * Abstract Server implementation
  14. *
  15. * @category Zend
  16. * @package Zend_Server
  17. */
  18. abstract class AbstractServer implements Server
  19. {
  20. /**
  21. * @var bool Flag; whether or not overwriting existing methods is allowed
  22. */
  23. protected $overwriteExistingMethods = false;
  24. /**
  25. * @var Definition
  26. */
  27. protected $table;
  28. /**
  29. * Constructor
  30. *
  31. * Setup server description
  32. *
  33. */
  34. public function __construct()
  35. {
  36. $this->table = new Definition();
  37. $this->table->setOverwriteExistingMethods($this->overwriteExistingMethods);
  38. }
  39. /**
  40. * Returns a list of registered methods
  41. *
  42. * Returns an array of method definitions.
  43. *
  44. * @return Definition
  45. */
  46. public function getFunctions()
  47. {
  48. return $this->table;
  49. }
  50. /**
  51. * Build callback for method signature
  52. *
  53. * @param Reflection\AbstractFunction $reflection
  54. * @return Method\Callback
  55. */
  56. protected function _buildCallback(Reflection\AbstractFunction $reflection)
  57. {
  58. $callback = new Method\Callback();
  59. if ($reflection instanceof Reflection\ReflectionMethod) {
  60. $callback->setType($reflection->isStatic() ? 'static' : 'instance')
  61. ->setClass($reflection->getDeclaringClass()->getName())
  62. ->setMethod($reflection->getName());
  63. } elseif ($reflection instanceof Reflection\ReflectionFunction) {
  64. $callback->setType('function')
  65. ->setFunction($reflection->getName());
  66. }
  67. return $callback;
  68. }
  69. /**
  70. * Build a method signature
  71. *
  72. * @param Reflection\AbstractFunction $reflection
  73. * @param null|string|object $class
  74. * @return Method\Definition
  75. * @throws Exception\RuntimeException on duplicate entry
  76. */
  77. protected function _buildSignature(Reflection\AbstractFunction $reflection, $class = null)
  78. {
  79. $ns = $reflection->getNamespace();
  80. $name = $reflection->getName();
  81. $method = empty($ns) ? $name : $ns . '.' . $name;
  82. if (!$this->overwriteExistingMethods && $this->table->hasMethod($method)) {
  83. throw new Exception\RuntimeException('Duplicate method registered: ' . $method);
  84. }
  85. $definition = new Method\Definition();
  86. $definition->setName($method)
  87. ->setCallback($this->_buildCallback($reflection))
  88. ->setMethodHelp($reflection->getDescription())
  89. ->setInvokeArguments($reflection->getInvokeArguments());
  90. foreach ($reflection->getPrototypes() as $proto) {
  91. $prototype = new Method\Prototype();
  92. $prototype->setReturnType($this->_fixType($proto->getReturnType()));
  93. foreach ($proto->getParameters() as $parameter) {
  94. $param = new Method\Parameter(array(
  95. 'type' => $this->_fixType($parameter->getType()),
  96. 'name' => $parameter->getName(),
  97. 'optional' => $parameter->isOptional(),
  98. ));
  99. if ($parameter->isDefaultValueAvailable()) {
  100. $param->setDefaultValue($parameter->getDefaultValue());
  101. }
  102. $prototype->addParameter($param);
  103. }
  104. $definition->addPrototype($prototype);
  105. }
  106. if (is_object($class)) {
  107. $definition->setObject($class);
  108. }
  109. $this->table->addMethod($definition);
  110. return $definition;
  111. }
  112. /**
  113. * Dispatch method
  114. *
  115. * @param Method\Definition $invocable
  116. * @param array $params
  117. * @return mixed
  118. */
  119. protected function _dispatch(Method\Definition $invocable, array $params)
  120. {
  121. $callback = $invocable->getCallback();
  122. $type = $callback->getType();
  123. if ('function' == $type) {
  124. $function = $callback->getFunction();
  125. return call_user_func_array($function, $params);
  126. }
  127. $class = $callback->getClass();
  128. $method = $callback->getMethod();
  129. if ('static' == $type) {
  130. return call_user_func_array(array($class, $method), $params);
  131. }
  132. $object = $invocable->getObject();
  133. if (!is_object($object)) {
  134. $invokeArgs = $invocable->getInvokeArguments();
  135. if (!empty($invokeArgs)) {
  136. $reflection = new ReflectionClass($class);
  137. $object = $reflection->newInstanceArgs($invokeArgs);
  138. } else {
  139. $object = new $class;
  140. }
  141. }
  142. return call_user_func_array(array($object, $method), $params);
  143. }
  144. /**
  145. * Map PHP type to protocol type
  146. *
  147. * @param string $type
  148. * @return string
  149. */
  150. abstract protected function _fixType($type);
  151. }