adding zend project folders into old campcaster.

This commit is contained in:
naomiaro 2010-12-07 14:19:27 -05:00
parent 56abfaf28e
commit 7ef0c18b26
4045 changed files with 1054952 additions and 0 deletions

View file

@ -0,0 +1,102 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Cache.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/** Zend_Server_Cache */
require_once 'Zend/Server/Cache.php';
/**
* Zend_Json_Server_Cache: cache Zend_Json_Server server definition and SMD
*
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Cache extends Zend_Server_Cache
{
/**
* Cache a service map description (SMD) to a file
*
* Returns true on success, false on failure
*
* @param string $filename
* @param Zend_Json_Server $server
* @return boolean
*/
public static function saveSmd($filename, Zend_Json_Server $server)
{
if (!is_string($filename)
|| (!file_exists($filename) && !is_writable(dirname($filename))))
{
return false;
}
if (0 === @file_put_contents($filename, $server->getServiceMap()->toJson())) {
return false;
}
return true;
}
/**
* Retrieve a cached SMD
*
* On success, returns the cached SMD (a JSON string); an failure, returns
* boolean false.
*
* @param string $filename
* @return string|false
*/
public static function getSmd($filename)
{
if (!is_string($filename)
|| !file_exists($filename)
|| !is_readable($filename))
{
return false;
}
if (false === ($smd = @file_get_contents($filename))) {
return false;
}
return $smd;
}
/**
* Delete a file containing a cached SMD
*
* @param string $filename
* @return bool
*/
public static function deleteSmd($filename)
{
if (is_string($filename) && file_exists($filename)) {
unlink($filename);
return true;
}
return false;
}
}

View file

@ -0,0 +1,198 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Error.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Error
{
const ERROR_PARSE = -32768;
const ERROR_INVALID_REQUEST = -32600;
const ERROR_INVALID_METHOD = -32601;
const ERROR_INVALID_PARAMS = -32602;
const ERROR_INTERNAL = -32603;
const ERROR_OTHER = -32000;
/**
* Allowed error codes
* @var array
*/
protected $_allowedCodes = array(
self::ERROR_PARSE,
self::ERROR_INVALID_REQUEST,
self::ERROR_INVALID_METHOD,
self::ERROR_INVALID_PARAMS,
self::ERROR_INTERNAL,
self::ERROR_OTHER,
);
/**
* Current code
* @var int
*/
protected $_code = -32000;
/**
* Error data
* @var mixed
*/
protected $_data;
/**
* Error message
* @var string
*/
protected $_message;
/**
* Constructor
*
* @param string $message
* @param int $code
* @param mixed $data
* @return void
*/
public function __construct($message = null, $code = -32000, $data = null)
{
$this->setMessage($message)
->setCode($code)
->setData($data);
}
/**
* Set error code
*
* @param int $code
* @return Zend_Json_Server_Error
*/
public function setCode($code)
{
if (!is_scalar($code)) {
return $this;
}
$code = (int) $code;
if (in_array($code, $this->_allowedCodes)) {
$this->_code = $code;
} elseif (in_array($code, range(-32099, -32000))) {
$this->_code = $code;
}
return $this;
}
/**
* Get error code
*
* @return int|null
*/
public function getCode()
{
return $this->_code;
}
/**
* Set error message
*
* @param string $message
* @return Zend_Json_Server_Error
*/
public function setMessage($message)
{
if (!is_scalar($message)) {
return $this;
}
$this->_message = (string) $message;
return $this;
}
/**
* Get error message
*
* @return string
*/
public function getMessage()
{
return $this->_message;
}
/**
* Set error data
*
* @param mixed $data
* @return Zend_Json_Server_Error
*/
public function setData($data)
{
$this->_data = $data;
return $this;
}
/**
* Get error data
*
* @return mixed
*/
public function getData()
{
return $this->_data;
}
/**
* Cast error to array
*
* @return array
*/
public function toArray()
{
return array(
'code' => $this->getCode(),
'message' => $this->getMessage(),
'data' => $this->getData(),
);
}
/**
* Cast error to JSON
*
* @return string
*/
public function toJson()
{
require_once 'Zend/Json.php';
return Zend_Json::encode($this->toArray());
}
/**
* Cast to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Exception.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/** Zend_Json_Exception */
require_once 'Zend/Json/Exception.php';
/**
* Zend_Json_Server exceptions
*
* @uses Zend_Json_Exception
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Exception extends Zend_Json_Exception
{
}

View file

@ -0,0 +1,289 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Request.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Request
{
/**
* Request ID
* @var mixed
*/
protected $_id;
/**
* Flag
* @var bool
*/
protected $_isMethodError = false;
/**
* Requested method
* @var string
*/
protected $_method;
/**
* Regex for method
* @var string
*/
protected $_methodRegex = '/^[a-z][a-z0-9_.]*$/i';
/**
* Request parameters
* @var array
*/
protected $_params = array();
/**
* JSON-RPC version of request
* @var string
*/
protected $_version = '1.0';
/**
* Set request state
*
* @param array $options
* @return Zend_Json_Server_Request
*/
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
} elseif ($key == 'jsonrpc') {
$this->setVersion($value);
}
}
return $this;
}
/**
* Add a parameter to the request
*
* @param mixed $value
* @param string $key
* @return Zend_Json_Server_Request
*/
public function addParam($value, $key = null)
{
if ((null === $key) || !is_string($key)) {
$index = count($this->_params);
$this->_params[$index] = $value;
} else {
$this->_params[$key] = $value;
}
return $this;
}
/**
* Add many params
*
* @param array $params
* @return Zend_Json_Server_Request
*/
public function addParams(array $params)
{
foreach ($params as $key => $value) {
$this->addParam($value, $key);
}
return $this;
}
/**
* Overwrite params
*
* @param array $params
* @return Zend_Json_Server_Request
*/
public function setParams(array $params)
{
$this->_params = array();
return $this->addParams($params);
}
/**
* Retrieve param by index or key
*
* @param int|string $index
* @return mixed|null Null when not found
*/
public function getParam($index)
{
if (array_key_exists($index, $this->_params)) {
return $this->_params[$index];
}
return null;
}
/**
* Retrieve parameters
*
* @return array
*/
public function getParams()
{
return $this->_params;
}
/**
* Set request method
*
* @param string $name
* @return Zend_Json_Server_Request
*/
public function setMethod($name)
{
if (!preg_match($this->_methodRegex, $name)) {
$this->_isMethodError = true;
} else {
$this->_method = $name;
}
return $this;
}
/**
* Get request method name
*
* @return string
*/
public function getMethod()
{
return $this->_method;
}
/**
* Was a bad method provided?
*
* @return bool
*/
public function isMethodError()
{
return $this->_isMethodError;
}
/**
* Set request identifier
*
* @param mixed $name
* @return Zend_Json_Server_Request
*/
public function setId($name)
{
$this->_id = (string) $name;
return $this;
}
/**
* Retrieve request identifier
*
* @return mixed
*/
public function getId()
{
return $this->_id;
}
/**
* Set JSON-RPC version
*
* @param string $version
* @return Zend_Json_Server_Request
*/
public function setVersion($version)
{
if ('2.0' == $version) {
$this->_version = '2.0';
} else {
$this->_version = '1.0';
}
return $this;
}
/**
* Retrieve JSON-RPC version
*
* @return string
*/
public function getVersion()
{
return $this->_version;
}
/**
* Set request state based on JSON
*
* @param string $json
* @return void
*/
public function loadJson($json)
{
require_once 'Zend/Json.php';
$options = Zend_Json::decode($json);
$this->setOptions($options);
}
/**
* Cast request to JSON
*
* @return string
*/
public function toJson()
{
$jsonArray = array(
'method' => $this->getMethod()
);
if (null !== ($id = $this->getId())) {
$jsonArray['id'] = $id;
}
$params = $this->getParams();
if (!empty($params)) {
$jsonArray['params'] = $params;
}
if ('2.0' == $this->getVersion()) {
$jsonArray['jsonrpc'] = '2.0';
}
require_once 'Zend/Json.php';
return Zend_Json::encode($jsonArray);
}
/**
* Cast request to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Http.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_Json_Server_Request
*/
require_once 'Zend/Json/Server/Request.php';
/**
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Request_Http extends Zend_Json_Server_Request
{
/**
* Raw JSON pulled from POST body
* @var string
*/
protected $_rawJson;
/**
* Constructor
*
* Pull JSON request from raw POST body and use to populate request.
*
* @return void
*/
public function __construct()
{
$json = file_get_contents('php://input');
$this->_rawJson = $json;
if (!empty($json)) {
$this->loadJson($json);
}
}
/**
* Get JSON from raw POST body
*
* @return string
*/
public function getRawJson()
{
return $this->_rawJson;
}
}

View file

@ -0,0 +1,250 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Response.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Response
{
/**
* Response error
* @var null|Zend_Json_Server_Error
*/
protected $_error;
/**
* Request ID
* @var mixed
*/
protected $_id;
/**
* Result
* @var mixed
*/
protected $_result;
/**
* Service map
* @var Zend_Json_Server_Smd
*/
protected $_serviceMap;
/**
* JSON-RPC version
* @var string
*/
protected $_version;
/**
* Set result
*
* @param mixed $value
* @return Zend_Json_Server_Response
*/
public function setResult($value)
{
$this->_result = $value;
return $this;
}
/**
* Get result
*
* @return mixed
*/
public function getResult()
{
return $this->_result;
}
// RPC error, if response results in fault
/**
* Set result error
*
* @param Zend_Json_Server_Error $error
* @return Zend_Json_Server_Response
*/
public function setError(Zend_Json_Server_Error $error)
{
$this->_error = $error;
return $this;
}
/**
* Get response error
*
* @return null|Zend_Json_Server_Error
*/
public function getError()
{
return $this->_error;
}
/**
* Is the response an error?
*
* @return bool
*/
public function isError()
{
return $this->getError() instanceof Zend_Json_Server_Error;
}
/**
* Set request ID
*
* @param mixed $name
* @return Zend_Json_Server_Response
*/
public function setId($name)
{
$this->_id = $name;
return $this;
}
/**
* Get request ID
*
* @return mixed
*/
public function getId()
{
return $this->_id;
}
/**
* Set JSON-RPC version
*
* @param string $version
* @return Zend_Json_Server_Response
*/
public function setVersion($version)
{
$version = (string) $version;
if ('2.0' == $version) {
$this->_version = '2.0';
} else {
$this->_version = null;
}
return $this;
}
/**
* Retrieve JSON-RPC version
*
* @return string
*/
public function getVersion()
{
return $this->_version;
}
/**
* Cast to JSON
*
* @return string
*/
public function toJson()
{
if ($this->isError()) {
$response = array(
'result' => null,
'error' => $this->getError()->toArray(),
'id' => $this->getId(),
);
} else {
$response = array(
'result' => $this->getResult(),
'id' => $this->getId(),
'error' => null,
);
}
if (null !== ($version = $this->getVersion())) {
$response['jsonrpc'] = $version;
}
require_once 'Zend/Json.php';
return Zend_Json::encode($response);
}
/**
* Retrieve args
*
* @return mixed
*/
public function getArgs()
{
return $this->_args;
}
/**
* Set args
*
* @param mixed $args
* @return self
*/
public function setArgs($args)
{
$this->_args = $args;
return $this;
}
/**
* Set service map object
*
* @param Zend_Json_Server_Smd $serviceMap
* @return Zend_Json_Server_Response
*/
public function setServiceMap($serviceMap)
{
$this->_serviceMap = $serviceMap;
return $this;
}
/**
* Retrieve service map
*
* @return Zend_Json_Server_Smd|null
*/
public function getServiceMap()
{
return $this->_serviceMap;
}
/**
* Cast to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View file

@ -0,0 +1,81 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Http.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @see Zend_Json_Server_Response
*/
require_once 'Zend/Json/Server/Response.php';
/**
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Response_Http extends Zend_Json_Server_Response
{
/**
* Emit JSON
*
* Send appropriate HTTP headers. If no Id, then return an empty string.
*
* @return string
*/
public function toJson()
{
$this->sendHeaders();
if (!$this->isError() && null === $this->getId()) {
return '';
}
return parent::toJson();
}
/**
* Send headers
*
* If headers are already sent, do nothing. If null ID, send HTTP 204
* header. Otherwise, send content type header based on content type of
* service map.
*
* @return void
*/
public function sendHeaders()
{
if (headers_sent()) {
return;
}
if (!$this->isError() && (null === $this->getId())) {
header('HTTP/1.1 204 No Content');
return;
}
if (null === ($smd = $this->getServiceMap())) {
return;
}
$contentType = $smd->getContentType();
if (!empty($contentType)) {
header('Content-Type: ' . $contentType);
}
}
}

View file

@ -0,0 +1,480 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Smd.php 20096 2010-01-06 02:05:09Z bkarwin $
*/
/**
* @category Zend
* @package Zend_Json
* @subpackage Server
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Smd
{
const ENV_JSONRPC_1 = 'JSON-RPC-1.0';
const ENV_JSONRPC_2 = 'JSON-RPC-2.0';
const SMD_VERSION = '2.0';
/**
* Content type
* @var string
*/
protected $_contentType = 'application/json';
/**
* Content type regex
* @var string
*/
protected $_contentTypeRegex = '#[a-z]+/[a-z][a-z-]+#i';
/**
* Service description
* @var string
*/
protected $_description;
/**
* Generate Dojo-compatible SMD
* @var bool
*/
protected $_dojoCompatible = false;
/**
* Current envelope
* @var string
*/
protected $_envelope = self::ENV_JSONRPC_1;
/**
* Allowed envelope types
* @var array
*/
protected $_envelopeTypes = array(
self::ENV_JSONRPC_1,
self::ENV_JSONRPC_2,
);
/**
* Service id
* @var string
*/
protected $_id;
/**
* Services offerred
* @var array
*/
protected $_services = array();
/**
* Service target
* @var string
*/
protected $_target;
/**
* Global transport
* @var string
*/
protected $_transport = 'POST';
/**
* Allowed transport types
* @var array
*/
protected $_transportTypes = array('POST');
/**
* Set object state via options
*
* @param array $options
* @return Zend_Json_Server_Smd
*/
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
/**
* Set transport
*
* @param string $transport
* @return Zend_Json_Server_Smd
*/
public function setTransport($transport)
{
if (!in_array($transport, $this->_transportTypes)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception(sprintf('Invalid transport "%s" specified', $transport));
}
$this->_transport = $transport;
return $this;
}
/**
* Get transport
*
* @return string
*/
public function getTransport()
{
return $this->_transport;
}
/**
* Set envelope
*
* @param string $envelopeType
* @return Zend_Json_Server_Smd
*/
public function setEnvelope($envelopeType)
{
if (!in_array($envelopeType, $this->_envelopeTypes)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception(sprintf('Invalid envelope type "%s"', $envelopeType));
}
$this->_envelope = $envelopeType;
return $this;
}
/**
* Retrieve envelope
*
* @return string
*/
public function getEnvelope()
{
return $this->_envelope;
}
// Content-Type of response; default to application/json
/**
* Set content type
*
* @param string $type
* @return Zend_Json_Server_Smd
*/
public function setContentType($type)
{
if (!preg_match($this->_contentTypeRegex, $type)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception(sprintf('Invalid content type "%s" specified', $type));
}
$this->_contentType = $type;
return $this;
}
/**
* Retrieve content type
*
* @return string
*/
public function getContentType()
{
return $this->_contentType;
}
/**
* Set service target
*
* @param string $target
* @return Zend_Json_Server_Smd
*/
public function setTarget($target)
{
$this->_target = (string) $target;
return $this;
}
/**
* Retrieve service target
*
* @return string
*/
public function getTarget()
{
return $this->_target;
}
/**
* Set service ID
*
* @param string $Id
* @return Zend_Json_Server_Smd
*/
public function setId($id)
{
$this->_id = (string) $id;
return $this->_id;
}
/**
* Get service id
*
* @return string
*/
public function getId()
{
return $this->_id;
}
/**
* Set service description
*
* @param string $description
* @return Zend_Json_Server_Smd
*/
public function setDescription($description)
{
$this->_description = (string) $description;
return $this->_description;
}
/**
* Get service description
*
* @return string
*/
public function getDescription()
{
return $this->_description;
}
/**
* Indicate whether or not to generate Dojo-compatible SMD
*
* @param bool $flag
* @return Zend_Json_Server_Smd
*/
public function setDojoCompatible($flag)
{
$this->_dojoCompatible = (bool) $flag;
return $this;
}
/**
* Is this a Dojo compatible SMD?
*
* @return bool
*/
public function isDojoCompatible()
{
return $this->_dojoCompatible;
}
/**
* Add Service
*
* @param Zend_Json_Server_Smd_Service|array $service
* @return void
*/
public function addService($service)
{
require_once 'Zend/Json/Server/Smd/Service.php';
if ($service instanceof Zend_Json_Server_Smd_Service) {
$name = $service->getName();
} elseif (is_array($service)) {
$service = new Zend_Json_Server_Smd_Service($service);
$name = $service->getName();
} else {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('Invalid service passed to addService()');
}
if (array_key_exists($name, $this->_services)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('Attempt to register a service already registered detected');
}
$this->_services[$name] = $service;
return $this;
}
/**
* Add many services
*
* @param array $services
* @return Zend_Json_Server_Smd
*/
public function addServices(array $services)
{
foreach ($services as $service) {
$this->addService($service);
}
return $this;
}
/**
* Overwrite existing services with new ones
*
* @param array $services
* @return Zend_Json_Server_Smd
*/
public function setServices(array $services)
{
$this->_services = array();
return $this->addServices($services);
}
/**
* Get service object
*
* @param string $name
* @return false|Zend_Json_Server_Smd_Service
*/
public function getService($name)
{
if (array_key_exists($name, $this->_services)) {
return $this->_services[$name];
}
return false;
}
/**
* Return services
*
* @return array
*/
public function getServices()
{
return $this->_services;
}
/**
* Remove service
*
* @param string $name
* @return boolean
*/
public function removeService($name)
{
if (array_key_exists($name, $this->_services)) {
unset($this->_services[$name]);
return true;
}
return false;
}
/**
* Cast to array
*
* @return array
*/
public function toArray()
{
if ($this->isDojoCompatible()) {
return $this->toDojoArray();
}
$transport = $this->getTransport();
$envelope = $this->getEnvelope();
$contentType = $this->getContentType();
$SMDVersion = self::SMD_VERSION;
$service = compact('transport', 'envelope', 'contentType', 'SMDVersion');
if (null !== ($target = $this->getTarget())) {
$service['target'] = $target;
}
if (null !== ($id = $this->getId())) {
$service['id'] = $id;
}
$services = $this->getServices();
if (!empty($services)) {
$service['services'] = array();
foreach ($services as $name => $svc) {
$svc->setEnvelope($envelope);
$service['services'][$name] = $svc->toArray();
}
$service['methods'] = $service['services'];
}
return $service;
}
/**
* Export to DOJO-compatible SMD array
*
* @return array
*/
public function toDojoArray()
{
$SMDVersion = '.1';
$serviceType = 'JSON-RPC';
$service = compact('SMDVersion', 'serviceType');
$target = $this->getTarget();
$services = $this->getServices();
if (!empty($services)) {
$service['methods'] = array();
foreach ($services as $name => $svc) {
$method = array(
'name' => $name,
'serviceURL' => $target,
);
$params = array();
foreach ($svc->getParams() as $param) {
$paramName = array_key_exists('name', $param) ? $param['name'] : $param['type'];
$params[] = array(
'name' => $paramName,
'type' => $param['type'],
);
}
if (!empty($params)) {
$method['parameters'] = $params;
}
$service['methods'][] = $method;
}
}
return $service;
}
/**
* Cast to JSON
*
* @return string
*/
public function toJson()
{
require_once 'Zend/Json.php';
return Zend_Json::encode($this->toArray());
}
/**
* Cast to string (JSON)
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
}

View file

@ -0,0 +1,473 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Json
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* @see Zend_Json_Server_Smd
*/
require_once 'Zend/Json/Server/Smd.php';
/**
* Create Service Mapping Description for a method
*
* @package Zend_Json
* @subpackage Server
* @version $Id: Service.php 20096 2010-01-06 02:05:09Z bkarwin $
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Json_Server_Smd_Service
{
/**#@+
* Service metadata
* @var string
*/
protected $_envelope = Zend_Json_Server_Smd::ENV_JSONRPC_1;
protected $_name;
protected $_return;
protected $_target;
protected $_transport = 'POST';
/**#@-*/
/**
* Allowed envelope types
* @var array
*/
protected $_envelopeTypes = array(
Zend_Json_Server_Smd::ENV_JSONRPC_1,
Zend_Json_Server_Smd::ENV_JSONRPC_2,
);
/**
* Regex for names
* @var string
*/
protected $_nameRegex = '/^[a-z][a-z0-9._]+$/i';
/**
* Parameter option types
* @var array
*/
protected $_paramOptionTypes = array(
'name' => 'is_string',
'optional' => 'is_bool',
'default' => null,
'description' => 'is_string',
);
/**
* Service params
* @var array
*/
protected $_params = array();
/**
* Mapping of parameter types to JSON-RPC types
* @var array
*/
protected $_paramMap = array(
'any' => 'any',
'arr' => 'array',
'array' => 'array',
'assoc' => 'object',
'bool' => 'boolean',
'boolean' => 'boolean',
'dbl' => 'float',
'double' => 'float',
'false' => 'boolean',
'float' => 'float',
'hash' => 'object',
'integer' => 'integer',
'int' => 'integer',
'mixed' => 'any',
'nil' => 'null',
'null' => 'null',
'object' => 'object',
'string' => 'string',
'str' => 'string',
'struct' => 'object',
'true' => 'boolean',
'void' => 'null',
);
/**
* Allowed transport types
* @var array
*/
protected $_transportTypes = array(
'POST',
);
/**
* Constructor
*
* @param string|array $spec
* @return void
* @throws Zend_Json_Server_Exception if no name provided
*/
public function __construct($spec)
{
if (is_string($spec)) {
$this->setName($spec);
} elseif (is_array($spec)) {
$this->setOptions($spec);
}
if (null == $this->getName()) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('SMD service description requires a name; none provided');
}
}
/**
* Set object state
*
* @param array $options
* @return Zend_Json_Server_Smd_Service
*/
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
if ('options' == strtolower($key)) {
continue;
}
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
/**
* Set service name
*
* @param string $name
* @return Zend_Json_Server_Smd_Service
* @throws Zend_Json_Server_Exception
*/
public function setName($name)
{
$name = (string) $name;
if (!preg_match($this->_nameRegex, $name)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception(sprintf('Invalid name "%s" provided for service; must follow PHP method naming conventions', $name));
}
$this->_name = $name;
return $this;
}
/**
* Retrieve name
*
* @return string
*/
public function getName()
{
return $this->_name;
}
/**
* Set Transport
*
* Currently limited to POST
*
* @param string $transport
* @return Zend_Json_Server_Smd_Service
*/
public function setTransport($transport)
{
if (!in_array($transport, $this->_transportTypes)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception(sprintf('Invalid transport "%s"; please select one of (%s)', $transport, implode(', ', $this->_transportTypes)));
}
$this->_transport = $transport;
return $this;
}
/**
* Get transport
*
* @return string
*/
public function getTransport()
{
return $this->_transport;
}
/**
* Set service target
*
* @param string $target
* @return Zend_Json_Server_Smd_Service
*/
public function setTarget($target)
{
$this->_target = (string) $target;
return $this;
}
/**
* Get service target
*
* @return string
*/
public function getTarget()
{
return $this->_target;
}
/**
* Set envelope type
*
* @param string $envelopeType
* @return Zend_Json_Server_Smd_Service
*/
public function setEnvelope($envelopeType)
{
if (!in_array($envelopeType, $this->_envelopeTypes)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception(sprintf('Invalid envelope type "%s"; please specify one of (%s)', $envelopeType, implode(', ', $this->_envelopeTypes)));
}
$this->_envelope = $envelopeType;
return $this;
}
/**
* Get envelope type
*
* @return string
*/
public function getEnvelope()
{
return $this->_envelope;
}
/**
* Add a parameter to the service
*
* @param string|array $type
* @param array $options
* @param int|null $order
* @return Zend_Json_Server_Smd_Service
*/
public function addParam($type, array $options = array(), $order = null)
{
if (is_string($type)) {
$type = $this->_validateParamType($type);
} elseif (is_array($type)) {
foreach ($type as $key => $paramType) {
$type[$key] = $this->_validateParamType($paramType);
}
} else {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('Invalid param type provided');
}
$paramOptions = array(
'type' => $type,
);
foreach ($options as $key => $value) {
if (in_array($key, array_keys($this->_paramOptionTypes))) {
if (null !== ($callback = $this->_paramOptionTypes[$key])) {
if (!$callback($value)) {
continue;
}
}
$paramOptions[$key] = $value;
}
}
$this->_params[] = array(
'param' => $paramOptions,
'order' => $order,
);
return $this;
}
/**
* Add params
*
* Each param should be an array, and should include the key 'type'.
*
* @param array $params
* @return Zend_Json_Server_Smd_Service
*/
public function addParams(array $params)
{
ksort($params);
foreach ($params as $options) {
if (!is_array($options)) {
continue;
}
if (!array_key_exists('type', $options)) {
continue;
}
$type = $options['type'];
$order = (array_key_exists('order', $options)) ? $options['order'] : null;
$this->addParam($type, $options, $order);
}
return $this;
}
/**
* Overwrite all parameters
*
* @param array $params
* @return Zend_Json_Server_Smd_Service
*/
public function setParams(array $params)
{
$this->_params = array();
return $this->addParams($params);
}
/**
* Get all parameters
*
* Returns all params in specified order.
*
* @return array
*/
public function getParams()
{
$params = array();
$index = 0;
foreach ($this->_params as $param) {
if (null === $param['order']) {
if (array_search($index, array_keys($params), true)) {
++$index;
}
$params[$index] = $param['param'];
++$index;
} else {
$params[$param['order']] = $param['param'];
}
}
ksort($params);
return $params;
}
/**
* Set return type
*
* @param string|array $type
* @return Zend_Json_Server_Smd_Service
*/
public function setReturn($type)
{
if (is_string($type)) {
$type = $this->_validateParamType($type, true);
} elseif (is_array($type)) {
foreach ($type as $key => $returnType) {
$type[$key] = $this->_validateParamType($returnType, true);
}
} else {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('Invalid param type provided ("' . gettype($type) .'")');
}
$this->_return = $type;
return $this;
}
/**
* Get return type
*
* @return string|array
*/
public function getReturn()
{
return $this->_return;
}
/**
* Cast service description to array
*
* @return array
*/
public function toArray()
{
$name = $this->getName();
$envelope = $this->getEnvelope();
$target = $this->getTarget();
$transport = $this->getTransport();
$parameters = $this->getParams();
$returns = $this->getReturn();
if (empty($target)) {
return compact('envelope', 'transport', 'parameters', 'returns');
}
return $paramInfo = compact('envelope', 'target', 'transport', 'parameters', 'returns');
}
/**
* Return JSON encoding of service
*
* @return string
*/
public function toJson()
{
$service = array($this->getName() => $this->toArray());
require_once 'Zend/Json.php';
return Zend_Json::encode($service);
}
/**
* Cast to string
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
/**
* Validate parameter type
*
* @param string $type
* @return true
* @throws Zend_Json_Server_Exception
*/
protected function _validateParamType($type, $isReturn = false)
{
if (!is_string($type)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('Invalid param type provided ("' . $type .'")');
}
if (!array_key_exists($type, $this->_paramMap)) {
$type = 'object';
}
$paramType = $this->_paramMap[$type];
if (!$isReturn && ('null' == $paramType)) {
require_once 'Zend/Json/Server/Exception.php';
throw new Zend_Json_Server_Exception('Invalid param type provided ("' . $type . '")');
}
return $paramType;
}
}