Merge remote-tracking branch 'origin/new_definition'

This commit is contained in:
Francis Besset 2011-08-07 11:35:27 +02:00
commit 07ef0a62f4
36 changed files with 1711 additions and 29 deletions

View File

@ -53,6 +53,7 @@ class SoapWebServiceController extends ContainerAware
$this->soapRequest = SoapRequest::createFromHttpRequest($this->container->get('request')); $this->soapRequest = SoapRequest::createFromHttpRequest($this->container->get('request'));
$this->soapServer = $webServiceContext->getServerFactory()->create($this->soapRequest, $this->soapResponse); $this->soapServer = $webServiceContext->getServerFactory()->create($this->soapRequest, $this->soapResponse);
$this->soapServer->setObject($this); $this->soapServer->setObject($this);
ob_start(); ob_start();

View File

@ -10,6 +10,7 @@
<parameter key="besimple.soap.definition.loader.annot_dir.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationDirectoryLoader</parameter> <parameter key="besimple.soap.definition.loader.annot_dir.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationDirectoryLoader</parameter>
<parameter key="besimple.soap.definition.loader.annot_file.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationFileLoader</parameter> <parameter key="besimple.soap.definition.loader.annot_file.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationFileLoader</parameter>
<parameter key="besimple.soap.definition.loader.annot_class.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationClassLoader</parameter> <parameter key="besimple.soap.definition.loader.annot_class.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationClassLoader</parameter>
<parameter key="besimple.soap.definition.loader.annot_complextype.class">BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader</parameter>
</parameters> </parameters>
<services> <services>
@ -29,6 +30,10 @@
<tag name="besimple.soap.definition.loader" /> <tag name="besimple.soap.definition.loader" />
<argument type="service" id="annotation_reader" /> <argument type="service" id="annotation_reader" />
</service> </service>
<service id="besimple.soap.definition.loader.annot_complextype" class="%besimple.soap.definition.loader.annot_complextype.class%" public="false">
<argument type="service" id="annotation_reader" />
</service>
</services> </services>
</container> </container>

View File

@ -50,7 +50,9 @@
<service id="besimple.soap.binder.response.documentwrapped" class="%besimple.soap.binder.response.documentwrapped.class%" /> <service id="besimple.soap.binder.response.documentwrapped" class="%besimple.soap.binder.response.documentwrapped.class%" />
<service id="besimple.soap.definition.dumper.wsdl.rpcliteral" class="%besimple.soap.definition.dumper.wsdl.rpcliteral.class%" /> <service id="besimple.soap.definition.dumper.wsdl.rpcliteral" class="%besimple.soap.definition.dumper.wsdl.rpcliteral.class%">
<argument type="service" id="besimple.soap.definition.loader.annot_complextype" />
</service>
<service id="besimple.soap.converter.repository" class="%besimple.soap.converter.repository.class%" /> <service id="besimple.soap.converter.repository" class="%besimple.soap.converter.repository.class%" />

0
Resources/doc/_static/.gitkeep vendored Normal file
View File

226
Resources/doc/conf.py Normal file
View File

@ -0,0 +1,226 @@
# -*- coding: utf-8 -*-
#
# BeSimpleSoapBundle documentation build configuration file, created by
# sphinx-quickstart on Mon Aug 1 22:24:10 2011.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
from sphinx.highlighting import lexers
from pygments.lexers.web import PhpLexer
lexers['php'] = PhpLexer(startinline=True)
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
sys.path.append(os.path.abspath('_exts'))
# -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.viewcode']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'BeSimpleSoapBundle'
copyright = u'2011, Christian Kerl, Francis Besset'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.0.0'
# The full version, including alpha/beta/rc tags.
release = '1.0.0-DEV'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = []
# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
#pygments_style = 'sphinx'
pygments_style = 'perldoc'
# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# -- Options for HTML output ---------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_domain_indices = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None
# Output file base name for HTML help builder.
htmlhelp_basename = 'BeSimpleSoapBundledoc'
# -- Options for LaTeX output --------------------------------------------------
# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'
# The font size ('10pt', '11pt' or '12pt').
#latex_font_size = '10pt'
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'BeSimpleSoapBundle.tex', u'BeSimpleSoapBundle Documentation',
u'Christian Kerl, Francis Besset', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# If true, show page references after internal links.
#latex_show_pagerefs = False
# If true, show URL addresses after external links.
#latex_show_urls = False
# Additional stuff for the LaTeX preamble.
#latex_preamble = ''
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_domain_indices = True
# -- Options for manual page output --------------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'besimplesoapbundle', u'BeSimpleSoapBundle Documentation',
[u'Christian Kerl, Francis Besset'], 1)
]
# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'http://docs.python.org/': None}

27
Resources/doc/index.rst Normal file
View File

@ -0,0 +1,27 @@
BeSimpleSoapBundle
==================
The BeSimpleSoapBundle is a Symfony2 bundle to build WSDL and SOAP based web services.
It is based on the `ckWebServicePlugin`_ for symfony.
Reference Guide
---------------
.. toctree::
:maxdepth: 1
:numbered:
reference/installation
reference/configuration
Tutorial
--------
.. toctree::
:maxdepth: 1
:numbered:
tutorial/array
tutorial/complex_type
.. _`ckWebServicePlugin`: http://www.symfony-project.org/plugins/ckWebServicePlugin

View File

@ -0,0 +1,51 @@
Configuration
=============
Routing
-------
Include the `BeSimpleSoapBundle`'s routing configuration in your routing file (you can choose the prefix arbitrarily):
.. code-block:: yaml
# app/config/routing.yml
_besimple_soap:
resource: "@BeSimpleSoapBundle/Resources/config/routing/webservicecontroller.xml"
prefix: /ws
Config
------
Configure your first web service in your config file:
.. code-block:: yaml
# app/config/config.yml
be_simple_soap:
services:
DemoApi:
namespace: http://mysymfonyapp.com/ws/DemoApi/1.0/
binding: rpc-literal
resource: "@AcmeDemoBundle/Controller/DemoController.php"
resource_type: annotation
Annotations for Controllers
---------------------------
.. code-block:: php
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
use BeSimple\SoapBundle\Soap\SoapResponse;
class DemoController extends Controller
{
/**
* @Soap\Method("Hello")
* @Soap\Param("name", phpType = "string")
* @Soap\Result(phpType = "string")
*/
public function helloAction($name)
{
return new SoapResponse(sprintf('Hello %s!', $name));
}
}

View File

@ -0,0 +1,70 @@
Installation
============
Requirements
------------
Install and enable PHP's SOAP extension
Download `Zend\\Soap`_ and `Zend\\Mime`_ or add in `deps` file
.. code-block:: ini
; deps file
[Zend\Soap]
git=http://github.com/BeSimple/zend-soap.git
target=/zend-framework/library/Zend/Soap
[Zend\Mime]
git=http://github.com/BeSimple/zend-mime.git
target=/zend-framework/library/Zend/Mime
Add `Zend` library in autoload.php
.. code-block:: php
// app/autoload.php
$loader->registerNamespaces(array(
'Zend' => __DIR__.'/../vendor/zend-framework/library',
// your other namespaces
));
Installation
------------
`Download`_ the bundle or add in `deps` file
.. code-block:: ini
; deps file
[BeSimpleSoapBundle]
git=http://github.com/BeSimple/BeSimpleSoapBundle.git
target=/bundles/BeSimple/SoapBundle
Add `BeSimple` in autoload.php
.. code-block:: php
// app/autoload.php
$loader->registerNamespaces(array(
'BeSimple' => __DIR__.'/../vendor/bundles',
// your other namespaces
));
Add `BeSimpleSoapBundle` in your Kernel class
.. code-block:: php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new new BeSimple\SoapBundle\BeSimpleSoapBundle(),
// ...
);
}
.. _`Zend\\Soap`: http://github.com/BeSimple/zend-soap
.. _`Zend\\Mime`: http://github.com/BeSimple/zend-mime
.. _`Download`: http://github.com/BeSimple/BeSimpleSoapBundle

View File

@ -0,0 +1,26 @@
Array
=====
Controller
----------
.. code-block:: php
namespace My\App\Controller;
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
use BeSimple\SoapBundle\Soap\SoapResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
class DemoController extends ContainerAware
{
/**
* @Soap\Method("isString")
* @Soap\Param("strings", phpType = "string[]")
* @Soap\Result(phpType = "boolean")
*/
public function helloAction(array $strings)
{
return new SoapResponse(true);
}
}

View File

@ -0,0 +1,120 @@
Complex Type
============
This tutorial explains how to do to return a complex type.
If your SOAP function takes a complex type as input, this tutorial is
valid. You'll just have to adapt the input parameters of your method.
Controller
----------
.. code-block:: php
namespace My\App\Controller;
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
use BeSimple\SoapBundle\Soap\SoapResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
class DemoController extends ContainerAware
{
/**
* @Soap\Method("getUser")
* @Soap\Param("name", phpType = "string")
*
* Specify \My\App\Entity\User phpType
* Warning: Do not forget the first backslash
* @Soap\Result(phpType = "\My\App\Entity\User")
*/
public function getUserAction($name)
{
$user = $this->container->getDoctrine()->getRepository('MyApp:User')->findOneByName($name);
if (!$user) {
throw new \SoapFault('USER_NOT_FOUND', sprintf('The user with the name "%s" can not be found', $name));
}
return new SoapResponse($user);
}
}
User class
----------
You can expose public property and public method (getter and setter).
.. code-block:: php
namespace My\App\Entity;
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
class User
{
/**
* @Soap\PropertyComplexType("string")
*/
public $firstname;
/**
* @Soap\PropertyComplexType("string")
*/
public $lastname;
private $id;
private $username;
private $email;
/**
* @Soap\MethodComplexType("int", name="user_id", nillable=true)
*/
public function getId()
{
return $this->id;
}
/**
* @Soap\MethodComplexType("string", setter="setUsername")
*/
public function getUsername()
{
return $this->username;
}
/**
* @Soap\MethodComplexType("string", setter="setEmail")
*/
public function getEmail()
{
return $this->email;
}
public function setUsername($username)
{
$this->username = $username;
}
public function setEmail($email)
{
$this->email = $email;
}
}
PropertyComplexType
-------------------
`PropertyComplexType` accepts the following options:
* **name**: To override the original name of the property
* **nillable**: To specify that the value can be null
MethodComplexType
-------------------
`MethodComplexType` accepts the following options:
* **name**: To override the original name of the property
* **nillable**: To specify that the value can be null
* **setter**: The set method name value. *Mandatory if the complex type is passed as a parameter to a function.*

View File

@ -12,6 +12,9 @@ namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method; use BeSimple\SoapBundle\ServiceDefinition\Method;
/**
* @author Christian Kerl <christian-kerl@web.de>
*/
class DocumentLiteralWrappedRequestMessageBinder implements MessageBinderInterface class DocumentLiteralWrappedRequestMessageBinder implements MessageBinderInterface
{ {
public function processMessage(Method $messageDefinition, $message) public function processMessage(Method $messageDefinition, $message)

View File

@ -12,6 +12,9 @@ namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method; use BeSimple\SoapBundle\ServiceDefinition\Method;
/**
* @author Christian Kerl <christian-kerl@web.de>
*/
class DocumentLiteralWrappedResponseMessageBinder implements MessageBinderInterface class DocumentLiteralWrappedResponseMessageBinder implements MessageBinderInterface
{ {
public function processMessage(Method $messageDefinition, $message) public function processMessage(Method $messageDefinition, $message)

View File

@ -12,6 +12,9 @@ namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method; use BeSimple\SoapBundle\ServiceDefinition\Method;
/**
* @author Christian Kerl <christian-kerl@web.de>
*/
interface MessageBinderInterface interface MessageBinderInterface
{ {
/** /**
@ -20,5 +23,5 @@ interface MessageBinderInterface
* *
* @return mixed * @return mixed
*/ */
function processMessage(Method $messageDefinition, $message); function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array());
} }

View File

@ -11,17 +11,25 @@
namespace BeSimple\SoapBundle\ServiceBinding; namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method; use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
/**
* @author Christian Kerl <christian-kerl@web.de>
* @author Francis Besset <francis.besset@gmail.com>
*/
class RpcLiteralRequestMessageBinder implements MessageBinderInterface class RpcLiteralRequestMessageBinder implements MessageBinderInterface
{ {
public function processMessage(Method $messageDefinition, $message) private $messageRefs = array();
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{ {
$result = array(); $result = array();
$i = 0; $i = 0;
foreach ($messageDefinition->getArguments() as $argument) { foreach ($messageDefinition->getArguments() as $argument) {
if (isset($message[$i])) { if (isset($message[$i])) {
$result[$argument->getName()] = $message[$i]; $result[$argument->getName()] = $this->processType($argument->getType()->getPhpType(), $message[$i], $definitionComplexTypes);
} }
$i++; $i++;
@ -29,4 +37,62 @@ class RpcLiteralRequestMessageBinder implements MessageBinderInterface
return $result; return $result;
} }
private function processType($phpType, $message, array $definitionComplexTypes)
{
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
$isArray = true;
$type = $match[1];
} else {
$isArray = false;
$type = $phpType;
}
if (isset($definitionComplexTypes[$type])) {
if ($isArray) {
$array = array();
foreach ($message->item as $complexType) {
$array[] = $this->getInstanceOfType($type, $complexType, $definitionComplexTypes);
}
$message = $array;
} else {
$message = $this->getInstanceOfType($type, $message, $definitionComplexTypes);
}
} elseif ($isArray) {
$message = $message->item;
}
return $message;
}
private function getInstanceOfType($phpType, $message, array $definitionComplexTypes)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}
$this->messageRefs[$hash] =
$instanceType = new $phpType();
foreach ($definitionComplexTypes[$phpType] as $type) {
$value = $this->processType($type->getValue(), $message->{$type->getName()}, $definitionComplexTypes);
if ($type instanceof PropertyComplexType) {
$instanceType->{$type->getOriginalName()} = $value;
} elseif ($type instanceof MethodComplexType) {
if (!$type->getSetter()) {
throw new \LogicException();
}
$instanceType->{$type->getSetter()}($value);
} else {
throw new \InvalidArgumentException();
}
}
return $instanceType;
}
} }

View File

@ -11,11 +11,86 @@
namespace BeSimple\SoapBundle\ServiceBinding; namespace BeSimple\SoapBundle\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method; use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
/**
* @author Christian Kerl <christian-kerl@web.de>
* @author Francis Besset <francis.besset@gmail.com>
*/
class RpcLiteralResponseMessageBinder implements MessageBinderInterface class RpcLiteralResponseMessageBinder implements MessageBinderInterface
{ {
public function processMessage(Method $messageDefinition, $message) private $messageRefs = array();
public function processMessage(Method $messageDefinition, $message, array $definitionComplexTypes = array())
{ {
$return = $messageDefinition->getReturn();
$class = $return->getPhpType();
$message = $this->processType($messageDefinition->getReturn()->getPhpType(), $message, $definitionComplexTypes);
return $message; return $message;
} }
private function processType($phpType, $message, array $definitionComplexTypes)
{
if (preg_match('/^([^\[]+)\[\]$/', $phpType, $match)) {
$isArray = true;
$type = $match[1];
} else {
$isArray = false;
$type = $phpType;
}
if (isset($definitionComplexTypes[$type])) {
if ($isArray) {
$array = array();
foreach ($message as $complexType) {
$array[] = $this->getInstanceOfStdClass($type, $complexType, $definitionComplexTypes);
}
$message = $array;
} else {
$message = $this->getInstanceOfStdClass($type, $message, $definitionComplexTypes);
}
}
return $message;
}
private function getInstanceOfStdClass($phpType, $message, $definitionComplexTypes)
{
$hash = spl_object_hash($message);
if (isset($this->messageRefs[$hash])) {
return $this->messageRefs[$hash];
}
$class = $phpType;
if ($class[0] == '\\') {
$class = substr($class, 1);
}
if (get_class($message) !== $class) {
throw new \InvalidArgumentException();
}
$stdClass = new \stdClass();
$this->messageRefs[$hash] = $stdClass;
foreach ($definitionComplexTypes[$phpType] as $type) {
if ($type instanceof PropertyComplexType) {
$value = $message->{$type->getOriginalName()};
} elseif ($type instanceof MethodComplexType) {
$value = $message->{$type->getOriginalName()}();
} else {
throw new \InvalidArgumentException();
}
$stdClass->{$type->getName()} = $this->processType($type->getValue(), $value, $definitionComplexTypes);
}
return $stdClass;
}
} }

View File

@ -15,6 +15,9 @@ use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
use BeSimple\SoapBundle\Soap\SoapHeader; use BeSimple\SoapBundle\Soap\SoapHeader;
use BeSimple\SoapBundle\Util\QName; use BeSimple\SoapBundle\Util\QName;
/**
* @author Christian Kerl <christian-kerl@web.de>
*/
class ServiceBinder class ServiceBinder
{ {
/** /**
@ -61,7 +64,7 @@ class ServiceBinder
$result = array(); $result = array();
$result['_controller'] = $methodDefinition->getController(); $result['_controller'] = $methodDefinition->getController();
$result = array_merge($result, $this->requestMessageBinder->processMessage($methodDefinition, $arguments)); $result = array_merge($result, $this->requestMessageBinder->processMessage($methodDefinition, $arguments, $this->definition->getDefinitionComplexTypes()));
return $result; return $result;
} }
@ -70,7 +73,7 @@ class ServiceBinder
{ {
$methodDefinition = $this->definition->getMethods()->get($name); $methodDefinition = $this->definition->getMethods()->get($name);
return $this->responseMessageBinder->processMessage($methodDefinition, $return); return $this->responseMessageBinder->processMessage($methodDefinition, $return, $this->definition->getDefinitionComplexTypes());
} }
protected function createSoapHeader(Header $headerDefinition, $data) protected function createSoapHeader(Header $headerDefinition, $data)

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Annotation;
/**
* @Annotation
*/
class MethodComplexType extends Configuration
{
private $name;
private $value;
private $setter;
private $isNillable = false;
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function getSetter()
{
return $this->setter;
}
public function isNillable()
{
return $this->isNillable;
}
public function setName($name)
{
$this->name = $name;
}
public function setValue($value)
{
$this->value = $value;
}
public function setSetter($setter)
{
$this->setter = $setter;
}
public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}
public function getAliasName()
{
return 'methodcomplextype';
}
}

View File

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Annotation;
/**
* @Annotation
*/
class PropertyComplexType extends Configuration
{
private $name;
private $value;
private $isNillable = false;
public function getName()
{
return $this->name;
}
public function getValue()
{
return $this->value;
}
public function isNillable()
{
return $this->isNillable;
}
public function setName($name)
{
$this->name = $name;
}
public function setValue($value)
{
$this->value = $value;
}
public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}
public function getAliasName()
{
return 'propertycomplextype';
}
}

View File

@ -13,6 +13,7 @@ namespace BeSimple\SoapBundle\ServiceDefinition\Dumper;
use BeSimple\SoapBundle\ServiceDefinition\Method; use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Type; use BeSimple\SoapBundle\ServiceDefinition\Type;
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition; use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use BeSimple\SoapBundle\Util\Assert; use BeSimple\SoapBundle\Util\Assert;
use BeSimple\SoapBundle\Util\QName; use BeSimple\SoapBundle\Util\QName;
@ -23,9 +24,15 @@ use Zend\Soap\Wsdl;
*/ */
class WsdlDumper implements DumperInterface class WsdlDumper implements DumperInterface
{ {
private $loader;
private $wsdl; private $wsdl;
private $definition; private $definition;
public function __construct(AnnotationComplexTypeLoader $loader)
{
$this->loader = $loader;
}
public function dumpServiceDefinition(ServiceDefinition $definition, array $options = array()) public function dumpServiceDefinition(ServiceDefinition $definition, array $options = array())
{ {
Assert::thatArgumentNotNull('definition', $definition); Assert::thatArgumentNotNull('definition', $definition);
@ -33,7 +40,7 @@ class WsdlDumper implements DumperInterface
$options = array_merge(array('endpoint' => '', 'stylesheet' => null), $options); $options = array_merge(array('endpoint' => '', 'stylesheet' => null), $options);
$this->definition = $definition; $this->definition = $definition;
$this->wsdl = new Wsdl($definition->getName(), $definition->getNamespace(), new WsdlTypeStrategy()); $this->wsdl = new Wsdl($definition->getName(), $definition->getNamespace(), new WsdlTypeStrategy($this->loader, $definition));
$port = $this->wsdl->addPortType($this->getPortTypeName()); $port = $this->wsdl->addPortType($this->getPortTypeName());
$binding = $this->wsdl->addBinding($this->getBindingName(), $this->qualify($this->getPortTypeName())); $binding = $this->wsdl->addBinding($this->getBindingName(), $this->qualify($this->getPortTypeName()));

View File

@ -10,13 +10,15 @@
namespace BeSimple\SoapBundle\ServiceDefinition\Dumper; namespace BeSimple\SoapBundle\ServiceDefinition\Dumper;
use BeSimple\SoapBundle\ServiceDefinition\ServiceDefinition;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\ComplexType;
use BeSimple\SoapBundle\Util\String; use BeSimple\SoapBundle\Util\String;
use Zend\Soap\Exception; use Zend\Soap\Exception;
use Zend\Soap\Wsdl; use Zend\Soap\Wsdl;
use Zend\Soap\Wsdl\Strategy; use Zend\Soap\Wsdl\Strategy;
use Zend\Soap\Wsdl\Strategy\ArrayOfTypeSequence; use Zend\Soap\Wsdl\Strategy\ArrayOfTypeSequence;
use Zend\Soap\Wsdl\Strategy\DefaultComplexType;
class WsdlTypeStrategy implements Strategy class WsdlTypeStrategy implements Strategy
{ {
@ -27,13 +29,16 @@ class WsdlTypeStrategy implements Strategy
*/ */
private $context; private $context;
private $loader;
private $definition;
private $typeStrategy; private $typeStrategy;
private $arrayStrategy; private $arrayStrategy;
public function __construct() public function __construct(AnnotationComplexTypeLoader $loader, ServiceDefinition $definition)
{ {
$this->typeStrategy = new DefaultComplexType(); $this->loader = $loader;
$this->arrayStrategy = new ArrayOfTypeSequence(); $this->definition = $definition;
} }
/** /**
@ -51,9 +56,11 @@ class WsdlTypeStrategy implements Strategy
/** /**
* Create a complex type based on a strategy * Create a complex type based on a strategy
* *
* @throws \Zend\Soap\WsdlException
* @param string $type * @param string $type
*
* @return string XSD type * @return string XSD type
*
* @throws \Zend\Soap\WsdlException
*/ */
public function addComplexType($type) public function addComplexType($type)
{ {
@ -61,9 +68,28 @@ class WsdlTypeStrategy implements Strategy
throw new \LogicException(sprintf('Cannot add complex type "%s", no context is set for this composite strategy.', $type)); throw new \LogicException(sprintf('Cannot add complex type "%s", no context is set for this composite strategy.', $type));
} }
$strategy = String::endsWith($type, '[]') ? $this->arrayStrategy : $this->typeStrategy; $strategy = String::endsWith($type, '[]') ? $this->getArrayStrategy() : $this->getTypeStrategy();
$strategy->setContext($this->context);
return $strategy->addComplexType($type); return $strategy->addComplexType($type);
} }
private function getArrayStrategy()
{
if (!$this->arrayStrategy) {
$this->arrayStrategy = new ArrayOfTypeSequence();
$this->arrayStrategy->setContext($this->context);
}
return $this->arrayStrategy;
}
private function getTypeStrategy()
{
if (!$this->typeStrategy) {
$this->typeStrategy = new ComplexType($this->loader, $this->definition);
$this->typeStrategy->setContext($this->context);
}
return $this->typeStrategy;
}
} }

View File

@ -32,10 +32,6 @@ use Symfony\Component\Config\Loader\LoaderResolver;
*/ */
class AnnotationClassLoader implements LoaderInterface class AnnotationClassLoader implements LoaderInterface
{ {
private $methodAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Method';
private $paramAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Param';
private $resultAnnotationClass = 'BeSimple\\SoapBundle\\ServiceDefinition\\Annotation\\Result';
protected $reader; protected $reader;
/** /**

View File

@ -0,0 +1,94 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Loader;
use BeSimple\SoapBundle\ServiceDefinition\Annotation\ComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\Util\Collection;
/**
* AnnotationComplexTypeLoader loads ServiceDefinition from a PHP class and its methods.
*
* Based on \Symfony\Component\Routing\Loader\AnnotationClassLoader
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class AnnotationComplexTypeLoader extends AnnotationClassLoader
{
private $propertyComplexTypeClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\PropertyComplexType';
private $methodComplexTypeClass = 'BeSimple\SoapBundle\ServiceDefinition\Annotation\MethodComplexType';
/**
* Loads a ServiceDefinition from annotations from a class.
*
* @param string $class A class name
* @param string $type The resource type
*
* @return ServiceDefinition A ServiceDefinition instance
*
* @throws \InvalidArgumentException When route can't be parsed
*/
public function load($class, $type = null)
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}
$class = new \ReflectionClass($class);
$collection = new Collection('getName');
foreach ($class->getProperties() as $property) {
if ($property->isPublic()) {
$complexType = $this->reader->getPropertyAnnotation($property, $this->propertyComplexTypeClass);
if ($complexType) {
$propertyComplexType = new PropertyComplexType();
$propertyComplexType->setValue($complexType->getValue());
$propertyComplexType->setNillable($complexType->isNillable());
if (!$complexType->getName()) {
$propertyComplexType->setName($property->getName());
} else {
$propertyComplexType->setName($complexType->getName());
$propertyComplexType->setOriginalName($property->getName());
}
$collection->add($propertyComplexType);
}
}
}
foreach ($class->getMethods() as $method) {
if ($method->isPublic()) {
$complexType = $this->reader->getMethodAnnotation($method, $this->methodComplexTypeClass);
if ($complexType) {
$methodComplexType = new MethodComplexType();
$methodComplexType->setValue($complexType->getValue());
$methodComplexType->setSetter($complexType->getSetter());
$methodComplexType->setNillable($complexType->isNillable());
if (!$complexType->getName()) {
$methodComplexType->setName($property->getName());
} else {
$methodComplexType->setName($complexType->getName());
$methodComplexType->setOriginalName($method->getName());
}
$collection->add($methodComplexType);
}
}
}
return $collection;
}
}

View File

@ -34,6 +34,8 @@ class ServiceDefinition
*/ */
private $headers; private $headers;
private $complexTypes = array();
public function __construct($name = null, $namespace = null, array $methods = array(), array $headers = array()) public function __construct($name = null, $namespace = null, array $methods = array(), array $headers = array())
{ {
$this->setName($name); $this->setName($name);
@ -127,4 +129,14 @@ class ServiceDefinition
return $types; return $types;
} }
public function addDefinitionComplexType($type, Collection $complexType)
{
$this->complexTypes[$type] = $complexType;
}
public function getDefinitionComplexTypes()
{
return $this->complexTypes;
}
} }

View File

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
abstract class BaseComplexType
{
private $name;
private $originalName;
private $value;
private $isNillable = false;
public function getName()
{
return $this->name;
}
public function getOriginalName()
{
return $this->originalName ?: $this->name;
}
public function getValue()
{
return $this->value;
}
public function isNillable()
{
return $this->isNillable;
}
public function setName($name)
{
$this->name = $name;
}
public function setOriginalName($originalName)
{
$this->originalName = $originalName;
}
public function setValue($value)
{
$this->value = $value;
}
public function setNillable($isNillable)
{
$this->isNillable = (bool) $isNillable;
}
}

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
use BeSimple\SoapBundle\ServiceDefinition\Loader\AnnotationComplexTypeLoader;
use Zend\Soap\Wsdl;
use Zend\Soap\Wsdl\Strategy\AbstractStrategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class ComplexType extends AbstractStrategy
{
private $loader;
private $definition;
public function __construct(AnnotationComplexTypeLoader $loader, $definition)
{
$this->loader = $loader;
$this->definition = $definition;
}
/**
* Add a complex type by recursivly using all the class properties fetched via Reflection.
*
* @param string $type Name of the class to be specified
* @return string XSD Type for the given PHP type
*/
public function addComplexType($type)
{
if (null !== $soapType = $this->scanRegisteredTypes($type)) {
return $soapType;
}
if (!$this->loader->supports($type)) {
throw new \InvalidArgumentException(sprintf('Cannot add a complex type "%s" that is not an object or where class could not be found in "ComplexType" strategy.', $type));
}
$dom = $this->getContext()->toDomDocument();
$soapTypeName = Wsdl::translateType($type);
$soapType = 'tns:'.$soapTypeName;
// Register type here to avoid recursion
$this->getContext()->addType($type, $soapType);
$complexType = $dom->createElement('xsd:complexType');
$complexType->setAttribute('name', $soapTypeName);
$all = $dom->createElement('xsd:all');
$definitionComplexType = $this->loader->load($type);
$this->definition->addDefinitionComplexType($type, $definitionComplexType);
foreach ($definitionComplexType as $annotationComplexType) {
$element = $dom->createElement('xsd:element');
$element->setAttribute('name', $propertyName = $annotationComplexType->getName());
$element->setAttribute('type', $this->getContext()->getType(trim($annotationComplexType->getValue())));
if ($annotationComplexType->isNillable()) {
$element->setAttribute('nillable', 'true');
}
$all->appendChild($element);
}
$complexType->appendChild($all);
$this->getContext()->getSchema()->appendChild($complexType);
return $soapType;
}
}

View File

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class MethodComplexType extends BaseComplexType
{
private $setter;
public function getSetter()
{
return $this->setter;
}
public function setSetter($setter)
{
$this->setter = $setter;
}
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\ServiceDefinition\Strategy;
/**
* @author Francis Besset <francis.besset@gmail.com>
*/
class PropertyComplexType extends BaseComplexType
{
}

View File

@ -0,0 +1,267 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\Tests\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Argument;
use BeSimple\SoapBundle\ServiceDefinition\Type;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\ServiceBinding\RpcLiteralRequestMessageBinder;
use BeSimple\SoapBundle\Util\Collection;
/**
* UnitTest for \BeSimple\SoapBundle\ServiceBinding\RpcLiteralRequestMessageBinder.
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class RpcLiteralRequestMessageBinderTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider messageProvider
*/
public function testProcessMessage(Method $method, $message, $assert)
{
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage($method, $message);
$this->assertSame($assert, $result);
}
public function testProcessMessageWithComplexType()
{
$attributes = new \stdClass();
$attributes->foo = 'bar';
$attributes->bar = 10;
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_argument', null, array(
new Argument('attributes', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes')),
)),
array($attributes),
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes']);
$this->assertSame('bar', $result['attributes']->foo);
$this->assertSame(10, $result['attributes']->bar);
$attributes1 = new \stdClass();
$attributes1->foo = 'foobar';
$attributes1->bar = 11;
$attributes2 = new \stdClass();
$attributes2->foo = 'barfoo';
$attributes2->bar = 12;
$message = new \stdClass();
$message->item = array($attributes1, $attributes2);
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_argument', null, array(
new Argument('attributes', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')),
)),
array($message),
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes'][0]);
$this->assertSame('foobar', $result['attributes'][0]->foo);
$this->assertSame(11, $result['attributes'][0]->bar);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['attributes'][1]);
$this->assertSame('barfoo', $result['attributes'][1]->foo);
$this->assertSame(12, $result['attributes'][1]->bar);
}
public function testProcessMessageWithComplexTypeMethods()
{
$methods = new \stdClass();
$methods->foo = 'bar';
$methods->bar = 23;
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_methods', null, array(
new Argument('setters', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters')),
)),
array($methods),
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters', $result['setters']);
$this->assertSame('bar', $result['setters']->getFoo());
$this->assertSame(23, $result['setters']->getBar());
}
public function testProcessMessageWithComplexTypeIntoComplexType()
{
$complexType = new \stdClass();
$foo = $complexType->foo = new \stdClass();
$foo->foo = 'hello';
$foo->bar = 24;
$bar = $complexType->bar = new \stdClass();
$bar->foo = 'bonjour';
$bar->bar = 1012;
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_complextype', null, array(
new Argument('complex_type', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType')),
)),
array($complexType),
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_type']);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', $result['complex_type']->getFoo());
$this->assertSame('hello', $result['complex_type']->getFoo()->foo);
$this->assertSame(24, $result['complex_type']->getFoo()->bar);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters', $result['complex_type']->bar);
$this->assertSame('bonjour', $result['complex_type']->bar->getFoo());
$this->assertSame(1012, $result['complex_type']->bar->getBar());
}
public function testProcessMessageWithComplexTypeReferences()
{
$complexType1 = new \stdClass();
$foo = $complexType1->foo = new \stdClass();
$foo->foo = 'hello';
$foo->bar = 24;
$bar = $complexType1->bar = new \stdClass();
$bar->foo = 'bonjour';
$bar->bar = 1012;
$complexType2 = new \stdClass();
$complexType2->foo = $foo;
$complexType2->bar = $bar;
$complexTypes = new \stdClass();
$complexTypes->item = array($complexType1, $complexType2);
$messageBinder = new RpcLiteralRequestMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextypes_references', null, array(
new Argument('complex_types', new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType[]')),
)),
array($complexTypes),
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_types'][0]);
$this->assertInstanceOf('BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType', $result['complex_types'][1]);
$this->assertSame($result['complex_types'][0]->getFoo(), $result['complex_types'][1]->getFoo());
$this->assertSame($result['complex_types'][0]->bar, $result['complex_types'][1]->bar);
}
public function messageProvider()
{
$messages = array();
$messages[] = array(
new Method('no_argument'),
array(),
array(),
);
$messages[] = array(
new Method('string_argument', null, array(
new Argument('foo', new Type('string')),
)),
array('bar'),
array('foo' => 'bar'),
);
$messages[] = array(
new Method('string_int_arguments', null, array(
new Argument('foo', new Type('string')),
new Argument('bar', new Type('int')),
)),
array('test', 20),
array('foo' => 'test', 'bar' => 20),
);
$strings = new \stdClass();
$strings->item = array('foo', 'bar', 'barfoo');
$messages[] = array(
new Method('array_string_arguments', null, array(
new Argument('foo', new Type('string[]')),
new Argument('bar', new Type('int')),
)),
array($strings, 4),
array('foo' => array('foo', 'bar', 'barfoo'), 'bar' => 4),
);
return $messages;
}
private function getDefinitionComplexTypes()
{
$this->definitionComplexTypes = array();
$this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes'] = $this->createPropertiesCollection(array(
array('foo', 'string'),
array('bar', 'int'),
));
$this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters'] = $this->createMethodsCollection(array(
array('foo', 'string', 'getFoo', 'setFoo'),
array('bar', 'int', 'getBar', 'setBar'),
));
$collection = $this->createMethodsCollection(array(
array('foo', '\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', 'getFoo', 'setFoo'),
));
$this->createPropertiesCollection(array(
array('bar', '\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters'),
), $collection);
$this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType'] = $collection;
return $this->definitionComplexTypes;
}
private function createPropertiesCollection(array $properties, Collection $collection = null)
{
$collection = $collection ?: new Collection('getName');
foreach ($properties as $property) {
$collectionProperty = new PropertyComplexType();
$collectionProperty->setName($property[0]);
$collectionProperty->setValue($property[1]);
$collection->add($collectionProperty);
}
return $collection;
}
private function createMethodsCollection(array $methods, Collection $collection = null)
{
$collection = $collection ?: new Collection('getName');
foreach ($methods as $method) {
$collectionMethod = new MethodComplexType();
$collectionMethod->setName($method[0]);
$collectionMethod->setValue($method[1]);
$collectionMethod->setOriginalName($method[2]);
$collectionMethod->setSetter($method[3]);
$collection->add($collectionMethod);
}
return $collection;
}
}

View File

@ -0,0 +1,227 @@
<?php
/*
* This file is part of the BeSimpleSoapBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapBundle\Tests\ServiceBinding;
use BeSimple\SoapBundle\ServiceDefinition\Method;
use BeSimple\SoapBundle\ServiceDefinition\Type;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\PropertyComplexType;
use BeSimple\SoapBundle\ServiceDefinition\Strategy\MethodComplexType;
use BeSimple\SoapBundle\ServiceBinding\RpcLiteralResponseMessageBinder;
use BeSimple\SoapBundle\Util\Collection;
use BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes;
use BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType;
use BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters;
/**
* UnitTest for \BeSimple\SoapBundle\ServiceBinding\RpcLiteralRequestMessageBinder.
*
* @author Francis Besset <francis.besset@gmail.com>
*/
class RpcLiteralResponseMessageBinderTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider messageProvider
*/
public function testProcessMessage(Method $method, $message, $assert)
{
$messageBinder = new RpcLiteralResponseMessageBinder();
$result = $messageBinder->processMessage($method, $message);
$this->assertSame($assert, $result);
}
public function testProcessMessageWithComplexType()
{
$definitionComplexTypes = $this->getDefinitionComplexTypes();
$attributes = new Attributes();
$attributes->foo = 'foobar';
$attributes->bar = 20349;
$messageBinder = new RpcLiteralResponseMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype', null, array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes')),
$attributes,
$definitionComplexTypes
);
$this->assertInstanceOf('stdClass', $result);
$this->assertSame('foobar', $result->foo);
$this->assertSame(20349, $result->bar);
$attributes1 = new Attributes();
$attributes1->foo = 'bar';
$attributes1->bar = 2929;
$attributes2 = new Attributes();
$attributes2->foo = 'foo';
$attributes2->bar = 123992;
$message = array($attributes1, $attributes2);
$messageBinder = new RpcLiteralResponseMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_argument', null, array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')),
$message,
$definitionComplexTypes
);
$this->assertTrue(is_array($result));
$this->assertInstanceOf('stdClass', $result[0]);
$this->assertSame('bar', $result[0]->foo);
$this->assertSame(2929, $result[0]->bar);
$this->assertInstanceOf('stdClass', $result[1]);
$this->assertSame('foo', $result[1]->foo);
$this->assertSame(123992, $result[1]->bar);
}
public function testProcessMessageWithComplexTypeMethods()
{
$setters = new Setters();
$setters->setFoo('foobar');
$setters->setBar(42);
$messageBinder = new RpcLiteralResponseMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_methods', null, array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters')),
$setters,
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('stdClass', $result);
$this->assertSame('foobar', $result->foo);
$this->assertSame(42, $result->bar);
}
public function testProcessMessageWithComplexTypeIntoComplexType()
{
$complexType = new ComplexType();
$foo = new Attributes();
$foo->foo = 'Hello world!';
$foo->bar = 4242;
$complexType->setFoo($foo);
$bar = new Setters();
$bar->setFoo('bar foo');
$bar->setBar(2424);
$complexType->bar = $bar;
$messageBinder = new RpcLiteralResponseMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_complextype', null, array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType')),
$complexType,
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('stdClass', $result);
$this->assertInstanceOf('stdClass', $result->foo);
$this->assertSame('Hello world!', $result->foo->foo);
$this->assertSame(4242, $result->foo->bar);
$this->assertInstanceOf('stdClass', $result->bar);
$this->assertSame('bar foo', $result->bar->foo);
$this->assertSame(2424, $result->bar->bar);
}
public function testProcessMessageWithComplexTypeReferences()
{
$attributes = new Attributes();
$attributes->foo = 'bar';
$attributes->bar = 2929;
$message = array($attributes, $attributes);
$messageBinder = new RpcLiteralResponseMessageBinder();
$result = $messageBinder->processMessage(
new Method('complextype_argument', null, array(), new Type('\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes[]')),
$message,
$this->getDefinitionComplexTypes()
);
$this->assertInstanceOf('stdClass', $result[0]);
$this->assertSame($result[0], $result[1]);
}
public function messageProvider()
{
$messages = array();
$messages[] = array(
new Method('boolean', null, array(), new Type('boolean')),
true,
true,
);
$messages[] = array(
new Method('strings', null, array(), new Type('string[]')),
array('hello', 'world'),
array('hello', 'world'),
);
return $messages;
}
private function getDefinitionComplexTypes()
{
$this->definitionComplexTypes = array();
$this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes'] = $this->createPropertiesCollection(array(
array('foo', 'string'),
array('bar', 'int'),
));
$this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters'] = $this->createMethodsCollection(array(
array('foo', 'string', 'getFoo', 'setFoo'),
array('bar', 'int', 'getBar', 'setBar'),
));
$collection = $this->createMethodsCollection(array(
array('foo', '\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Attributes', 'getFoo', 'setFoo'),
));
$this->createPropertiesCollection(array(
array('bar', '\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\Setters'),
), $collection);
$this->definitionComplexTypes['\BeSimple\SoapBundle\Tests\ServiceBinding\fixtures\ComplexType'] = $collection;
return $this->definitionComplexTypes;
}
private function createPropertiesCollection(array $properties, Collection $collection = null)
{
$collection = $collection ?: new Collection('getName');
foreach ($properties as $property) {
$collectionProperty = new PropertyComplexType();
$collectionProperty->setName($property[0]);
$collectionProperty->setValue($property[1]);
$collection->add($collectionProperty);
}
return $collection;
}
private function createMethodsCollection(array $methods, Collection $collection = null)
{
$collection = $collection ?: new Collection('getName');
foreach ($methods as $method) {
$collectionMethod = new MethodComplexType();
$collectionMethod->setName($method[0]);
$collectionMethod->setValue($method[1]);
$collectionMethod->setOriginalName($method[2]);
$collectionMethod->setSetter($method[3]);
$collection->add($collectionMethod);
}
return $collection;
}
}

View File

@ -0,0 +1,9 @@
<?php
namespace BeSimple\SoapBundle\Tests\ServiceBinding\fixtures;
class Attributes
{
public $foo;
public $bar;
}

View File

@ -0,0 +1,20 @@
<?php
namespace BeSimple\SoapBundle\Tests\ServiceBinding\fixtures;
class ComplexType
{
public $bar;
private $foo;
public function getFoo()
{
return $this->foo;
}
public function setFoo($foo)
{
$this->foo = $foo;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace BeSimple\SoapBundle\Tests\ServiceBinding\fixtures;
class Setters
{
private $foo;
private $bar;
public function getFoo()
{
return $this->foo;
}
public function setFoo($foo)
{
$this->foo = $foo;
}
public function getBar()
{
return $this->bar;
}
public function setBar($bar)
{
$this->bar = $bar;
}
}

View File

@ -70,6 +70,11 @@ class WebServiceContext
return $this->serviceDefinition; return $this->serviceDefinition;
} }
public function getWsdlFileContent($endpoint = null)
{
return file_get_contents($this->getWsdlFile($endpoint));
}
public function getWsdlFile($endpoint = null) public function getWsdlFile($endpoint = null)
{ {
$file = sprintf('%s/%s.%s.wsdl', $this->options['cache_dir'], $this->options['name'], md5($endpoint)); $file = sprintf('%s/%s.%s.wsdl', $this->options['cache_dir'], $this->options['name'], md5($endpoint));
@ -82,11 +87,6 @@ class WebServiceContext
return (string) $cache; return (string) $cache;
} }
public function getWsdlFileContent($endpoint = null)
{
return file_get_contents($this->getWsdlFile($endpoint));
}
public function getServiceBinder() public function getServiceBinder()
{ {
if (null === $this->serviceBinder) { if (null === $this->serviceBinder) {

View File

@ -13,7 +13,7 @@
> >
<php> <php>
<server name="SYMFONY" value="./vendor/symfony/src" /> <server name="SYMFONY" value="./vendor/symfony/src" />
<server name="ZEND" value="./vendor/zend/library" /> <server name="ZEND" value="./vendor/zend-framework/library" />
</php> </php>
<testsuites> <testsuites>

View File

@ -23,7 +23,8 @@ if (!is_dir($vendorDir = dirname(__FILE__).'/vendor')) {
$deps = array( $deps = array(
array('symfony', 'http://github.com/symfony/symfony.git', 'origin/HEAD'), array('symfony', 'http://github.com/symfony/symfony.git', 'origin/HEAD'),
array('zend', 'http://github.com/zendframework/zf2.git', 'origin/HEAD'), array('zend-framework/library/Zend/Soap', 'http://github.com/BeSimple/zend-soap.git', 'origin/HEAD'),
array('zend-framework/library/Zend/Mime', 'http://github.com/BeSimple/zend-mime.git', 'origin/HEAD'),
); );
foreach ($deps as $dep) { foreach ($deps as $dep) {