Service Proxy is a library that provides functionality to manage technical code over a class:
- Transactional context (not implemented yet)
- Security access (not implemented yet)
- Cache management
- Events (not implemented yet)
- Logs (not implemented yet)
The easiest way to install ServiceProxy is via composer.
Create the following composer.json
file and run the php composer.phar install
command to install it.
{
"require": {
"openclassrooms/service-proxy": "*"
}
}
<?php
require 'vendor/autoload.php';
use OpenClassrooms\ServiceProxy\ServiceProxy;
//do things
If you plan to use ServiceProxy in a Symfony2 project, check out the ServiceProxyBundle. The bundle provides an easy configuration option for this library.
use OpenClassrooms\ServiceProxy\Helpers\ServiceProxyHelper;
$serviceProxyFactory = $this->getServiceProxyFactory();
$proxy = $serviceProxyFactory->createProxy(new Class());
use OpenClassrooms\ServiceProxy\Helpers\ServiceProxyHelper;
$proxy = $this->getServiceProxyBuilder()
->create(new Class())
->withCache(new CacheProviderDecorator(new ArrayCache()))
->build();
See ProxyManager
use OpenClassrooms\ServiceProxy\Helpers\ServiceProxyHelper;
$serviceProxyFactory = $this->getServiceProxyFactory();
$serviceProxyFactory->setCacheProvider(new CacheProviderDecorator(new ArrayCache()));
$serviceProxyFactory->setProxyFactory($this->buildProxyFactory(new Configuration()));
$proxy = $serviceProxyFactory->createProxy(new Class());
use OpenClassrooms\ServiceProxy\Helpers\ServiceProxyHelper;
$proxyBuilder = $this->getServiceProxyBuilder();
$proxyBuilder->setProxyFactory($this->buildProxyFactory(new Configuration()));
$proxy = $proxyBuilder->create(new Class())
->withCache(new CacheProviderDecorator(new ArrayCache()))
->build();
@Cache annotation allows cache management.
namespace MyProject\AClass;
use OpenClassrooms\ServiceProxy\Annotations\Cache;
class AClass
{
/**
* @Cache
*
* @return mixed
*/
public function aMethod($aParameter)
{
// do things
return $data;
}
}
The id is equal to: md5('MyProject\AClass::aMethod::'.serialize($aParameter))
and the TTL is the default.
/**
* @Cache(lifetime=1000)
* Add a TTL of 1000 seconds
*/
/**
* @Cache(id="'key'")
* Set the id to "key"
*/
Supports Symfony ExpressionLanguage, for example:
/**
* @Cache(id="'key' ~ aParameter.field")
* Set the id to 'key'.$aParameter->field
*/
/**
* @Cache(namespace="'namespace'")
* Add a namespace to the id with a namespace id equals to "namespace"
*/
Supports Symfony ExpressionLanguage, for example:
/**
* @Cache(namespace="'namespace' ~ aParameter.field")
* Add a namespace to the id with a namespace id equals to 'namespace'.$aParameter->field
*/
- a class can not have different cache providers
This library is based on Ocramius\ProxyManager.