added initial support for service definition

This commit is contained in:
Christian Kerl
2010-10-07 15:16:56 +02:00
parent 783ced3b7b
commit 31d40380a6
18 changed files with 677 additions and 34 deletions

View File

@ -0,0 +1,10 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition\Dumper;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
interface DumperInterface
{
function dumpServiceDefinition(ServiceDefinition $definition);
}

View File

@ -0,0 +1,21 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
abstract class FileLoader implements LoaderInterface
{
protected $file;
public function __construct($file)
{
if (!file_exists($file)) {
throw new \InvalidArgumentException(sprintf('The service definition file %s does not exist', $file));
}
if (!is_readable($file)) {
throw new \InvalidArgumentException(sprintf('The service definition file %s is not readable', $file));
}
$this->file = $file;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
interface LoaderInterface
{
/**
* Loads the contents of the given ServiceDefinition.
*
* @param ServiceDefinition $definition
*/
function loadServiceDefinition(ServiceDefinition $definition);
}

View File

@ -0,0 +1,116 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition\Loader;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceType;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceDefinition;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceMethod;
use Bundle\WebServiceBundle\ServiceDefinition\ServiceHeader;
class XmlFileLoader extends FileLoader
{
public function loadServiceDefinition(ServiceDefinition $definition)
{
$xml = $this->parseFile($this->file);
if($definition->getName() != $xml['name'])
{
throw new \InvalidArgumentException();
}
foreach($xml->header as $header)
{
$definition->getHeaders()->add($this->parseHeader($header));
}
foreach($xml->method as $method)
{
$definition->getMethods()->add($this->parseMethod($method));
}
}
/**
* @param \SimpleXMLElement $node
*
* @return \Bundle\WebServiceBundle\ServiceDefinition\ServiceHeader
*/
protected function parseHeader(\SimpleXMLElement $node)
{
$header = new ServiceHeader((string)$node['name'], $this->parseType($node->type));
return $header;
}
/**
* @param \SimpleXMLElement $node
*
* @return \Bundle\WebServiceBundle\ServiceDefinition\ServiceMethod
*/
protected function parseMethod(\SimpleXMLElement $node)
{
$method = new ServiceMethod((string)$node['name'], (string)$node['controller']);
return $method;
}
/**
* @param \SimpleXMLElement $node
*
* @return \Bundle\WebServiceBundle\ServiceDefinition\ServiceType
*/
protected function parseType(\SimpleXMLElement $node)
{
$namespaces = $node->getDocNamespaces(true);
$qname = explode(':', $node['xml-type'], 2);
$xmlType = sprintf('{%s}%s', $namespaces[$qname[0]], $qname[1]);
$type = new ServiceType((string)$node['php-type'], $xmlType, (string)$node['converter']);
return $type;
}
/**
* @param string $file
*
* @return \SimpleXMLElement
*/
protected function parseFile($file)
{
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
if (!$dom->load($file, LIBXML_COMPACT)) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
}
if (!$dom->schemaValidate(__DIR__.'/schema/servicedefinition-1.0.xsd')) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
libxml_use_internal_errors(false);
return simplexml_import_dom($dom);
}
protected function getXmlErrors()
{
$errors = array();
foreach (libxml_get_errors() as $error) {
$errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
$error->code,
trim($error->message),
$error->file ? $error->file : 'n/a',
$error->line,
$error->column
);
}
libxml_clear_errors();
libxml_use_internal_errors(false);
return $errors;
}
}

View File

@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://christiankerl.github.com/WebServiceBundle/servicedefinition/1.0/" xmlns:tns="http://christiankerl.github.com/WebServiceBundle/servicedefinition/1.0/" elementFormDefault="qualified">
<element name="webservice" type="tns:WebserviceType" />
<complexType name="WebserviceType">
<sequence>
<element name="header" type="tns:HeaderType" minOccurs="0" maxOccurs="unbounded" />
<element name="method" type="tns:MethodType" maxOccurs="unbounded" />
</sequence>
<attribute name="name" type="string" use="required" />
</complexType>
<complexType name="HeaderType">
<complexContent>
<extension base="tns:TransferObjectType">
<sequence>
<element name="exception" type="tns:ExceptionType" minOccurs="0" maxOccurs="unbounded" />
</sequence>
<attribute name="name" type="ID" use="required" />
</extension>
</complexContent>
</complexType>
<complexType name="HeaderRefType">
<attribute name="name" type="IDREF" />
<attribute name="direction">
<simpleType>
<restriction base="string">
<enumeration value="in" />
<enumeration value="out" />
<enumeration value="inout" />
</restriction>
</simpleType>
</attribute>
</complexType>
<complexType name="MethodType">
<sequence>
<element name="exception" type="tns:ExceptionType" minOccurs="0" maxOccurs="unbounded" />
<element name="header" type="tns:HeaderRefType" minOccurs="0" maxOccurs="unbounded" />
<element name="argument" type="tns:ArgumentType" minOccurs="0" maxOccurs="unbounded" />
<element name="return" type="tns:ReturnType" minOccurs="0" maxOccurs="1" />
</sequence>
<attribute name="name" type="string" use="required" />
<attribute name="controller" type="string" use="required" />
</complexType>
<complexType name="TransferObjectType" abstract="true">
<sequence>
<element name="type" type="tns:TypeConversionType" />
</sequence>
</complexType>
<complexType name="ExceptionType">
<complexContent>
<extension base="tns:TransferObjectType">
<attribute name="name" type="string" use="required" />
</extension>
</complexContent>
</complexType>
<complexType name="ArgumentType">
<complexContent>
<extension base="tns:TransferObjectType">
<attribute name="name" type="string" use="required" />
</extension>
</complexContent>
</complexType>
<complexType name="ReturnType">
<complexContent>
<extension base="tns:TransferObjectType" />
</complexContent>
</complexType>
<complexType name="TypeConversionType">
<attribute name="php-type" type="string" use="required" />
<attribute name="xml-type" type="QName" use="required" />
<attribute name="converter" type="string" use="optional" />
</complexType>
</schema>

View File

@ -0,0 +1,80 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition;
use Bundle\WebServiceBundle\Util\Collection;
class ServiceDefinition
{
/**
* @var string
*/
private $name;
/**
* @var \Bundle\WebServiceBundle\Util\Collection
*/
private $methods;
/**
* @var \Bundle\WebServiceBundle\Util\Collection
*/
private $headers;
public function __construct($name = null, array $methods = array(), array $headers = array())
{
$this->setName($name);
$this->setMethods($methods);
$this->setHeaders($headers);
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return \Bundle\WebServiceBundle\Util\Collection
*/
public function getMethods()
{
return $this->methods;
}
/**
* @param array $methods
*/
public function setMethods($methods)
{
$this->methods = new Collection('getName');
$this->methods->addAll($methods);
}
/**
* @return \Bundle\WebServiceBundle\Util\Collection
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @param array $headers
*/
public function setHeaders($headers)
{
$this->headers = new Collection('getName');
$this->headers->addAll($headers);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition;
class ServiceHeader
{
private $name;
private $type;
public function __construct($name = null, ServiceType $type = null)
{
$this->setName($name);
$this->setType($type);
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getType()
{
return $this->type;
}
public function setType($type)
{
$this->type = $type;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition;
class ServiceMethod
{
private $name;
private $controller;
public function __construct($name = null, $controller = null)
{
$this->setName($name);
$this->setController($controller);
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getController()
{
return $this->controller;
}
public function setController($controller)
{
$this->controller = $controller;
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace Bundle\WebServiceBundle\ServiceDefinition;
class ServiceType
{
private $phpType;
private $xmlType;
private $converter;
public function __construct($phpType = null, $xmlType = null, $converter = null)
{
$this->setPhpType($phpType);
$this->setXmlType($xmlType);
$this->setConverter($converter);
}
public function getPhpType()
{
return $this->phpType;
}
public function setPhpType($value)
{
$this->phpType = $value;
}
public function getXmlType()
{
return $this->xmlType;
}
public function setXmlType($value)
{
$this->xmlType = $value;
}
public function getConverter()
{
return $this->converter;
}
public function setConverter($value)
{
$this->converter = $value;
}
}