1 | <?php |
||
21 | class Auth implements AdapterAwareInterface |
||
22 | { |
||
23 | /** |
||
24 | * Adapter |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $adapter = []; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * Identity |
||
33 | * |
||
34 | * @var Identity |
||
35 | */ |
||
36 | protected $identity; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Logger |
||
41 | * |
||
42 | * @var Logger |
||
43 | */ |
||
44 | protected $logger; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * Identity class |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $identity_class = Identity::class; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * Attribute map class |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $attribute_map_class = AttributeMap::class; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Initialize |
||
65 | * |
||
66 | * @param LoggerInterface $logger |
||
67 | * @param Iterable $config |
||
68 | * @return void |
||
69 | */ |
||
70 | public function __construct(LoggerInterface $logger, ? Iterable $config = null) |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Set options |
||
79 | * |
||
80 | * @param Iterable $config |
||
81 | * @return Auth |
||
82 | */ |
||
83 | public function setOptions(? Iterable $config = null) : Auth |
||
107 | |||
108 | |||
109 | /** |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | public function hasAdapter(string $name): bool |
||
113 | { |
||
114 | return isset($this->adapter[$name]); |
||
115 | } |
||
116 | |||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | */ |
||
121 | public function getDefaultAdapter(): array |
||
122 | { |
||
123 | return []; |
||
124 | } |
||
125 | |||
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | public function injectAdapter($adapter, ?string $name=null) : AdapterAwareInterface |
||
131 | { |
||
132 | if(!($adapter instanceof AdapterInterface)) { |
||
133 | throw new Exception('adapter needs to implement AdapterInterface'); |
||
134 | } |
||
135 | |||
136 | if($name === null) { |
||
137 | $name = get_class($adapter); |
||
138 | } |
||
139 | |||
140 | $this->logger->debug('inject auth adapter ['.$name.'] of type ['.get_class($adapter).']', [ |
||
141 | 'category' => get_class($this) |
||
142 | ]); |
||
143 | |||
144 | if ($this->hasAdapter($name)) { |
||
145 | throw new Exception('auth adapter '.$name.' is already registered'); |
||
146 | } |
||
147 | |||
148 | $this->adapter[$name] = $adapter; |
||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | |||
153 | /** |
||
154 | * {@inheritDoc} |
||
155 | */ |
||
156 | public function getAdapter(string $name) |
||
164 | |||
165 | |||
166 | /** |
||
167 | * {@inheritDoc} |
||
168 | */ |
||
169 | public function getAdapters(array $adapters = []): array |
||
185 | |||
186 | |||
187 | /** |
||
188 | * Create identity |
||
189 | * |
||
190 | * @param AdapterInterface $adapter |
||
191 | * @return Identity |
||
192 | */ |
||
193 | protected function createIdentity(AdapterInterface $adapter): Identity |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Authenticate |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function requireOne(): bool |
||
240 | |||
241 | |||
242 | /** |
||
243 | * Get identity |
||
244 | * |
||
245 | * @return Identity |
||
246 | */ |
||
247 | public function getIdentity(): Identity |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Check if valid identity exists |
||
259 | * |
||
260 | * @return bool |
||
261 | */ |
||
262 | public function isAuthenticated(): bool |
||
266 | } |
||
267 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: