PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/library/Zend/Tool/Framework/Provider/Signature.php

https://github.com/Martin1982/IBMessagingWorkshopServer
PHP | 391 lines | 183 code | 60 blank | 148 comment | 36 complexity | 0248814359e5e737189c8076097c8484 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_Tool
  17. * @subpackage Framework
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Signature.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @see Zend_Reflection_Class
  24. */
  25. // require_once 'Zend/Reflection/Class.php';
  26. /**
  27. * @see Zend_Tool_Framework_Registry
  28. */
  29. // require_once 'Zend/Tool/Framework/Registry/EnabledInterface.php';
  30. /**
  31. * @see Zend_Tool_Framework_Action_Base
  32. */
  33. // require_once 'Zend/Tool/Framework/Action/Base.php';
  34. /**
  35. * The purpose of Zend_Tool_Framework_Provider_Signature is to derive
  36. * callable signatures from the provided provider.
  37. *
  38. * @category Zend
  39. * @package Zend_Tool
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. class Zend_Tool_Framework_Provider_Signature implements Zend_Tool_Framework_Registry_EnabledInterface
  44. {
  45. /**
  46. * @var Zend_Tool_Framework_Registry
  47. */
  48. protected $_registry = null;
  49. /**
  50. * @var Zend_Tool_Framework_Provider_Interface
  51. */
  52. protected $_provider = null;
  53. /**
  54. * @var string
  55. */
  56. protected $_name = null;
  57. /**
  58. * @var array
  59. */
  60. protected $_specialties = array();
  61. /**
  62. * @var array
  63. */
  64. protected $_actionableMethods = array();
  65. /**
  66. * @var unknown_type
  67. */
  68. protected $_actions = array();
  69. /**
  70. * @var Zend_Reflection_Class
  71. */
  72. protected $_providerReflection = null;
  73. /**
  74. * @var bool
  75. */
  76. protected $_isProcessed = false;
  77. /**
  78. * Constructor
  79. *
  80. * @param Zend_Tool_Framework_Provider_Interface $provider
  81. */
  82. public function __construct(Zend_Tool_Framework_Provider_Interface $provider)
  83. {
  84. $this->_provider = $provider;
  85. $this->_providerReflection = new Zend_Reflection_Class($provider);
  86. }
  87. /**
  88. * setRegistry()
  89. *
  90. * @param Zend_Tool_Framework_Registry_Interface $registry
  91. * @return Zend_Tool_Framework_Provider_Signature
  92. */
  93. public function setRegistry(Zend_Tool_Framework_Registry_Interface $registry)
  94. {
  95. $this->_registry = $registry;
  96. return $this;
  97. }
  98. public function process()
  99. {
  100. if ($this->_isProcessed) {
  101. return;
  102. }
  103. $this->_process();
  104. }
  105. /**
  106. * getName() of the provider
  107. *
  108. * @return unknown
  109. */
  110. public function getName()
  111. {
  112. return $this->_name;
  113. }
  114. /**
  115. * Get the provider for this signature
  116. *
  117. * @return Zend_Tool_Framework_Provider_Interface
  118. */
  119. public function getProvider()
  120. {
  121. return $this->_provider;
  122. }
  123. /**
  124. * getProviderReflection()
  125. *
  126. * @return Zend_Reflection_Class
  127. */
  128. public function getProviderReflection()
  129. {
  130. return $this->_providerReflection;
  131. }
  132. /**
  133. * getSpecialities()
  134. *
  135. * @return array
  136. */
  137. public function getSpecialties()
  138. {
  139. return $this->_specialties;
  140. }
  141. /**
  142. * getActions()
  143. *
  144. * @return array Array of Actions
  145. */
  146. public function getActions()
  147. {
  148. return $this->_actions;
  149. }
  150. /**
  151. * getActionableMethods()
  152. *
  153. * @return array
  154. */
  155. public function getActionableMethods()
  156. {
  157. return $this->_actionableMethods;
  158. }
  159. /**
  160. * getActionableMethod() - Get an actionable method by name, this will return an array of
  161. * useful information about what can be exectued on this provider
  162. *
  163. * @param string $methodName
  164. * @return array
  165. */
  166. public function getActionableMethod($methodName)
  167. {
  168. if (isset($this->_actionableMethods[$methodName])) {
  169. return $this->_actionableMethods[$methodName];
  170. }
  171. return false;
  172. }
  173. /**
  174. * getActionableMethodByActionName() - Get an actionable method by its action name, this
  175. * will return an array of useful information about what can be exectued on this provider
  176. *
  177. * @param string $actionName
  178. * @return array
  179. */
  180. public function getActionableMethodByActionName($actionName, $specialtyName = '_Global')
  181. {
  182. foreach ($this->_actionableMethods as $actionableMethod) {
  183. if ($actionName == $actionableMethod['actionName']
  184. && $specialtyName == $actionableMethod['specialty']) {
  185. return $actionableMethod;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * _process() is called at construction time and is what will build the signature information
  192. * for determining what is actionable
  193. *
  194. */
  195. protected function _process()
  196. {
  197. $this->_isProcessed = true;
  198. $this->_processName();
  199. $this->_processSpecialties();
  200. $this->_processActionableMethods();
  201. }
  202. /**
  203. * _processName();
  204. *
  205. */
  206. protected function _processName()
  207. {
  208. if (method_exists($this->_provider, 'getName')) {
  209. $this->_name = $this->_provider->getName();
  210. }
  211. if ($this->_name == null) {
  212. $className = get_class($this->_provider);
  213. $name = substr($className, strrpos($className, '_')+1);
  214. $name = preg_replace('#(Provider|Manifest)$#', '', $name);
  215. $this->_name = $name;
  216. }
  217. }
  218. /**
  219. * _processSpecialties() - Break out the specialty names for this provider
  220. *
  221. */
  222. protected function _processSpecialties()
  223. {
  224. $specialties = array();
  225. if ($this->_providerReflection->hasMethod('getSpecialties')) {
  226. $specialties = $this->_provider->getSpecialties();
  227. if (!is_array($specialties)) {
  228. // require_once 'Zend/Tool/Framework/Provider/Exception.php';
  229. throw new Zend_Tool_Framework_Provider_Exception(
  230. 'Provider ' . get_class($this->_provider) . ' must return an array for method getSpecialties().'
  231. );
  232. }
  233. } else {
  234. $defaultProperties = $this->_providerReflection->getDefaultProperties();
  235. $specialties = (isset($defaultProperties['_specialties'])) ? $defaultProperties['_specialties'] : array();
  236. if (!is_array($specialties)) {
  237. // require_once 'Zend/Tool/Framework/Provider/Exception.php';
  238. throw new Zend_Tool_Framework_Provider_Exception(
  239. 'Provider ' . get_class($this->_provider) . '\'s property $_specialties must be an array.'
  240. );
  241. }
  242. }
  243. $this->_specialties = array_merge(array('_Global'), $specialties);
  244. }
  245. /**
  246. * _processActionableMethods() - process all methods that can be called on this provider.
  247. *
  248. */
  249. protected function _processActionableMethods()
  250. {
  251. $specialtyRegex = '#(.*)(' . implode('|', $this->_specialties) . ')$#i';
  252. $methods = $this->_providerReflection->getMethods();
  253. $actionableMethods = array();
  254. foreach ($methods as $method) {
  255. $methodName = $method->getName();
  256. /**
  257. * the following will determine what methods are actually actionable
  258. * public, non-static, non-underscore prefixed, classes that dont
  259. * contain the name "
  260. */
  261. if (!$method->getDeclaringClass()->isInstantiable()
  262. || !$method->isPublic()
  263. || $methodName[0] == '_'
  264. || $method->isStatic()
  265. || in_array($methodName, array('getContextClasses', 'getName')) // other protected public methods will nee to go here
  266. ) {
  267. continue;
  268. }
  269. /**
  270. * check to see if the method was a required method by a Zend_Tool_* interface
  271. */
  272. foreach ($method->getDeclaringClass()->getInterfaces() as $methodDeclaringClassInterface) {
  273. if (strpos($methodDeclaringClassInterface->getName(), 'Zend_Tool_') === 0
  274. && $methodDeclaringClassInterface->hasMethod($methodName)) {
  275. continue 2;
  276. }
  277. }
  278. $actionableName = ucfirst($methodName);
  279. if (substr($actionableName, -6) == 'Action') {
  280. $actionableName = substr($actionableName, 0, -6);
  281. }
  282. $actionableMethods[$methodName]['methodName'] = $methodName;
  283. $matches = null;
  284. if (preg_match($specialtyRegex, $actionableName, $matches)) {
  285. $actionableMethods[$methodName]['actionName'] = $matches[1];
  286. $actionableMethods[$methodName]['specialty'] = $matches[2];
  287. } else {
  288. $actionableMethods[$methodName]['actionName'] = $actionableName;
  289. $actionableMethods[$methodName]['specialty'] = '_Global';
  290. }
  291. // get the action, and create non-existent actions when they dont exist (the true part below)
  292. $action = $this->_registry->getActionRepository()->getAction($actionableMethods[$methodName]['actionName']);
  293. if ($action == null) {
  294. $action = new Zend_Tool_Framework_Action_Base($actionableMethods[$methodName]['actionName']);
  295. $this->_registry->getActionRepository()->addAction($action);
  296. }
  297. $actionableMethods[$methodName]['action'] = $action;
  298. if (!in_array($actionableMethods[$methodName]['action'], $this->_actions)) {
  299. $this->_actions[] = $actionableMethods[$methodName]['action'];
  300. }
  301. $parameterInfo = array();
  302. $position = 1;
  303. foreach ($method->getParameters() as $parameter) {
  304. $currentParam = $parameter->getName();
  305. $parameterInfo[$currentParam]['position'] = $position++;
  306. $parameterInfo[$currentParam]['optional'] = $parameter->isOptional();
  307. $parameterInfo[$currentParam]['default'] = ($parameter->isOptional()) ? $parameter->getDefaultValue() : null;
  308. $parameterInfo[$currentParam]['name'] = $currentParam;
  309. $parameterInfo[$currentParam]['type'] = 'string';
  310. $parameterInfo[$currentParam]['description'] = null;
  311. }
  312. $matches = null;
  313. if (($docComment = $method->getDocComment()) != '' &&
  314. (preg_match_all('/@param\s+(\w+)+\s+(\$\S+)\s+(.*?)(?=(?:\*\s*@)|(?:\*\/))/s', $docComment, $matches)))
  315. {
  316. for ($i=0; $i <= count($matches[0])-1; $i++) {
  317. $currentParam = ltrim($matches[2][$i], '$');
  318. if ($currentParam != '' && isset($parameterInfo[$currentParam])) {
  319. $parameterInfo[$currentParam]['type'] = $matches[1][$i];
  320. $descriptionSource = $matches[3][$i];
  321. if ($descriptionSource != '') {
  322. $parameterInfo[$currentParam]['description'] = trim($descriptionSource);
  323. }
  324. }
  325. }
  326. }
  327. $actionableMethods[$methodName]['parameterInfo'] = $parameterInfo;
  328. }
  329. $this->_actionableMethods = $actionableMethods;
  330. }
  331. }