定制 kaiseki/container-builder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kaiseki/container-builder

Composer 安装命令:

composer require kaiseki/container-builder

包简介

Create a PSR-11 container with laminas-servicemanager with laminas-di

README 文档

README

Create a PSR-11 dependency injection container with laminas-servicemanager and laminas-di

Table of Contents

Summary

This package is a bare-bone implementation that allows you to set up a dependency injection (DI) container quickly and easily. Use this package to build high-quality WordPress themes and plugins with modern PHP development methods.

It's not limited to WordPress though. That's just what we use it for. You can technically utilize it wherever you want.

Why you should use it?

While it may be daunting or seem like overhead at a first glance to use a DI container, it has many advantages. Once you clear the first hurdle of learning the basics, you will never want to go back doing things the old way.

To counter your first concern: No, this is not a framework. This package is simply a ready-to-use implementation to get you going with writing clean and awesome quality-code quickly. What you make of it is up to you.

But let's get back to the advantages of using a DI container:

  1. Firstly, you'll learn how to become a better developer.
  2. You will create cleaner and more readable code.
  3. Become more productive by creating reusable packages.
  4. Maintaining large projects becomes much easier.
  5. Your code will be more testable.

Technical Details

Important: You will need composer and set up autoloading to use this package.

The container builder creates a PSR-11 container, configured to use laminas-servicemanager and laminas-di.

After you have set up the autoloading and use the container builder you are ready to use dependency injection in your code.

What does the DI container do for you:

  • Have it autowire your classes and packages for you.
  • Require and use third-party composer packages quickly to easily extend your code.
  • Reuse code you have already written.
  • Create your desired namespaces and folder structure without any restrictions (other than conforming to PSR-4) and everything will work out of the box.
  • Enjoy advanced code completion inside your IDE.
  • Centralize your configuration instead of having it scattered all over the project.

Usage (in a WordPress theme)

We'll explain the usage of this package inside a WordPress theme.

Typically kaiseki/container-builder is used in combination with packages that provide ConfigProviders. ConfigProviders are simple callables without dependencies, that return a config array.

In our example we additionaly set up the kaiseki/wp-hook package, that we use in many projects ourselves. It helps us to quickly register packages that will automatically set themselves up by hooking into WordPress actions and filters. This step is purely optional.

Installation

composer require kaiseki/container-builder

Setup

Add this to the top of your theme's functions.php file:

<?php

declare(strict_types=1);

if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
    throw new RuntimeException('Autoloader is not installed. Please run "composer install".');
}
require __DIR__ . '/vendor/autoload.php';

$container = (new \Kaiseki\ContainerBuilder\ContainerBuilder(require __DIR__ . '/resources/config-providers.php'))
    ->withConfigFolder(__DIR__ . '/resources/config')
    ->build();

First, we'll define the path to a file resources/config-providers.php that returns an array of ConfigProviders that we wish to load. More on that later.

Next we define the folder that will contain our configuration files with withConfigFolder(). All files inside this folder ending with .global.php will be loaded first and files ending with .local.php will be loaded last. This allows you to override settings in the global config with local settings (which you should obviously add to .gitignore).

In the last step, we build our container with build().

Install and initialize the wp-hook package (optional)

Now that our container is built we can initialize the kaiseki/wp-hook package. All packages that want to hook into WordPress actions and filters will do so at this point.

Install kaiseki/wp-hook first,

composer require kaiseki/wp-hook

Then add this line of code after the container was built.

// Call the HookProviderInterface to register hooks
$container->get(\Kaiseki\WordPress\Hook\HookProviderRegistry::class)->registerCallbacks();

Config Providers

So what's actually a ConfigProvider?

In the most basic sense, they return a configuration array.

Often they will contain information about dependencies.

ConfigProviders are for example the place where you instruct your application which factory should create a Class that gets injected into another Class.

They also contain aliases so your application knows which implementation of an Interface to inject into a Class that depends on it.

You can read more on that by checking out the documentation for the laminas-servicemanager.

In our case we want to load our two packages' ConfigProviders so we'll add those to resources/config-providers.php:

<?php

return [
    \Kaiseki\WordPress\Config\ConfigProvider::class,
    \Kaiseki\WordPress\Hook\ConfigProvider::class,
];

kaiseki/container-builder 适用场景与选型建议

kaiseki/container-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 kaiseki/container-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kaiseki/container-builder 我们能提供哪些服务?
定制开发 / 二次开发

基于 kaiseki/container-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-02-06