1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace WShafer\PSR11FlySystem; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use WShafer\PSR11FlySystem\Adaptor\AdaptorMapper; |
8
|
|
|
use WShafer\PSR11FlySystem\Cache\CacheMapper; |
9
|
|
|
use WShafer\PSR11FlySystem\Config\MainConfig; |
10
|
|
|
use WShafer\PSR11FlySystem\Service\AdaptorManager; |
11
|
|
|
use WShafer\PSR11FlySystem\Service\CacheManager; |
12
|
|
|
|
13
|
|
|
class FlySystemManagerFactory |
14
|
|
|
{ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
1 |
|
public function __invoke(ContainerInterface $container) |
18
|
|
|
{ |
19
|
|
|
/** @var MainConfig $config */ |
20
|
1 |
|
$config = $this->getConfig($container); |
21
|
|
|
|
22
|
|
|
/** @var AdaptorManager $adaptorManager */ |
23
|
1 |
|
$adaptorManager = $this->getAdaptorManager($container); |
24
|
|
|
|
25
|
|
|
/** @var CacheManager $cacheManager */ |
26
|
1 |
|
$cacheManager = $this->getCacheManager($container); |
27
|
|
|
|
28
|
1 |
|
return new FlySystemManager($config, $adaptorManager, $cacheManager, $container); |
29
|
|
|
} |
30
|
|
|
|
31
|
7 |
|
public function getConfig(ContainerInterface $container) |
32
|
|
|
{ |
33
|
7 |
|
if (!$this->config) { |
34
|
7 |
|
$config = $this->getConfigArray($container); |
35
|
7 |
|
$this->config = new MainConfig($config); |
36
|
|
|
} |
37
|
|
|
|
38
|
6 |
|
return $this->config; |
39
|
|
|
} |
40
|
|
|
|
41
|
7 |
|
protected function getConfigArray(ContainerInterface $container) |
42
|
|
|
{ |
43
|
|
|
// Symfony config is parameters. // |
44
|
7 |
|
if (method_exists($container, 'getParameter') |
45
|
7 |
|
&& method_exists($container, 'hasParameter') |
46
|
7 |
|
&& $container->hasParameter('flysystem') |
|
|
|
|
47
|
|
|
) { |
48
|
1 |
|
return ['flysystem' => $container->getParameter('flysystem')]; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Zend uses config key |
52
|
6 |
|
if ($container->has('config')) { |
53
|
4 |
|
return $container->get('config'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Slim Config comes from "settings" |
57
|
2 |
|
if ($container->has('settings')) { |
58
|
1 |
|
return ['flysystem' => $container->get('settings')['flysystem']]; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
public function getAdaptorManager(ContainerInterface $container) |
65
|
|
|
{ |
66
|
|
|
/** @var MainConfig $config */ |
67
|
2 |
|
$config = $this->getConfig($container); |
68
|
|
|
|
69
|
|
|
/** @var AdaptorMapper $adaptorMapper */ |
70
|
2 |
|
$adaptorMapper = $this->getAdaptorMapper($container); |
71
|
|
|
|
72
|
2 |
|
return new AdaptorManager($config, $adaptorMapper, $container); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
public function getAdaptorMapper(ContainerInterface $container) |
76
|
|
|
{ |
77
|
3 |
|
return new AdaptorMapper($container); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function getCacheManager(ContainerInterface $container) |
81
|
|
|
{ |
82
|
|
|
/** @var MainConfig $config */ |
83
|
2 |
|
$config = $this->getConfig($container); |
84
|
|
|
|
85
|
|
|
/** @var CacheMapper $cacheMapper */ |
86
|
2 |
|
$cacheMapper = $this->getCacheMapper($container); |
87
|
|
|
|
88
|
2 |
|
return new CacheManager($config, $cacheMapper, $container); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
public function getCacheMapper(ContainerInterface $container) |
92
|
|
|
{ |
93
|
3 |
|
return new CacheMapper($container); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: