PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Tool/Framework/Provider/Signature.php

https://github.com/gryzz/crystal_magento
PHP | 390 lines | 182 code | 60 blank | 148 comment | 34 complexity | 4f2f662f8db80c41acb0a5a735b6cb88 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-2009 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 18951 2009-11-12 16:26:19Z alexander $
  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-2009 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)
  181. {
  182. foreach ($this->_actionableMethods as $actionableMethod) {
  183. if ($actionName == $actionableMethod['actionName']) {
  184. return $actionableMethod;
  185. }
  186. }
  187. return false;
  188. }
  189. /**
  190. * _process() is called at construction time and is what will build the signature information
  191. * for determining what is actionable
  192. *
  193. */
  194. protected function _process()
  195. {
  196. $this->_isProcessed = true;
  197. $this->_processName();
  198. $this->_processSpecialties();
  199. $this->_processActionableMethods();
  200. }
  201. /**
  202. * _processName();
  203. *
  204. */
  205. protected function _processName()
  206. {
  207. if (method_exists($this->_provider, 'getName')) {
  208. $this->_name = $this->_provider->getName();
  209. }
  210. if ($this->_name == null) {
  211. $className = get_class($this->_provider);
  212. $name = substr($className, strrpos($className, '_')+1);
  213. $name = preg_replace('#(Provider|Manifest)$#', '', $name);
  214. $this->_name = $name;
  215. }
  216. }
  217. /**
  218. * _processSpecialties() - Break out the specialty names for this provider
  219. *
  220. */
  221. protected function _processSpecialties()
  222. {
  223. $specialties = array();
  224. if ($this->_providerReflection->hasMethod('getSpecialties')) {
  225. $specialties = $this->_provider->getSpecialties();
  226. if (!is_array($specialties)) {
  227. #require_once 'Zend/Tool/Framework/Provider/Exception.php';
  228. throw new Zend_Tool_Framework_Provider_Exception(
  229. 'Provider ' . get_class($this->_provider) . ' must return an array for method getSpecialties().'
  230. );
  231. }
  232. } else {
  233. $defaultProperties = $this->_providerReflection->getDefaultProperties();
  234. $specialties = (isset($defaultProperties['_specialties'])) ? $defaultProperties['_specialties'] : array();
  235. if (!is_array($specialties)) {
  236. #require_once 'Zend/Tool/Framework/Provider/Exception.php';
  237. throw new Zend_Tool_Framework_Provider_Exception(
  238. 'Provider ' . get_class($this->_provider) . '\'s property $_specialties must be an array.'
  239. );
  240. }
  241. }
  242. $this->_specialties = array_merge(array('_Global'), $specialties);
  243. }
  244. /**
  245. * _processActionableMethods() - process all methods that can be called on this provider.
  246. *
  247. */
  248. protected function _processActionableMethods()
  249. {
  250. $specialtyRegex = '#(.*)(' . implode('|', $this->_specialties) . ')$#i';
  251. $methods = $this->_providerReflection->getMethods();
  252. $actionableMethods = array();
  253. foreach ($methods as $method) {
  254. $methodName = $method->getName();
  255. /**
  256. * the following will determine what methods are actually actionable
  257. * public, non-static, non-underscore prefixed, classes that dont
  258. * contain the name "
  259. */
  260. if (!$method->getDeclaringClass()->isInstantiable()
  261. || !$method->isPublic()
  262. || $methodName[0] == '_'
  263. || $method->isStatic()
  264. || in_array($methodName, array('getContextClasses', 'getName')) // other protected public methods will nee to go here
  265. ) {
  266. continue;
  267. }
  268. /**
  269. * check to see if the method was a required method by a Zend_Tool_* interface
  270. */
  271. foreach ($method->getDeclaringClass()->getInterfaces() as $methodDeclaringClassInterface) {
  272. if (strpos($methodDeclaringClassInterface->getName(), 'Zend_Tool_') === 0
  273. && $methodDeclaringClassInterface->hasMethod($methodName)) {
  274. continue 2;
  275. }
  276. }
  277. $actionableName = ucfirst($methodName);
  278. if (substr($actionableName, -6) == 'Action') {
  279. $actionableName = substr($actionableName, 0, -6);
  280. }
  281. $actionableMethods[$methodName]['methodName'] = $methodName;
  282. $matches = null;
  283. if (preg_match($specialtyRegex, $actionableName, $matches)) {
  284. $actionableMethods[$methodName]['actionName'] = $matches[1];
  285. $actionableMethods[$methodName]['specialty'] = $matches[2];
  286. } else {
  287. $actionableMethods[$methodName]['actionName'] = $actionableName;
  288. $actionableMethods[$methodName]['specialty'] = '_Global';
  289. }
  290. // get the action, and create non-existent actions when they dont exist (the true part below)
  291. $action = $this->_registry->getActionRepository()->getAction($actionableMethods[$methodName]['actionName']);
  292. if ($action == null) {
  293. $action = new Zend_Tool_Framework_Action_Base($actionableMethods[$methodName]['actionName']);
  294. $this->_registry->getActionRepository()->addAction($action);
  295. }
  296. $actionableMethods[$methodName]['action'] = $action;
  297. if (!in_array($actionableMethods[$methodName]['action'], $this->_actions)) {
  298. $this->_actions[] = $actionableMethods[$methodName]['action'];
  299. }
  300. $parameterInfo = array();
  301. $position = 1;
  302. foreach ($method->getParameters() as $parameter) {
  303. $currentParam = $parameter->getName();
  304. $parameterInfo[$currentParam]['position'] = $position++;
  305. $parameterInfo[$currentParam]['optional'] = $parameter->isOptional();
  306. $parameterInfo[$currentParam]['default'] = ($parameter->isOptional()) ? $parameter->getDefaultValue() : null;
  307. $parameterInfo[$currentParam]['name'] = $currentParam;
  308. $parameterInfo[$currentParam]['type'] = 'string';
  309. $parameterInfo[$currentParam]['description'] = null;
  310. }
  311. $matches = null;
  312. if (($docComment = $method->getDocComment()) != '' &&
  313. (preg_match_all('/@param\s+(\w+)+\s+(\$\S+)\s+(.*?)(?=(?:\*\s*@)|(?:\*\/))/s', $docComment, $matches)))
  314. {
  315. for ($i=0; $i <= count($matches[0])-1; $i++) {
  316. $currentParam = ltrim($matches[2][$i], '$');
  317. if ($currentParam != '' && isset($parameterInfo[$currentParam])) {
  318. $parameterInfo[$currentParam]['type'] = $matches[1][$i];
  319. $descriptionSource = $matches[3][$i];
  320. if ($descriptionSource != '') {
  321. $parameterInfo[$currentParam]['description'] = trim($descriptionSource);
  322. }
  323. }
  324. }
  325. }
  326. $actionableMethods[$methodName]['parameterInfo'] = $parameterInfo;
  327. }
  328. $this->_actionableMethods = $actionableMethods;
  329. }
  330. }