-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
2,208 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# Remote Object Proxy | ||
|
||
The remote object implementation is a mechanism that enables an local object to control an other object on an other server. | ||
Each call method on the local object will do a network call to get information or execute operations on the remote object. | ||
|
||
## What is remote object proxy ? | ||
|
||
A remote object is based on an interface. The remote interface defines the API that a consumer can call. This interface | ||
must be implemented both by the client and the RPC server. | ||
|
||
## Usage examples | ||
|
||
RPC server side code : | ||
|
||
```php | ||
interface FooServiceInterface | ||
{ | ||
public function foo(); | ||
} | ||
|
||
class Foo implements FooServiceInterface | ||
{ | ||
/** | ||
* Foo function | ||
* @return string | ||
*/ | ||
public function foo() | ||
{ | ||
return 'bar remote'; | ||
} | ||
} | ||
|
||
$server = new Zend\XmlRpc\Server(); | ||
$server->setClass('Foo', 'FooServiceInterface'); // my FooServiceInterface implementation | ||
$server->handle(); | ||
``` | ||
|
||
Client side code (proxy) : | ||
|
||
```php | ||
interface FooServiceInterface | ||
{ | ||
public function foo(); | ||
} | ||
|
||
$factory = new \ProxyManager\Factory\RemoteObjectFactory(); | ||
$adapter = new \ProxyManager\Factory\RemoteObject\Adapter\XmlRpc( | ||
'http://127.0.0.1/xmlrpc.php' | ||
); | ||
|
||
$proxy = $factory->createProxy('FooServiceInterface', $adapter); | ||
|
||
var_dump($proxy->foo()); // "bar remote" | ||
``` | ||
|
||
Three adapters are available by default : `ProxyManager\Factory\RemoteObject\Adapter\XmlRpc`, `ProxyManager\Factory\RemoteObject\Adapter\JsonRpc` & `ProxyManager\Factory\RemoteObject\Adapter\Soap`. Custom adapter must implement `ProxyManager\Factory\RemoteObject\AdapterInterface` : | ||
|
||
```php | ||
interface AdapterInterface | ||
{ | ||
/** | ||
* Call remote object | ||
* | ||
* @param string $wrappedClass | ||
* @param string $method | ||
* @param array $params | ||
*/ | ||
public function call($wrappedClass, $method, array $params = array()); | ||
} | ||
``` | ||
|
||
It is very easy to create your own implementation (Rest for example) ! | ||
|
||
## Adapter usages examples | ||
|
||
* Example with JsonRpc adapter : | ||
|
||
Json server side code : | ||
|
||
```php | ||
interface FooServiceInterface | ||
{ | ||
public function foo(); | ||
} | ||
|
||
class Foo implements FooServiceInterface | ||
{ | ||
/** | ||
* Foo function | ||
* @return string | ||
*/ | ||
public function foo() | ||
{ | ||
return 'bar remote'; | ||
} | ||
} | ||
|
||
$server = new Zend\Json\Server\Server(); | ||
$server->setClass('Foo', 'FooServiceInterface'); // my FooServiceInterface implementation | ||
$server->handle(); | ||
``` | ||
|
||
Client side code (proxy) : | ||
|
||
```php | ||
interface FooServiceInterface | ||
{ | ||
public function foo(); | ||
} | ||
|
||
$factory = new \ProxyManager\Factory\RemoteObjectFactory(); | ||
$adapter = new \ProxyManager\Factory\RemoteObject\Adapter\JsonRpc( | ||
'http://127.0.0.1/jsonrpc.php' | ||
); | ||
|
||
$proxy = $factory->createProxy('FooServiceInterface', $adapter); | ||
|
||
var_dump($proxy->foo()); // "bar remote" | ||
``` | ||
|
||
* Example with Soap adapter : | ||
|
||
Json server side code : | ||
|
||
```php | ||
interface FooServiceInterface | ||
{ | ||
public function foo(); | ||
} | ||
|
||
class Foo implements FooServiceInterface | ||
{ | ||
/** | ||
* Foo function | ||
* @return string | ||
*/ | ||
public function foo() | ||
{ | ||
return 'bar remote'; | ||
} | ||
} | ||
|
||
$server = new Zend\Soap\Server(__DIR__ . '/soap.wsdl'); | ||
$server->setClass('Foo'); // my FooServiceInterface implementation | ||
$server->handle(); | ||
``` | ||
|
||
Client side code (proxy) : | ||
|
||
```php | ||
interface FooServiceInterface | ||
{ | ||
public function foo(); | ||
} | ||
|
||
$factory = new \ProxyManager\Factory\RemoteObjectFactory(); | ||
$adapter = new \ProxyManager\Factory\RemoteObject\Adapter\Soap( | ||
'http://127.0.0.1/soap.php' | ||
); | ||
|
||
$proxy = $factory->createProxy('FooServiceInterface', $adapter); | ||
|
||
var_dump($proxy->foo()); // "bar remote" | ||
``` | ||
|
||
## Tuning performance for production | ||
|
||
See [Tuning ProxyManager for Production](https://github.com/Ocramius/ProxyManager/blob/master/docs/tuning-for-production.md). |
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
73 changes: 73 additions & 0 deletions
73
src/ProxyManager/Factory/RemoteObject/Adapter/BaseAdapter.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace ProxyManager\Factory\RemoteObject\Adapter; | ||
|
||
use ProxyManager\Factory\RemoteObject\AdapterInterface; | ||
|
||
/** | ||
* Remote Object base adapter | ||
* | ||
* @author Vincent Blanchon <blanchon.vincent@gmail.com> | ||
* @license MIT | ||
*/ | ||
abstract class BaseAdapter implements AdapterInterface | ||
{ | ||
/** | ||
* URI of the webservice endpoint | ||
* @var string | ||
*/ | ||
protected $uri; | ||
|
||
/** | ||
* Constructor | ||
* @param string $uri | ||
*/ | ||
public function __construct($uri) | ||
{ | ||
$this->uri = $uri; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function call($wrappedClass, $method, array $params = array()) | ||
{ | ||
$client = $this->getClient(); | ||
$serviceName = $this->assemble($wrappedClass, $method); | ||
|
||
return $client->call($serviceName, $params); | ||
} | ||
|
||
/** | ||
* Assembly of the service name will be used by the adapter | ||
* | ||
* @param string $wrappedClass | ||
* @param string $method | ||
* | ||
* @return string Service name | ||
*/ | ||
abstract protected function assemble($wrappedClass, $method); | ||
|
||
/** | ||
* Build webservices client | ||
* | ||
* @return \Zend\Server\Client | ||
*/ | ||
abstract public function getClient(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace ProxyManager\Factory\RemoteObject\Adapter; | ||
|
||
use Zend\Json\Server\Client; | ||
use ProxyManager\Proxy\Exception\RemoteObjectException; | ||
|
||
/** | ||
* Remote Object JSON RPC adapter | ||
* | ||
* @author Vincent Blanchon <blanchon.vincent@gmail.com> | ||
* @license MIT | ||
*/ | ||
class JsonRpc extends BaseAdapter | ||
{ | ||
/** | ||
* JsonRpc client | ||
* @var \Zend\Json\Server\Client | ||
*/ | ||
private $client; | ||
|
||
/** | ||
* Rpc client building | ||
* | ||
* @param string $uri | ||
* | ||
* @throws \ProxyManager\Proxy\Exception\RemoteObjectException | ||
*/ | ||
public function __construct($uri) | ||
{ | ||
if (! class_exists('Zend\Json\Server\Client')) { | ||
throw new RemoteObjectException('JsonRpc adapter does not exists. Please install zend-json package.'); | ||
} | ||
|
||
if (empty($uri)) { | ||
throw new RemoteObjectException('Webservices URI is required'); | ||
} | ||
|
||
parent::__construct($uri); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function assemble($wrappedClass, $method) | ||
{ | ||
return $wrappedClass . '.' . $method; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getClient() | ||
{ | ||
if ($this->client) { | ||
return $this->client; | ||
} | ||
|
||
return $this->client = new Client($this->uri); | ||
} | ||
} |
Oops, something went wrong.