Skip to end of metadata
Go to start of metadata

<p>The various server classes in Zend Framework 1 were developed over a number of years. While an attempt was made to keep them consistent (all of them follow the API of PHP's SoapServer class), they diverged on a number of fronts, while still implementing similar functionality.</p>

<p>As examples:</p>

<li>Zend_XmlRpc_Server and Zend_Json_Server both each have a way to provide API-like documentation on the services exposed - XML-RPC via the system.* methods, JSON-RPC via SMD. The information they need to communicate is basically identical, with the exception of how types are communicated. AMF has done similarly, with its Adobe_Introspector subcomponent.</li>
<li>All servers need a way to store information on the services attached to them. This information includes callbacks, the name they expose via the server, and signature information. This is primarily done via the Zend_Server_Reflection component, but we have diverged from that (both JSON-RPC and AMF allow different permutations of this).</li>
<li>The AMF server allows a lot more functionality than the other servers – it provides functionality surrounding automatic scanning of directories for service classes and types, attachment of ACLs based on signatures, and more. As a result, usage of that component is quite different for end users than the other server components.</li>
<li>Several server components utilize functionality such as header() which have side effects that make testing difficult.</li>

<p>Additionally, there are problems with some of the functionality. As an example, each relies on Zend_Server_Reflection in order to define the prototypes, which in turn enforces that incoming signatures are valid before proxying on to the underlying callbacks. This has several problems.</p>

<li>It is a runtime task, and can be expensive, particularly if you have large numbers of classes or functions you plan to expose.</li>
<li>Serialization of the structure has proved formidable. Often, the structure provided by Zend_Server_Reflection is complex enough that it's faster to regenerate it from Reflection than to build it from the serialized structure.</li>
<li>Difficulty/impossibility of customizing what gets attached. As an example, calling <code>setClass()</code> automatically exposes every public method; this may not be desired (e.g., setters likely should be omitted).</li>

<p>At the most fundamental level, each of the components – Zend_Amf_Server, Zend_Json_Server, Zend_Soap_Server, and Zend_XmlRpc_Server – are RPC servers, and as such could benefit from a consistent structure and workflow. The architecture should be crafted to be flexible enough to allow developers to customize that workflow (for instance, to hook in ACLs, etc.), without requiring modifying or extending core functionality.</p>

<h2>Proposed Architecture</h2>

<p>I propose the following core architecture for server components.</p>

<li><strong>Service</strong>: A class that describes a single RPC callback. It would consist of:
<li><strong>name</strong>: the name by which it is exposed by the server</li>
<li><strong>target</strong>: the actual callback that will be invoked. Most likely, this will compose a Zend\Stdlib\CallbackHandler, as this latter handles things like lazy-loading. However, you would be able to pass a callback of any kind here to create it.</li>
<li><strong>signatures</strong>: an array of signatures by which it may be invoked (see the <strong>Signature</strong> item below)</li>
<li><strong>return</strong>: an array of possible return types</li>
<li><strong>description</strong>: an optional description detailing what the service does</li>
<li><strong>toArray()</strong>: provides an array representation of the service. <strong>Note</strong>: if the target contains an object, this may not be possible or suitable.</li>
<li><strong>ServiceAggregate</strong>: A class aggregating one or more <strong>Service</strong> objects. This would expose the following methods:
<li><strong>add(Service $service)</strong></li>
<li><strong>addFromArray(array $array)</strong>: acts as a factory, and attaches immediately</li>
<li><strong>Signature</strong>: <strong>Iterable</strong> class detailing a single valid signature. Aggregates <strong>Param</strong> objects</li>
<li><strong>Param</strong>: Class describing a single method parameter. Consists of:
<li><strong>name</strong>: Name of parameter</li>
<li><strong>type</strong>: Type of parameter</li>
<li><strong>order</strong>: Order of parameter within method</li>
<li><strong>optional</strong>: Whether or not the parameter is optional</li>
<li><strong>default value</strong>: Default value for the parameter, if optional</li>
<li><strong>description</strong>: Optional description of the parameter's purpose</li>
<li>Note that a <strong>Param</strong> object only requires a single <strong>type</strong>. This is because for every permutation of types for a given parameter, we should report a different signature, to ensure validity.</li>
<li><strong>Server</strong>: A <strong>Dispatchable</strong> class that composes a <strong>ServerAggregate</strong>. This class would compose an <strong>EventManager</strong> instance.</li>

<p>Each server class would have protocol-specific Request and Response objects. These would handle the following tasks:</p>

<li>Martialling input. Most protocols have a specific request format required, and the Request object would ensure that it (a) received that format, and (b) it's able to translate it to native PHP structures.</li>
<li>Martialling output. Most protocols have a specific output format, and the Request object would ensure that the result of operations is properlly exposed.</li>

<p>Additionally, if the protocol requires it, a "Fault" object would be provided, which would represent an error condition. This object would encapsulate any protocol-specific error codes, and would decorate a Response object.</p>

<p>Finally, the assumption will be that the Request or Response object provided to the <code>dispatch()</code> method may not be of the protocol-specific type. Either the Request/Response objects would allow decorating the original objects, or we would provide a default event listener that could martial the protocol-specific variants from the provided instances.</p>

<p>A convenience method, <code>handle()</code>, would be provided that would automatically martial a Request and Response object for you, and pass them to the <code>dispatch()</code> method.</p>

<li><strong>ServiceCompiler</strong>: A suite of classes that act as <strong>ServiceAggregates</strong> and which would be used to generate <strong>Service</strong> instances. These would focus on three primary use cases:
<li><strong>Runtime</strong>: Provided one or more classes, objects, or function names, would use Reflection to generate Service and ServiceAggregate objects.</li>
<li><strong>Configuration</strong>: Given an array or array-like data structure (such as Zend\Config), would work as a factory to generate the Service and ServiceAggregate objects.</li>
<li><strong>Manual</strong>: Developer manually creates Service and ServiceAggregate objects.</li>
<li>Combination of the above.</li>


<p>Typically, you'll first build your services.</p>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
// Reflection:
$services = new ReflectionCompiler();
$services->setClass('My\Service\Foo', 'foo'); // under namespace "foo"
$services->addFunction('My\endpoint', 'my'); // under namespace "my"

// Configuration:
$services = new ConfigurationCompiler();
$services->setConfig($config); // An array or Traversable

// Manually:
$signature = new Signature();
$signature->add(new Param('message', 'string', 0));
$service = new Service();

Unknown macro: { return 'Hello, ' . $message; }

->setDescription('Hello world service');
$services = new ServiceAggregate();

// Combination:
$services = new ReflectionCompiler();
$services->setClass('My\Service\Foo', 'foo'); // under namespace "foo"
$services->addFunction('My\endpoint', 'my'); // under namespace "my"

$config = new ConfigurationCompiler();
$config->setConfig($config); // An array or Traversable

foreach ($config as $service) {

<p>Once you have your services ready, you can pass them to your server:</p>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$server = new Server();

<p>Now we can handle the request:</p>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
$response = $server->handle();

<p>If we wanted to add ACL checks, we might do the following:</p>

<ac:macro ac:name="code"><ac:default-parameter>php</ac:default-parameter><ac:plain-text-body><![CDATA[
// Assume $acl and $user are already created
$server->events()->attach('handle', function($e) use ($acl, $user) {
$request = $e->getRequest();
$resource = $request->getMethod();

if ($acl->isAllowed($user, $resource))

Unknown macro: { // Okay to continue; return; }

// Failed, so let's return a fault
$fault = new Fault();
return $fault;
}, 100); // execute at high priority


<li>Shared infrastructure == less maintenance. The only real place the server components would diverge is the request/response implementations.</li>
<li>EventManager would provide a consistent way to alter workflows across protocols, and allow bringing the same capabilities across all implementations.</li>
<li>Separating the service definitions into a common architecture ensures re-usability, and will also make it simpler to create serializable or programmatic definitions sculpted to exactly what you plan to expose.</li>

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 06, 2011


    <p>change this:</p>

    <p>target: the actual callback that will be invoked. Most likely, this will compose a Zend\Stdlib\CallbackHandler, as this latter handles things like lazy-loading. However, you would be able to pass a callback of any kind here to create it.</p>

    <p>to read as:</p>

    <p>target: the actual callback that will be invoked. In order to handle things like lazy-loading and callback normalization the provided callback will be wrapped by Zend\Stdlib\CallbackHandler. This also allows the caller to pass a callback of any kind (see: <a class="external-link" href=""></a>).</p>

  2. Oct 11, 2011

    <p>About SoapServer :</p>
    <li>Signature and Param is related to WsdlGenerator?</li>
    <li>How match a request by a service in the ServiceCompiler/ServiceAggregates?</li>
    <li>SoapServer can handle wsdl display (opt-in behavior)?</li>
    <li>We have only an event (ie handle)?</li>
    <li>ServiceCompiler in mode Configuration has lazy-loader for (production) performances?</li>
    <li>Fault is an object? We need a type SoapFault for the SoapServer I think.</li>

    1. Oct 11, 2011

      <p>1. Signature and param would be consumed by a WsdlGenerator (just like they would for JSON SMD or XML-RPC system.methodDescription)<br />
      2. The server would retrieve the requested method from the request, and check to see if it has a Service matching that name in its ServiceAggregate. If so, it passes the parameters on to the callback associated with the matching Service.<br />
      3. Returning WSDL/SMD/etc. are up to either (a) individual server implementations (e.g., if we have a GET request, return this information) or (b) the application (i.e., create specific routes/controllers/etc. for this).<br />
      4. Probably we would have three events: request, handle, response. This would allow for manipulating the request and response object pre/post handling separately from the "handling" of the request (i.e., dispatching to a Service callback).<br />
      5. Lazy-loading would be likely handled by use of lazy-loading in the CallbackHandler (this is implemented already in Zend\Stdlib\CallbackHandler). See the above comment from Wil Moore.<br />
      6. A Fault is simply a special type of Response, and would be protocol-specific.</p>

  3. Feb 26, 2013

    <p>Nevermind, noticed it was already answered in you're initial draft.</p>

  4. Mar 02, 2013

    <p>I have started working on this component, currently it's a stand alone module but im hoping it could be merged into 2.3-4 or 3.0 </p>