tobento/service-booting

Booting manager for creating PHP applications.

1.0.5 2024-04-26 17:52 UTC

This package is auto-updated.

Last update: 2024-10-26 18:54:48 UTC


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.0 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

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