dealroadshow/proximity
最新稳定版本:v1.0.1
Composer 安装命令:
composer require dealroadshow/proximity
包简介
Library that allows to create PHP proxy objects
README 文档
README
Motivation
This library is inspired by the brilliant
Ocramius/ProxyManager library.
The mentioned library is lacking some features for now,
such as ability to create proxies from the class instances with non-nullable
typed properties. Unfortunately, this great library is not actively supported,
therefore dealroadshow/proximity library tries to implement
some of ProxyManager's functionality.
Usage
You can easily create proxy instances by using ProxyFactory class:
<?php $factory = new \Dealroadshow\Proximity\ProxyFactory( new \Dealroadshow\Proximity\ProxyGenerator(), '/path/where/proxy/classes/will/be/stored' ); $myObject = new Foo(); $proxy = $factory->proxy($myObject);
You may also want to intercept calls to object's methods before or after the method body was executed. In order to do that, use interceptors:
use Dealroadshow\Proximity\ProxyFactory; use Dealroadshow\Proximity\ProxyGenerator; use Dealroadshow\Proximity\MethodsInterception\BodyInterceptorInterface; use Dealroadshow\Proximity\ProxyInterface; use Dealroadshow\Proximity\MethodsInterception\BodyInterceptionResult; use Dealroadshow\Proximity\ProxyOptions; use Dealroadshow\Proximity\MethodsInterception\ResultInterceptorInterface; use Dealroadshow\Proximity\MethodsInterception\InterceptionContext; class Foo { public function bar(): void { echo 'Bar!', PHP_EOL; } } $factory = new ProxyFactory( new ProxyGenerator(), '/path/where/proxy/classes/will/be/stored' ); $bodyInterceptor = new class implements BodyInterceptorInterface { public function beforeMethodBody(ProxyInterface $proxy, object $object, string $methodName, array $methodArgs) : BodyInterceptionResult { echo "Method $methodName() is about to be executed!\n"; return new BodyInterceptionResult(preventMethodBody: false); } }; $resultInterceptor = new class implements ResultInterceptorInterface { public function afterMethodBody(ProxyInterface $proxy, object $object, string $methodName, array $methodArgs, InterceptionContext $context): void { echo "Method $methodName() just finished execution!\n"; } }; $foo = new Foo(); $proxy = $factory->proxy( $foo, new ProxyOptions( ['bar' => [$bodyInterceptor]], ['bar' => [$resultInterceptor]], ) ); $proxy->bar();
The following output will be generated:
Method bar() is about to be executed!
Bar!
Method bar() just finished execution!
You can also prevent method body execution from body interceptors by providing true
as a first argument to BodyInterceptionResult constructor. If you're preventing the body
execution, you may also provide return value for the method, by passing it as a second
parameter to BodyInterceptionResult constructor.
To replace method's return value from result interceptors, use $context argument:
public function afterMethodBody(ProxyInterface $proxy, object $object, string $methodName, array $methodArgs, InterceptionContext $context): void { // do some stuff, then: $context->returnValue = 'New value'; }
dealroadshow/proximity 适用场景与选型建议
dealroadshow/proximity 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 186.88k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dealroadshow/proximity 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dealroadshow/proximity 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 186.88k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-17