Bootstrap

The Bootstrap feature in Zemit Core is responsible for setting up and handling the application using the Mvc design pattern or the Cli (console). It follows a series of steps to ensure that the application is properly initialized and configured. These steps include setting a shared event manager, custom initialization, reading configuration files and command line parameters, setting up the default Dependency Injection (DI) container, preparing PHP configuration, and registering service providers. The Bootstrap also prepares and loads the different modules of the application before finally handling it. By using the Bootstrap, you can easily set up and manage your application with Zemit Core.

Quick Start

To create a customized bootstrap with the Zemit Core framework, you will need to create a new class that extends from the Zemit\Bootstrap class. This will allow you to personalize the bootstrap to your needs by overriding any of its methods as desired. For example, you may want to initialize the configuration with your own class. Keep in mind that you are free to customize the bootstrap to fit your specific requirements and workflow.

Method #2: During the initialize phase

If you wish to fully customize the creation of each instance, you can override the initialize method to create your own instance. Please note that Zemit will also fire both events (before and after), but only when they were supposed to be called and not at the initialization.

// ./app/Bootstrap.php
namespace App;

use App\Bootstrap\Config;

class Bootstrap extends \Zemit\Bootstrap
{
    public function initialize() {
        $this->config ??= new Config();
        $this->di->setShared('config', $this->config);
        
        $this->router ??= new Router();
        $this->di->setShared('router', $this->router);
        
        ...
    }
}

Method #3: During the registering phase

You can fully customize each steps at your convenience by overriding the methods.

// ./app/Bootstrap.php
namespace App;

use App\Bootstrap\Config;
use App\Provider\Config\ServiceProvider as ConfigServiceProvider;
use Zemit\Config\ConfigInterface;

class Bootstrap extends \Zemit\Bootstrap
{
    public function registerConfig(): void
    {
        if (!$this->di->has('config')) {
            $configService = new ConfigServiceProvider($this->di);
            $configService->register($this->di);
        }
        $this->config ??= $this->di->get('config');
    }
    
    public function registerServices(): void
    {
        ...
    }
    
    public function registerModules(): void
    {
        ...
    }
    
    public function registerRouter(): void
    {
        ...
    }
}

Last updated