Created
September 12, 2012 09:46
-
-
Save Hounddog/3705602 to your computer and use it in GitHub Desktop.
Smd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* V5 | |
* LICENSE | |
* | |
* Insert License here | |
* | |
* @package Service | |
*/ | |
namespace Smd\Service; | |
use Zend\Json\Server\Smd, | |
Zend\Json\Server\Smd\Service, | |
Zend\ServiceManager\ServiceLocatorInterface, | |
Zend\Mvc\Router\RouteStackInterface; | |
/** | |
* Super Type Service for Smd. | |
* @category Smd | |
* @package Service | |
* @copyright Copyright (c) 2012 Doyousoft | |
* @license $license_information | |
* @version $Id | |
*/ | |
class AbstractService | |
extends Service | |
{ | |
protected $serviceLocator; | |
protected $router; | |
/** | |
* Allowed envelope types | |
* @var array | |
*/ | |
protected $_envelopeTypes = array( | |
Smd::ENV_JSONRPC_1, | |
Smd::ENV_JSONRPC_2, | |
); | |
/** | |
* Allowed transport types | |
* @var array | |
*/ | |
protected $_transportTypes = array( | |
'POST', 'GET' | |
); | |
/** | |
* Constructor | |
* | |
* @param string $name | |
* @param array $params | |
* @throws Zend_Json_Server_Exception if no name provided | |
*/ | |
public function __construct($name, $params, ServiceLocatorInterface $serviceLocator) | |
{ | |
$this->setName($name); | |
$this->setParams($params); | |
$this->serviceLocator = $serviceLocator; | |
echo $serviceLocator->get('application'); | |
// echo $this->router; | |
} | |
/** | |
* Set Docblock Options | |
* @param Zend_Reflection_Docblock $docBlock [description] | |
*/ | |
public function setDocBlockOptions(Zend_Reflection_Docblock $docBlock) | |
{ | |
$methods = get_class_methods($this); | |
$tags = $docBlock->getTags(); | |
foreach ($tags as $tag) { | |
$name = $tag->getName(); | |
$method = 'set' . ucfirst($name); | |
if ( in_array($method, $methods) ) { | |
if ( $name == 'return' ) { | |
$this->$method(trim($tag->getType())); | |
continue; | |
} | |
$this->$method(trim($tag->getDescription())); | |
} | |
} | |
} | |
/** | |
* Get Content Type | |
* | |
* @return string | |
*/ | |
public function getContentType() | |
{ | |
return $this->contentType; | |
} | |
/** | |
* Set ContentType | |
* | |
* @param string $contentType | |
*/ | |
public function setContentType($contentType) | |
{ | |
$this->contentType = $contentType; | |
} | |
/** | |
* Set service target | |
* | |
* @param string $target | |
* @return Zend\Json\Server\Smd\Service | |
*/ | |
public function setTarget($target) | |
{ | |
$router = $this->router; | |
echo $router; | |
exit; | |
if (!$router instanceof RouteStackInterface) { | |
throw new Exception\DomainException( | |
__METHOD__ | |
. ' cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed' | |
); | |
} | |
$this->target = (string) $target; | |
return $this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* V5 | |
* LICENSE | |
* | |
* Insert License here | |
* | |
*/ | |
namespace Smd; | |
use DysBase\Module\AbstractModule; | |
/** | |
* Base Module for Applicationion | |
* @category Smd | |
* @package Module | |
* @copyright Copyright (c) 2012 Doyousoft | |
* @license $license_information | |
* @version $Id | |
*/ | |
class Module extends AbstractModule | |
{ | |
public function getServiceConfig() | |
{ | |
return array( | |
'factories' => array( | |
'smd_factory' => 'Smd\Factory\SmdFactory', | |
), | |
); | |
} | |
public function getDir() | |
{ | |
return __DIR__; | |
} | |
public function getNamespace() | |
{ | |
return __NAMESPACE__; | |
} | |
public function getDoctrineConfig() | |
{ | |
return array(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* V5 | |
* LICENSE | |
* | |
* Insert License here | |
* | |
* @package Smd | |
*/ | |
namespace Smd\Service; | |
use Smd\Service\AbstractService; | |
use Zend\Mvc\MvcEvent; | |
use Zend\Mvc\Router\RouteMatch; | |
use Zend\Code\Reflection\ClassReflection; | |
use Zend\Code\Reflection\PropertyReflection; | |
/** | |
* Rest Service for Smd. | |
* @category Smd | |
* @package Service | |
* @copyright Copyright (c) 2012 Doyousoft | |
* @license $license_information | |
* @version $Id | |
*/ | |
class Rest | |
extends AbstractService | |
{ | |
/** | |
* Content Type | |
* @var string | |
*/ | |
protected $contentType = 'application/json'; | |
/** | |
* Service metadata Transport | |
* @var string | |
*/ | |
protected $transport = 'REST'; | |
//protected $_envelope = Pwb_Smd::ENV_PATH; | |
/** | |
* Reflection from Controller Class | |
* @var Zend_Reflection_Class | |
*/ | |
protected $controller; | |
/** | |
* Class Name | |
* @var string | |
*/ | |
protected $className; | |
/** | |
* Dto Name | |
* @var string | |
*/ | |
protected $dtoName; | |
/** | |
* Set Service Params | |
* @param array $params | |
*/ | |
public function setParams(array $params) | |
{ | |
$this->setTarget($params['route']); | |
$this->className =$params['controller']; | |
$this->dtoName = $params['dto']; | |
$this->setControllerParameters(); | |
$this->setReturn($this->getDto()); | |
} | |
/** | |
* Set all parameters | |
* | |
* @return Rest | |
*/ | |
public function setControllerParameters() | |
{ | |
$reflectionClass = new ClassReflection($this->className); | |
$propertyByName = $reflectionClass->getProperty('criteria'); | |
$docComment = $propertyByName->getDocBlock(); | |
if ( !$docComment ) { | |
throw new \Smd\Service\Exception\InvalidArgumentException( | |
$e->getMessage(), | |
500, | |
$e | |
); | |
$this->setError( | |
'DocBlock missing for _criteres in ' . | |
$this->_className | |
); | |
} | |
$docParams = $docComment->getTags('param'); | |
if ( !$docParams ) { | |
$this->setError( | |
'Parameters description missing for _criteres in ' . | |
$this->_className | |
); | |
} | |
foreach ($docParams as $key => $param) { | |
$optional = false; | |
$name = $param->getVariableName(); | |
if ( empty($name) ) { | |
$this->setError( | |
'Parameters Variable missing for _criteres in ' . | |
$this->_className | |
); | |
} | |
if ( preg_match('/\[optional\]/is', $param->getDescription()) ) { | |
$optional = true; | |
} | |
$option[ 'name' ] = substr($param->getVariableName(), 1); | |
$option[ 'optional' ] = $optional; | |
$this->addParam($param->getType(), $option); | |
} | |
return $this; | |
} | |
/** | |
* Load Service Data from class | |
* @param object $class | |
*/ | |
private function loadClass($class) | |
{ | |
/* $this->_controller = new ReflectionClass($class); | |
$docBlock = $this->_controller->getDocblock(); | |
$contentType = $docBlock->getTag('contentType'); | |
$envelope = $docBlock->getTag('envelope'); | |
$transport = $docBlock->getTag('transport'); | |
if ( $contentType ) { | |
$this->setContentType($contentType->getDescription()); | |
} | |
if ( $envelope ) { | |
$this->setEnvelope(trim($envelope->getDescription())); | |
} | |
if ( $transport ) { | |
$this->setTransport(trim($transport->getDescription())); | |
}*/ | |
} | |
/** | |
* Get Dto Data for Return | |
* @return array | |
*/ | |
public function getDto() | |
{ | |
$dtoReflection = new ClassReflection($this->dtoName); | |
$properties = $dtoReflection->getProperties( | |
PropertyReflection::IS_PUBLIC | |
); | |
$dto = array( ); | |
foreach ($properties as $property) { | |
$docComment = $property->getDocBlock(); | |
if ( !$docComment ) { | |
throw new \Smd\Service\Exception\InvalidArgumentException( | |
'DocBlock missing for dto property "' . | |
$property->getName() . '" in ' . $this->dtoName | |
); | |
} | |
if ( !$docComment->hasTag('var') ) { | |
throw new \Smd\Service\Exception\InvalidArgumentException( | |
'Description incorrect or missing for dto property "' . | |
$property->getName() . '" in ' . $this->dtoName | |
); | |
} | |
$docVar = trim($docComment->getTag('var')); | |
if (trim(strtolower($docVar)) == 'array') { | |
$docParams = $docComment->getTags('param'); | |
$docRef = $docComment->getTag('ref'); | |
$docDto = $docComment->getTag('dto'); | |
$params = array( ); | |
if ( $docParams ) { | |
foreach ($docParams as $param) { | |
// echo $param->getType(); | |
//$params[ $param->getDescription() ] = array( | |
// 'type' => $param->getType() | |
//); | |
} | |
$dto[ $property->getName() ][ 'items' ] = $params; | |
} else if ( $docRef ) { | |
$params[ '$ref' ] = $docRef->getDescription(); | |
$dto[ $property->getName() ][ 'items' ] = $params; | |
} else if ( $docDto ) { | |
$dto[ $property->getName() ][ 'items' ] = $this->_getDto( | |
trim($docDto->getDescription()) | |
); | |
} else { | |
$dto[ $property->getName() ][ 'items' ] = false; | |
} | |
} else if ( $docComment->getTag('ref') ) { | |
$docRef = $docComment->getTag('ref'); | |
$params[ '$ref' ] = $docRef->getContent(); | |
$dto[ $property->getName() ] = $params; | |
} | |
$dto[ $property->getName() ][ 'type' ] = $docVar; | |
} | |
return $dto; | |
} | |
/** | |
* 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) { | |
$typeArray = array( ); | |
$typeArray[ 'type' ] = $this->_validateParamType( | |
$returnType[ 'type' ], true | |
); | |
if ( $returnType[ 'type' ] == 'array' ) { | |
if ( $returnType[ 'items' ] ) { | |
$typeArray[ 'items' ] = $returnType[ 'items' ]; | |
} | |
} | |
if ( | |
isset( | |
$returnType[ '$ref' ]) | |
&& $returnType[ 'type' ] != 'array' | |
) { | |
$typeArray[ '$ref' ] = $returnType[ '$ref' ]; | |
unset($typeArray[ 'type' ]); | |
} | |
$type[ $key ] = $typeArray; | |
} | |
} 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; | |
} | |
/** | |
* Cast service description to array | |
* | |
* @return array | |
*/ | |
public function toArray() | |
{ | |
$paramInfo = array(); | |
$paramInfo['envelope'] = $this->getEnvelope(); | |
if (null !== ($target = $this->getTarget())) { | |
$paramInfo['target'] = $target; | |
} | |
$paramInfo['transport'] = $this->getTransport(); | |
$paramInfo['contentType'] = $this->getContentType(); | |
$paramInfo['parameters'] = $this->getParams(); | |
$paramInfo['returns'] = $this->getReturn(); | |
return $paramInfo; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* V5 | |
* LICENSE | |
* | |
* Insert License here | |
* | |
* @package Factory | |
*/ | |
namespace Smd\Factory; | |
use Zend\Json\Server\Smd; | |
use Smd\Service\Rest; | |
use Zend\ServiceManager\FactoryInterface; | |
use Zend\ServiceManager\ServiceLocatorInterface; | |
/** | |
* Smd factory. | |
* @category Smd | |
* @package Factory | |
* @copyright Copyright (c) 2012 Doyousoft | |
* @license $license_information | |
* @version $Id | |
*/ | |
class SmdFactory extends Smd implements FactoryInterface | |
{ | |
protected $serviceLocator; | |
public function createService(ServiceLocatorInterface $serviceLocator) | |
{ | |
$config = $serviceLocator->get('config'); | |
$this->setDescription('Powerboutique API'); | |
$this->setId('/api'); | |
$this->serviceLocator = $serviceLocator; | |
$smdConfig = $config['smd']; | |
foreach ($smdConfig as $name => $params) { | |
$this->addService($name, $params); | |
} | |
return $this->toArray(); | |
} | |
/** | |
* Add Service | |
* @param [type] $name [description] | |
* @param [type] $params [description] | |
*/ | |
public function addService($name, $params) | |
{ | |
$this->addRestController($name, $params); | |
} | |
/** | |
* Add Rest Controller | |
* | |
* @param string $name | |
* @param array $params | |
* @return Smd | |
*/ | |
public function addRestController($name, $params) | |
{ | |
$service = new Rest($name, $params, $this->serviceLocator); | |
$name = $service->getName(); | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment