/library/Zend/Amf/Parser/TypeLoader.php

https://github.com/MarcelloDuarte/zf2 · PHP · 197 lines · 85 code · 18 blank · 94 comment · 7 complexity · 539ca76b3314b1ab1ffece255d3b1535 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_Amf
  17. * @subpackage Parser
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Amf\Parser;
  25. use Zend\Loader\PluginBroker;
  26. /**
  27. * Loads a local class and executes the instantiation of that class.
  28. *
  29. * @todo PHP 5.3 can drastically change this class w/ namespace and the new call_user_func w/ namespace
  30. * @uses Zend\Amf\Exception
  31. * @uses Zend\Amf\Value\Messaging\AcknowledgeMessage
  32. * @uses Zend\Amf\Value\Messaging\AsyncMessage
  33. * @uses Zend\Amf\Value\Messaging\CommandMessage
  34. * @uses Zend\Amf\Value\Messaging\ErrorMessage
  35. * @uses Zend\Amf\Value\Messaging\RemotingMessage
  36. * @package Zend_Amf
  37. * @subpackage Parser
  38. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. final class TypeLoader
  42. {
  43. /**
  44. * @var string callback class
  45. */
  46. public static $callbackClass;
  47. /**
  48. * @var array AMF class map
  49. */
  50. public static $classMap = array (
  51. 'flex.messaging.messages.AcknowledgeMessage' => 'Zend\\Amf\\Value\\Messaging\\AcknowledgeMessage',
  52. 'flex.messaging.messages.ErrorMessage' => 'Zend\\Amf\\Value\\Messaging\\AsyncMessage',
  53. 'flex.messaging.messages.CommandMessage' => 'Zend\\Amf\\Value\\Messaging\\CommandMessage',
  54. 'flex.messaging.messages.ErrorMessage' => 'Zend\\Amf\\Value\\Messaging\\ErrorMessage',
  55. 'flex.messaging.messages.RemotingMessage' => 'Zend\\Amf\\Value\\Messaging\\RemotingMessage',
  56. 'flex.messaging.io.ArrayCollection' => 'Zend\\Amf\\Value\\Messaging\\ArrayCollection',
  57. );
  58. /**
  59. * @var array Default class map
  60. */
  61. protected static $_defaultClassMap = array(
  62. 'flex.messaging.messages.AcknowledgeMessage' => 'Zend\\Amf\\Value\\Messaging\\AcknowledgeMessage',
  63. 'flex.messaging.messages.ErrorMessage' => 'Zend\\Amf\\Value\\Messaging\\AsyncMessage',
  64. 'flex.messaging.messages.CommandMessage' => 'Zend\\Amf\\Value\\Messaging\\CommandMessage',
  65. 'flex.messaging.messages.ErrorMessage' => 'Zend\\Amf\\Value\\Messaging\\ErrorMessage',
  66. 'flex.messaging.messages.RemotingMessage' => 'Zend\\Amf\\Value\\Messaging\\RemotingMessage',
  67. 'flex.messaging.io.ArrayCollection' => 'Zend\\Amf\\Value\\Messaging\\ArrayCollection',
  68. );
  69. /**
  70. * @var \Zend\Loader\PluginBroker
  71. */
  72. protected static $_resourceBroker = null;
  73. /**
  74. * Load the mapped class type into a callback.
  75. *
  76. * @param string $className
  77. * @return object|false
  78. */
  79. public static function loadType($className)
  80. {
  81. $class = self::getMappedClassName($className);
  82. if(!$class) {
  83. $class = str_replace('.', '\\', $className);
  84. }
  85. if (!class_exists($class)) {
  86. return "stdClass";
  87. }
  88. return $class;
  89. }
  90. /**
  91. * Looks up the supplied call name to its mapped class name
  92. *
  93. * @param string $className
  94. * @return string
  95. */
  96. public static function getMappedClassName($className)
  97. {
  98. $mappedName = array_search($className, self::$classMap);
  99. if ($mappedName) {
  100. return $mappedName;
  101. }
  102. $mappedName = array_search($className, array_flip(self::$classMap));
  103. if ($mappedName) {
  104. return $mappedName;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Map PHP class names to ActionScript class names
  110. *
  111. * Allows users to map the class names of there action script classes
  112. * to the equivelent php class name. Used in deserialization to load a class
  113. * and serialiation to set the class name of the returned object.
  114. *
  115. * @param string $asClassName
  116. * @param string $phpClassName
  117. * @return void
  118. */
  119. public static function setMapping($asClassName, $phpClassName)
  120. {
  121. self::$classMap[$asClassName] = $phpClassName;
  122. }
  123. /**
  124. * Reset type map
  125. *
  126. * @return void
  127. */
  128. public static function resetMap()
  129. {
  130. self::$classMap = self::$_defaultClassMap;
  131. }
  132. /**
  133. * Set loader for resource type handlers
  134. *
  135. * @param \Zend\Loader\PluginBroker $loader
  136. */
  137. public static function setResourceBroker(PluginBroker $broker)
  138. {
  139. self::$_resourceBroker = $broker;
  140. }
  141. /**
  142. * Get plugin class that handles this resource
  143. *
  144. * @param resource $resource Resource type
  145. * @return object Resource class
  146. */
  147. public static function getResourceParser($resource)
  148. {
  149. if (self::$_resourceBroker) {
  150. $type = preg_replace("/[^A-Za-z0-9_]/", " ", get_resource_type($resource));
  151. $type = str_replace(" ","", ucwords($type));
  152. return self::$_resourceBroker->load($type);
  153. }
  154. return false;
  155. }
  156. /**
  157. * Convert resource to a serializable object
  158. *
  159. * @param resource $resource
  160. * @return mixed
  161. */
  162. public static function handleResource($resource)
  163. {
  164. if (!self::$_resourceBroker) {
  165. throw new Exception\InvalidArgumentException('Unable to handle resources - resource plugin broker not set');
  166. }
  167. try {
  168. while (is_resource($resource)) {
  169. $parser = self::getResourceParser($resource);
  170. $resource = $parser->parse($resource);
  171. }
  172. return $resource;
  173. } catch(Exception $e) {
  174. throw new Exception\InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  175. } catch(\Exception $e) {
  176. throw new Exception\RuntimeException('Can not serialize resource type: '. get_resource_type($resource), 0, $e);
  177. }
  178. }
  179. }