1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Container; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Dependency injection instance provider. |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
class Provider |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var boolean |
33
|
|
|
*/ |
34
|
|
|
private $singleton = true; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $type; |
39
|
|
|
/** |
40
|
|
|
* @var mixed |
41
|
|
|
*/ |
42
|
|
|
private $instance; |
43
|
|
|
/** |
44
|
|
|
* @var callable |
45
|
|
|
*/ |
46
|
|
|
private $factory; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @internal |
50
|
|
|
*/ |
51
|
9 |
|
public function __construct(string $type, $factory, bool $singleton = true) |
52
|
|
|
{ |
53
|
9 |
|
if (!class_exists($type) && !interface_exists($type)) { |
54
|
1 |
|
throw new \InvalidArgumentException("Unknown class or interface: '$type'"); |
55
|
|
|
} |
56
|
8 |
|
$this->type = $type; |
57
|
8 |
|
if (!is_object($factory) || !method_exists($factory, '__invoke')) { |
58
|
2 |
|
throw new \InvalidArgumentException('"factory" parameter must be a Closure or an object with an __invoke method'); |
59
|
|
|
} |
60
|
6 |
|
$this->factory = $factory; |
61
|
6 |
|
$this->singleton = boolval($singleton); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets the value instance. |
66
|
|
|
* |
67
|
|
|
* @param \Caridea\Container\Container $container The owning container |
68
|
|
|
* @return mixed The value instance |
69
|
|
|
*/ |
70
|
5 |
|
public function get(Container $container) |
71
|
|
|
{ |
72
|
5 |
|
if ($this->singleton) { |
73
|
4 |
|
if ($this->instance === null) { |
74
|
4 |
|
$f = $this->factory; |
75
|
4 |
|
$this->instance = $f($container); |
76
|
|
|
} |
77
|
4 |
|
return $this->instance; |
78
|
|
|
} else { |
79
|
1 |
|
$f = $this->factory; |
80
|
1 |
|
return $f($container); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Whether this provider always returns the same instance. |
86
|
|
|
* |
87
|
|
|
* @return bool True if this provider always returns the same instance |
88
|
|
|
*/ |
89
|
2 |
|
public function isSingleton(): bool |
90
|
|
|
{ |
91
|
2 |
|
return $this->singleton; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the type of instance. |
96
|
|
|
* |
97
|
|
|
* @return string gets the type of this instance |
98
|
|
|
*/ |
99
|
6 |
|
public function getType(): string |
100
|
|
|
{ |
101
|
6 |
|
return $this->type; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|