micropackage/dochooks
Composer 安装命令:
composer require micropackage/dochooks
包简介
DocHooks - annotated WordPress hooks
README 文档
README
🧬 About DocHooks
The Laravel or Symfony projects are using method annotations for various things. This helps to have the project organized without too much code. WordPress has no implementation of such concept and this package is a remedy.
DocHooks package allows you to do:
class Example extends HookAnnotations { /** * @action test */ public function test_action() {} /** * @filter test 5 */ public function test_filter( $val, $arg ) { return $val; } /** * @shortcode test */ public function test_shortcode( $atts, $content ) { return 'This is test'; } } $example = new Example(); $example->add_hooks();
Instead of old:
$example = new Example(); add_action( 'test', [ $example, 'test_action' ] ); add_filter( 'test', [ $example, 'test_filter' ], 5, 2 ); add_shortcode( 'test', [ $example, 'test_shortcode' ] );
💾 Installation
composer require micropackage/dochooks
🕹 Usage
Annotations
@action <hook_name*> <priority>
@filter <hook_name*> <priority>
@shortcode <shortcode_name*>
The hook and shortcode name is required while default priority is 10.
You don't provide the argument count, the class will figure it out. Just use the callback params you want.
Test if DocHooks are working
When OPCache has the save_comments and load_comments disabled, this package won't work, because the comments are stripped down. See the fallback solution.
use Micropackage\DocHooks\Helper; (bool) Helper::is_enabled();
Using within the class
You can extend the HookAnnotations class:
use Micropackage\DocHooks\HookAnnotations; class Example extends HookAnnotations { /** * @action test */ public function test_action() {} } $example = new Example(); $example->add_hooks();
Or use the Trait:
use Micropackage\DocHooks\HookTrait; class Example { use HookTrait; /** * @action test */ public function test_action() {} } $example = new Example(); $example->add_hooks();
Using as a standalone object
use Micropackage\DocHooks\Helper; class Example { /** * @action test */ public function test_action() {} } $example = Helper::hook( new Example() );
Fallback
Because the HookAnnotations object stores the called hooks in _called_doc_hooks property, you are able to pull them out and parse them into a list of old add_action, add_filter and add_shortcode functions.
For this you'll need a central "repository" of all objects with hooks ie. Runtime class. See the example of this approach in the Notification plugin, which uses the WP CLI to dump all the hooks into separate file.
📦 About the Micropackage project
Micropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.
The aim is to have multiple packages which can be put together to create something bigger by defining only the structure.
Micropackages are maintained by BracketSpace.
📖 Changelog
📃 License
This software is released under MIT license. See the LICENSE file for more information.
micropackage/dochooks 适用场景与选型建议
micropackage/dochooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63.37k 次下载、GitHub Stars 达 37, 最近一次更新时间为 2020 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 micropackage/dochooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 micropackage/dochooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 63.37k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 37
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2020-01-07