pinkcrab / wp-hook-subscriber
为钩子创建单个订阅者,是 PinkCrab Perique 框架的一部分
2.0.1
2023-06-21 21:48 UTC
Requires
- php: >=7.4.0
- pinkcrab/perique-framework-core: 2.0.*
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: <=1.0.0
- gin0115/wpunit-helpers: 1.1.*
- php-stubs/wordpress-stubs: 6.2.*
- phpstan/phpstan: 1.*
- phpunit/phpunit: ^8.5 || ^9.0
- roots/wordpress: 6.2.*
- symfony/var-dumper: <=6.2.7
- szepeviktor/phpstan-wordpress: <=1.3.1
- vlucas/phpdotenv: <=5.5.0
- wp-coding-standards/wpcs: <=2.3.0
- wp-phpunit/wp-phpunit: 6.2.*
- yoast/phpunit-polyfills: ^1.0.0 || ^2.0.0
- dev-master
- 2.0.1
- 2.0.0
- 1.0.1
- 1.0.0
- 0.2.2
- 0.2.1
- 0.2.0
- dev-dependabot/composer/szepeviktor/phpstan-wordpress-lte-1.3.3
- dev-dependabot/composer/wp-coding-standards/wpcs-lte-3.1.0
- dev-dependabot/composer/php-stubs/wordpress-stubs-6.2.staror-6.3.star
- dev-dependabot/composer/wp-phpunit/wp-phpunit-6.2.staror-6.3.star
- dev-dependabot/composer/roots/wordpress-6.2.staror-6.3.star
- dev-develop
- dev-feature/update-dev-deps-perique-2_0_1
- dev-feature/push-to-perique-1.x
- dev-release/0.2.1
This package is auto-updated.
Last update: 2024-09-17 14:10:16 UTC
README
Perique 钩子订阅者
为钩子创建单个订阅者,是 PinkCrab 插件框架的一部分
要求
需要 PinkCrab Perique 框架 V2.0.*
安装
$ composer require pinkcrab/wp-hook-subscriber
此模块允许创建单个钩子订阅,以便与 WordPress 建立接口。底层仍然使用 PinkCrab 框架相同的注册过程,但为单个调用提供了干净的抽象。
每个扩展提供的基类的类,其钩子都将添加到加载器中,无论是在定义的操作还是延迟操作。允许完全使用 DI 容器。
由于加载器注册钩子调用的方式,类是在初始化钩子时实例化的。这可能会对 WooCommerce 和其他可扩展插件造成问题,因为一些全局变量稍后才会填充。Hook_Subscriber 允许延迟构造,因此您的回调将在全局作用域中创建。
非延迟订阅者
class On_Single_Hook extends Abstract_Hook_Subscription { /** * The hook to register the subscriber * @var string */ protected ?string $hook = 'some_hook'; /** * Some service * @param My_Service */ protected $my_service; public function __construct( My_Service $my_service ) { $this->my_service = $my_service; } /** * Callback * @param mixed ...$args */ public function execute( ...$args ): void { // Args are accessed in the order they are passed. // do_action('foo', 'first','second','third',.....); //$args[0] = first, $args[1] = second, $args[2] = third, ..... if ( $args[0] === 'something' ) { $this->my_service->do_something( $args[1] ); } } } // Would be called by do_action('some_hook', 'something', ['some','data','to do','something']);
延迟订阅者
class Deferred_Hook extends Abstract_Hook_Subscription { /** * The hook to register the subscriber * @var string */ protected ?string $hook = 'some_hook'; /** * Deferred hook to call * * @var string|null */ protected ?string $deferred_hook = 'some_global_populated'; /** * Our global data * @param Some_Global|null */ protected $some_global; public function __construct() { global $some_global; $this->some_global = $some_global; } /** * Callback * @param mixed ...$args */ public function execute( ...$args ): void { // Depends on a global which is set later than init. if ( $args[0] === 'something' && ! empty( $this->some_global ) ) { do_something( $this->some_global->some_property, $args[1] ); } } }
在另一个插件或 wp-core 中,$some_global 被填充,然后我们可以在创建和实际调用我们的钩子时随时挂钩。
function acme_plugin_function(){ global $some_global; // Currently empty/null $some_global = new Some_Global(); do_action('some_global_populated', ['some', 'data']); }
当 some_global_populated 触发时,会创建一个 Deferred_Hook 的新实例并注册回调。这使我们无论何时都能访问 Some_Global。我们最终创建了两个延迟钩子的实例,一次在初始化时注册第一次调用,然后在实际的延迟钩子中再次创建,用于实际的钩子调用。
以前的版本
- 对于 Perique V1.0.* 使用版本 1.0.*
- 对于 Perique V0.4.* 使用版本 0.2.2
- 对于 Perique V0.3.* 使用版本 0.2.1
变更日志
- 2.0.1 - 更新开发依赖项
- 2.0.0 - 停止支持 PHP 7.2 & 7.3 并添加对 Perique V2.0.* 的支持
- 1.0.1 - 停止支持 PHP 7.1,添加 PHP8 支持,更新所有依赖项并添加第三方质量检查(Scrutinizer & CodeClimate)
- 1.0.0 - 现在支持 Perique 及其从 Registerable 到 Hookable 接口命名的迁移。
- ---- 核心从 PinkCrab 插件框架重命名为 Perique ----
- 0.2.2 更新测试和代码以反映框架 0.4.* 的变化
- 0.2.1 添加了额外的测试和覆盖率报告。
- 0.2.0 - 从初始 Event_Hook 命名迁移,并对延迟钩子的添加方式进行了少量更改,使用 DI 重新创建新实例,而不是重置状态。