|
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 |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
?ContainerInterface $container = null |
|
38
|
|
|
) { |
|
39
|
|
|
$this->container = $container; |
|
40
|
|
|
} |
|
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
|
|
|
public function buildStorageAdapter( |
|
50
|
|
|
string $adapterClass, |
|
51
|
|
|
array $options |
|
52
|
|
|
): AdapterInterface { |
|
53
|
|
|
if (!class_exists($adapterClass)) { |
|
54
|
|
|
$adapterClass = '\Phauthentic\Infrastructure\Storage\Factories\\' . $adapterClass . 'Factory'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (!class_exists($adapterClass)) { |
|
58
|
|
|
throw AdapterFactoryNotFoundException::fromName($adapterClass); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($this->container !== null) { |
|
62
|
|
|
return $this->container->get($adapterClass)->build($options); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return (new $adapterClass())->build($options); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|