1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Composite Utils package. |
4
|
|
|
* |
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the |
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @package spaark/composite-utils |
11
|
|
|
* @author Emily Shepherd <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Service; |
16
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite; |
18
|
|
|
use Spaark\CompositeUtils\Factory\Reflection\ReflectionCompositeFactory; |
19
|
|
|
use Spaark\CompositeUtils\Model\Collection\Map\HashMap; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
*/ |
23
|
|
|
class ReflectionCompositeProvider |
24
|
|
|
implements ReflectionCompositeProviderInterface |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ReflectionCompositeProviderInterface |
28
|
|
|
*/ |
29
|
|
|
protected static $default; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the default ReflectionCompositeProviderInteface used by |
33
|
|
|
* the application |
34
|
|
|
* |
35
|
|
|
* If one has not been set, a new ReflectionCompositeProvider is |
36
|
|
|
* instanciated on the fly |
37
|
|
|
* |
38
|
|
|
* @return ReflectionCompositeProviderInterface |
39
|
|
|
*/ |
40
|
23 |
|
public static function getDefault() |
41
|
|
|
: ReflectionCompositeProviderInterface |
42
|
|
|
{ |
43
|
23 |
|
if (!static::$default) |
44
|
|
|
{ |
45
|
1 |
|
static::$default = new static(); |
46
|
|
|
} |
47
|
|
|
|
48
|
23 |
|
return static::$default; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the default ReflectionCompositeProviderInterface used by |
53
|
|
|
* the applciation |
54
|
|
|
* |
55
|
|
|
* @param ReflectionCompositeProviderInterface $default |
56
|
|
|
*/ |
57
|
1 |
|
public static function setDefault |
58
|
|
|
( |
59
|
|
|
ReflectionCompositeProviderInterface $default |
60
|
|
|
) |
61
|
|
|
{ |
62
|
1 |
|
static::$default = $default; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Cache used by this provider |
67
|
|
|
* |
68
|
|
|
* @var HashMap |
69
|
|
|
*/ |
70
|
|
|
private $cache; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Builds the provider by instanciating its cache |
74
|
|
|
*/ |
75
|
2 |
|
public function __construct() |
76
|
|
|
{ |
77
|
2 |
|
$this->cache = new HashMap(); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets a ReflectionComposite, either by using a cached version of |
82
|
|
|
* it or building it on the fly |
83
|
|
|
* |
84
|
|
|
* @param string $classname |
85
|
|
|
* @return ReflectionComposite |
86
|
|
|
*/ |
87
|
2 |
|
public function get(string $classname) : ReflectionComposite |
88
|
|
|
{ |
89
|
2 |
|
if (!$this->cache->containsKey($classname)) |
|
|
|
|
90
|
|
|
{ |
91
|
2 |
|
$this->cache[$classname] = |
92
|
|
|
( |
93
|
2 |
|
ReflectionCompositeFactory::fromClassName |
94
|
|
|
( |
95
|
2 |
|
$classname |
96
|
|
|
) |
97
|
|
|
) |
98
|
2 |
|
->build(); |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return $this->cache[$classname]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|