Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/openclassrooms/serviceproxy
https://github.com/openclassrooms/serviceproxy
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/openclassrooms/serviceproxy
- Owner: OpenClassrooms
- License: mit
- Created: 2015-08-23T19:52:21.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T16:14:02.000Z (8 months ago)
- Last Synced: 2024-04-03T17:34:14.361Z (8 months ago)
- Language: PHP
- Size: 406 KB
- Stars: 0
- Watchers: 21
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Service Proxy
[![Build Status](https://travis-ci.org/OpenClassrooms/ServiceProxy.svg?branch=master)](https://travis-ci.org/OpenClassrooms/ServiceProxy)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/e0840e44-8f14-4620-96cf-76300727e808/mini.png)](https://insight.sensiolabs.com/projects/e0840e44-8f14-4620-96cf-76300727e808)
![PHPStan](https://img.shields.io/badge/PHPStan-level%207-brightgreen.svg?style=flat)
![PHP](https://img.shields.io/badge/PHP-%3E=%207.4-brightgreen.svg?style=flat)
![Coverage](../coverage/coverage.svg?raw=true)Service Proxy is a library that provides functionality to manage technical code over a class:
- Transactional context
- Security access
- Cache management
- Events
- Logs (not implemented yet)## Installation
The easiest way to install ServiceProxy is via [composer](http://getcomposer.org/).Create the following `composer.json` file and run the `php composer.phar install` command to install it.
```json
{
"require": {
"openclassrooms/service-proxy": "*"
}
}
```
```php
method()->threwException()`
- To get the exception (null if no exception was thrown) `$instance->method()->getException()`
- To get the return value (null in case of an exception) `$instance->method()->getReturnValue()`#### Early return
- If a prefix interceptor returns a response with early return parameter set to `true` ex: `Response($data, true)`, the method won't be executed and the suffix interceptors won't be called.
- If a suffix interceptor returns a response with early return parameter set to `true`, the exception won't be thrown, in the case of a method that throws an exception.You can create your own interceptors, or use the built-in ones:
### Handlers
Handlers are used by interceptors to manage the infrastructure code.
To be able to use built-in interceptors, you need to implement the built-in handlers contracts.- All handles need to implement `OpenClassrooms\ServiceProxy\Handler\Contract\AnnotationHandler`.
- Each handler must have a unique name, you can use the `getName` method to return it.
- Each handler must return true if it's the default handler or false if not, you can use the `isDefault` method to return it.
- You can't have two handlers with the same name by annotation.
- You can have only one default handler, by annotation.
- If you have only one handler by annotation, it will be the default one.**example:**
```php
use OpenClassrooms\ServiceProxy\Annotation\Cache;/**
* @Cache(handler="in_memory")
* to select the in_memory handler
*/```
## Usage
### Instantiation#### Symfony
Check out the [ServiceProxyBundle](http://github.com/openclassrooms/ServiceProxyBundle).
The bundle provides an easy configuration option for this library.#### Manual
##### Example
First implement the handlers```php
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';
}
...
}
```Then you can inject the handlers into the interceptors:
```php
$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());
```#### Built-in interceptors
- [Cache](docs/Interceptor/cache.md)
- [Event](docs/Interceptor/event.md)
- [Security](docs/Interceptor/security.md)
- [Transaction](docs/Interceptor/transaction.md)## Acknowledgments
This library is based on [Ocramius\ProxyManager](https://github.com/Ocramius/ProxyManager).