/library/Zend/Amf/Parse/TypeLoader.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 240 lines · 106 code · 22 blank · 112 comment · 11 complexity · 9790b3238e466b323c817fdabfc51346 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 Parse
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: TypeLoader.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Amf_Value_Messaging_AcknowledgeMessage
  24. */
  25. require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
  26. /**
  27. * @see Zend_Amf_Value_Messaging_AsyncMessage
  28. */
  29. require_once 'Zend/Amf/Value/Messaging/AsyncMessage.php';
  30. /**
  31. * @see Zend_Amf_Value_Messaging_CommandMessage
  32. */
  33. require_once 'Zend/Amf/Value/Messaging/CommandMessage.php';
  34. /**
  35. * @see Zend_Amf_Value_Messaging_ErrorMessage
  36. */
  37. require_once 'Zend/Amf/Value/Messaging/ErrorMessage.php';
  38. /**
  39. * @see Zend_Amf_Value_Messaging_RemotingMessage
  40. */
  41. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  42. /**
  43. * Loads a local class and executes the instantiation of that class.
  44. *
  45. * @todo PHP 5.3 can drastically change this class w/ namespace and the new call_user_func w/ namespace
  46. * @package Zend_Amf
  47. * @subpackage Parse
  48. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  49. * @license http://framework.zend.com/license/new-bsd New BSD License
  50. */
  51. final class Zend_Amf_Parse_TypeLoader
  52. {
  53. /**
  54. * @var string callback class
  55. */
  56. public static $callbackClass;
  57. /**
  58. * @var array AMF class map
  59. */
  60. public static $classMap = array(
  61. 'flex.messaging.messages.AcknowledgeMessage' => 'Zend_Amf_Value_Messaging_AcknowledgeMessage',
  62. 'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_AsyncMessage',
  63. 'flex.messaging.messages.CommandMessage' => 'Zend_Amf_Value_Messaging_CommandMessage',
  64. 'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_ErrorMessage',
  65. 'flex.messaging.messages.RemotingMessage' => 'Zend_Amf_Value_Messaging_RemotingMessage',
  66. 'flex.messaging.io.ArrayCollection' => 'Zend_Amf_Value_Messaging_ArrayCollection',
  67. );
  68. /**
  69. * @var array Default class map
  70. */
  71. protected static $_defaultClassMap = array(
  72. 'flex.messaging.messages.AcknowledgeMessage' => 'Zend_Amf_Value_Messaging_AcknowledgeMessage',
  73. 'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_AsyncMessage',
  74. 'flex.messaging.messages.CommandMessage' => 'Zend_Amf_Value_Messaging_CommandMessage',
  75. 'flex.messaging.messages.ErrorMessage' => 'Zend_Amf_Value_Messaging_ErrorMessage',
  76. 'flex.messaging.messages.RemotingMessage' => 'Zend_Amf_Value_Messaging_RemotingMessage',
  77. 'flex.messaging.io.ArrayCollection' => 'Zend_Amf_Value_Messaging_ArrayCollection',
  78. );
  79. /**
  80. * @var Zend_Loader_PluginLoader_Interface
  81. */
  82. protected static $_resourceLoader = null;
  83. /**
  84. * Load the mapped class type into a callback.
  85. *
  86. * @param string $className
  87. *
  88. * @return object|false
  89. */
  90. public static function loadType ($className)
  91. {
  92. $class = self::getMappedClassName($className);
  93. if (!$class) {
  94. $class = str_replace('.', '_', $className);
  95. }
  96. if (!class_exists($class)) {
  97. return "stdClass";
  98. }
  99. return $class;
  100. }
  101. /**
  102. * Looks up the supplied call name to its mapped class name
  103. *
  104. * @param string $className
  105. *
  106. * @return string
  107. */
  108. public static function getMappedClassName ($className)
  109. {
  110. $mappedName = array_search($className, self::$classMap);
  111. if ($mappedName) {
  112. return $mappedName;
  113. }
  114. $mappedName = array_search($className, array_flip(self::$classMap));
  115. if ($mappedName) {
  116. return $mappedName;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Map PHP class names to ActionScript class names
  122. *
  123. * Allows users to map the class names of there action script classes
  124. * to the equivelent php class name. Used in deserialization to load a class
  125. * and serialiation to set the class name of the returned object.
  126. *
  127. * @param string $asClassName
  128. * @param string $phpClassName
  129. *
  130. * @return void
  131. */
  132. public static function setMapping ($asClassName, $phpClassName)
  133. {
  134. self::$classMap[$asClassName] = $phpClassName;
  135. }
  136. /**
  137. * Reset type map
  138. *
  139. * @return void
  140. */
  141. public static function resetMap ()
  142. {
  143. self::$classMap = self::$_defaultClassMap;
  144. }
  145. /**
  146. * Set loader for resource type handlers
  147. *
  148. * @param Zend_Loader_PluginLoader_Interface $loader
  149. */
  150. public static function setResourceLoader (Zend_Loader_PluginLoader_Interface $loader)
  151. {
  152. self::$_resourceLoader = $loader;
  153. }
  154. /**
  155. * Add directory to the list of places where to look for resource handlers
  156. *
  157. * @param string $prefix
  158. * @param string $dir
  159. */
  160. public static function addResourceDirectory ($prefix, $dir)
  161. {
  162. if (self::$_resourceLoader) {
  163. self::$_resourceLoader->addPrefixPath($prefix, $dir);
  164. }
  165. }
  166. /**
  167. * Get plugin class that handles this resource
  168. *
  169. * @param resource $resource Resource type
  170. *
  171. * @return string Class name
  172. */
  173. public static function getResourceParser ($resource)
  174. {
  175. if (self::$_resourceLoader) {
  176. $type = preg_replace("/[^A-Za-z0-9_]/", " ", get_resource_type($resource));
  177. $type = str_replace(" ", "", ucwords($type));
  178. return self::$_resourceLoader->load($type);
  179. }
  180. return false;
  181. }
  182. /**
  183. * Convert resource to a serializable object
  184. *
  185. * @param resource $resource
  186. *
  187. * @return mixed
  188. */
  189. public static function handleResource ($resource)
  190. {
  191. if (!self::$_resourceLoader) {
  192. require_once 'Zend/Amf/Exception.php';
  193. throw new Zend_Amf_Exception( 'Unable to handle resources - resource plugin loader not set' );
  194. }
  195. try {
  196. while (is_resource($resource)) {
  197. $resclass = self::getResourceParser($resource);
  198. if (!$resclass) {
  199. require_once 'Zend/Amf/Exception.php';
  200. throw new Zend_Amf_Exception( 'Can not serialize resource type: ' . get_resource_type($resource) );
  201. }
  202. $parser = new $resclass();
  203. if (is_callable(array( $parser, 'parse' ))) {
  204. $resource = $parser->parse($resource);
  205. } else {
  206. require_once 'Zend/Amf/Exception.php';
  207. throw new Zend_Amf_Exception( "Could not call parse() method on class $resclass" );
  208. }
  209. }
  210. return $resource;
  211. } catch (Zend_Amf_Exception $e) {
  212. throw new Zend_Amf_Exception( $e->getMessage(), $e->getCode(), $e );
  213. } catch (Exception $e) {
  214. require_once 'Zend/Amf/Exception.php';
  215. throw new Zend_Amf_Exception( 'Can not serialize resource type: ' . get_resource_type($resource), 0, $e );
  216. }
  217. }
  218. }