Introspector.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_Amf
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Amf_Parse_TypeLoader */
  21. require_once 'Zend/Amf/Parse/TypeLoader.php';
  22. /** Zend_Reflection_Class */
  23. require_once 'Zend/Reflection/Class.php';
  24. /** Zend_Server_Reflection */
  25. require_once 'Zend/Server/Reflection.php';
  26. /**
  27. * This class implements a service for generating AMF service descriptions as XML.
  28. *
  29. * @package Zend_Amf
  30. * @subpackage Adobe
  31. * @copyright Copyright (c) 2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Amf_Adobe_Introspector
  35. {
  36. /**
  37. * Options used:
  38. * - server: instance of Zend_Amf_Server to use
  39. * - directories: directories where class files may be looked up
  40. *
  41. * @var array Introspector options
  42. */
  43. protected $_options;
  44. /**
  45. * @var DOMElement DOM element to store types
  46. */
  47. protected $_types;
  48. /**
  49. * @var array Map of the known types
  50. */
  51. protected $_typesMap = array();
  52. /**
  53. * @var DOMDocument XML document to store data
  54. */
  55. protected $_xml;
  56. /**
  57. * Constructor
  58. *
  59. * @return void
  60. */
  61. public function __construct()
  62. {
  63. $this->_xml = new DOMDocument('1.0', 'utf-8');
  64. }
  65. /**
  66. * Create XML definition on an AMF service class
  67. *
  68. * @param string $serviceClass Service class name
  69. * @param array $options invocation options
  70. * @return string XML with service class introspection
  71. */
  72. public function introspect($serviceClass, $options = array())
  73. {
  74. $this->_options = $options;
  75. if (strpbrk($serviceClass, '\\/<>')) {
  76. return $this->_returnError('Invalid service name');
  77. }
  78. // Transform com.foo.Bar into com_foo_Bar
  79. $serviceClass = str_replace('.' , '_', $serviceClass);
  80. // Introspect!
  81. if (!class_exists($serviceClass)) {
  82. require_once 'Zend/Loader.php';
  83. Zend_Loader::loadClass($serviceClass, $this->_getServicePath());
  84. }
  85. $serv = $this->_xml->createElement('service-description');
  86. $serv->setAttribute('xmlns', 'http://ns.adobe.com/flex/service-description/2008');
  87. $this->_types = $this->_xml->createElement('types');
  88. $this->_ops = $this->_xml->createElement('operations');
  89. $r = Zend_Server_Reflection::reflectClass($serviceClass);
  90. $this->_addService($r, $this->_ops);
  91. $serv->appendChild($this->_types);
  92. $serv->appendChild($this->_ops);
  93. $this->_xml->appendChild($serv);
  94. return $this->_xml->saveXML();
  95. }
  96. /**
  97. * Authentication handler
  98. *
  99. * @param Zend_Acl $acl
  100. * @return unknown_type
  101. */
  102. public function initAcl(Zend_Acl $acl)
  103. {
  104. return false; // we do not need auth for this class
  105. }
  106. /**
  107. * Generate map of public class attributes
  108. *
  109. * @param string $typename type name
  110. * @param DOMElement $typexml target XML element
  111. * @return void
  112. */
  113. protected function _addClassAttributes($typename, DOMElement $typexml)
  114. {
  115. // Do not try to autoload here because _phpTypeToAS should
  116. // have already attempted to load this class
  117. if (!class_exists($typename, false)) {
  118. return;
  119. }
  120. $rc = new Zend_Reflection_Class($typename);
  121. foreach ($rc->getProperties() as $prop) {
  122. if (!$prop->isPublic()) {
  123. continue;
  124. }
  125. $propxml = $this->_xml->createElement('property');
  126. $propxml->setAttribute('name', $prop->getName());
  127. $type = $this->_registerType($this->_getPropertyType($prop));
  128. $propxml->setAttribute('type', $type);
  129. $typexml->appendChild($propxml);
  130. }
  131. }
  132. /**
  133. * Build XML service description from reflection class
  134. *
  135. * @param Zend_Server_Reflection_Class $refclass
  136. * @param DOMElement $target target XML element
  137. * @return void
  138. */
  139. protected function _addService(Zend_Server_Reflection_Class $refclass, DOMElement $target)
  140. {
  141. foreach ($refclass->getMethods() as $method) {
  142. if (!$method->isPublic()
  143. || $method->isConstructor()
  144. || ('__' == substr($method->name, 0, 2))
  145. ) {
  146. continue;
  147. }
  148. foreach ($method->getPrototypes() as $proto) {
  149. $op = $this->_xml->createElement('operation');
  150. $op->setAttribute('name', $method->getName());
  151. $rettype = $this->_registerType($proto->getReturnType());
  152. $op->setAttribute('returnType', $rettype);
  153. foreach ($proto->getParameters() as $param) {
  154. $arg = $this->_xml->createElement('argument');
  155. $arg->setAttribute('name', $param->getName());
  156. $type = $param->getType();
  157. if ($type == 'mixed' && ($pclass = $param->getClass())) {
  158. $type = $pclass->getName();
  159. }
  160. $ptype = $this->_registerType($type);
  161. $arg->setAttribute('type', $ptype);
  162. $op->appendChild($arg);
  163. }
  164. $target->appendChild($op);
  165. }
  166. }
  167. }
  168. /**
  169. * Extract type of the property from DocBlock
  170. *
  171. * @param Zend_Reflection_Property $prop reflection property object
  172. * @return string Property type
  173. */
  174. protected function _getPropertyType(Zend_Reflection_Property $prop)
  175. {
  176. $docBlock = $prop->getDocComment();
  177. if (!$docBlock) {
  178. return 'Unknown';
  179. }
  180. if (!$docBlock->hasTag('var')) {
  181. return 'Unknown';
  182. }
  183. $tag = $docBlock->getTag('var');
  184. return trim($tag->getDescription());
  185. }
  186. /**
  187. * Get the array of service directories
  188. *
  189. * @return array Service class directories
  190. */
  191. protected function _getServicePath()
  192. {
  193. if (isset($this->_options['server'])) {
  194. return $this->_options['server']->getDirectory();
  195. }
  196. if (isset($this->_options['directories'])) {
  197. return $this->_options['directories'];
  198. }
  199. return array();
  200. }
  201. /**
  202. * Map from PHP type name to AS type name
  203. *
  204. * @param string $typename PHP type name
  205. * @return string AS type name
  206. */
  207. protected function _phpTypeToAS($typename)
  208. {
  209. if (class_exists($typename)) {
  210. $vars = get_class_vars($typename);
  211. if (isset($vars['_explicitType'])) {
  212. return $vars['_explicitType'];
  213. }
  214. }
  215. if (false !== ($asname = Zend_Amf_Parse_TypeLoader::getMappedClassName($typename))) {
  216. return $asname;
  217. }
  218. return $typename;
  219. }
  220. /**
  221. * Register new type on the system
  222. *
  223. * @param string $typename type name
  224. * @return string New type name
  225. */
  226. protected function _registerType($typename)
  227. {
  228. // Known type - return its AS name
  229. if (isset($this->_typesMap[$typename])) {
  230. return $this->_typesMap[$typename];
  231. }
  232. // Standard types
  233. if (in_array($typename, array('void', 'null', 'mixed', 'unknown_type'))) {
  234. return 'Unknown';
  235. }
  236. if (in_array($typename, array('int', 'integer', 'bool', 'boolean', 'float', 'string', 'object', 'Unknown', 'stdClass', 'array'))) {
  237. return $typename;
  238. }
  239. // Resolve and store AS name
  240. $asTypeName = $this->_phpTypeToAS($typename);
  241. $this->_typesMap[$typename] = $asTypeName;
  242. // Create element for the name
  243. $typeEl = $this->_xml->createElement('type');
  244. $typeEl->setAttribute('name', $asTypeName);
  245. $this->_addClassAttributes($typename, $typeEl);
  246. $this->_types->appendChild($typeEl);
  247. return $asTypeName;
  248. }
  249. /**
  250. * Return error with error message
  251. *
  252. * @param string $msg Error message
  253. * @return string
  254. */
  255. protected function _returnError($msg)
  256. {
  257. return 'ERROR: $msg';
  258. }
  259. }