承接 tobento/service-booting 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tobento/service-booting

最新稳定版本:2.0

Composer 安装命令:

composer require tobento/service-booting

包简介

Booting manager for creating PHP applications.

README 文档

README

Booting for any PHP applications.

Table of Contents

Getting started

Add the latest version of the booting service project running this command.

composer require tobento/service-booting

Requirements

  • PHP 8.4 or greater

Highlights

  • Framework-agnostic, will work with any project
  • Decoupled design

Documentation

Booter

Create 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)

Parameters explanation

Parameter Description
bootFactory The boot factory.
name The name of the booter.
bootMethods The boot methods the booter calls if exists, in the order defined.
terminateMethods The terminate methods the booter calls if exists, in the order defined.

Register Boots

Each boot class is registered once. If you register the same class again it will just overwrite it.

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

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

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

Booting

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

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

You may call the booting methods as many times as you want. By default the boot methods gets called once, except declared otherwise with the constant REBOOTABLE in the boot classes. See Rebooting.

Example

$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
*/

Misc

get

Returns the specified boot registry if exist, otherwise NULL.

use Tobento\Service\Booting\BootRegistry;

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

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

getBoot

Returns the specified boot if exist, otherwise NULL.

use Tobento\Service\Booting\BootInterface;

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

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

getBoots

Returns the registered boots.

use Tobento\Service\Booting\BootRegistry;

$boots = $booter->getBoots();

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

getBooted

For debugging purposes, you might want to get boots booted.

$booted = $booter->getBooted();

Boots

Create Boot

You can create a Boot by simply exenting Tobento\Service\Booting\Boot:

use Tobento\Service\Booting\Boot;

class MyBoot extends Boot
{
    //
}

Currently, your Boot doesn't do anything. Depending on the booter boot and terminate methods defined, you can now define these methods in your Boot class which support method injection (autowiring).

use Tobento\Service\Booting\Boot;

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

Dependent Boot

If your Boot depends on another Boot you may ensure that the Boot has always been initiated before by using the constant BOOT.

use Tobento\Service\Booting\Boot;

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

Boot Priority

You may declare a boot priority by using the constant PRIORITY. The default priority is 1000.

use Tobento\Service\Booting\Boot;

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

Rebooting

By default, when the booter calls the Booting methods muliple times, the boot method gets call once only. You may define methods as rebootable by using the constant REBOOTABLE.

use Tobento\Service\Booting\Boot;

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

Boot Info

You may add some info for your boot methods by using the constant 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.',
    ];
}

Credits

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-04