PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Server/Reflection/Function/Abstract.php

https://github.com/sluther/portsensor
PHP | 497 lines | 238 code | 54 blank | 205 comment | 34 complexity | 1a4bc7bef34a7a83a11e097c1041c6a0 MD5 | raw file
Possible License(s): 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_Controller
  17. * @copyright Copyright (c) 2005-2007 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-2007 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 5756 2007-07-18 21:07:42Z thomas $
  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. $paramTypes = array();
  307. foreach ($paramTypesTmp as $i => $param) {
  308. $tmp = explode('|', $param);
  309. if ($parameters[$i]->isOptional()) {
  310. array_unshift($tmp, null);
  311. }
  312. $paramTypes[] = $tmp;
  313. }
  314. $this->_buildSignatures($return, $returnDesc, $paramTypes, $paramDesc);
  315. }
  316. /**
  317. * Proxy reflection calls
  318. *
  319. * @param string $method
  320. * @param array $args
  321. * @return mixed
  322. */
  323. public function __call($method, $args)
  324. {
  325. if (method_exists($this->_reflection, $method)) {
  326. return call_user_func_array(array($this->_reflection, $method), $args);
  327. }
  328. throw new Zend_Server_Reflection_Exception('Invalid reflection method ("' .$method. '")');
  329. }
  330. /**
  331. * Retrieve configuration parameters
  332. *
  333. * Values are retrieved by key from {@link $_config}. Returns null if no
  334. * value found.
  335. *
  336. * @param string $key
  337. * @return mixed
  338. */
  339. public function __get($key)
  340. {
  341. if (isset($this->_config[$key])) {
  342. return $this->_config[$key];
  343. }
  344. return null;
  345. }
  346. /**
  347. * Set configuration parameters
  348. *
  349. * Values are stored by $key in {@link $_config}.
  350. *
  351. * @param string $key
  352. * @param mixed $value
  353. * @return void
  354. */
  355. public function __set($key, $value)
  356. {
  357. $this->_config[$key] = $value;
  358. }
  359. /**
  360. * Set method's namespace
  361. *
  362. * @param string $namespace
  363. * @return void
  364. */
  365. public function setNamespace($namespace)
  366. {
  367. if (empty($namespace)) {
  368. $this->_namespace = '';
  369. return;
  370. }
  371. if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) {
  372. throw new Zend_Server_Reflection_Exception('Invalid namespace');
  373. }
  374. $this->_namespace = $namespace;
  375. }
  376. /**
  377. * Return method's namespace
  378. *
  379. * @return string
  380. */
  381. public function getNamespace()
  382. {
  383. return $this->_namespace;
  384. }
  385. /**
  386. * Set the description
  387. *
  388. * @param string $string
  389. * @return void
  390. */
  391. public function setDescription($string)
  392. {
  393. if (!is_string($string)) {
  394. throw new Zend_Server_Reflection_Exception('Invalid description');
  395. }
  396. $this->_description = $string;
  397. }
  398. /**
  399. * Retrieve the description
  400. *
  401. * @return void
  402. */
  403. public function getDescription()
  404. {
  405. return $this->_description;
  406. }
  407. /**
  408. * Retrieve all prototypes as array of
  409. * {@link Zend_Server_Reflection_Prototype Zend_Server_Reflection_Prototypes}
  410. *
  411. * @return array
  412. */
  413. public function getPrototypes()
  414. {
  415. return $this->_prototypes;
  416. }
  417. /**
  418. * Retrieve additional invocation arguments
  419. *
  420. * @return array
  421. */
  422. public function getInvokeArguments()
  423. {
  424. return $this->_argv;
  425. }
  426. /**
  427. * Wakeup from serialization
  428. *
  429. * Reflection needs explicit instantiation to work correctly. Re-instantiate
  430. * reflection object on wakeup.
  431. *
  432. * @return void
  433. */
  434. public function __wakeup()
  435. {
  436. if ($this->_reflection instanceof ReflectionMethod) {
  437. $class = new ReflectionClass($this->_class);
  438. $this->_reflection = new ReflectionMethod($class->newInstance(), $this->getName());
  439. } else {
  440. $this->_reflection = new ReflectionFunction($this->getName());
  441. }
  442. }
  443. }