rmp-up/wp-di
Composer 安装命令:
composer require rmp-up/wp-di
包简介
Simple Dependency Injection / Service Container for more OOP in WordPress (PSR-11 compatible)
README 文档
README
WP DI
PHP Dependency Injection for WordPress (based on Pimple)
This is nothing new but we added some magic:
- Compatible with projects using Pimple already
- Configuration via plain arrays, Yaml or other
- "Less WordPress more OOP"
and still searching for other magic to apply.
Getting started
Add the package to your project
composer require rmp-up/wp-di
and set up the container provider
RmpUp\WpDi\WpDi::load( require 'services.php' ); $container = \RmpUp\WpDi\WpDi::pimple();
Friends of YAML can add composer require symfony/yaml
and use
RmpUp\WpDi\WpDi::load( RmpUp\WpDi\Yaml::parseFile( 'services.yaml' ) );
Features
A full documentation can be found in the documentation of the latest releases. The following is just a sneak peek into some of the possibilities. Try using the "preload.php" when you have PHP >= 7.4 or a composer workflow to get even more performance:
{
"autoload": {
"files": [ "vendor/rmp-up/wp-di/preload.php" ]
}
}
Services and parameters
Define services as known from classical DI but also ...
- Primitive parameters as usual
- Default values for options
- Path to templates
- Inject all of them into services
- Allow factories to create services
# Primitive parameters as usual parameters: some: "primitives" like: 42 # Default values for options options: _my_plugin_rating: 5/7 _this_is: cool # Path to templates templates: admin-view: my-own-plugin/template-parts/fester.php frontend-view: my-own-plugin/public/coogan.jpg # looks up the file in theme, theme-compat and plugin directory # Inject all of them into services services: SimpleOne: arguments: - "Hello there!" - 1337 SomeThing: arguments: - "%like%" # the parameter - "%_this_is%" # the option - "%frontend-view%" # path to the template - "@SimpleOne" # the other service
Register services in WordPress
Services can also be used to ...
- Add actions / filters
- Add Meta-Boxes
- Register Post-Types
- Register Shortcodes
- Register Widgets
- Add WP-CLI commands
services: StrrevEverything: filter: the_content # calling `::__invoke` for the "the_content"-filter BackendAdminListThing: meta_box: title: Greatest box in the World! screen: post MyOwnPostType: post_type: animals # cast service to array and forward to register_post_type BestShortcodeEver: shortcode: shortcode_wont_die widget: ~ # Shortcode and widget at once. Wow! DoItCommand: wp_cli: do-it: __invoke doit: __invoke seriously do-it do-it do-it: seriously # cli commands mapped to methods
Use tags to enhance YAML
Within YAML you can:
- Access PHP-Constants
- Concatenate text
- Translate text
Mostly lazy to get the best performance.
services: # Access PHP-Constants InjectingConstants: arguments: - !php/const ABSPATH # Concatenate text ThisIsSomeTemplate: arguments: - !join [ !php/const WP_CONTENT_DIR, "/plugins/grey-matter/walter.jpg" ] # Translations within YAML ThisThingNeedsTranslations: arguments: - !__ [ Who is Adam?, dark ] - !esc_attr__ [ white ] # ... many more translation functions available ...
All of this is only possible when using \RmpUp\WpDi\Yaml::parseFile(...)
or ::parse(...).
Contributing
We used this in some projects and still maintain/enhance it, so please open an issue if there is anything we can help with.
If you'd like to contribute, please fork the repository and make changes as you'd like. Pull requests are warmly welcome.
Related projects
Please also note the following projects about dependency injection container in WordPress:
Licensing
See the LICENSE.txt for details.
rmp-up/wp-di 适用场景与选型建议
rmp-up/wp-di 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.89k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2019 年 04 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rmp-up/wp-di 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rmp-up/wp-di 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2019-04-27