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

/cakesocial.local/app/vendors/zend/laboratory/Zend_Tool/library/ZendL/Tool/Rpc/Provider/Signature.php

https://github.com/miamiruby/cakestuff
PHP | 179 lines | 131 code | 44 blank | 4 comment | 27 complexity | 7c9a07da83eecc358f80fde50047f4f5 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. class ZendL_Tool_Rpc_Provider_Signature
  3. {
  4. protected $_provider = null;
  5. protected $_name = null;
  6. protected $_specialties = array();
  7. protected $_actionableMethods = array();
  8. protected $_actions = array();
  9. /**
  10. * @var ReflectionClass
  11. */
  12. protected $_providerReflection = null;
  13. public function __construct(ZendL_Tool_Rpc_Provider_Interface $provider)
  14. {
  15. $this->_provider = $provider;
  16. $this->_providerReflection = new ReflectionClass($provider);
  17. $this->_process();
  18. }
  19. public function getName()
  20. {
  21. return $this->_name;
  22. }
  23. public function getProvider()
  24. {
  25. return $this->_provider;
  26. }
  27. public function getSpecialties()
  28. {
  29. return $this->_specialties;
  30. }
  31. public function getActionableMethods()
  32. {
  33. return $this->_actionableMethods;
  34. }
  35. public function getActionableMethod($actionName)
  36. {
  37. foreach ($this->_actionableMethods as $actionableMethodName => $actionableMethod) {
  38. if ($actionName == $actionableMethod['actionName']) {
  39. return $actionableMethod;
  40. }
  41. }
  42. return false;
  43. }
  44. protected function _process()
  45. {
  46. $this->_processName();
  47. $this->_processSpecialties();
  48. $this->_processActionableMethods();
  49. //$this->_processMethodSignatures();
  50. }
  51. protected function _processName()
  52. {
  53. if (method_exists($this->_provider, 'getName')) {
  54. $this->_name = $this->_provider->getName();
  55. }
  56. if ($this->_name == null) {
  57. $className = get_class($this->_provider);
  58. $name = substr($className, strrpos($className, '_')+1);
  59. $name = preg_replace('#(Tool|Provider|Manifest)$#', '', $name);
  60. $this->_name = $name;
  61. }
  62. }
  63. protected function _processSpecialties()
  64. {
  65. $specialties = array();
  66. if ($this->_providerReflection->hasMethod('getSpecialties')) {
  67. $specialties = $this->_provider->getSpecialties();
  68. if (!is_array($specialties)) {
  69. throw new ZendL_Tool_Rpc_Exception('Provider ' . get_class($this->_provider) . ' must return an array for method getSpecialties().');
  70. }
  71. } else {
  72. $defaultProperties = $this->_providerReflection->getDefaultProperties();
  73. $specialties = (isset($defaultProperties['_specialties'])) ? $defaultProperties['_specialties'] : array();
  74. if (!is_array($specialties)) {
  75. throw new ZendL_Tool_Rpc_Exception('Provider ' . get_class($this->_provider) . '\'s property $_specialties must be an array.');
  76. }
  77. }
  78. $this->_specialties = array_merge(array('_Global'), $specialties);
  79. }
  80. protected function _processActionableMethods()
  81. {
  82. $specialtyRegex = '#(.*)(' . implode('|', $this->_specialties) . ')$#i';
  83. $methods = $this->_providerReflection->getMethods();
  84. $actionableMethods = array();
  85. foreach ($methods as $method) {
  86. $methodName = $method->getName();
  87. if (!$method->getDeclaringClass()->isInstantiable() || !$method->isPublic() || $methodName[0] == '_') {
  88. continue;
  89. }
  90. $actionableName = ucfirst($methodName);
  91. if (substr($actionableName, -6) == 'Action') {
  92. $actionableName = substr($actionableName, 0, -6);
  93. }
  94. $actionableMethods[$methodName]['methodName'] = $methodName;
  95. if (preg_match($specialtyRegex, $actionableName, $matches)) {
  96. $actionableMethods[$methodName]['actionName'] = $matches[1];
  97. $actionableMethods[$methodName]['specialty'] = $matches[2];
  98. } else {
  99. $actionableMethods[$methodName]['actionName'] = $actionableName;
  100. $actionableMethods[$methodName]['specialty'] = '_Global';
  101. }
  102. $actionableMethods[$methodName]['action'] = ZendL_Tool_Rpc_Provider_Registry::getInstance()->getAction($actionableMethods[$methodName]['actionName']);
  103. if (!in_array($actionableMethods[$methodName]['action'], $this->_actions)) {
  104. $this->_actions[] = $actionableMethods[$methodName]['action'];
  105. }
  106. $parameterInfo = array();
  107. $position = 1;
  108. foreach ($method->getParameters() as $parameter) {
  109. $currentParam = $parameter->getName();
  110. $parameterInfo[$currentParam]['position'] = $position++;
  111. $parameterInfo[$currentParam]['optional'] = $parameter->isOptional();
  112. $parameterInfo[$currentParam]['default'] = ($parameter->isOptional()) ? $parameter->getDefaultValue() : null;
  113. $parameterInfo[$currentParam]['name'] = $currentParam;
  114. $parameterInfo[$currentParam]['type'] = 'string';
  115. $parameterInfo[$currentParam]['description'] = null;
  116. }
  117. if (($docComment = $method->getDocComment()) != '' &&
  118. (preg_match_all('/@param\s+(\w+)+\s+(\$\S+)\s+(.*?)(?=(?:\*\s*@)|(?:\*\/))/s', $docComment, $matches)))
  119. {
  120. for ($i=0; $i <= count($matches[0])-1; $i++) {
  121. $currentParam = ltrim($matches[2][$i], '$');
  122. if ($currentParam != '' && isset($parameterInfo[$currentParam])) {
  123. $parameterInfo[$currentParam]['type'] = $matches[1][$i];
  124. $descriptionSource = $matches[3][$i];
  125. if ($descriptionSource != '') {
  126. $parameterInfo[$currentParam]['description'] = trim($descriptionSource);
  127. }
  128. }
  129. }
  130. }
  131. $actionableMethods[$methodName]['parameterInfo'] = $parameterInfo;
  132. }
  133. $this->_actionableMethods = $actionableMethods;
  134. }
  135. }