Configuration of individual object state reset values
You can restrict the resetting of object states within the container by declaring the \Kaspi\DiContainer\ObjectResetters PHP class in your configuration file, along with a collection of values that define how the states of the specified objects should be reset.
The collection used to specify these state resets consists of key-value pairs: the key is the container identifier[1] used to retrieve the corresponding PHP class via the container's get() method while the value instructs the \Kaspi\DiContainer\ObjectResetters class on how to perform the state reset for that object (see here for details).
IMPORTANT
When you manually declare the \Kaspi\DiContainer\ObjectResetters PHP class, object state resets will be performed only for the PHP classes explicitly included in your collection.
Example of an ObjectResetters class declaration
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function doReset(): void
{
// reset class state here
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
public function doResetPropertyFoo(): void
{
// reset the class property state here
}
public function doResetPropertyBar(): void
{
// reset the class property state here
}
}<?php
declare(strict_types=1);
// file: /app/src/Helpers/Resetters.php
namespace App\Helpers;
use App\Services\Baz;
final class Resetters
{
public static function resetObject(object $object): void
{
if ($object instanceof Baz) {
// reset the state of `App\Services\Baz` here
return;
}
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
final class Baz
{
}<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Services;
use Kaspi\DiContainer\Interfaces\ResetInterface;
final class Qux implements ResetInterface
{
public function reset(): void
{
// reset class state here
}
}You must define the values for resetting the state of objects specifically for the following PHP classes:
\App\Services\Bar: Use an anonymous function that calls the methodsdoResetPropertyFoo()anddoResetPropertyBar(), which reset the state of the class's two properties.\App\Services\Qux: Implement thereset()method as defined by the\Kaspi\DiContainer\Interfaces\ResetInterfaceinterface.
In the object_resetters.php configuration file, define the PHP class \Kaspi\DiContainer\ObjectResetters using the diAutowire() helper function, ensuring that the $isSingleton parameter is set to true.
<?php
declare(strict_types=1);
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use Kaspi\DiContainer\ObjectResetters;
use Framework\AppFactory;
$container = (new DiContainerBuilder())
->import('App\\', '/app/src')
->load('/app/config/object_resetters.php')
->build();
$app = AppFactory::create(
// ... some dependency here
container: $container;
);
$resetters = $container->get(ObjectResetters::class);
// Event loop
while ($request = $app->getRequest()) {
$response = $app->handle($request);
$app->emit($response);
$resetters->reset();
}<?php
declare(strict_types=1);
// file: /app/config/object_resetters.php
use Kaspi\DiContainer\ObjectResetters;
use App\Services\{Bar, Qux};
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
$resetters = [
Bar::class => static function (Bar $bar): void {
$bar->doResetPropertyFoo();
$bar->doResetPropertyBar();
},
Qux::class => 'reset',
];
yield diAutowire(ObjectResetters::class, isSingleton: true)
->setup('setup', [$resetters]);
};IMPORTANT
Please note that the $resetters argument should be passed to the setup() method as an array, given that the method's $arguments parameter is defined as an array type.
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. ↩︎