Giter Site home page Giter Site logo

serviceproxy's Introduction

Service Proxy

Build Status SensioLabsInsight Coverage Status

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)

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

Instantiation

If you plan to use ServiceProxy in a Symfony2 project, check out the ServiceProxyBundle. The bundle provides an easy configuration option 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 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.

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 can not have different cache providers

Acknowledgments

This library is based on Ocramius\ProxyManager.

serviceproxy's People

Contributors

romainkuzniak avatar emilyreese avatar openclassrooms-admin avatar

Watchers

James Cloos avatar Arnaud Lefevre avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.