1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace tkanstantsin\fileupload; |
||
5 | |||
6 | use League\Flysystem\Filesystem; |
||
7 | use tkanstantsin\fileupload\config\Factory as AliasFactory; |
||
8 | use tkanstantsin\fileupload\config\InvalidConfigException; |
||
9 | use tkanstantsin\fileupload\formatter\Factory as FormatterFactory; |
||
10 | use tkanstantsin\fileupload\formatter\File as FileFormatter; |
||
11 | use tkanstantsin\fileupload\formatter\icon\IconGenerator; |
||
12 | use tkanstantsin\fileupload\model\BaseObject; |
||
13 | use tkanstantsin\fileupload\model\ICacheStateful; |
||
14 | use tkanstantsin\fileupload\model\IFile; |
||
15 | use tkanstantsin\fileupload\saver\Saver; |
||
16 | |||
17 | /** |
||
18 | * Class FileComponent |
||
19 | */ |
||
20 | class FileManager extends BaseObject |
||
21 | { |
||
22 | /** |
||
23 | * For uploading |
||
24 | * @var Filesystem |
||
25 | */ |
||
26 | public $contentFS; |
||
27 | /** |
||
28 | * Web accessible FS |
||
29 | * @var Filesystem |
||
30 | */ |
||
31 | public $cacheFS; |
||
32 | |||
33 | /** |
||
34 | * @see config\Alias |
||
35 | * @var array |
||
36 | */ |
||
37 | public $aliasArray; |
||
38 | /** |
||
39 | * @see config\Alias |
||
40 | * @var array |
||
41 | */ |
||
42 | public $defaultAlias = [ |
||
43 | 'maxSize' => config\Alias::DEFAULT_MAX_SIZE, |
||
44 | 'maxCount' => config\Alias::DEFAULT_MAX_COUNT, |
||
45 | 'hashMethod' => config\Alias::DEFAULT_HASH_METHOD, |
||
46 | 'cacheHashLength' => config\Alias::DEFAULT_HASH_LENGTH, |
||
47 | ]; |
||
48 | /** |
||
49 | * @var AliasFactory |
||
50 | */ |
||
51 | private $aliasFactory; |
||
52 | |||
53 | /** |
||
54 | * Icon set class for icons |
||
55 | * @see formatter\icon\FontAwesome |
||
56 | * @see formatter\icon\ElusiveIcons |
||
57 | * @var string|null |
||
58 | */ |
||
59 | public $iconSet; |
||
60 | /** |
||
61 | * @var IconGenerator |
||
62 | */ |
||
63 | private $iconGenerator; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | public $formatterConfigArray = []; |
||
69 | /** |
||
70 | * @var FormatterFactory |
||
71 | */ |
||
72 | private $formatterFactory; |
||
73 | |||
74 | /** |
||
75 | * Check initialization parameters and parse configs |
||
76 | * @throws config\InvalidConfigException |
||
77 | */ |
||
78 | 1 | public function init(): void |
|
79 | { |
||
80 | 1 | parent::init(); |
|
81 | |||
82 | 1 | if (!($this->contentFS instanceof Filesystem)) { |
|
83 | throw new InvalidConfigException(sprintf('ContentFS must be instance of %s.', Filesystem::class)); |
||
84 | } |
||
85 | 1 | if (!($this->cacheFS instanceof Filesystem)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
86 | throw new InvalidConfigException(sprintf('CacheFS must be instance of %s.', Filesystem::class)); |
||
87 | } |
||
88 | |||
89 | 1 | $this->iconGenerator = IconGenerator::build($this->iconSet); |
|
90 | 1 | $this->formatterFactory = new FormatterFactory((array) $this->formatterConfigArray); |
|
91 | 1 | $this->aliasFactory = AliasFactory::build($this->defaultAlias); |
|
92 | 1 | $this->aliasFactory->addMultiple($this->aliasArray); |
|
93 | 1 | } |
|
94 | |||
95 | |||
96 | /* PROXY methods */ |
||
97 | |||
98 | /** |
||
99 | * @param string $name |
||
100 | * @return config\Alias |
||
101 | * @throws \RuntimeException |
||
102 | */ |
||
103 | 1 | public function getAliasConfig(string $name): config\Alias |
|
104 | { |
||
105 | 1 | return $this->aliasFactory->getAliasConfig($name); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param string $name |
||
110 | * @return string|null |
||
111 | * @throws \RuntimeException |
||
112 | */ |
||
113 | public function getModelByAlias(string $name): ?string |
||
114 | { |
||
115 | return $this->getAliasConfig($name)->class ?? null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @see IconGenerator::getIcon() |
||
120 | * @param null|string $extension |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getIcon(?string $extension): ?string |
||
124 | { |
||
125 | return $this->iconGenerator->getIcon($extension); |
||
126 | } |
||
127 | |||
128 | |||
129 | /* OTHER methods */ |
||
130 | |||
131 | /** |
||
132 | * @param IFile $file |
||
133 | * @param string $format |
||
134 | * @param array $formatterConfig |
||
135 | * @return FileFormatter |
||
136 | * @throws \RuntimeException |
||
137 | */ |
||
138 | public function buildFormatter(IFile $file, string $format, array $formatterConfig = []): FileFormatter |
||
139 | { |
||
140 | $alias = $this->getAliasConfig($file->getModelAlias()); |
||
141 | |||
142 | return $this->formatterFactory->build($file, $alias, $this->contentFS, $format, $formatterConfig); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Caches file and returns url to it. |
||
147 | * @param IFile $file |
||
148 | * @param string $format |
||
149 | * @param array $formatterConfig |
||
150 | * @return string|null path to image in cacheFS or null if fails |
||
151 | * @throws \InvalidArgumentException |
||
152 | * @throws \RuntimeException |
||
153 | * @throws \League\Flysystem\FileNotFoundException |
||
154 | * @throws InvalidConfigException |
||
155 | */ |
||
156 | public function getFilePath(IFile $file, string $format, array $formatterConfig = []): ?string |
||
157 | { |
||
158 | $alias = $this->getAliasConfig($file->getModelAlias()); |
||
159 | $formatter = $this->buildFormatter($file, $format, $formatterConfig); |
||
160 | $targetPath = $alias->getAssetPath($file, $format); |
||
161 | |||
162 | if (!$this->cacheFile($file, $formatter, $targetPath)) { |
||
163 | return null; |
||
164 | } |
||
165 | |||
166 | return $targetPath; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Caches file available in web. |
||
171 | * |
||
172 | * @param IFile $file |
||
173 | * @param FileFormatter $formatter |
||
174 | * @param string $targetPath |
||
175 | * @return bool |
||
176 | * @throws \InvalidArgumentException |
||
177 | * @throws InvalidConfigException |
||
178 | * @throws \League\Flysystem\FileNotFoundException |
||
179 | */ |
||
180 | protected function cacheFile(IFile $file, FileFormatter $formatter, string $targetPath): bool |
||
181 | { |
||
182 | if ($file instanceof ICacheStateful && $file->getIsCached($formatter->name)) { |
||
183 | return true; |
||
184 | } |
||
185 | |||
186 | return (new Saver($file, $this->cacheFS, $targetPath))->save($formatter); |
||
187 | } |
||
188 | |||
189 | } |
||
190 |