Kernels and providers

A kernel owns one run of a module. Extend M4W\Core\Abstract\AbstractKernel and pass its instance to Framework::run($kernel). The final constructor takes a Framework and exposes it to hooks as $this->app.

Kernel settings

serviceProviders(): array · required
Return an ordered list of classes extending AbstractServiceProvider. Return [] for a kernel with no application providers. Each class is constructed with the current application. Duplicate classes and invalid entries raise a LogicException during registration.
$disableDefaultFacades · default false
The kernel prepends FacadeServiceProvider to your list. That provider registers 'consoles' as a shared ConsoleService. Set protected bool $disableDefaultFacades = true; in your kernel to skip this provider. Direct constructor injection of ConsoleService still works. If you use a facade, select a global container and provide a shared binding for its accessor.
registerExceptionHandler(): void · optional
The application already binds M4W\Core\Interfaces\ExceptionHandler to the default console handler as a singleton. Override this hook to replace that binding before startup. A custom handler implements handle(Throwable $exception): void.

Execution order

  1. Prepare. The kernel calls registerExceptionHandler(), then bootstrap(). Use the latter to read application settings before providers are registered. Both hooks are empty by default.
  2. Register. Each provider’s required register() method declares bindings. Registration follows the order returned by the kernel, including the default provider when enabled.
  3. Boot. After every provider has registered, the kernel calls each provider’s optional boot() in registration order. Services registered by later providers are now available.
  4. Execute. The kernel calls your required execute() method. The starter dispatches CLI arguments here. Another kernel could use this point to enter a worker loop.
  5. Shut down. The kernel’s shutdown() runs first, followed by provider shutdown() hooks in reverse registration order.

Register a service

The starter’s provider uses the class itself as both key and implementation. This registration happens during startup. The first lookup creates the service and resolves its dependencies.

PHP
namespace App\ServiceProviders;

use App\Services\BaseService;
use M4W\Core\Providers\AbstractServiceProvider;

class BaseServiceProvider extends AbstractServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(BaseService::class);
    }
}

List BaseServiceProvider::class in your kernel’s serviceProviders(). Use $this->app->make(BaseService::class) inside execute() to obtain that application’s instance.

Failure and cleanup

The kernel reports a startup or execution exception and rethrows it after cleanup. A provider whose register() threw still receives shutdown(). Track which resources you acquired so that your cleanup hook can also run after partial initialization.

If cleanup fails, remaining cleanup hooks still run. A prior startup or execution error takes precedence. Otherwise the first cleanup error propagates. Each kernel instance can run only once, including after failure. Create a fresh kernel for another run and a fresh application when its bindings also need isolation.