1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mosaic\Filesystem\Adapters\Flysystem; |
4
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use Dropbox\Client; |
8
|
|
|
use Interop\Container\Definition\DefinitionProviderInterface; |
9
|
|
|
use League\Flysystem\Adapter\Ftp; |
10
|
|
|
use League\Flysystem\Adapter\Local; |
11
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter; |
12
|
|
|
use League\Flysystem\Dropbox\DropboxAdapter; |
13
|
|
|
use Mosaic\Common\Conventions\FolderStructureConvention; |
14
|
|
|
use Mosaic\Filesystem\Providers\FlystemProvider; |
15
|
|
|
|
16
|
|
|
final class Component implements \Mosaic\Common\Components\Component |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var FolderStructureConvention |
20
|
|
|
*/ |
21
|
|
|
private $folderStructure; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var DiskResolverCollection |
25
|
|
|
*/ |
26
|
|
|
private $diskResolvers; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $custom = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param FolderStructureConvention $folderStructure |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct(FolderStructureConvention $folderStructure) |
37
|
|
|
{ |
38
|
10 |
|
$this->diskResolvers = new DiskResolverCollection; |
39
|
10 |
|
$this->folderStructure = $folderStructure; |
40
|
|
|
|
41
|
10 |
|
$this->local('local', $this->folderStructure->storagePath()); |
42
|
10 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return DefinitionProviderInterface[] |
46
|
|
|
*/ |
47
|
1 |
|
public function getProviders() : array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
1 |
|
new FlystemProvider($this->getDiskResolvers()) |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* @param callable $resolver |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
10 |
|
public function disk(string $name, callable $resolver) |
60
|
|
|
{ |
61
|
10 |
|
$this->diskResolvers->add($name, $resolver); |
62
|
|
|
|
63
|
10 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $name |
68
|
|
|
* @param string $path |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
10 |
|
public function local(string $name, string $path) |
72
|
|
|
{ |
73
|
|
|
$this->disk($name, function () use ($path) { |
74
|
2 |
|
return new Local($path); |
75
|
10 |
|
}); |
76
|
|
|
|
77
|
10 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $settings |
82
|
|
|
* @param string $name |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
1 |
|
public function ftp(array $settings, string $name = 'ftp') |
86
|
|
|
{ |
87
|
|
|
$this->disk($name, function () use ($settings) { |
88
|
|
|
return new Ftp($settings); |
89
|
1 |
|
}); |
90
|
|
|
|
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $bucket |
96
|
|
|
* @param array $settings |
97
|
|
|
* @param string $name |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
1 |
|
public function aws(string $bucket, array $settings, string $name = 'aws') |
101
|
|
|
{ |
102
|
|
|
$this->disk($name, function () use ($bucket, $settings) { |
103
|
|
|
|
104
|
|
|
$client = S3Client::factory($settings); |
105
|
|
|
|
106
|
|
|
return new AwsS3Adapter($client, $bucket); |
107
|
1 |
|
}); |
108
|
|
|
|
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $accessToken |
114
|
|
|
* @param string $appSecret |
115
|
|
|
* @param string $name |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function dropbox(string $accessToken, string $appSecret, string $name = 'dropbox') |
119
|
|
|
{ |
120
|
1 |
|
$this->disk($name, function () use ($accessToken, $appSecret) { |
121
|
|
|
return new DropboxAdapter( |
122
|
|
|
new Client($accessToken, $appSecret) |
123
|
|
|
); |
124
|
1 |
|
}); |
125
|
|
|
|
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* @param callable $callback |
132
|
|
|
*/ |
133
|
1 |
|
public static function extend(string $name, callable $callback) |
134
|
|
|
{ |
135
|
1 |
|
self::$custom[$name] = $callback; |
136
|
1 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return DiskResolverCollection |
140
|
|
|
*/ |
141
|
8 |
|
public function getDiskResolvers() |
142
|
|
|
{ |
143
|
8 |
|
return $this->diskResolvers; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $name |
148
|
|
|
* @param array $params |
149
|
|
|
* @return Component |
150
|
|
|
*/ |
151
|
2 |
|
public function __call(string $name, array $params = []) |
152
|
|
|
{ |
153
|
2 |
|
if (isset(self::$custom[$name])) { |
154
|
1 |
|
return $this->disk($name, self::$custom[$name]); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
throw new BadMethodCallException; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|