PHP code example of openclassrooms / service-proxy

1. Go to this page and download the library: Download openclassrooms/service-proxy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

openclassrooms / service-proxy example snippets



OpenClassrooms\ServiceProxy\ServiceProxy;

//do things

use OpenClassrooms\ServiceProxy\Annotation\Cache;

/**
 * @Cache(handler="in_memory")
 * to select the in_memory handler
 */

 

use OpenClassrooms\ServiceProxy\Handler\Contract\CacheHandler;

class InMemoryCacheHandler implements CacheHandler
{
    public function getName(): string
    {
        return 'in_memory';
    }
    ...
}

class RedisCacheHandler implements CacheHandler
{
    public function getName(): string
    {
        return 'redis';
    }
    ...
}

$cacheInterceptor = new CacheInterceptor([new ArrayCacheHandler(), new RedisCacheHandler()]);
$prefixInterceptors = [
    $cacheInterceptor,
    new EventInterceptor([/* event handlers */]),
    new TransactionInterceptor([/* transaction handlers */]),
    new SecurityInterceptor([/* security handlers */]),
];

$suffixInterceptors = [
    $cacheInterceptor,
    new EventInterceptor(),
    new TransactionInterceptor(),
];

$serviceProxyFactory = new ProxyFactory(
    new Configuration(), //if no proxies directory is provided, the system tmp dir is used
    $prefixInterceptors,
    $SecurityInterceptor,
);
$proxy = $serviceProxyFactory->createProxy(new Class());