PageRenderTime 253ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Server/Abstract.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 242 lines | 115 code | 23 blank | 104 comment | 12 complexity | d095cee74c00857722a1cf4810722442 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Server_Interface */
  21. require_once 'Zend/Server/Interface.php';
  22. /**
  23. * Zend_Server_Definition
  24. */
  25. require_once 'Zend/Server/Definition.php';
  26. /**
  27. * Zend_Server_Method_Definition
  28. */
  29. require_once 'Zend/Server/Method/Definition.php';
  30. /**
  31. * Zend_Server_Method_Callback
  32. */
  33. require_once 'Zend/Server/Method/Callback.php';
  34. /**
  35. * Zend_Server_Method_Prototype
  36. */
  37. require_once 'Zend/Server/Method/Prototype.php';
  38. /**
  39. * Zend_Server_Method_Parameter
  40. */
  41. require_once 'Zend/Server/Method/Parameter.php';
  42. /**
  43. * Zend_Server_Abstract
  44. *
  45. * @category Zend
  46. * @package Zend_Server
  47. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  48. * @license http://framework.zend.com/license/new-bsd New BSD License
  49. * @version $Id: Abstract.php 23775 2011-03-01 17:25:24Z ralph $
  50. */
  51. abstract class Zend_Server_Abstract implements Zend_Server_Interface
  52. {
  53. /**
  54. * @deprecated
  55. * @var array List of PHP magic methods (lowercased)
  56. */
  57. protected static $magic_methods = array(
  58. '__call',
  59. '__clone',
  60. '__construct',
  61. '__destruct',
  62. '__get',
  63. '__isset',
  64. '__set',
  65. '__set_state',
  66. '__sleep',
  67. '__tostring',
  68. '__unset',
  69. '__wakeup',
  70. );
  71. /**
  72. * @var bool Flag; whether or not overwriting existing methods is allowed
  73. */
  74. protected $_overwriteExistingMethods = false;
  75. /**
  76. * @var Zend_Server_Definition
  77. */
  78. protected $_table;
  79. /**
  80. * Constructor
  81. *
  82. * Setup server description
  83. *
  84. * @return void
  85. */
  86. public function __construct()
  87. {
  88. $this->_table = new Zend_Server_Definition();
  89. $this->_table->setOverwriteExistingMethods($this->_overwriteExistingMethods);
  90. }
  91. /**
  92. * Returns a list of registered methods
  93. *
  94. * Returns an array of method definitions.
  95. *
  96. * @return Zend_Server_Definition
  97. */
  98. public function getFunctions()
  99. {
  100. return $this->_table;
  101. }
  102. /**
  103. * Lowercase a string
  104. *
  105. * Lowercase's a string by reference
  106. *
  107. * @deprecated
  108. * @param string $string value
  109. * @param string $key
  110. * @return string Lower cased string
  111. */
  112. public static function lowerCase(&$value, &$key)
  113. {
  114. trigger_error(__CLASS__ . '::' . __METHOD__ . '() is deprecated and will be removed in a future version', E_USER_NOTICE);
  115. return $value = strtolower($value);
  116. }
  117. /**
  118. * Build callback for method signature
  119. *
  120. * @param Zend_Server_Reflection_Function_Abstract $reflection
  121. * @return Zend_Server_Method_Callback
  122. */
  123. protected function _buildCallback(Zend_Server_Reflection_Function_Abstract $reflection)
  124. {
  125. $callback = new Zend_Server_Method_Callback();
  126. if ($reflection instanceof Zend_Server_Reflection_Method) {
  127. $callback->setType($reflection->isStatic() ? 'static' : 'instance')
  128. ->setClass($reflection->getDeclaringClass()->getName())
  129. ->setMethod($reflection->getName());
  130. } elseif ($reflection instanceof Zend_Server_Reflection_Function) {
  131. $callback->setType('function')
  132. ->setFunction($reflection->getName());
  133. }
  134. return $callback;
  135. }
  136. /**
  137. * Build a method signature
  138. *
  139. * @param Zend_Server_Reflection_Function_Abstract $reflection
  140. * @param null|string|object $class
  141. * @return Zend_Server_Method_Definition
  142. * @throws Zend_Server_Exception on duplicate entry
  143. */
  144. protected function _buildSignature(Zend_Server_Reflection_Function_Abstract $reflection, $class = null)
  145. {
  146. $ns = $reflection->getNamespace();
  147. $name = $reflection->getName();
  148. $method = empty($ns) ? $name : $ns . '.' . $name;
  149. if (!$this->_overwriteExistingMethods && $this->_table->hasMethod($method)) {
  150. require_once 'Zend/Server/Exception.php';
  151. throw new Zend_Server_Exception('Duplicate method registered: ' . $method);
  152. }
  153. $definition = new Zend_Server_Method_Definition();
  154. $definition->setName($method)
  155. ->setCallback($this->_buildCallback($reflection))
  156. ->setMethodHelp($reflection->getDescription())
  157. ->setInvokeArguments($reflection->getInvokeArguments());
  158. foreach ($reflection->getPrototypes() as $proto) {
  159. $prototype = new Zend_Server_Method_Prototype();
  160. $prototype->setReturnType($this->_fixType($proto->getReturnType()));
  161. foreach ($proto->getParameters() as $parameter) {
  162. $param = new Zend_Server_Method_Parameter(array(
  163. 'type' => $this->_fixType($parameter->getType()),
  164. 'name' => $parameter->getName(),
  165. 'optional' => $parameter->isOptional(),
  166. ));
  167. if ($parameter->isDefaultValueAvailable()) {
  168. $param->setDefaultValue($parameter->getDefaultValue());
  169. }
  170. $prototype->addParameter($param);
  171. }
  172. $definition->addPrototype($prototype);
  173. }
  174. if (is_object($class)) {
  175. $definition->setObject($class);
  176. }
  177. $this->_table->addMethod($definition);
  178. return $definition;
  179. }
  180. /**
  181. * Dispatch method
  182. *
  183. * @param Zend_Server_Method_Definition $invocable
  184. * @param array $params
  185. * @return mixed
  186. */
  187. protected function _dispatch(Zend_Server_Method_Definition $invocable, array $params)
  188. {
  189. $callback = $invocable->getCallback();
  190. $type = $callback->getType();
  191. if ('function' == $type) {
  192. $function = $callback->getFunction();
  193. return call_user_func_array($function, $params);
  194. }
  195. $class = $callback->getClass();
  196. $method = $callback->getMethod();
  197. if ('static' == $type) {
  198. return call_user_func_array(array($class, $method), $params);
  199. }
  200. $object = $invocable->getObject();
  201. if (!is_object($object)) {
  202. $invokeArgs = $invocable->getInvokeArguments();
  203. if (!empty($invokeArgs)) {
  204. $reflection = new ReflectionClass($class);
  205. $object = $reflection->newInstanceArgs($invokeArgs);
  206. } else {
  207. $object = new $class;
  208. }
  209. }
  210. return call_user_func_array(array($object, $method), $params);
  211. }
  212. /**
  213. * Map PHP type to protocol type
  214. *
  215. * @param string $type
  216. * @return string
  217. */
  218. abstract protected function _fixType($type);
  219. }