Helper function diAutowire()
The helper function diAutowire() registers a PHP class in the container.
Function signature:
use Kaspi\DiContainer\Interfaces\DiDefinition\{
DiDefinitionArgumentsInterface as Args,
DiDefinitionSetupAutowireInterface as Setup,
DiDefinitionTagArgumentInterface as Tag,
DiDefinitionResetterSetterInterface as Resetter
};
\Kaspi\DiContainer\diAutowire(
string $definition,
?bool $isSingleton = null,
bool $isLazy = false,
): Args & Setup & Tag & ResetterParameters:
$definition– The FQCN [1] of the PHP class being declared.$isSingleton– The container will return the same object instance (singleton pattern). If set tonull, the value will be determined based on the container configuration.$isLazy– declare a PHP class as lazy object.
TIP
Review the following sections of the documentation:
The diAutowire() function returns an object that provides the following methods:
bindArguments()– passes arguments to a PHP class constructor.setup()– declares dependency injection via a mutable setter method [2].setupImmutable()– declares dependency injection via an immutable setter method [3].bindTag()– assigns a tag with metadata.setResetter()– specifies a value (or callback) used to reset the object's state.
Container identifier for diAutowire()
The helper function diAutowire() can pass an identifier [4] to the container from the $definition parameter, making it accessible via the container's get() and has() methods.
A configuration file with an automatically generated container identifier:
<?php
declare(strict_types=1);
use App\Services\Foo;
use function Kaspi\DiContainer\{diAutowire, diParameter};
return static function (): Generator {
yield Foo::class => diAutowire(Foo::class)
yield diAutowire(Foo::class)
};The container identifier for a PHP class being declared can be specified as an arbitrary string.
<?php
declare(strict_types=1);
use App\Services\Foo;
use function Kaspi\DiContainer\{diAutowire, diParameter};
return static function (): Generator {
/**
* $container->get('services.foo_one')
*/
yield diAutowire(Foo::class);
yield 'services.foo_one' => diAutowire(Foo::class);
/**
* $container->get('services.foo_two')
*/
yield 'services.foo_two' => diAutowire(Foo::class);
};bindArguments()
Arguments for a PHP class constructor.
The bindArguments() method for defining parameters is described by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionArgumentsInterface interface.
Method signature:
DiDefinitionArgumentsInterface::bindArguments(
mixed ...$argument
)Parameters:
$argument– arguments.
When specifying how a dependency should be injected into a particular parameter using bindArguments(), you can use named arguments.
If the method does not explicitly specify how a dependency should be injected into a given parameter, the container will handle the injection automatically based on its configuration.
TIP
- The
$argumentparameter inbindArguments()can accept helper functions such asdiGet(),diValue(),diAutowire(), and others. - For injecting scalar-type dependencies, it is recommended to use container parameters.
WARNING
The bindArguments() method overwrites any previously added arguments.
Example of dependency injection via the bindArguments() method
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/ReportMaker.php
namespace App\Services;
use App\Storages\ReportStorage;
final class ReportMaker
{
public function __construct(
public readonly ReportStorage $storage,
public readonly int $lastDays,
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Storages/ReportStorage.php
namespace App\Storages;
final class ReportStorage
{
public function __construct(
public readonly string $dir
) {}
}You need to use the bindArguments() method to specify how the constructor should inject a dependency into a particular parameter:
- into the
$lastDaysparameter of the\App\Services\ReportMakerclass; - into the
$dirparameter of the\App\Storages\ReportStorageclass.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\ReportMaker;
$container = (new DiContainerBuilder())
->load('/app/config/report_services.php')
->build();
$report = $container->get(ReportMaker::class);
var_dump($report->lastDays);
var_dump($report->storage->dir);<?php
declare(strict_types=1);
// file: /app/config/report_services.php
use App\Services\ReportMaker;
use App\Storages\ReportStorage;
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(ReportStorage::class)
->bindArguments(
dir: '/var/data'
);
yield diAutowire(ReportMaker::class)
->bindArguments(
lastDays: 10
);
// other services
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
int(10)
string(9) "/var/data"Dependency injection via setter methods
In addition to dependency injection via a PHP class constructor, it is also possible to inject dependencies using setter methods.
setup()
The setup() method injects a dependency into a PHP class via a mutable setter method [2:1] and is defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionSetupAutowireInterface interface.
Method signature:
DiDefinitionSetupAutowireInterface::setup(
string $method,
array $arguments = []
)Parameters:
$method– the name of a setter method within the class.$arguments– the arguments for the setter method's parameters.
The $arguments parameter passes arguments to a setter method.
When defining dependency injection for a specific parameter, use the name of that parameter as the array key within $arguments.
Setter method parameters that do not receive explicit arguments will be injected automatically by the container, based on its configuration.
Arguments for a setter method can utilize helper functions such as diGet(), diValue(), diAutowire(), and others.
For the injection of scalar-type dependencies, it is recommended to use container parameters.
Example of implementation via the setup() method
PHP class:
<?php
declare(strict_types=1);
// file: /app/src/Storages/ReportStorage.php
namespace App\Storages;
final class ReportStorage
{
private int $permission = 0777;
public function __construct(
public readonly string $dir
) {}
public function setPermission(int $permission): void
{
$this->permission = $permission;
}
public function getPermission(): int
{
return $this->permission;
}
}Dependency injection must be performed via the setter method \App\Storages\ReportsStorage::setPermission().
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Storages\ReportsStorage;
$container = (new DiContainerBuilder())
->load('/app/config/storage_service.php')
->build();
$storage = $container->get(ReportsStorage::class);
var_dump($storage->getPermission());<?php
declare(strict_types=1);
// file: /app/config/storage_service.php
use App\Storages\ReportsStorage;
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(ReportsStorage::class)
->bindArguments(
dir: '/var/data'
)
->setup(
'setPermission',
[0600]
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
int(384)setupImmutable()
The setupImmutable() method injects a dependency into a PHP class via an immutable setter method[3:1] and is defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionSetupAutowireInterface interface.
The setter method must return self, static, or the type of the PHP class being declared within the diAutowire() helper function.
Method signature:
DiDefinitionSetupAutowireInterface::setupImmutable(
string $method,
array $arguments = []
)Parameters:
$method– the name of a setter method within the class.$arguments– the arguments for the setter method's parameters.
The $arguments parameter passes arguments to a setter method.
When defining dependency injection for a specific parameter, use the name of that parameter as the array key within $arguments.
Setter method parameters that do not receive explicit arguments will be injected automatically by the container, based on its configuration.
Arguments for a setter method can utilize helper functions such as diGet(), diValue(), diAutowire(), and others.
For the injection of scalar-type dependencies, it is recommended to use container parameters.
bindTag()
Tags allow a PHP class being declared to be associated with a collection of elements marked with the same tag.
The bindTag() method is defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionTagArgumentInterface interface.
Method signature:
DiDefinitionTagArgumentInterface::bindTag(
string $name,
array $options = [],
null|int|string $priority = null
)TIP
For more details, see the guide on working with tags.
setResetter()
A value used for resetting an object's state.
The setResetter() method is defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionResetterSetterInterface interface.
DiDefinitionResetterSetterInterface::setResetter(
callable|false|string $resetter
)TIP
See the detailed information in the section “Resetting object state in long-running processes”.
Fully Qualified Class Name – the full name of a class or interface, including its namespace. ↩︎
A mutable setter method is a method used to change the value of an object's internal property; it modifies the existing object in place and typically returns
void. ↩︎ ↩︎An immutable setter method is a method that does not modify the current object, but instead creates and returns a new instance of it with an updated value. ↩︎ ↩︎
A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the
get()method. ↩︎