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 League\Flysystem\AdapterInterface; |
20
|
|
|
use Phauthentic\Infrastructure\Storage\Exception\AdapterFactoryNotFoundException; |
21
|
|
|
use Psr\Container\ContainerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* StorageFactory - Manages and instantiates storage engine adapters. |
25
|
|
|
*/ |
26
|
|
|
class StorageAdapterFactory implements StorageAdapterFactoryInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \Psr\Container\ContainerInterface |
30
|
|
|
*/ |
31
|
|
|
protected ?ContainerInterface $container; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \Psr\Container\ContainerInterface|null $container Container |
35
|
|
|
*/ |
36
|
2 |
|
public function __construct( |
37
|
|
|
?ContainerInterface $container = null |
38
|
|
|
) { |
39
|
2 |
|
$this->container = $container; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Instantiates Flystem adapters. |
44
|
|
|
* |
45
|
|
|
* @param string $adapterClass Adapter alias or classname |
46
|
|
|
* @param array $options Options |
47
|
|
|
* @return \League\Flysystem\AdapterInterface |
48
|
|
|
*/ |
49
|
2 |
|
public function buildStorageAdapter( |
50
|
|
|
string $adapterClass, |
51
|
|
|
array $options |
52
|
|
|
): AdapterInterface { |
53
|
2 |
|
$adapterClass = $this->checkAndResolveAdapterClass($adapterClass); |
54
|
|
|
|
55
|
1 |
|
if ($this->container !== null) { |
56
|
|
|
return $this->container->get($adapterClass)->build($options); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return (new $adapterClass())->build($options); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $adapterClass Adapter Class name or string |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
2 |
|
protected function checkAndResolveAdapterClass(string $adapterClass): string |
67
|
|
|
{ |
68
|
2 |
|
if (!class_exists($adapterClass)) { |
69
|
2 |
|
$adapterClass = '\Phauthentic\Infrastructure\Storage\Factories\\' . $adapterClass . 'Factory'; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
if (!class_exists($adapterClass)) { |
73
|
1 |
|
throw AdapterFactoryNotFoundException::fromName($adapterClass); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return $adapterClass; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|