|
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\Injection; |
|
20
|
|
|
|
|
21
|
|
|
use Phalcon\Container\Exception\NoSuchProperty; |
|
22
|
|
|
use Phalcon\Container\Resolver\Blueprint; |
|
23
|
|
|
use Phalcon\Container\Resolver\Resolver; |
|
24
|
|
|
use ReflectionException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A generic factory to create repeated instances of a single class. Note that |
|
28
|
|
|
* it does not implement the LazyInterface, so it is not automatically invoked |
|
29
|
|
|
* when resolving params and setters. |
|
30
|
|
|
* |
|
31
|
|
|
* @property Blueprint $blueprint |
|
32
|
|
|
* @property array|Blueprint[] $contextualBlueprints |
|
33
|
|
|
* @property Resolver $resolver |
|
34
|
|
|
*/ |
|
35
|
|
|
class Factory |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Override params for the class. |
|
39
|
|
|
* |
|
40
|
|
|
* @var Blueprint |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $blueprint; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Blueprints that are only used within the context of this factory. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array|Blueprint[] |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $contextualBlueprints = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The Resolver. |
|
53
|
|
|
* |
|
54
|
|
|
* @var Resolver |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $resolver; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* Constructor. |
|
61
|
|
|
* |
|
62
|
|
|
* @param Resolver $resolver A Resolver to provide class-creation |
|
63
|
|
|
* specifics. |
|
64
|
|
|
* |
|
65
|
|
|
* @param Blueprint $blueprint |
|
66
|
|
|
*/ |
|
67
|
17 |
|
public function __construct( |
|
68
|
|
|
Resolver $resolver, |
|
69
|
|
|
Blueprint $blueprint |
|
70
|
|
|
) { |
|
71
|
17 |
|
$this->resolver = $resolver; |
|
72
|
17 |
|
$this->blueprint = $blueprint; |
|
73
|
17 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Invoke the Factory object as a function to use the Factory to create |
|
77
|
|
|
* a new instance of the specified class; pass sequential parameters as |
|
78
|
|
|
* as yet another set of constructor parameter overrides. |
|
79
|
|
|
* |
|
80
|
|
|
* Why the overrides for the overrides? So that any package that needs a |
|
81
|
|
|
* factory can make its own, using sequential params in a function; then |
|
82
|
|
|
* the factory call can be replaced by a call to this Factory. |
|
83
|
|
|
* |
|
84
|
|
|
* @param mixed ...$params |
|
85
|
|
|
* |
|
86
|
|
|
* @return object |
|
87
|
|
|
* @throws ReflectionException |
|
88
|
|
|
* @throws NoSuchProperty |
|
89
|
|
|
*/ |
|
90
|
16 |
|
public function __invoke(...$params): object |
|
91
|
|
|
{ |
|
92
|
16 |
|
return $this->resolver->resolve( |
|
93
|
16 |
|
$this->blueprint->withParams($params), |
|
94
|
16 |
|
$this->contextualBlueprints |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param Blueprint $blueprint |
|
100
|
|
|
* |
|
101
|
|
|
* @return Factory |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function withContext(Blueprint $blueprint): Factory |
|
104
|
|
|
{ |
|
105
|
1 |
|
$clone = clone $this; |
|
106
|
|
|
|
|
107
|
1 |
|
$clone->contextualBlueprints[] = $blueprint; |
|
108
|
|
|
|
|
109
|
1 |
|
return $clone; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|