1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Core\Container; |
13
|
|
|
|
14
|
|
|
use Psr\Container\ContainerExceptionInterface; |
15
|
|
|
use Spiral\Core\Exception\Container\AutowireException; |
16
|
|
|
use Spiral\Core\FactoryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Provides ability to delegate option to container. |
20
|
|
|
*/ |
21
|
|
|
final class Autowire |
22
|
|
|
{ |
23
|
|
|
/** @var object|null */ |
24
|
|
|
private $target; |
25
|
|
|
|
26
|
|
|
/** @var mixed */ |
27
|
|
|
private $alias; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $parameters; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Autowire constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $alias |
36
|
|
|
* @param array $parameters |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $alias, array $parameters = []) |
39
|
|
|
{ |
40
|
|
|
$this->alias = $alias; |
41
|
|
|
$this->parameters = $parameters; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $an_array |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
|
|
public static function __set_state($an_array) |
49
|
|
|
{ |
50
|
|
|
return new static($an_array['alias'], $an_array['parameters']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Init the autowire based on string or array definition. |
55
|
|
|
* |
56
|
|
|
* @param mixed $definition |
57
|
|
|
* @return Autowire |
58
|
|
|
*/ |
59
|
|
|
public static function wire($definition): Autowire |
60
|
|
|
{ |
61
|
|
|
if ($definition instanceof self) { |
62
|
|
|
return $definition; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (is_string($definition)) { |
66
|
|
|
return new Autowire($definition); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (is_array($definition) && isset($definition['class'])) { |
70
|
|
|
return new Autowire( |
71
|
|
|
$definition['class'], |
72
|
|
|
$definition['options'] ?? $definition['params'] ?? [] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_object($definition)) { |
77
|
|
|
$autowire = new self(get_class($definition), []); |
78
|
|
|
$autowire->target = $definition; |
79
|
|
|
return $autowire; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new AutowireException('Invalid autowire definition'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param FactoryInterface $factory |
87
|
|
|
* @param array $parameters Context specific parameters (always prior to declared ones). |
88
|
|
|
* @return mixed |
89
|
|
|
* |
90
|
|
|
* @throws AutowireException No entry was found for this identifier. |
91
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
92
|
|
|
*/ |
93
|
|
|
public function resolve(FactoryInterface $factory, array $parameters = []) |
94
|
|
|
{ |
95
|
|
|
if ($this->target !== null) { |
96
|
|
|
// pre-wired |
97
|
|
|
return $this->target; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $factory->make($this->alias, array_merge($this->parameters, $parameters)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|