1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
10
|
|
|
* @author Florian Krämer |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phauthentic\Infrastructure\Storage; |
18
|
|
|
|
19
|
|
|
use Iterator; |
20
|
|
|
use League\Flysystem\AdapterInterface; |
21
|
|
|
use ArrayIterator; |
22
|
|
|
use RuntimeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Adapter Collection |
26
|
|
|
*/ |
27
|
|
|
class AdapterCollection implements AdapterCollectionInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected array $adapters = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
3 |
|
*/ |
37
|
|
|
public function __construct() |
38
|
3 |
|
{ |
39
|
3 |
|
$this->adapters = []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $name Name |
44
|
|
|
* @param \League\Flysystem\AdapterInterface $adapter Adapter |
45
|
3 |
|
* @return void |
46
|
|
|
*/ |
47
|
3 |
|
public function add($name, AdapterInterface $adapter) |
48
|
|
|
{ |
49
|
|
|
if ($this->has($name)) { |
50
|
|
|
throw new RuntimeException(sprintf( |
51
|
|
|
'An adapter with the name `%s` already exists in the collection', |
52
|
|
|
$name |
53
|
|
|
)); |
54
|
3 |
|
} |
55
|
3 |
|
|
56
|
|
|
$this->adapters[$name] = $adapter; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name Name |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function remove(string $name): void |
64
|
|
|
{ |
65
|
|
|
unset($this->adapters[$name]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $name Name |
70
|
3 |
|
* @return bool |
71
|
|
|
*/ |
72
|
3 |
|
public function has(string $name): bool |
73
|
|
|
{ |
74
|
|
|
return isset($this->adapters[$name]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name Name |
79
|
2 |
|
* @return \League\Flysystem\AdapterInterface |
80
|
|
|
*/ |
81
|
2 |
|
public function get(string $name): AdapterInterface |
82
|
|
|
{ |
83
|
|
|
if (!$this->has($name)) { |
84
|
|
|
throw new RuntimeException(sprintf( |
85
|
|
|
'A factory registered with the name `%s` is not part of the collection.', |
86
|
|
|
$name |
87
|
|
|
)); |
88
|
2 |
|
} |
89
|
|
|
|
90
|
|
|
return $this->adapters[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Empties the collection |
95
|
|
|
* |
96
|
1 |
|
* @return void |
97
|
|
|
*/ |
98
|
1 |
|
public function empty(): void |
99
|
1 |
|
{ |
100
|
|
|
unset($this->adapters); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
1 |
|
* @return array |
105
|
|
|
*/ |
106
|
1 |
|
public function getNameToClassmap(): array |
107
|
1 |
|
{ |
108
|
|
|
if (empty($this->adapters)) { |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$map = []; |
113
|
|
|
foreach ($this->adapters as $name => $object) { |
114
|
|
|
$map[$name] = get_class($object); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $map; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
1 |
|
* @inheritDoc |
122
|
|
|
*/ |
123
|
1 |
|
public function getIterator(): Iterator |
124
|
|
|
{ |
125
|
|
|
return new ArrayIterator($this->adapters); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|