PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 514 lines | 257 code | 54 blank | 203 comment | 37 complexity | d8bbca7c42563a555a9342d370262bd1 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. /**
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  46. * @license http://framework.zend.com/license/new-bsd New BSD License
  47. * @version $Id: Abstract.php 23775 2011-03-01 17:25:24Z ralph $
  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. // Try and auto-determine type, based on reflection
  279. $paramTypesTmp = array();
  280. foreach ($parameters as $i => $param) {
  281. $paramType = 'mixed';
  282. if ($param->isArray()) {
  283. $paramType = 'array';
  284. }
  285. $paramTypesTmp[$i] = $paramType;
  286. }
  287. }
  288. // Set method description
  289. $this->setDescription($helpText);
  290. // Get all param types as arrays
  291. if (!isset($paramTypesTmp) && (0 < $paramCount)) {
  292. $paramTypesTmp = array_fill(0, $paramCount, 'mixed');
  293. } elseif (!isset($paramTypesTmp)) {
  294. $paramTypesTmp = array();
  295. } elseif (count($paramTypesTmp) < $paramCount) {
  296. $start = $paramCount - count($paramTypesTmp);
  297. for ($i = $start; $i < $paramCount; ++$i) {
  298. $paramTypesTmp[$i] = 'mixed';
  299. }
  300. }
  301. // Get all param descriptions as arrays
  302. if (!isset($paramDesc) && (0 < $paramCount)) {
  303. $paramDesc = array_fill(0, $paramCount, '');
  304. } elseif (!isset($paramDesc)) {
  305. $paramDesc = array();
  306. } elseif (count($paramDesc) < $paramCount) {
  307. $start = $paramCount - count($paramDesc);
  308. for ($i = $start; $i < $paramCount; ++$i) {
  309. $paramDesc[$i] = '';
  310. }
  311. }
  312. if (count($paramTypesTmp) != $paramCount) {
  313. require_once 'Zend/Server/Reflection/Exception.php';
  314. throw new Zend_Server_Reflection_Exception(
  315. 'Variable number of arguments is not supported for services (except optional parameters). '
  316. . 'Number of function arguments in ' . $function->getDeclaringClass()->getName() . '::'
  317. . $function->getName() . '() must correspond to actual number of arguments described in the '
  318. . 'docblock.');
  319. }
  320. $paramTypes = array();
  321. foreach ($paramTypesTmp as $i => $param) {
  322. $tmp = explode('|', $param);
  323. if ($parameters[$i]->isOptional()) {
  324. array_unshift($tmp, null);
  325. }
  326. $paramTypes[] = $tmp;
  327. }
  328. $this->_buildSignatures($return, $returnDesc, $paramTypes, $paramDesc);
  329. }
  330. /**
  331. * Proxy reflection calls
  332. *
  333. * @param string $method
  334. * @param array $args
  335. * @return mixed
  336. */
  337. public function __call($method, $args)
  338. {
  339. if (method_exists($this->_reflection, $method)) {
  340. return call_user_func_array(array($this->_reflection, $method), $args);
  341. }
  342. require_once 'Zend/Server/Reflection/Exception.php';
  343. throw new Zend_Server_Reflection_Exception('Invalid reflection method ("' .$method. '")');
  344. }
  345. /**
  346. * Retrieve configuration parameters
  347. *
  348. * Values are retrieved by key from {@link $_config}. Returns null if no
  349. * value found.
  350. *
  351. * @param string $key
  352. * @return mixed
  353. */
  354. public function __get($key)
  355. {
  356. if (isset($this->_config[$key])) {
  357. return $this->_config[$key];
  358. }
  359. return null;
  360. }
  361. /**
  362. * Set configuration parameters
  363. *
  364. * Values are stored by $key in {@link $_config}.
  365. *
  366. * @param string $key
  367. * @param mixed $value
  368. * @return void
  369. */
  370. public function __set($key, $value)
  371. {
  372. $this->_config[$key] = $value;
  373. }
  374. /**
  375. * Set method's namespace
  376. *
  377. * @param string $namespace
  378. * @return void
  379. */
  380. public function setNamespace($namespace)
  381. {
  382. if (empty($namespace)) {
  383. $this->_namespace = '';
  384. return;
  385. }
  386. if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) {
  387. require_once 'Zend/Server/Reflection/Exception.php';
  388. throw new Zend_Server_Reflection_Exception('Invalid namespace');
  389. }
  390. $this->_namespace = $namespace;
  391. }
  392. /**
  393. * Return method's namespace
  394. *
  395. * @return string
  396. */
  397. public function getNamespace()
  398. {
  399. return $this->_namespace;
  400. }
  401. /**
  402. * Set the description
  403. *
  404. * @param string $string
  405. * @return void
  406. */
  407. public function setDescription($string)
  408. {
  409. if (!is_string($string)) {
  410. require_once 'Zend/Server/Reflection/Exception.php';
  411. throw new Zend_Server_Reflection_Exception('Invalid description');
  412. }
  413. $this->_description = $string;
  414. }
  415. /**
  416. * Retrieve the description
  417. *
  418. * @return void
  419. */
  420. public function getDescription()
  421. {
  422. return $this->_description;
  423. }
  424. /**
  425. * Retrieve all prototypes as array of
  426. * {@link Zend_Server_Reflection_Prototype Zend_Server_Reflection_Prototypes}
  427. *
  428. * @return array
  429. */
  430. public function getPrototypes()
  431. {
  432. return $this->_prototypes;
  433. }
  434. /**
  435. * Retrieve additional invocation arguments
  436. *
  437. * @return array
  438. */
  439. public function getInvokeArguments()
  440. {
  441. return $this->_argv;
  442. }
  443. /**
  444. * Wakeup from serialization
  445. *
  446. * Reflection needs explicit instantiation to work correctly. Re-instantiate
  447. * reflection object on wakeup.
  448. *
  449. * @return void
  450. */
  451. public function __wakeup()
  452. {
  453. if ($this->_reflection instanceof ReflectionMethod) {
  454. $class = new ReflectionClass($this->_class);
  455. $this->_reflection = new ReflectionMethod($class->newInstance(), $this->getName());
  456. } else {
  457. $this->_reflection = new ReflectionFunction($this->getName());
  458. }
  459. }
  460. }