horat1us/yii2-static-behavior
Composer 安装命令:
composer require horat1us/yii2-static-behavior
包简介
Static Behavior for Yii2
关键字:
README 文档
README
This package implements static behavior to allow you append behavior-like event handlers to external objects.
Usage example: You use package that contains ActiveRecord. If you need to handle some events of this record (beforeInsert, afterInsert etc.), this package definitely for you.
It uses static yii\base\Event::on() and yii\base\Event::off() under the hood.
The main advantage of using this static behavior:
- event handler class (ItemInterface) will be instantiated before event will be called, so dependencies will be lazy-loaded too.
Installation
composer require horat1us/yii2-static-behavior
Structure
- StaticBehavior - main class that deals with attaching and detaching handlers.
- Bootstrap - configurable application bootstrap class. It use StaticBehavior to attach handlers before application request and detach after request.
- ItemInterface - describes simple event handler (to be used in StaticBehavior).
- Item - abstract ItemInterface implementation. Allows to configure handlers (for example methods) for different events.
- Bootstrap\Behavior - behavior to attach handlers before action and detach after controller/module action. Should be used if some handlers will be used only when module executed.
Usage
StaticBehavior
First, you need to implement ItemInterface. Then, configure your StaticBehavior with implemented items.
Note: you can use \Closure instead of item classes (not recommended)
<?php namespace Example; use yii\db; use yii\base; use Horat1us\Yii\StaticBehavior; class ExternalRecord extends db\ActiveRecord { // some implementation you cannot change } $staticBehavior = new StaticBehavior([ 'target' => ExternalRecord::class, 'items' => [ base\Model::EVENT_BEFORE_VALIDATE => function(base\Event $event) { // you can handle event in \Closure }, base\Model::EVENT_AFTER_VALIDATE => [ // custom item implementation 'class' => StaticBehavior\Item::class, ], db\ActiveRecord::EVENT_INIT => [ // or use interface to implement items 'class' => StaticBehavior\ItemInterface::class, ], ], ]); // To attach event handlers to class $staticBehavior->attach(); // To detach attached event handlers from class $staticBehavior->detach();
Bootstrap
To bootstrap your application you should use Bootstrap. It will attach handlers before application request and detach it after request.
<?php use Horat1us\Yii\StaticBehavior; // config.php return [ 'bootstrap' => [ 'class' => StaticBehavior\Bootstrap::class, 'behaviors' => [ // StaticBehavior references. See previous section for examples. ], ], ];
Item
Main purpose of items - lazy dependency injection for event handlers. You can define dependencies in constructor or use yii2-way configuration (prefered).
Item that handles yii\db\ActiveRecord events:
Example (handle yii\db\ActiveRecord events)
Bootstrap\Behavior
This behavior should be used when some handlers will be used only in one module. For example:
- sending messages after authentication
- sending message with token for two-factor authentication
It can be used with controller or any another component (you will need to configure events).
Example (authorization tokens)
Contributors
License
horat1us/yii2-static-behavior 适用场景与选型建议
horat1us/yii2-static-behavior 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.53k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「bootstrap」 「static」 「Behavior」 「yii2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 horat1us/yii2-static-behavior 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 horat1us/yii2-static-behavior 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 horat1us/yii2-static-behavior 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A CakePHP plugin to provide easy access to zumba/swivel
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
This is a set of Yii2 behaviors extensions
Views for the package MedicOneSystems Livewire Datatables with Bootstrap 4
Model behaviors for the li₃ PHP framework
统计信息
- 总下载量: 1.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-06