tobento/service-booting

创建PHP应用的启动管理器。

1.0.5 2024-04-26 17:52 UTC

This package is auto-updated.

Last update: 2024-09-26 18:37:31 UTC


README

为任何PHP应用提供启动功能。

目录

入门

使用以下命令添加运行中的booting服务项目的最新版本。

composer require tobento/service-booting

要求

  • PHP 8.0或更高版本

亮点

  • 框架无关,可与任何项目一起使用
  • 解耦设计

文档

Booter

创建Booter

use Tobento\Service\Booting\Booter;
use Tobento\Service\Booting\BooterInterface;
use Tobento\Service\Booting\AutowiringBootFactory;
use Tobento\Service\Container\Container;

// Any PSR-11 container
$container = new Container();

$booter = new Booter(
    bootFactory: new AutowiringBootFactory($container),
    name: 'app',
    bootMethods: ['register', 'boot'],
    terminateMethods: ['terminate'],
);

var_dump($booter instanceof BooterInterface);
// bool(true)

参数说明

注册Boots

每个启动类只注册一次。如果再次注册相同的类,它将简单地覆盖它。

$booter->register(new ConfigBoot());

// You may set a priority for the boot:
$booter->register(DebugBoot::class, priority: 2000);

$booter->register(
    HttpBoot::class,
    RoutingBoot::class,
);

启动

// calls the boot methods:
$booter->boot();

// calls the terminate methods
$booter->terminate();

您可以无限次调用启动方法。默认情况下,启动方法只会调用一次,除非在启动类中用常量 REBOOTABLE 明确声明。请参阅 重启

示例

$booter->register(ConfigBoot::class);

$booter->register(DebugBoot::class, priority: 2000);

$booter->register(
    HttpBoot::class,
    RoutingBoot::class,
);

$booter->boot();
$booter->terminate();

/*
boot: DebugBoot::class
boot: ConfigBoot::class
boot: HttpBoot::class
boot: RoutingBoot::class
terminate: RoutingBoot::class
terminate: HttpBoot::class
terminate: ConfigBoot::class
terminate: DebugBoot::class
*/

杂项

get

如果存在,返回指定的启动注册表,否则返回NULL。

use Tobento\Service\Booting\BootRegistry;

$boot = $booter->get(MyBoot::class);

var_dump($boot instanceof BootRegistry);
// bool(true)

getBoot

如果存在,返回指定的启动,否则返回NULL。

use Tobento\Service\Booting\BootInterface;

$boot = $booter->getBoot(MyBoot::class);

var_dump($boot instanceof BootInterface);
// bool(true)

getBoots

返回已注册的Boots。

use Tobento\Service\Booting\BootRegistry;

$boots = $booter->getBoots();

foreach($boots as $boot) {
    var_dump($boot instanceof BootRegistry);
    // bool(true)
}

getBooted

为了调试目的,您可能想获取已启动的Boots。

$booted = $booter->getBooted();

Boots

创建Boot

您可以通过简单地实例化 Tobento\Service\Booting\Boot 来创建一个Boot。

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    //
}

目前,您的Boot不执行任何操作。根据booter定义的 bootterminate 方法,您现在可以在支持方法注入(自动装配)的Boot类中定义这些方法。

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    public function boot(): void
    {
        // Do something
    }
    
    public function terminate(): void
    {
        // Do something
    }    
}

依赖Boot

如果您的Boot依赖于另一个Boot,您可以使用常量 BOOT 来确保Boot始终在它之前启动。

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    public const BOOT = [
        AnotherBoot::class,
    ];
    
    public function boot(AnotherBoot $boot): void
    {
        // Do something
    }    
}

启动优先级

您可以使用常量 PRIORITY 声明启动优先级。默认优先级是1000。

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    public const PRIORITY = 2000;
}

重启

默认情况下,当booter多次调用启动方法时,启动方法只调用一次。您可以使用常量 REBOOTABLE 将方法定义为可重启的。

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    public const REBOOTABLE = ['terminate'];
}

启动信息

您可以使用常量 INFO 为您的启动方法添加一些信息。

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    public const INFO = [
        'boot' => 'Some description what the boot method does.',
        'terminate' => 'Some description what the terminate method does.',
    ];
}

鸣谢