openclassrooms/service-proxy
Composer 安装命令:
composer require openclassrooms/service-proxy
包简介
Library
README 文档
README
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.
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
Concepts
Interceptors
Interceptors are used as decorators to react to the method execution.
for example using @Cache annotation, is a condition to enable the cache interceptor.
There is two types of interceptors:
Prefix interceptors :
Interceptors that are called before the method execution, they must implement OpenClassrooms\ServiceProxy\Interceptor\Contract\PrefixInterceptor
Three methods are called:
prefix: called before the method execution. Should return instance ofOpenClassrooms\ServiceProxy\Interceptor\Response\Response.supportsPrefix: called to know if the interceptor should be called, for example in the case of the cache interceptor, it will check that the method has the@Cacheannotation.getPrefixPriority: called to know the priority of the interceptor, the higher the priority, the earlier the interceptor will be called.
Suffix interceptors :
Interceptors that are called after the method execution, even if an exception is thrown, they must implement OpenClassrooms\ServiceProxy\Interceptor\Contract\SuffixInterceptor
Three methods are called:
suffix: called after the method execution even if an exception is thrown. should return instance ofOpenClassrooms\ServiceProxy\Interceptor\Response\Response.supportsSuffix: called to know if the interceptor should be called, for example in the case of the cache interceptor, it will check that the method has the@Cacheannotation.getSuffixPriority: called to know the priority of the interceptor, the higher the priority, the earlier the interceptor will be called.
Handling exceptions
If you want to react to an exception thrown by the method, you can check for the exception in the suffix interceptor.
- To check
$instance->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
trueex: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
getNamemethod to return it. - Each handler must return true if it's the default handler or false if not, you can use the
isDefaultmethod 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:
use OpenClassrooms\ServiceProxy\Annotation\Cache; /** * @Cache(handler="in_memory") * to select the in_memory handler */
Usage
Instantiation
Symfony
Check out the ServiceProxyBundle. The bundle provides an easy configuration option for this library.
Manual
Example
First implement the handlers
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:
$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
Acknowledgments
This library is based on Ocramius\ProxyManager.
openclassrooms/service-proxy 适用场景与选型建议
openclassrooms/service-proxy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 409.28k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 openclassrooms/service-proxy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 openclassrooms/service-proxy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 409.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-21