File indexing completed on 2024-06-16 05:29:51

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Amf
0017  * @subpackage Parse
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Amf_Value_Messaging_AcknowledgeMessage
0025  */
0026 // require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
0027 /**
0028  * @see Zend_Amf_Value_Messaging_AsyncMessage
0029  */
0030 // require_once 'Zend/Amf/Value/Messaging/AsyncMessage.php';
0031 /**
0032  * @see Zend_Amf_Value_Messaging_CommandMessage
0033  */
0034 // require_once 'Zend/Amf/Value/Messaging/CommandMessage.php';
0035 /**
0036  * @see Zend_Amf_Value_Messaging_ErrorMessage
0037  */
0038 // require_once 'Zend/Amf/Value/Messaging/ErrorMessage.php';
0039 /**
0040  * @see Zend_Amf_Value_Messaging_RemotingMessage
0041  */
0042 // require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
0043 
0044 /**
0045  * Loads a local class and executes the instantiation of that class.
0046  *
0047  * @todo       PHP 5.3 can drastically change this class w/ namespace and the new call_user_func w/ namespace
0048  * @package    Zend_Amf
0049  * @subpackage Parse
0050  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0051  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0052  */
0053 final class Zend_Amf_Parse_TypeLoader
0054 {
0055     /**
0056      * @var string callback class
0057      */
0058     public static $callbackClass;
0059 
0060     /**
0061      * @var array AMF class map
0062      */
0063     public static $classMap = array (
0064         'flex.messaging.messages.AcknowledgeMessage' => 'Zend_Amf_Value_Messaging_AcknowledgeMessage',
0065         'flex.messaging.messages.ErrorMessage'       => 'Zend_Amf_Value_Messaging_AsyncMessage',
0066         'flex.messaging.messages.CommandMessage'     => 'Zend_Amf_Value_Messaging_CommandMessage',
0067         'flex.messaging.messages.ErrorMessage'       => 'Zend_Amf_Value_Messaging_ErrorMessage',
0068         'flex.messaging.messages.RemotingMessage'    => 'Zend_Amf_Value_Messaging_RemotingMessage',
0069         'flex.messaging.io.ArrayCollection'          => 'Zend_Amf_Value_Messaging_ArrayCollection',
0070     );
0071 
0072     /**
0073      * @var array Default class map
0074      */
0075     protected static $_defaultClassMap = array(
0076         'flex.messaging.messages.AcknowledgeMessage' => 'Zend_Amf_Value_Messaging_AcknowledgeMessage',
0077         'flex.messaging.messages.ErrorMessage'       => 'Zend_Amf_Value_Messaging_AsyncMessage',
0078         'flex.messaging.messages.CommandMessage'     => 'Zend_Amf_Value_Messaging_CommandMessage',
0079         'flex.messaging.messages.ErrorMessage'       => 'Zend_Amf_Value_Messaging_ErrorMessage',
0080         'flex.messaging.messages.RemotingMessage'    => 'Zend_Amf_Value_Messaging_RemotingMessage',
0081         'flex.messaging.io.ArrayCollection'          => 'Zend_Amf_Value_Messaging_ArrayCollection',
0082     );
0083 
0084     /**
0085      * @var Zend_Loader_PluginLoader_Interface
0086      */
0087     protected static $_resourceLoader = null;
0088 
0089 
0090     /**
0091      * Load the mapped class type into a callback.
0092      *
0093      * @param  string $className
0094      * @return object|false
0095      */
0096     public static function loadType($className)
0097     {
0098         $class    = self::getMappedClassName($className);
0099         if(!$class) {
0100             $class = str_replace('.', '_', $className);
0101         }
0102         if (!class_exists($class)) {
0103             return "stdClass";
0104         }
0105         return $class;
0106     }
0107 
0108     /**
0109      * Looks up the supplied call name to its mapped class name
0110      *
0111      * @param  string $className
0112      * @return string
0113      */
0114     public static function getMappedClassName($className)
0115     {
0116         $mappedName = array_search($className, self::$classMap);
0117 
0118         if ($mappedName) {
0119             return $mappedName;
0120         }
0121 
0122         $mappedName = array_search($className, array_flip(self::$classMap));
0123 
0124         if ($mappedName) {
0125             return $mappedName;
0126         }
0127 
0128         return false;
0129     }
0130 
0131     /**
0132      * Map PHP class names to ActionScript class names
0133      *
0134      * Allows users to map the class names of there action script classes
0135      * to the equivelent php class name. Used in deserialization to load a class
0136      * and serialiation to set the class name of the returned object.
0137      *
0138      * @param  string $asClassName
0139      * @param  string $phpClassName
0140      * @return void
0141      */
0142     public static function setMapping($asClassName, $phpClassName)
0143     {
0144         self::$classMap[$asClassName] = $phpClassName;
0145     }
0146 
0147     /**
0148      * Reset type map
0149      *
0150      * @return void
0151      */
0152     public static function resetMap()
0153     {
0154         self::$classMap = self::$_defaultClassMap;
0155     }
0156 
0157     /**
0158      * Set loader for resource type handlers
0159      *
0160      * @param Zend_Loader_PluginLoader_Interface $loader
0161      */
0162     public static function setResourceLoader(Zend_Loader_PluginLoader_Interface $loader)
0163     {
0164         self::$_resourceLoader = $loader;
0165     }
0166 
0167     /**
0168      * Add directory to the list of places where to look for resource handlers
0169      *
0170      * @param string $prefix
0171      * @param string $dir
0172      */
0173     public static function addResourceDirectory($prefix, $dir)
0174     {
0175         if(self::$_resourceLoader) {
0176             self::$_resourceLoader->addPrefixPath($prefix, $dir);
0177         }
0178     }
0179 
0180     /**
0181      * Get plugin class that handles this resource
0182      *
0183      * @param resource $resource Resource type
0184      * @return string Class name
0185      */
0186     public static function getResourceParser($resource)
0187     {
0188         if(self::$_resourceLoader) {
0189             $type = preg_replace("/[^A-Za-z0-9_]/", " ", get_resource_type($resource));
0190             $type = str_replace(" ","", ucwords($type));
0191             return self::$_resourceLoader->load($type);
0192         }
0193         return false;
0194     }
0195 
0196     /**
0197      * Convert resource to a serializable object
0198      *
0199      * @param resource $resource
0200      * @return mixed
0201      */
0202     public static function handleResource($resource)
0203     {
0204         if(!self::$_resourceLoader) {
0205             // require_once 'Zend/Amf/Exception.php';
0206             throw new Zend_Amf_Exception('Unable to handle resources - resource plugin loader not set');
0207         }
0208         try {
0209             while(is_resource($resource)) {
0210                 $resclass = self::getResourceParser($resource);
0211                 if(!$resclass) {
0212                     // require_once 'Zend/Amf/Exception.php';
0213                     throw new Zend_Amf_Exception('Can not serialize resource type: '. get_resource_type($resource));
0214                 }
0215                 $parser = new $resclass();
0216                 if(is_callable(array($parser, 'parse'))) {
0217                     $resource = $parser->parse($resource);
0218                 } else {
0219                     // require_once 'Zend/Amf/Exception.php';
0220                     throw new Zend_Amf_Exception("Could not call parse() method on class $resclass");
0221                 }
0222             }
0223             return $resource;
0224         } catch(Zend_Amf_Exception $e) {
0225             throw new Zend_Amf_Exception($e->getMessage(), $e->getCode(), $e);
0226         } catch(Exception $e) {
0227             // require_once 'Zend/Amf/Exception.php';
0228             throw new Zend_Amf_Exception('Can not serialize resource type: '. get_resource_type($resource), 0, $e);
0229         }
0230     }
0231 }