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 aLogicExceptionduring registration. $disableDefaultFacades· defaultfalse- The kernel prepends
FacadeServiceProviderto your list. That provider registers'consoles'as a sharedConsoleService. Setprotected bool $disableDefaultFacades = true;in your kernel to skip this provider. Direct constructor injection ofConsoleServicestill 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\ExceptionHandlerto the default console handler as a singleton. Override this hook to replace that binding before startup. A custom handler implementshandle(Throwable $exception): void.
Execution order
- Prepare. The kernel calls
registerExceptionHandler(), thenbootstrap(). Use the latter to read application settings before providers are registered. Both hooks are empty by default. - Register. Each provider’s required
register()method declares bindings. Registration follows the order returned by the kernel, including the default provider when enabled. - 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. - 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. - Shut down. The kernel’s
shutdown()runs first, followed by providershutdown()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.
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.