定制 decodelabs/kingdom 二次开发

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

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

decodelabs/kingdom

Composer 安装命令:

composer require decodelabs/kingdom

包简介

Service container and management system

README 文档

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Service container management system

Kingdom provides a set of simple but powerful interfaces for building structures that can contain your whole application logic and services.

Installation

This package requires PHP 8.4 or higher.

Install via Composer:

composer require decodelabs/kingdom

Usage

Kingdom works in tandem with Monarch to give developers an easy way to manage dependency injection, application containment, service access and more.

An implementation of the Kingdom interface can be considered the root of your application in which all other services are contained. A project can have multiple Kingdom instances which are in turn controlled by the static Monarch class. Only one Kingdom instance can be considered active at a time.

You can think of Monarch and Kingdom in a similar way to how you would think of a monarch and a kingdom in real life - the monarch rules over the kingdom and the kingdom has a boundary around it, isolating it from the outside world.

You shouldn't need to interact with the monarch for many things as the kingdom should be able to provide most of the resources that are needed; only bother the monarch when you have to!

Container

The heart of a Kingdom is its container. Instead of dealing directly with PSR-11 containers which only provide a rudimentary access scheme, Kingdom uses an adapter system that allows implementations to create a richer and more powerful service access mechanism.

Pandora implements both the PSR-11 interface and Kingdom's ContainerAdapter interface, allowing it to function directly as a service container for Kingdom without any additional configuration.

If you are building your application on top of Fabric, a Kingdom implementation is provided for you using Pandora as the container by default.

See the ContainerAdapter interface for more information on the methods available.

Services

Items held within the container can implement the Service interface to set them apart from other items in the container. The interface requires the implementation of a method to provide an instance of the service based on the contents on the container. A default implementation is provided in the ServiceTrait trait.

Services can be accessed in a Kingdom instance using the getService method.

use DecodeLabs\Kingdom\Service;
use DecodeLabs\Kingdom\ServiceTrait;

class MyService implements Service
{
    use ServiceTrait;
}

$service = $myKingdom->getService(MyService::class);

However, because a Kingdom is intended to be isolated and managed by the Monarch package, it is recommended to use the Monarch::getService method instead, which will delegate to the active Kingdom instance.

use DecodeLabs\Monarch;

$service = Monarch::getService(MyService::class);

The active Kingdom instance should be registered with Monarch during your bootstrap process. If you use Genesis for your bootstrapping, this will be taken care of for you, otherwise:

use DecodeLabs\Monarch;

Monarch::setKingdom($myKingdom);

Runtime

Once a Kingdom has been initialized, it's main purpose is to run the application. This is determined by an instance of the Runtime interface, which acts like a kernel for your application.

There are default implementations of HTTP and CLI runtimes provided by Harvest and Clip respectively.

Once bootstrapped, run your application:

$myKingdom->run();

Past this point, the container and service structure should be used to auto-wire the loading process with automatic dependency injection when constructing objects and services.

This can generally be handled in your own libraries by using Slingshot, or in a pinch, using Monarch::getService().

Fabric

When using Kingdom in a Fabric application, you can use your Kingdom implementation as the place to set up your application's container and services.

namespace My\Namespace;

use DecodeLabs\Fabric\Kingdom as FabricKingdom;
use DecodeLabs\Harvest\Profile as HttpProfile;
use DecodeLabs\Harvest\Middleware\Cors;

class Kingdom extends FabricKingdom
{
    public protected(set) string $name = 'DecodeLabs Playground';

    public function initialize(): void
    {
        parent::initialize();

        Monarch::getPaths()->alias('@public', '@run/public');
        Monarch::getPaths()->alias('@components', '@run/src/@components');

        $this->container->prepare(
            HttpProfile::class,
            fn (HttpProfile $profile) => $profile->add(Cors::class, allow: ['*'])
        );

        $this->container->setFactory(
            SomeOtherThing::class,
            fn () => new SomeOtherThing()
        );

        // ...etc
    }
}

Licensing

Kingdom is licensed under the MIT License. See LICENSE for the full license text.

decodelabs/kingdom 适用场景与选型建议

decodelabs/kingdom 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.46k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 decodelabs/kingdom 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-20