/framework/vendor/zend/Zend/Server/Abstract.php
PHP | 242 lines | 115 code | 23 blank | 104 comment | 12 complexity | e6a0cc03ab8653d4530f9167241951d3 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_Server 17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 18 * @license http://framework.zend.com/license/new-bsd New BSD License 19 */ 20 21/** Zend_Server_Interface */ 22require_once 'Zend/Server/Interface.php'; 23 24/** 25 * Zend_Server_Definition 26 */ 27require_once 'Zend/Server/Definition.php'; 28 29/** 30 * Zend_Server_Method_Definition 31 */ 32require_once 'Zend/Server/Method/Definition.php'; 33 34/** 35 * Zend_Server_Method_Callback 36 */ 37require_once 'Zend/Server/Method/Callback.php'; 38 39/** 40 * Zend_Server_Method_Prototype 41 */ 42require_once 'Zend/Server/Method/Prototype.php'; 43 44/** 45 * Zend_Server_Method_Parameter 46 */ 47require_once 'Zend/Server/Method/Parameter.php'; 48 49/** 50 * Zend_Server_Abstract 51 * 52 * @category Zend 53 * @package Zend_Server 54 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 55 * @license http://framework.zend.com/license/new-bsd New BSD License 56 * @version $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $ 57 */ 58abstract class Zend_Server_Abstract implements Zend_Server_Interface 59{ 60 /** 61 * @deprecated 62 * @var array List of PHP magic methods (lowercased) 63 */ 64 protected static $magic_methods = array( 65 '__call', 66 '__clone', 67 '__construct', 68 '__destruct', 69 '__get', 70 '__isset', 71 '__set', 72 '__set_state', 73 '__sleep', 74 '__tostring', 75 '__unset', 76 '__wakeup', 77 ); 78 79 /** 80 * @var bool Flag; whether or not overwriting existing methods is allowed 81 */ 82 protected $_overwriteExistingMethods = false; 83 84 /** 85 * @var Zend_Server_Definition 86 */ 87 protected $_table; 88 89 /** 90 * Constructor 91 * 92 * Setup server description 93 * 94 * @return void 95 */ 96 public function __construct() 97 { 98 $this->_table = new Zend_Server_Definition(); 99 $this->_table->setOverwriteExistingMethods($this->_overwriteExistingMethods); 100 } 101 102 /** 103 * Returns a list of registered methods 104 * 105 * Returns an array of method definitions. 106 * 107 * @return Zend_Server_Definition 108 */ 109 public function getFunctions() 110 { 111 return $this->_table; 112 } 113 114 /** 115 * Lowercase a string 116 * 117 * Lowercase's a string by reference 118 * 119 * @deprecated 120 * @param string $string value 121 * @param string $key 122 * @return string Lower cased string 123 */ 124 public static function lowerCase(&$value, &$key) 125 { 126 trigger_error(__CLASS__ . '::' . __METHOD__ . '() is deprecated and will be removed in a future version', E_USER_NOTICE); 127 return $value = strtolower($value); 128 } 129 130 /** 131 * Build callback for method signature 132 * 133 * @param Zend_Server_Reflection_Function_Abstract $reflection 134 * @return Zend_Server_Method_Callback 135 */ 136 protected function _buildCallback(Zend_Server_Reflection_Function_Abstract $reflection) 137 { 138 $callback = new Zend_Server_Method_Callback(); 139 if ($reflection instanceof Zend_Server_Reflection_Method) { 140 $callback->setType($reflection->isStatic() ? 'static' : 'instance') 141 ->setClass($reflection->getDeclaringClass()->getName()) 142 ->setMethod($reflection->getName()); 143 } elseif ($reflection instanceof Zend_Server_Reflection_Function) { 144 $callback->setType('function') 145 ->setFunction($reflection->getName()); 146 } 147 return $callback; 148 } 149 150 /** 151 * Build a method signature 152 * 153 * @param Zend_Server_Reflection_Function_Abstract $reflection 154 * @param null|string|object $class 155 * @return Zend_Server_Method_Definition 156 * @throws Zend_Server_Exception on duplicate entry 157 */ 158 protected function _buildSignature(Zend_Server_Reflection_Function_Abstract $reflection, $class = null) 159 { 160 $ns = $reflection->getNamespace(); 161 $name = $reflection->getName(); 162 $method = empty($ns) ? $name : $ns . '.' . $name; 163 164 if (!$this->_overwriteExistingMethods && $this->_table->hasMethod($method)) { 165 require_once 'Zend/Server/Exception.php'; 166 throw new Zend_Server_Exception('Duplicate method registered: ' . $method); 167 } 168 169 $definition = new Zend_Server_Method_Definition(); 170 $definition->setName($method) 171 ->setCallback($this->_buildCallback($reflection)) 172 ->setMethodHelp($reflection->getDescription()) 173 ->setInvokeArguments($reflection->getInvokeArguments()); 174 175 foreach ($reflection->getPrototypes() as $proto) { 176 $prototype = new Zend_Server_Method_Prototype(); 177 $prototype->setReturnType($this->_fixType($proto->getReturnType())); 178 foreach ($proto->getParameters() as $parameter) { 179 $param = new Zend_Server_Method_Parameter(array( 180 'type' => $this->_fixType($parameter->getType()), 181 'name' => $parameter->getName(), 182 'optional' => $parameter->isOptional(), 183 )); 184 if ($parameter->isDefaultValueAvailable()) { 185 $param->setDefaultValue($parameter->getDefaultValue()); 186 } 187 $prototype->addParameter($param); 188 } 189 $definition->addPrototype($prototype); 190 } 191 if (is_object($class)) { 192 $definition->setObject($class); 193 } 194 $this->_table->addMethod($definition); 195 return $definition; 196 } 197 198 /** 199 * Dispatch method 200 * 201 * @param Zend_Server_Method_Definition $invocable 202 * @param array $params 203 * @return mixed 204 */ 205 protected function _dispatch(Zend_Server_Method_Definition $invocable, array $params) 206 { 207 $callback = $invocable->getCallback(); 208 $type = $callback->getType(); 209 210 if ('function' == $type) { 211 $function = $callback->getFunction(); 212 return call_user_func_array($function, $params); 213 } 214 215 $class = $callback->getClass(); 216 $method = $callback->getMethod(); 217 218 if ('static' == $type) { 219 return call_user_func_array(array($class, $method), $params); 220 } 221 222 $object = $invocable->getObject(); 223 if (!is_object($object)) { 224 $invokeArgs = $invocable->getInvokeArguments(); 225 if (!empty($invokeArgs)) { 226 $reflection = new ReflectionClass($class); 227 $object = $reflection->newInstanceArgs($invokeArgs); 228 } else { 229 $object = new $class; 230 } 231 } 232 return call_user_func_array(array($object, $method), $params); 233 } 234 235 /** 236 * Map PHP type to protocol type 237 * 238 * @param string $type 239 * @return string 240 */ 241 abstract protected function _fixType($type); 242}