Tereta/Di Module

Overview

Dependency Injection module. Automatically resolves class dependencies through typed constructor parameters.

Dependency Injection (DI)

Automatic Dependency Resolution

Simply specify types in the constructor — the container will find and create the required instances:

class MyController
{
    public function __construct(
        protected RequestModel $requestModel,
        protected Output $output
    ) {
    }
}

Singleton

Classes using the Singleton trait are available via the ::singleton() static method. The container automatically resolves their dependencies on first call.

Connection::singleton();

Factory

Factories create instances through the container with DI support:

Model::factory()->create(['table' => 'customer']);

Class Registration

use Tereta\Di\Services\Container;

Container::singleton()->register(MyInterface::class, MyImplementation::class);

Class Scanning by Meta Attributes

The Composer service scans PSR-4 paths and finds all classes implementing a given interface or extending a base class. Used by the framework for automatic registration of controllers, commands, and models.
For example:

foreach (ComposerService::singleton()->get(CoreModel::class) as $model) {
    $tableName = $model->getAttribute(CoreModel::class)?->getArgument('table');
    $modelFactory->register($tableName, $model->getName());
}

Author and License

Author: Tereta Alexander
Website: tereta.dev
License: Apache License 2.0. See LICENSE.

 www.████████╗███████╗██████╗ ███████╗████████╗ █████╗
     ╚══██╔══╝██╔════╝██╔══██╗██╔════╝╚══██╔══╝██╔══██╗
        ██║   █████╗  ██████╔╝█████╗     ██║   ███████║
        ██║   ██╔══╝  ██╔══██╗██╔══╝     ██║   ██╔══██║
        ██║   ███████╗██║  ██║███████╗   ██║   ██║  ██║
        ╚═╝   ╚══════╝╚═╝  ╚═╝╚══════╝   ╚═╝   ╚═╝  ╚═╝
                                                      .dev

Copyright (c) 2024-2026 Tereta Alexander