PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/zend/Zend/Server/Reflection/Function/Abstract.php

http://github.com/moodle/moodle
PHP | 503 lines | 248 code | 53 blank | 202 comment | 36 complexity | f1bd10b3cdd048ec8aae9b8ca659e0c7 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Server_Reflection_Node
  22. */
  23. require_once 'Zend/Server/Reflection/Node.php';
  24. /**
  25. * Zend_Server_Reflection_Parameter
  26. */
  27. require_once 'Zend/Server/Reflection/Parameter.php';
  28. /**
  29. * Zend_Server_Reflection_Prototype
  30. */
  31. require_once 'Zend/Server/Reflection/Prototype.php';
  32. /**
  33. * Function/Method Reflection
  34. *
  35. * Decorates a ReflectionFunction. Allows setting and retrieving an alternate
  36. * 'service' name (i.e., the name to be used when calling via a service),
  37. * setting and retrieving the description (originally set using the docblock
  38. * contents), retrieving the callback and callback type, retrieving additional
  39. * method invocation arguments, and retrieving the
  40. * method {@link Zend_Server_Reflection_Prototype prototypes}.
  41. *
  42. * @category Zend
  43. * @package Zend_Server
  44. * @subpackage Reflection
  45. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. * @version $Id$
  48. */
  49. abstract class Zend_Server_Reflection_Function_Abstract
  50. {
  51. /**
  52. * @var ReflectionFunction
  53. */
  54. protected $_reflection;
  55. /**
  56. * Additional arguments to pass to method on invocation
  57. * @var array
  58. */
  59. protected $_argv = array();
  60. /**
  61. * Used to store extra configuration for the method (typically done by the
  62. * server class, e.g., to indicate whether or not to instantiate a class).
  63. * Associative array; access is as properties via {@link __get()} and
  64. * {@link __set()}
  65. * @var array
  66. */
  67. protected $_config = array();
  68. /**
  69. * Declaring class (needed for when serialization occurs)
  70. * @var string
  71. */
  72. protected $_class;
  73. /**
  74. * Function/method description
  75. * @var string
  76. */
  77. protected $_description = '';
  78. /**
  79. * Namespace with which to prefix function/method name
  80. * @var string
  81. */
  82. protected $_namespace;
  83. /**
  84. * Prototypes
  85. * @var array
  86. */
  87. protected $_prototypes = array();
  88. private $_return;
  89. private $_returnDesc;
  90. private $_paramDesc;
  91. private $_sigParams;
  92. private $_sigParamsDepth;
  93. /**
  94. * Constructor
  95. *
  96. * @param ReflectionFunction $r
  97. */
  98. public function __construct(Reflector $r, $namespace = null, $argv = array())
  99. {
  100. // In PHP 5.1.x, ReflectionMethod extends ReflectionFunction. In 5.2.x,
  101. // both extend ReflectionFunctionAbstract. So, we can't do normal type
  102. // hinting in the prototype, but instead need to do some explicit
  103. // testing here.
  104. if ((!$r instanceof ReflectionFunction)
  105. && (!$r instanceof ReflectionMethod)) {
  106. require_once 'Zend/Server/Reflection/Exception.php';
  107. throw new Zend_Server_Reflection_Exception('Invalid reflection class');
  108. }
  109. $this->_reflection = $r;
  110. // Determine namespace
  111. if (null !== $namespace){
  112. $this->setNamespace($namespace);
  113. }
  114. // Determine arguments
  115. if (is_array($argv)) {
  116. $this->_argv = $argv;
  117. }
  118. // If method call, need to store some info on the class
  119. if ($r instanceof ReflectionMethod) {
  120. $this->_class = $r->getDeclaringClass()->getName();
  121. }
  122. // Perform some introspection
  123. $this->_reflect();
  124. }
  125. /**
  126. * Create signature node tree
  127. *
  128. * Recursive method to build the signature node tree. Increments through
  129. * each array in {@link $_sigParams}, adding every value of the next level
  130. * to the current value (unless the current value is null).
  131. *
  132. * @param Zend_Server_Reflection_Node $parent
  133. * @param int $level
  134. * @return void
  135. */
  136. protected function _addTree(Zend_Server_Reflection_Node $parent, $level = 0)
  137. {
  138. if ($level >= $this->_sigParamsDepth) {
  139. return;
  140. }
  141. foreach ($this->_sigParams[$level] as $value) {
  142. $node = new Zend_Server_Reflection_Node($value, $parent);
  143. if ((null !== $value) && ($this->_sigParamsDepth > $level + 1)) {
  144. $this->_addTree($node, $level + 1);
  145. }
  146. }
  147. }
  148. /**
  149. * Build the signature tree
  150. *
  151. * Builds a signature tree starting at the return values and descending
  152. * through each method argument. Returns an array of
  153. * {@link Zend_Server_Reflection_Node}s.
  154. *
  155. * @return array
  156. */
  157. protected function _buildTree()
  158. {
  159. $returnTree = array();
  160. foreach ((array) $this->_return as $value) {
  161. $node = new Zend_Server_Reflection_Node($value);
  162. $this->_addTree($node);
  163. $returnTree[] = $node;
  164. }
  165. return $returnTree;
  166. }
  167. /**
  168. * Build method signatures
  169. *
  170. * Builds method signatures using the array of return types and the array of
  171. * parameters types
  172. *
  173. * @param array $return Array of return types
  174. * @param string $returnDesc Return value description
  175. * @param array $params Array of arguments (each an array of types)
  176. * @param array $paramDesc Array of parameter descriptions
  177. * @return array
  178. */
  179. protected function _buildSignatures($return, $returnDesc, $paramTypes, $paramDesc)
  180. {
  181. $this->_return = $return;
  182. $this->_returnDesc = $returnDesc;
  183. $this->_paramDesc = $paramDesc;
  184. $this->_sigParams = $paramTypes;
  185. $this->_sigParamsDepth = count($paramTypes);
  186. $signatureTrees = $this->_buildTree();
  187. $signatures = array();
  188. $endPoints = array();
  189. foreach ($signatureTrees as $root) {
  190. $tmp = $root->getEndPoints();
  191. if (empty($tmp)) {
  192. $endPoints = array_merge($endPoints, array($root));
  193. } else {
  194. $endPoints = array_merge($endPoints, $tmp);
  195. }
  196. }
  197. foreach ($endPoints as $node) {
  198. if (!$node instanceof Zend_Server_Reflection_Node) {
  199. continue;
  200. }
  201. $signature = array();
  202. do {
  203. array_unshift($signature, $node->getValue());
  204. $node = $node->getParent();
  205. } while ($node instanceof Zend_Server_Reflection_Node);
  206. $signatures[] = $signature;
  207. }
  208. // Build prototypes
  209. $params = $this->_reflection->getParameters();
  210. foreach ($signatures as $signature) {
  211. $return = new Zend_Server_Reflection_ReturnValue(array_shift($signature), $this->_returnDesc);
  212. $tmp = array();
  213. foreach ($signature as $key => $type) {
  214. $param = new Zend_Server_Reflection_Parameter($params[$key], $type, (isset($this->_paramDesc[$key]) ? $this->_paramDesc[$key] : null));
  215. $param->setPosition($key);
  216. $tmp[] = $param;
  217. }
  218. $this->_prototypes[] = new Zend_Server_Reflection_Prototype($return, $tmp);
  219. }
  220. }
  221. /**
  222. * Use code reflection to create method signatures
  223. *
  224. * Determines the method help/description text from the function DocBlock
  225. * comment. Determines method signatures using a combination of
  226. * ReflectionFunction and parsing of DocBlock @param and @return values.
  227. *
  228. * @param ReflectionFunction $function
  229. * @return array
  230. */
  231. protected function _reflect()
  232. {
  233. $function = $this->_reflection;
  234. $helpText = '';
  235. $signatures = array();
  236. $returnDesc = '';
  237. $paramCount = $function->getNumberOfParameters();
  238. $paramCountRequired = $function->getNumberOfRequiredParameters();
  239. $parameters = $function->getParameters();
  240. $docBlock = $function->getDocComment();
  241. if (!empty($docBlock)) {
  242. // Get help text
  243. if (preg_match(':/\*\*\s*\r?\n\s*\*\s(.*?)\r?\n\s*\*(\s@|/):s', $docBlock, $matches))
  244. {
  245. $helpText = $matches[1];
  246. $helpText = preg_replace('/(^\s*\*\s)/m', '', $helpText);
  247. $helpText = preg_replace('/\r?\n\s*\*\s*(\r?\n)*/s', "\n", $helpText);
  248. $helpText = trim($helpText);
  249. }
  250. // Get return type(s) and description
  251. $return = 'void';
  252. if (preg_match('/@return\s+(\S+)/', $docBlock, $matches)) {
  253. $return = explode('|', $matches[1]);
  254. if (preg_match('/@return\s+\S+\s+(.*?)(@|\*\/)/s', $docBlock, $matches))
  255. {
  256. $value = $matches[1];
  257. $value = preg_replace('/\s?\*\s/m', '', $value);
  258. $value = preg_replace('/\s{2,}/', ' ', $value);
  259. $returnDesc = trim($value);
  260. }
  261. }
  262. // Get param types and description
  263. if (preg_match_all('/@param\s+([^\s]+)/m', $docBlock, $matches)) {
  264. $paramTypesTmp = $matches[1];
  265. if (preg_match_all('/@param\s+\S+\s+(\$\S+)\s+(.*?)(@|\*\/)/s', $docBlock, $matches))
  266. {
  267. $paramDesc = $matches[2];
  268. foreach ($paramDesc as $key => $value) {
  269. $value = preg_replace('/\s?\*\s/m', '', $value);
  270. $value = preg_replace('/\s{2,}/', ' ', $value);
  271. $paramDesc[$key] = trim($value);
  272. }
  273. }
  274. }
  275. } else {
  276. $helpText = $function->getName();
  277. $return = 'void';
  278. }
  279. // Set method description
  280. $this->setDescription($helpText);
  281. // Get all param types as arrays
  282. if (!isset($paramTypesTmp) && (0 < $paramCount)) {
  283. $paramTypesTmp = array_fill(0, $paramCount, 'mixed');
  284. } elseif (!isset($paramTypesTmp)) {
  285. $paramTypesTmp = array();
  286. } elseif (count($paramTypesTmp) < $paramCount) {
  287. $start = $paramCount - count($paramTypesTmp);
  288. for ($i = $start; $i < $paramCount; ++$i) {
  289. $paramTypesTmp[$i] = 'mixed';
  290. }
  291. }
  292. // Get all param descriptions as arrays
  293. if (!isset($paramDesc) && (0 < $paramCount)) {
  294. $paramDesc = array_fill(0, $paramCount, '');
  295. } elseif (!isset($paramDesc)) {
  296. $paramDesc = array();
  297. } elseif (count($paramDesc) < $paramCount) {
  298. $start = $paramCount - count($paramDesc);
  299. for ($i = $start; $i < $paramCount; ++$i) {
  300. $paramDesc[$i] = '';
  301. }
  302. }
  303. if (count($paramTypesTmp) != $paramCount) {
  304. require_once 'Zend/Server/Reflection/Exception.php';
  305. throw new Zend_Server_Reflection_Exception(
  306. 'Variable number of arguments is not supported for services (except optional parameters). '
  307. . 'Number of function arguments must correspond to actual number of arguments described in a docblock '
  308. . '(function was ' . $function->getName() . ')');
  309. }
  310. $paramTypes = array();
  311. foreach ($paramTypesTmp as $i => $param) {
  312. $tmp = explode('|', $param);
  313. if ($parameters[$i]->isOptional()) {
  314. array_unshift($tmp, null);
  315. }
  316. $paramTypes[] = $tmp;
  317. }
  318. $this->_buildSignatures($return, $returnDesc, $paramTypes, $paramDesc);
  319. }
  320. /**
  321. * Proxy reflection calls
  322. *
  323. * @param string $method
  324. * @param array $args
  325. * @return mixed
  326. */
  327. public function __call($method, $args)
  328. {
  329. if (method_exists($this->_reflection, $method)) {
  330. return call_user_func_array(array($this->_reflection, $method), $args);
  331. }
  332. require_once 'Zend/Server/Reflection/Exception.php';
  333. throw new Zend_Server_Reflection_Exception('Invalid reflection method ("' .$method. '")');
  334. }
  335. /**
  336. * Retrieve configuration parameters
  337. *
  338. * Values are retrieved by key from {@link $_config}. Returns null if no
  339. * value found.
  340. *
  341. * @param string $key
  342. * @return mixed
  343. */
  344. public function __get($key)
  345. {
  346. if (isset($this->_config[$key])) {
  347. return $this->_config[$key];
  348. }
  349. return null;
  350. }
  351. /**
  352. * Set configuration parameters
  353. *
  354. * Values are stored by $key in {@link $_config}.
  355. *
  356. * @param string $key
  357. * @param mixed $value
  358. * @return void
  359. */
  360. public function __set($key, $value)
  361. {
  362. $this->_config[$key] = $value;
  363. }
  364. /**
  365. * Set method's namespace
  366. *
  367. * @param string $namespace
  368. * @return void
  369. */
  370. public function setNamespace($namespace)
  371. {
  372. if (empty($namespace)) {
  373. $this->_namespace = '';
  374. return;
  375. }
  376. if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) {
  377. require_once 'Zend/Server/Reflection/Exception.php';
  378. throw new Zend_Server_Reflection_Exception('Invalid namespace');
  379. }
  380. $this->_namespace = $namespace;
  381. }
  382. /**
  383. * Return method's namespace
  384. *
  385. * @return string
  386. */
  387. public function getNamespace()
  388. {
  389. return $this->_namespace;
  390. }
  391. /**
  392. * Set the description
  393. *
  394. * @param string $string
  395. * @return void
  396. */
  397. public function setDescription($string)
  398. {
  399. if (!is_string($string)) {
  400. require_once 'Zend/Server/Reflection/Exception.php';
  401. throw new Zend_Server_Reflection_Exception('Invalid description');
  402. }
  403. $this->_description = $string;
  404. }
  405. /**
  406. * Retrieve the description
  407. *
  408. * @return void
  409. */
  410. public function getDescription()
  411. {
  412. return $this->_description;
  413. }
  414. /**
  415. * Retrieve all prototypes as array of
  416. * {@link Zend_Server_Reflection_Prototype Zend_Server_Reflection_Prototypes}
  417. *
  418. * @return array
  419. */
  420. public function getPrototypes()
  421. {
  422. return $this->_prototypes;
  423. }
  424. /**
  425. * Retrieve additional invocation arguments
  426. *
  427. * @return array
  428. */
  429. public function getInvokeArguments()
  430. {
  431. return $this->_argv;
  432. }
  433. /**
  434. * Wakeup from serialization
  435. *
  436. * Reflection needs explicit instantiation to work correctly. Re-instantiate
  437. * reflection object on wakeup.
  438. *
  439. * @return void
  440. */
  441. public function __wakeup()
  442. {
  443. if ($this->_reflection instanceof ReflectionMethod) {
  444. $class = new ReflectionClass($this->_class);
  445. $this->_reflection = new ReflectionMethod($class->newInstance(), $this->getName());
  446. } else {
  447. $this->_reflection = new ReflectionFunction($this->getName());
  448. }
  449. }
  450. }