Introspector.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. Zend_Loader::loadClass($serviceClass, $this->_getServicePath());
  83. }
  84. $serv = $this->_xml->createElement('service-description');
  85. $serv->setAttribute('xmlns', 'http://ns.adobe.com/flex/service-description/2008');
  86. $this->_types = $this->_xml->createElement('types');
  87. $this->_ops = $this->_xml->createElement('operations');
  88. $r = Zend_Server_Reflection::reflectClass($serviceClass);
  89. $this->_addService($r, $this->_ops);
  90. $serv->appendChild($this->_types);
  91. $serv->appendChild($this->_ops);
  92. $this->_xml->appendChild($serv);
  93. return $this->_xml->saveXML();
  94. }
  95. /**
  96. * Authentication handler
  97. *
  98. * @param Zend_Acl $acl
  99. * @return unknown_type
  100. */
  101. public function initAcl(Zend_Acl $acl)
  102. {
  103. return false; // we do not need auth for this class
  104. }
  105. /**
  106. * Generate map of public class attributes
  107. *
  108. * @param string $typename type name
  109. * @param DOMElement $typexml target XML element
  110. * @return void
  111. */
  112. protected function _addClassAttributes($typename, DOMElement $typexml)
  113. {
  114. // Do not try to autoload here because _phpTypeToAS should
  115. // have already attempted to load this class
  116. if (!class_exists($typename, false)) {
  117. return;
  118. }
  119. $rc = new Zend_Reflection_Class($typename);
  120. foreach ($rc->getProperties() as $prop) {
  121. if (!$prop->isPublic()) {
  122. continue;
  123. }
  124. $propxml = $this->_xml->createElement('property');
  125. $propxml->setAttribute('name', $prop->getName());
  126. $type = $this->_registerType($this->_getPropertyType($prop));
  127. $propxml->setAttribute('type', $type);
  128. $typexml->appendChild($propxml);
  129. }
  130. }
  131. /**
  132. * Build XML service description from reflection class
  133. *
  134. * @param Zend_Server_Reflection_Class $refclass
  135. * @param DOMElement $target target XML element
  136. * @return void
  137. */
  138. protected function _addService(Zend_Server_Reflection_Class $refclass, DOMElement $target)
  139. {
  140. foreach ($refclass->getMethods() as $method) {
  141. if (!$method->isPublic()
  142. || $method->isConstructor()
  143. || ('__' == substr($method->name, 0, 2))
  144. ) {
  145. continue;
  146. }
  147. foreach ($method->getPrototypes() as $proto) {
  148. $op = $this->_xml->createElement('operation');
  149. $op->setAttribute('name', $method->getName());
  150. $rettype = $this->_registerType($proto->getReturnType());
  151. $op->setAttribute('returnType', $rettype);
  152. foreach ($proto->getParameters() as $param) {
  153. $arg = $this->_xml->createElement('argument');
  154. $arg->setAttribute('name', $param->getName());
  155. $type = $param->getType();
  156. if ($type == 'mixed' && ($pclass = $param->getClass())) {
  157. $type = $pclass->getName();
  158. }
  159. $ptype = $this->_registerType($type);
  160. $arg->setAttribute('type', $ptype);
  161. $op->appendChild($arg);
  162. }
  163. $target->appendChild($op);
  164. }
  165. }
  166. }
  167. /**
  168. * Extract type of the property from DocBlock
  169. *
  170. * @param Zend_Reflection_Property $prop reflection property object
  171. * @return string Property type
  172. */
  173. protected function _getPropertyType(Zend_Reflection_Property $prop)
  174. {
  175. $docBlock = $prop->getDocComment();
  176. if (!$docBlock) {
  177. return 'Unknown';
  178. }
  179. if (!$docBlock->hasTag('var')) {
  180. return 'Unknown';
  181. }
  182. $tag = $docBlock->getTag('var');
  183. return trim($tag->getDescription());
  184. }
  185. /**
  186. * Get the array of service directories
  187. *
  188. * @return array Service class directories
  189. */
  190. protected function _getServicePath()
  191. {
  192. if (isset($this->_options['server'])) {
  193. return $this->_options['server']->getDirectory();
  194. }
  195. if (isset($this->_options['directories'])) {
  196. return $this->_options['directories'];
  197. }
  198. return array();
  199. }
  200. /**
  201. * Map from PHP type name to AS type name
  202. *
  203. * @param string $typename PHP type name
  204. * @return string AS type name
  205. */
  206. protected function _phpTypeToAS($typename)
  207. {
  208. if (class_exists($typename)) {
  209. $vars = get_class_vars($typename);
  210. if (isset($vars['_explicitType'])) {
  211. return $vars['_explicitType'];
  212. }
  213. }
  214. if (false !== ($asname = Zend_Amf_Parse_TypeLoader::getMappedClassName($typename))) {
  215. return $asname;
  216. }
  217. return $typename;
  218. }
  219. /**
  220. * Register new type on the system
  221. *
  222. * @param string $typename type name
  223. * @return string New type name
  224. */
  225. protected function _registerType($typename)
  226. {
  227. // Known type - return its AS name
  228. if (isset($this->_typesMap[$typename])) {
  229. return $this->_typesMap[$typename];
  230. }
  231. // Standard types
  232. if (in_array($typename, array('null', 'mixed', 'unknown_type'))) {
  233. return 'Unknown';
  234. }
  235. if (in_array($typename, array('void', 'string', 'object', 'Unknown', 'stdClass', 'array'))) {
  236. return $typename;
  237. }
  238. // Resolve and store AS name
  239. $asTypeName = $this->_phpTypeToAS($typename);
  240. $this->_typesMap[$typename] = $asTypeName;
  241. // Create element for the name
  242. $typeEl = $this->_xml->createElement('type');
  243. $typeEl->setAttribute('name', $asTypeName);
  244. $this->_addClassAttributes($typename, $typeEl);
  245. $this->_types->appendChild($typeEl);
  246. return $asTypeName;
  247. }
  248. /**
  249. * Return error with error message
  250. *
  251. * @param string $msg Error message
  252. * @return string
  253. */
  254. protected function _returnError($msg)
  255. {
  256. return 'ERROR: $msg';
  257. }
  258. }