sagittaracc/php-python-decorator
Composer 安装命令:
composer require sagittaracc/php-python-decorator
包简介
Python style decorator for PHP
README 文档
README
Python style decorators for PHP
Requirements
PHP 8.1 or higher
Install
composer require sagittaracc/php-python-decorator
Example
How long it takes to run a method. See the Timer decorator
use Sagittaracc\PhpPythonDecorator\Decorator; class Calc { use Decorator; #[Timer] function sum($a, $b) { sleep(1); return $a + $b; } }
This is how you can call it
$calc = new Calc(); echo call_decorator_func_array([$calc, 'sum'], [1, 2]); // Total execution: 1.00034234 ms; Result: 3
Or inline
$timerOnSum = (new Timer)->wrapper(fn($a, $b) => $calc->sum($a, $b)); echo $timerOnSum(1, 2); // Total execution: 1.00034234 ms; Result: 3
Generics
use Sagittaracc\PhpPythonDecorator\Decorator; use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T; use Sagittaracc\PhpPythonDecorator\modules\validation\core\validators\ArrayOf; #[T] class Box { use Decorator; #[ArrayOf(T::class)] public $items; public function addItem(#[T] $item) { $this->items[] = $item; } } $box = new Box(); $box(Pen::class); // new Box<Pen>(); call_decorator_func_array([$box, 'addItem'], [new Pencil]); // throws a GenericError
Validation
use Sagittaracc\PhpPythonDecorator\Decorator; use Sagittaracc\PhpPythonDecorator\tests\examples\Progress; use Sagittaracc\PhpPythonDecorator\tests\validators\Length; use Sagittaracc\PhpPythonDecorator\tests\validators\SerializeOf; use Sagittaracc\PhpPythonDecorator\tests\validators\In; use Sagittaracc\PhpPythonDecorator\tests\validators\LessThan; use Sagittaracc\PhpPythonDecorator\tests\validators\UInt8; class Progress { use Decorator; #[UInt8] public $max; #[UInt8] #[LessThan('max')] public $pos; #[In('progress', 'finish', 'aborted')] public $status; #[Length(32)] public string $caption; } $progress = new Progress(); set_decorator_prop($progress, 'max', 255); // max uint8 - 255 set_decorator_prop($progress, 'pos', 100); // should be less than max set_decorator_prop($progress, 'status', 'progress'); // status is one of possible cases (progress, finish or aborted) set_decorator_prop($progress, 'caption', 'in progress ...'); // just a string (max length is 32)
Rpc
use Sagittaracc\PhpPythonDecorator\Decorator; use Sagittaracc\PhpPythonDecorator\modules\rpc\core\Rpc; #[Rpc] class Controller { use Decorator; public function sum($a, $b) { return $a + $b; } }
in index.php
$requestBody = file_get_contents('php://input'); $controller = new Controller(); $controller($requestBody);
in terminal
$ curl -d "{"id":1,"method":"sum","params":[1,4]}" http://localhost:4000 {"json-rpc":"2.0","id":1,"result":5}
Console
use Sagittaracc\PhpPythonDecorator\Decorator; use Sagittaracc\PhpPythonDecorator\modules\console\core\Console; class Controller { use Decorator; #[Console('hello')] function greetingPerson($name) { return "Hello, $name"; } }
in the command line it would be calling for example something like this:
php index.php -c hello --name Yuriy
then in index.php you should read the command and the parameters and after that call it like this:
(new Console('hello'))->setParameters(['name' => 'Yuriy'])->getMethod(Controller::class)->run();
sagittaracc/php-python-decorator 适用场景与选型建议
sagittaracc/php-python-decorator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73 次下载、GitHub Stars 达 14, 最近一次更新时间为 2023 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sagittaracc/php-python-decorator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sagittaracc/php-python-decorator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 73
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-03