File indexing completed on 2024-12-22 05:37:01

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_Server
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  */
0020 
0021 /** Zend_Server_Interface */
0022 // require_once 'Zend/Server/Interface.php';
0023 
0024 /**
0025  * Zend_Server_Definition
0026  */
0027 // require_once 'Zend/Server/Definition.php';
0028 
0029 /**
0030  * Zend_Server_Method_Definition
0031  */
0032 // require_once 'Zend/Server/Method/Definition.php';
0033 
0034 /**
0035  * Zend_Server_Method_Callback
0036  */
0037 // require_once 'Zend/Server/Method/Callback.php';
0038 
0039 /**
0040  * Zend_Server_Method_Prototype
0041  */
0042 // require_once 'Zend/Server/Method/Prototype.php';
0043 
0044 /**
0045  * Zend_Server_Method_Parameter
0046  */
0047 // require_once 'Zend/Server/Method/Parameter.php';
0048 
0049 /**
0050  * Zend_Server_Abstract
0051  *
0052  * @category   Zend
0053  * @package    Zend_Server
0054  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0055  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0056  * @version    $Id$
0057  */
0058 abstract class Zend_Server_Abstract implements Zend_Server_Interface
0059 {
0060     /**
0061      * @deprecated
0062      * @var array List of PHP magic methods (lowercased)
0063      */
0064     protected static $magic_methods = array(
0065         '__call',
0066         '__clone',
0067         '__construct',
0068         '__destruct',
0069         '__get',
0070         '__isset',
0071         '__set',
0072         '__set_state',
0073         '__sleep',
0074         '__tostring',
0075         '__unset',
0076         '__wakeup',
0077     );
0078 
0079     /**
0080      * @var bool Flag; whether or not overwriting existing methods is allowed
0081      */
0082     protected $_overwriteExistingMethods = false;
0083 
0084     /**
0085      * @var Zend_Server_Definition
0086      */
0087     protected $_table;
0088 
0089     /**
0090      * Constructor
0091      *
0092      * Setup server description
0093      *
0094      * @return void
0095      */
0096     public function __construct()
0097     {
0098         $this->_table = new Zend_Server_Definition();
0099         $this->_table->setOverwriteExistingMethods($this->_overwriteExistingMethods);
0100     }
0101 
0102     /**
0103      * Returns a list of registered methods
0104      *
0105      * Returns an array of method definitions.
0106      *
0107      * @return Zend_Server_Definition
0108      */
0109     public function getFunctions()
0110     {
0111         return $this->_table;
0112     }
0113 
0114     /**
0115      * Lowercase a string
0116      *
0117      * Lowercase's a string by reference
0118      *
0119      * @deprecated
0120      * @param  string $string value
0121      * @param  string $key
0122      * @return string Lower cased string
0123      */
0124     public static function lowerCase(&$value, &$key)
0125     {
0126         trigger_error(__CLASS__ . '::' . __METHOD__ . '() is deprecated and will be removed in a future version', E_USER_NOTICE);
0127         return $value = strtolower($value);
0128     }
0129 
0130     /**
0131      * Build callback for method signature
0132      *
0133      * @param  Zend_Server_Reflection_Function_Abstract $reflection
0134      * @return Zend_Server_Method_Callback
0135      */
0136     protected function _buildCallback(Zend_Server_Reflection_Function_Abstract $reflection)
0137     {
0138         $callback = new Zend_Server_Method_Callback();
0139         if ($reflection instanceof Zend_Server_Reflection_Method) {
0140             $callback->setType($reflection->isStatic() ? 'static' : 'instance')
0141                      ->setClass($reflection->getDeclaringClass()->getName())
0142                      ->setMethod($reflection->getName());
0143         } elseif ($reflection instanceof Zend_Server_Reflection_Function) {
0144             $callback->setType('function')
0145                      ->setFunction($reflection->getName());
0146         }
0147         return $callback;
0148     }
0149 
0150     /**
0151      * Build a method signature
0152      *
0153      * @param  Zend_Server_Reflection_Function_Abstract $reflection
0154      * @param  null|string|object $class
0155      * @return Zend_Server_Method_Definition
0156      * @throws Zend_Server_Exception on duplicate entry
0157      */
0158     protected function _buildSignature(Zend_Server_Reflection_Function_Abstract $reflection, $class = null)
0159     {
0160         $ns         = $reflection->getNamespace();
0161         $name       = $reflection->getName();
0162         $method     = empty($ns) ? $name : $ns . '.' . $name;
0163 
0164         if (!$this->_overwriteExistingMethods && $this->_table->hasMethod($method)) {
0165             // require_once 'Zend/Server/Exception.php';
0166             throw new Zend_Server_Exception('Duplicate method registered: ' . $method);
0167         }
0168 
0169         $definition = new Zend_Server_Method_Definition();
0170         $definition->setName($method)
0171                    ->setCallback($this->_buildCallback($reflection))
0172                    ->setMethodHelp($reflection->getDescription())
0173                    ->setInvokeArguments($reflection->getInvokeArguments());
0174 
0175         foreach ($reflection->getPrototypes() as $proto) {
0176             $prototype = new Zend_Server_Method_Prototype();
0177             $prototype->setReturnType($this->_fixType($proto->getReturnType()));
0178             foreach ($proto->getParameters() as $parameter) {
0179                 $param = new Zend_Server_Method_Parameter(array(
0180                     'type'     => $this->_fixType($parameter->getType()),
0181                     'name'     => $parameter->getName(),
0182                     'optional' => $parameter->isOptional(),
0183                 ));
0184                 if ($parameter->isDefaultValueAvailable()) {
0185                     $param->setDefaultValue($parameter->getDefaultValue());
0186                 }
0187                 $prototype->addParameter($param);
0188             }
0189             $definition->addPrototype($prototype);
0190         }
0191         if (is_object($class)) {
0192             $definition->setObject($class);
0193         }
0194         $this->_table->addMethod($definition);
0195         return $definition;
0196     }
0197 
0198     /**
0199      * Dispatch method
0200      *
0201      * @param  Zend_Server_Method_Definition $invocable
0202      * @param  array $params
0203      * @return mixed
0204      */
0205     protected function _dispatch(Zend_Server_Method_Definition $invocable, array $params)
0206     {
0207         $callback = $invocable->getCallback();
0208         $type     = $callback->getType();
0209 
0210         if ('function' == $type) {
0211             $function = $callback->getFunction();
0212             return call_user_func_array($function, $params);
0213         }
0214 
0215         $class  = $callback->getClass();
0216         $method = $callback->getMethod();
0217 
0218         if ('static' == $type) {
0219             return call_user_func_array(array($class, $method), $params);
0220         }
0221 
0222         $object = $invocable->getObject();
0223         if (!is_object($object)) {
0224             $invokeArgs = $invocable->getInvokeArguments();
0225             if (!empty($invokeArgs)) {
0226                 $reflection = new ReflectionClass($class);
0227                 $object     = $reflection->newInstanceArgs($invokeArgs);
0228             } else {
0229                 $object = new $class;
0230             }
0231         }
0232         return call_user_func_array(array($object, $method), $params);
0233     }
0234 
0235     /**
0236      * Map PHP type to protocol type
0237      *
0238      * @param  string $type
0239      * @return string
0240      */
0241     abstract protected function _fixType($type);
0242 }