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

https://github.com/praveensingh85/MyEventDashboard1 · PHP · 231 lines · 106 code · 18 blank · 107 comment · 10 complexity · e47d4b66152873c52d5a488e12b42f0a 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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  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-2010 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. * @return object|false
  88. */
  89. public static function loadType($className)
  90. {
  91. $class = self::getMappedClassName($className);
  92. if(!$class) {
  93. $class = str_replace('.', '_', $className);
  94. }
  95. if (!class_exists($class)) {
  96. return "stdClass";
  97. }
  98. return $class;
  99. }
  100. /**
  101. * Looks up the supplied call name to its mapped class name
  102. *
  103. * @param string $className
  104. * @return string
  105. */
  106. public static function getMappedClassName($className)
  107. {
  108. $mappedName = array_search($className, self::$classMap);
  109. if ($mappedName) {
  110. return $mappedName;
  111. }
  112. $mappedName = array_search($className, array_flip(self::$classMap));
  113. if ($mappedName) {
  114. return $mappedName;
  115. }
  116. return false;
  117. }
  118. /**
  119. * Map PHP class names to ActionScript class names
  120. *
  121. * Allows users to map the class names of there action script classes
  122. * to the equivelent php class name. Used in deserialization to load a class
  123. * and serialiation to set the class name of the returned object.
  124. *
  125. * @param string $asClassName
  126. * @param string $phpClassName
  127. * @return void
  128. */
  129. public static function setMapping($asClassName, $phpClassName)
  130. {
  131. self::$classMap[$asClassName] = $phpClassName;
  132. }
  133. /**
  134. * Reset type map
  135. *
  136. * @return void
  137. */
  138. public static function resetMap()
  139. {
  140. self::$classMap = self::$_defaultClassMap;
  141. }
  142. /**
  143. * Set loader for resource type handlers
  144. *
  145. * @param Zend_Loader_PluginLoader_Interface $loader
  146. */
  147. public static function setResourceLoader(Zend_Loader_PluginLoader_Interface $loader)
  148. {
  149. self::$_resourceLoader = $loader;
  150. }
  151. /**
  152. * Add directory to the list of places where to look for resource handlers
  153. *
  154. * @param string $prefix
  155. * @param string $dir
  156. */
  157. public static function addResourceDirectory($prefix, $dir)
  158. {
  159. if(self::$_resourceLoader) {
  160. self::$_resourceLoader->addPrefixPath($prefix, $dir);
  161. }
  162. }
  163. /**
  164. * Get plugin class that handles this resource
  165. *
  166. * @param resource $resource Resource type
  167. * @return string Class name
  168. */
  169. public static function getResourceParser($resource)
  170. {
  171. if(self::$_resourceLoader) {
  172. $type = preg_replace("/[^A-Za-z0-9_]/", " ", get_resource_type($resource));
  173. $type = str_replace(" ","", ucwords($type));
  174. return self::$_resourceLoader->load($type);
  175. }
  176. return false;
  177. }
  178. /**
  179. * Convert resource to a serializable object
  180. *
  181. * @param resource $resource
  182. * @return mixed
  183. */
  184. public static function handleResource($resource)
  185. {
  186. if(!self::$_resourceLoader) {
  187. require_once 'Zend/Amf/Exception.php';
  188. throw new Zend_Amf_Exception('Unable to handle resources - resource plugin loader not set');
  189. }
  190. try {
  191. while(is_resource($resource)) {
  192. $resclass = self::getResourceParser($resource);
  193. if(!$resclass) {
  194. require_once 'Zend/Amf/Exception.php';
  195. throw new Zend_Amf_Exception('Can not serialize resource type: '. get_resource_type($resource));
  196. }
  197. $parser = new $resclass();
  198. if(is_callable(array($parser, 'parse'))) {
  199. $resource = $parser->parse($resource);
  200. } else {
  201. require_once 'Zend/Amf/Exception.php';
  202. throw new Zend_Amf_Exception("Could not call parse() method on class $resclass");
  203. }
  204. }
  205. return $resource;
  206. } catch(Zend_Amf_Exception $e) {
  207. throw new Zend_Amf_Exception($e->getMessage(), $e->getCode(), $e);
  208. } catch(Exception $e) {
  209. require_once 'Zend/Amf/Exception.php';
  210. throw new Zend_Amf_Exception('Can not serialize resource type: '. get_resource_type($resource), 0, $e);
  211. }
  212. }
  213. }