weew/kernel
Composer 安装命令:
composer require weew/kernel
包简介
Simple kernel for registration and invocation of service providers.
README 文档
README
Table of contents
Installation
composer require weew/kernel
Introduction
Kernel is responsible for the bootstrap process of service providers. It offers you a easy and intuitive way to register your own providers. The boot process consists of three steps - instantiation, initialization and booting. There is also an additional step - shutdown. This gives your providers a lot of flexibility on when to do what.
Usage
Creating a provider
Any class can be used as a provider. If the provider has any of these methods configure, initialize, boot, shutdown, the container will invoke them accordingly. It does not require a specific interface. This is by choice, I'll explain why I chose this solution in one of the future readme updates.
class MyServiceProvider {} // or class MyServiceProvider { public function configure() {} public function initialize() {} public function boot() {} public function shutdown() {} }
Registering providers
It is fairly easy to create a kernel and register your own providers.
$kernel = new Kernel(); $kernel->addProviders([ MyServiceProvider::class, AnotherServiceProvider::class, ]);
Configuration
When you configure the kernel, all of its service providers get instantiated and configured.
$kernel->configure();
Initialization
When you initialize the kernel, all of its service providers get initialized.
$kernel->initialize();
Booting
On boot, all service providers will be booted. This is a good place to setup your provider and do some work.
$kernel->boot();
Shutdown
This will shutdown the kernel and all of its providers.
$kernel->shutdown();
Extension
The kernel comes without a container. Out of the box the service providers will be very limited since they have no way to share anything. There are several workarounds for this.
Sharing data between providers
The easiest way to share data between providers is to use kernel's shared arguments.
class MyProvider { public function boot(IDictionary $shared) { $shared->get('container')['foo'] = 'bar'; } } $kernel = new Kernel(); $container = []; $kernel->getSharedArguments()->set('container', $container); $kernel->addProvider(MyProvider::class);
Custom container support
A better way to enable container access for your providers is to replace the default implementation of the IProviderInvoker with your own. In this example I'll be using this powerful container.
class ContainerProviderInvoker implements IProviderInvoker { private $container; public function __construct(IContainer $container) { $this->container = $container; } public function create($providerClass, IDictionary $shared) { $this->container->get($providerClass, ['shared' => $shared]); } public function configure($provider, IDictionary $shared) { $this->container->callMethod($provider, 'configure', ['shared' => $shared]); } public function initialize($provider, IDictionary $shared) { $this->container->callMethod($provider, 'initialize', ['shared' => $shared]); } public function boot($provider, IDictionary $shared) { $this->container->callMethod($provider, 'boot', ['shared' => $shared]); } public function shutdown($provider, IDictionary $shared) { $this->container->callMethod($provider, 'shutdown', ['shared' => $shared]); } } $container = new Container(); $invoker = new ContainerProviderInvoker($container); $kernel = new Kernel($invoker); // or $kernel->setProviderInvoker($invoker);
From now on all providers will benefit from constructor and method injection and will be able to share anything in the container. Depending on which container package you use the IProviderInvoker implementation may vary, but the idea stays the same.
Existing container integrations
There is an integration available for the weew/container container. See weew/kernel-container-aware.
Related projects
- PHP Container works very well together with this package.
weew/kernel 适用场景与选型建议
weew/kernel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 346 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 weew/kernel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 weew/kernel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 346
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-16