1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WShafer\PSR11FlySystem; |
5
|
|
|
|
6
|
|
|
use League\Flysystem\Filesystem; |
7
|
|
|
use League\Flysystem\MountManager; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use WShafer\PSR11FlySystem\Exception\InvalidContainerException; |
10
|
|
|
|
11
|
|
|
class FlySystemFactory |
12
|
|
|
{ |
13
|
|
|
/** @var string */ |
14
|
|
|
protected $fileSystemName = 'default'; |
15
|
|
|
|
16
|
|
|
/** @var FlySystemManager */ |
17
|
|
|
protected static $flySystemManager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ContainerInterface $container |
21
|
|
|
* |
22
|
|
|
* @return Filesystem|MountManager |
23
|
|
|
*/ |
24
|
3 |
|
public function __invoke(ContainerInterface $container) |
25
|
|
|
{ |
26
|
3 |
|
$manager = static::getFlySystemManager($container); |
27
|
3 |
|
$fileSystemName = $this->getFileSystemName(); |
28
|
3 |
|
return $manager->get($fileSystemName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Magic method for constructing FileSystems by service name |
33
|
|
|
* |
34
|
|
|
* @param $name |
35
|
|
|
* @param $arguments |
36
|
|
|
* |
37
|
|
|
* @return Filesystem|MountManager |
38
|
|
|
*/ |
39
|
2 |
|
public static function __callStatic($name, $arguments) |
40
|
|
|
{ |
41
|
2 |
|
if (empty($arguments[0]) |
42
|
2 |
|
|| !$arguments[0] instanceof ContainerInterface |
43
|
|
|
) { |
44
|
1 |
|
throw new InvalidContainerException( |
45
|
1 |
|
'Argument 0 must be an instance of a PSR-11 container' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$factory = new static(); |
50
|
1 |
|
$factory->setFileSystemName($name); |
51
|
1 |
|
return $factory($arguments[0]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
4 |
|
public function getFileSystemName(): string |
58
|
|
|
{ |
59
|
4 |
|
return $this->fileSystemName; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $fileSystemName |
64
|
|
|
*/ |
65
|
3 |
|
public function setFileSystemName(string $fileSystemName) |
66
|
|
|
{ |
67
|
3 |
|
$this->fileSystemName = $fileSystemName; |
68
|
3 |
|
} |
69
|
|
|
|
70
|
6 |
|
public static function getFlySystemManager(ContainerInterface $container) : FlySystemManager |
71
|
|
|
{ |
72
|
|
|
// @codeCoverageIgnoreStart |
73
|
|
|
if (!static::$flySystemManager) { |
74
|
|
|
$factory = new FlySystemManagerFactory(); |
75
|
|
|
static::setFlySystemManager($factory($container)); |
76
|
|
|
} |
77
|
|
|
// @codeCoverageIgnoreEnd |
78
|
|
|
|
79
|
6 |
|
return static::$flySystemManager; |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
public static function setFlySystemManager(FlySystemManager $flySystemManager) |
83
|
|
|
{ |
84
|
6 |
|
static::$flySystemManager = $flySystemManager; |
85
|
6 |
|
} |
86
|
|
|
} |
87
|
|
|
|