1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Di; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Psr\Container\ContainerInterface; |
10
|
|
|
|
11
|
|
|
use function get_class; |
12
|
|
|
use function gettype; |
13
|
|
|
use function is_int; |
14
|
|
|
use function is_object; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* State resetter allows resetting state of the services that are currently stored in the container and have "reset" |
18
|
|
|
* callback defined. The reset should be triggered after each request-response cycle in case you build long-running |
19
|
|
|
* applications with tools like [Swoole](https://www.swoole.co.uk/) or [RoadRunner](https://roadrunner.dev/). |
20
|
|
|
*/ |
21
|
|
|
final class StateResetter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Closure[]|self[] |
25
|
|
|
*/ |
26
|
|
|
private array $resetters = []; |
27
|
|
|
private ContainerInterface $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ContainerInterface $container Container to reset. |
31
|
|
|
*/ |
32
|
8 |
|
public function __construct(ContainerInterface $container) |
33
|
|
|
{ |
34
|
8 |
|
$this->container = $container; |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Reset the container. |
39
|
|
|
*/ |
40
|
5 |
|
public function reset(): void |
41
|
|
|
{ |
42
|
5 |
|
foreach ($this->resetters as $resetter) { |
43
|
5 |
|
if ($resetter instanceof self) { |
44
|
3 |
|
$resetter->reset(); |
45
|
3 |
|
continue; |
46
|
|
|
} |
47
|
5 |
|
$resetter($this->container); |
48
|
|
|
} |
49
|
5 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Closure[]|self[] $resetters Array of reset callbacks. Each callback has access to the private and |
53
|
|
|
* protected properties of the service instance, so you can set initial state of the service efficiently |
54
|
|
|
* without creating a new instance. |
55
|
|
|
*/ |
56
|
8 |
|
public function setResetters(array $resetters): void |
57
|
|
|
{ |
58
|
8 |
|
$this->resetters = []; |
59
|
8 |
|
foreach ($resetters as $serviceId => $callback) { |
60
|
8 |
|
if (is_int($serviceId)) { |
61
|
4 |
|
if (!($callback instanceof self)) { |
62
|
1 |
|
throw new InvalidArgumentException( |
63
|
1 |
|
'State resetter object should be instance of "' . self::class . '".' |
64
|
|
|
); |
65
|
|
|
} |
66
|
3 |
|
$this->resetters[] = $callback; |
67
|
3 |
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
if (!($callback instanceof Closure)) { |
71
|
1 |
|
throw new InvalidArgumentException( |
72
|
|
|
'Callback for state resetter should be closure in format ' . |
73
|
|
|
'`function (ContainerInterface $container): void`. ' . |
74
|
1 |
|
'Got "' . $this->getType($callback) . '".' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @var mixed $instance */ |
79
|
6 |
|
$instance = $this->container->get($serviceId); |
80
|
6 |
|
if (!is_object($instance)) { |
81
|
1 |
|
throw new InvalidArgumentException( |
82
|
1 |
|
'State resetter support reset only objects. Container returns ' . gettype($instance) . '.' |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
$this->resetters[] = $callback->bindTo($instance, get_class($instance)); |
87
|
|
|
} |
88
|
5 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param mixed $variable |
92
|
|
|
*/ |
93
|
1 |
|
private function getType($variable): string |
94
|
|
|
{ |
95
|
1 |
|
return is_object($variable) ? get_class($variable) : gettype($variable); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|