|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PHPassword\Locator\Proxy; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use PHPassword\Locator\Facade\EmptyFacade; |
|
7
|
|
|
use PHPassword\Locator\Facade\FacadeInterface; |
|
8
|
|
|
use PHPassword\Locator\Factory\EmptyFactory; |
|
9
|
|
|
use PHPassword\Locator\Factory\FactoryInterface; |
|
10
|
|
|
use PHPassword\Locator\Locator; |
|
11
|
|
|
|
|
12
|
|
|
class LocatorProxy implements LocatorProxyInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $classNameSchema = '%1$s\\%1$s%2$s'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \ArrayObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $searchableNamespaces; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $locateName; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Locator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $locator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var FactoryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $factory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var FacadeInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $facade; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* LocatorProxy constructor. |
|
46
|
|
|
* @param \ArrayObject $searchableNamespaces |
|
47
|
|
|
* @param string $locateName |
|
48
|
|
|
* @param Locator $locator |
|
49
|
|
|
*/ |
|
50
|
11 |
|
public function __construct(\ArrayObject $searchableNamespaces, string $locateName, Locator $locator) |
|
51
|
|
|
{ |
|
52
|
11 |
|
$this->searchableNamespaces = $searchableNamespaces; |
|
53
|
11 |
|
$this->locateName = $locateName; |
|
54
|
11 |
|
$this->locator = $locator; |
|
55
|
11 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return FactoryInterface |
|
59
|
|
|
*/ |
|
60
|
8 |
|
public function factory(): FactoryInterface |
|
61
|
|
|
{ |
|
62
|
8 |
|
if($this->factory === null) { |
|
63
|
|
|
try { |
|
64
|
8 |
|
$className = $this->searchInNamespaces('Factory', FactoryInterface::class); |
|
65
|
5 |
|
} catch (NotFoundException $e) { |
|
66
|
5 |
|
$className = EmptyFactory::class; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
8 |
|
$this->factory = new $className; |
|
70
|
8 |
|
$this->factory->setLocator($this->locator); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
return $this->factory; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return FacadeInterface |
|
78
|
|
|
*/ |
|
79
|
6 |
|
public function facade(): FacadeInterface |
|
80
|
|
|
{ |
|
81
|
6 |
|
if($this->facade === null) { |
|
82
|
|
|
try { |
|
83
|
6 |
|
$className = $this->searchInNamespaces('Facade', FacadeInterface::class); |
|
84
|
5 |
|
} catch (NotFoundException $e) { |
|
85
|
5 |
|
$className = EmptyFacade::class; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
6 |
|
$this->facade = new $className; |
|
89
|
6 |
|
$this->facade->setLocator($this->locator); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
return $this->facade; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $expectedClassBaseName |
|
97
|
|
|
* @param string $expectedInterface |
|
98
|
|
|
* @return string |
|
99
|
|
|
* @throws NotFoundException |
|
100
|
|
|
*/ |
|
101
|
11 |
|
private function searchInNamespaces(string $expectedClassBaseName, string $expectedInterface) |
|
102
|
|
|
{ |
|
103
|
11 |
|
foreach($this->searchableNamespaces as $namespace){ |
|
104
|
9 |
|
$expectedClassName = $namespace . sprintf(self::$classNameSchema, $this->locateName, $expectedClassBaseName); |
|
105
|
9 |
|
if(class_exists($expectedClassName) === true |
|
106
|
9 |
|
&& in_array($expectedInterface, class_implements($expectedClassName))){ |
|
107
|
9 |
|
return $expectedClassName; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
7 |
|
throw new NotFoundException(sprintf('Class %s not found in module %s', $expectedClassBaseName, $this->locateName)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |