|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Implementation of this file has been influenced by AuraPHP |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/auraphp/Aura.Di |
|
14
|
|
|
* @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phalcon\Container; |
|
20
|
|
|
|
|
21
|
|
|
use Phalcon\Container; |
|
22
|
|
|
use ReflectionException; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Resolves object specifications using the DI container. |
|
26
|
|
|
* |
|
27
|
|
|
* @property Container $container |
|
28
|
|
|
*/ |
|
29
|
|
|
class ResolutionHelper |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* The DI container. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Container |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Container $container The DI container. |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function __construct(Container $container) |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->container = $container; |
|
46
|
5 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Resolves an object specification. |
|
50
|
|
|
* |
|
51
|
|
|
* @param mixed $spec The object specification. |
|
52
|
|
|
* |
|
53
|
|
|
* @return array|object |
|
54
|
|
|
* @throws Exception\ServiceNotFound |
|
55
|
|
|
* @throws ReflectionException |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function __invoke($spec) |
|
58
|
|
|
{ |
|
59
|
4 |
|
if (is_string($spec)) { |
|
60
|
1 |
|
return $this->resolve($spec); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
if (is_array($spec) && is_string($spec[0])) { |
|
64
|
2 |
|
$spec[0] = $this->resolve($spec[0]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
return $spec; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get a named service or a new instance from the Container |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $spec the name of the service or class to instantiate |
|
74
|
|
|
* |
|
75
|
|
|
* @return object |
|
76
|
|
|
* @throws Exception\ServiceNotFound |
|
77
|
|
|
* @throws ReflectionException |
|
78
|
|
|
*/ |
|
79
|
3 |
|
protected function resolve($spec) |
|
80
|
|
|
{ |
|
81
|
3 |
|
if ($this->container->has($spec)) { |
|
82
|
2 |
|
return $this->container->get($spec); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
return $this->container->newInstance($spec); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|