Abstract.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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-2009 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-2009 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 2504 2011-12-28 07:35:29Z liu21st $
  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, $this->_paramDesc[$key]);
  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. }
  309. $paramTypes = array();
  310. foreach ($paramTypesTmp as $i => $param) {
  311. $tmp = explode('|', $param);
  312. if ($parameters[$i]->isOptional()) {
  313. array_unshift($tmp, null);
  314. }
  315. $paramTypes[] = $tmp;
  316. }
  317. $this->_buildSignatures($return, $returnDesc, $paramTypes, $paramDesc);
  318. }
  319. /**
  320. * Proxy reflection calls
  321. *
  322. * @param string $method
  323. * @param array $args
  324. * @return mixed
  325. */
  326. public function __call($method, $args)
  327. {
  328. if (method_exists($this->_reflection, $method)) {
  329. return call_user_func_array(array($this->_reflection, $method), $args);
  330. }
  331. require_once 'Zend/Server/Reflection/Exception.php';
  332. throw new Zend_Server_Reflection_Exception('Invalid reflection method ("' .$method. '")');
  333. }
  334. /**
  335. * Retrieve configuration parameters
  336. *
  337. * Values are retrieved by key from {@link $_config}. Returns null if no
  338. * value found.
  339. *
  340. * @param string $key
  341. * @return mixed
  342. */
  343. public function __get($key)
  344. {
  345. if (isset($this->_config[$key])) {
  346. return $this->_config[$key];
  347. }
  348. return null;
  349. }
  350. /**
  351. * Set configuration parameters
  352. *
  353. * Values are stored by $key in {@link $_config}.
  354. *
  355. * @param string $key
  356. * @param mixed $value
  357. * @return void
  358. */
  359. public function __set($key, $value)
  360. {
  361. $this->_config[$key] = $value;
  362. }
  363. /**
  364. * Set method's namespace
  365. *
  366. * @param string $namespace
  367. * @return void
  368. */
  369. public function setNamespace($namespace)
  370. {
  371. if (empty($namespace)) {
  372. $this->_namespace = '';
  373. return;
  374. }
  375. if (!is_string($namespace) || !preg_match('/[a-z0-9_\.]+/i', $namespace)) {
  376. require_once 'Zend/Server/Reflection/Exception.php';
  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. require_once 'Zend/Server/Reflection/Exception.php';
  400. throw new Zend_Server_Reflection_Exception('Invalid description');
  401. }
  402. $this->_description = $string;
  403. }
  404. /**
  405. * Retrieve the description
  406. *
  407. * @return void
  408. */
  409. public function getDescription()
  410. {
  411. return $this->_description;
  412. }
  413. /**
  414. * Retrieve all prototypes as array of
  415. * {@link Zend_Server_Reflection_Prototype Zend_Server_Reflection_Prototypes}
  416. *
  417. * @return array
  418. */
  419. public function getPrototypes()
  420. {
  421. return $this->_prototypes;
  422. }
  423. /**
  424. * Retrieve additional invocation arguments
  425. *
  426. * @return array
  427. */
  428. public function getInvokeArguments()
  429. {
  430. return $this->_argv;
  431. }
  432. /**
  433. * Wakeup from serialization
  434. *
  435. * Reflection needs explicit instantiation to work correctly. Re-instantiate
  436. * reflection object on wakeup.
  437. *
  438. * @return void
  439. */
  440. public function __wakeup()
  441. {
  442. if ($this->_reflection instanceof ReflectionMethod) {
  443. $class = new ReflectionClass($this->_class);
  444. $this->_reflection = new ReflectionMethod($class->newInstance(), $this->getName());
  445. } else {
  446. $this->_reflection = new ReflectionFunction($this->getName());
  447. }
  448. }
  449. }