marcojetson/php-decorators
Composer 安装命令:
composer require marcojetson/php-decorators
包简介
Python-like decorators for PHP
README 文档
README
Python-like decorators for PHP
Disclaimer
Please note that this is a proof of concept, use it at your own risk.
Usage
Specify decorators with the @Decorate annotation using one of the following formats:
- Class name::method name
- Class name (must implement __invoke)
- Function name
Decorators take the original method and the context as arguments and must return a function that takes the same arguments the original method does.
It's possible to add as many decorators as desired, always returning a valid callable for the next decorator.
(almost) Real life examples
In memory cache
class InMemoryCacheDecorator { private static $cache; public function __invoke($callable) { return function () use ($callable) { $args = func_get_args(); $key = serialize([$callable, $args]); if (!isset(static::$cache[$key])) { static::$cache[$key] = call_user_func_array($callable, $args); } return static::$cache[$key]; }; } } class MyClass { use Decorator\AnnotationDecoratorFactoryTrait; /** * @Decorate InMemoryCacheDecorator */ public function myMethod($name) { sleep(1); return 'Hello ' . $name; } } $x = MyClass::factory(); // or use new AnnotationDecorator(new MyClass()); echo $x->myMethod('Marco'), PHP_EOL; echo $x->myMethod('Marco'), ' (this time is cached)', PHP_EOL; echo $x->myMethod('Marco'), ' (this time is cached)', PHP_EOL; echo $x->myMethod('World'), PHP_EOL; echo $x->myMethod('World'), ' (this time is cached)', PHP_EOL; echo $x->myMethod('World'), ' (this time is cached)', PHP_EOL;
Cart promos
class Cart { private $total = 100; /** * @Decorate halfVat * @Decorate fixedDiscount */ public function calcTotal($vat) { return $this->total * (1 + $vat / 100); } } function halfVat($callable) { return function ($vat) use ($callable) { return $callable($vat / 2); }; } function fixedDiscount($callable) { return function ($vat) use ($callable) { $total = $callable($vat) - 5; return $total < 0 ? 0 : $total; }; } /** @var Cart $order */ $order = new Decorator\AnnotationDecorator(new Cart()); echo $order->calcTotal(21), PHP_EOL;
Controller requirements
function require_http_post($callable, $context) { return function () use ($callable, $context) { if ($context->getMethod() !== 'POST') { throw new \Exception('Not supported'); } return call_user_func_array($callable, func_get_args()); }; } class AddressController { use Decorator\AnnotationDecoratorFactoryTrait; private $method; public function __construct($method) { $this->method = $method; } public function getMethod() { return $this->method; } /** * @Decorate require_http_post */ public function deleteAction($id) { // delete... return 'success'; } } echo AddressController::factory('POST')->deleteAction(1), PHP_EOL; echo AddressController::factory('GET')->deleteAction(1), PHP_EOL;
Gotchas
- Only works for methods
- Not completely transparent, you need to use the factory method
- No parameters type hinting for the decorated class
marcojetson/php-decorators 适用场景与选型建议
marcojetson/php-decorators 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「decorator」 「decorate」 「annotation decorators」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marcojetson/php-decorators 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marcojetson/php-decorators 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marcojetson/php-decorators 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A throttler for symfony/http-client to handle rate limits
A simple Trait that implements the Decorator pattern on Laravel eloquent models.
Symfony bundle to add arbitrary metadata to classes and their constants by annotation
Renders templates with a variety of template engines.
Advanced Dependency Injection container built on top of default Yii2 container.
hyperf 注解日志
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-06-18