pixelplugin/wp-container

WordPress 容器插件

v1.2.0 2023-11-19 00:06 UTC

This package is auto-updated.

Last update: 2024-09-19 02:12:20 UTC


README

pixelplugin/wp-container 是一个 WordPress 插件,根据 WordPress 容器 API 提供全局 PSR 兼容的依赖容器。

安装

composer require pixelplugin/wp-container

https://packagist.org.cn/packages/pixelplugin/wp-container

使用方法

  1. 安装此插件
  2. 使用 composer 在您的项目中引入 pixelplugin/wp-container-api
  3. 现在您可以使用容器 API

过滤器

wp_container

/**
 * @param array $definitions container definitions.
 */
apply_filters('wp_container', $definitions);

此过滤器用于配置代码的容器定义。它支持以下格式

  • ClassName::class 以向容器添加一些类,但通常不需要,因为容器支持自动装配。
  • InterfaceName::class => ClassName::class 以指定某个类/接口的另一个实现。
  • ClassName::class => $instance 以将类名与预创建的实例绑定。
  • ClassName::class => $callable 以将类名与某个可调用的工厂绑定。

此过滤器在 after_setup_theme 动作中使用以创建容器,因此容器在 init 钩子(或同一 after_setup_theme 但具有较低优先级)上完全可用。