1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Silverback API Components Bundle Project |
||
5 | * |
||
6 | * (c) Daniel West <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Silverback\ApiComponentsBundle\Imagine; |
||
15 | |||
16 | use League\Flysystem\Filesystem; |
||
17 | use League\Flysystem\Visibility; |
||
18 | use Liip\ImagineBundle\Binary\BinaryInterface; |
||
19 | use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; |
||
20 | |||
21 | /** |
||
22 | * @author Daniel West <[email protected]> |
||
23 | */ |
||
24 | class FlysystemCacheResolver implements ResolverInterface |
||
25 | { |
||
26 | private Filesystem $filesystem; |
||
27 | private string $webRoot; |
||
28 | private string $cachePrefix; |
||
29 | private string $cacheRoot; |
||
30 | private string $visibility; |
||
31 | |||
32 | public function __construct(Filesystem $filesystem, string $rootUrl, $cachePrefix = 'media/cache', $visibility = Visibility::PUBLIC) |
||
33 | { |
||
34 | $this->filesystem = $filesystem; |
||
35 | $this->webRoot = rtrim($rootUrl, '/'); |
||
36 | $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/'); |
||
37 | $this->cacheRoot = $this->cachePrefix; |
||
38 | $this->visibility = $visibility; |
||
39 | } |
||
40 | |||
41 | public function isStored($path, $filter): bool |
||
42 | { |
||
43 | return $this->filesystem->fileExists($this->getFilePath($path, $filter)); |
||
44 | } |
||
45 | |||
46 | public function resolve($path, $filter): string |
||
47 | { |
||
48 | return sprintf('%s/%s', rtrim($this->webRoot, '/'), ltrim($this->getFileUrl($path, $filter), '/')); |
||
49 | } |
||
50 | |||
51 | public function store(BinaryInterface $binary, $path, $filter): void |
||
52 | { |
||
53 | $this->filesystem->write($this->getFilePath($path, $filter), $binary->getContent(), ['visibility' => $this->visibility, 'mimetype' => $binary->getMimeType()]); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
54 | } |
||
55 | |||
56 | public function remove(array $paths, array $filters): void |
||
57 | { |
||
58 | if (empty($paths) && empty($filters)) { |
||
59 | return; |
||
60 | } |
||
61 | |||
62 | if (empty($paths)) { |
||
63 | foreach ($filters as $filter) { |
||
64 | $filterCacheDir = $this->cacheRoot . '/' . $filter; |
||
65 | $this->filesystem->deleteDirectory($filterCacheDir); |
||
66 | } |
||
67 | |||
68 | return; |
||
69 | } |
||
70 | |||
71 | foreach ($paths as $path) { |
||
72 | foreach ($filters as $filter) { |
||
73 | if ($this->filesystem->fileExists($this->getFilePath($path, $filter))) { |
||
74 | $this->filesystem->delete($this->getFilePath($path, $filter)); |
||
75 | } |
||
76 | } |
||
77 | } |
||
78 | } |
||
79 | |||
80 | protected function getFilePath($path, $filter): string |
||
81 | { |
||
82 | return $this->getFileUrl($path, $filter); |
||
83 | } |
||
84 | |||
85 | protected function getFileUrl($path, $filter): string |
||
86 | { |
||
87 | // crude way of sanitizing URL scheme ("protocol") part |
||
88 | $path = str_replace('://', '---', $path); |
||
89 | |||
90 | return $this->cachePrefix . '/' . $filter . '/' . ltrim($path, '/'); |
||
91 | } |
||
92 | } |
||
93 |