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

/library/Zend/Server/Reflection/Function/Abstract.php

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