Skip to content

emilyreese/ServiceProxy

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service Proxy

Build Status SensioLabsInsight Coverage Status

Service Proxy is a library that provides facilities 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)

Installation

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

Instanciation

If you plan to use ServiceProxy in a Symfony2 project, checkout the ServiceProxyBundle. The bundle provide an easy configuration for this library.

Basic

Factory
use OpenClassrooms\ServiceProxy\Helpers\ServiceProxyHelper;

$serviceProxyFactory = $this->getServiceProxyFactory();
$proxy = $serviceProxyFactory->createProxy(new Class());
Builder
use OpenClassrooms\ServiceProxy\Helpers\ServiceProxyHelper;

$proxy = $this->getServiceProxyBuilder()
              ->create(new Class())
              ->withCache(new CacheProviderDecorator(new ArrayCache()))
              ->build();

Custom

See ProxyManager

Factory
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());
Builder
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

@Cache annotation allows to manage cache.

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 one.

Other options:

Lifetime:
/**
 * @Cache(lifetime=1000)
 * Add a TTL of 1000 seconds
 */
Id (key):
/**
 * @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
 */
Namespace:
/**
 * @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
 */

Known limitations

  • a class could not have different cache providers

Acknowledgments

This library is based on the top of Ocramius\ProxyManager.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%