1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/twig-svg-extension |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
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
|
|
|
namespace Ocubom\Twig\Extension\Svg\Provider\FileSystem; |
13
|
|
|
|
14
|
|
|
use Ocubom\Twig\Extension\Svg\Exception\LoaderException; |
15
|
|
|
use Ocubom\Twig\Extension\Svg\Loader\LoaderInterface; |
16
|
|
|
use Ocubom\Twig\Extension\Svg\Svg; |
17
|
|
|
use Ocubom\Twig\Extension\Svg\Util\PathCollection; |
18
|
|
|
use Symfony\Component\Filesystem\Path; |
19
|
|
|
|
20
|
|
|
class FileSystemLoader implements LoaderInterface |
21
|
|
|
{ |
22
|
|
|
private PathCollection $searchPath; |
23
|
|
|
|
24
|
20 |
|
public function __construct(PathCollection $searchPath) |
25
|
|
|
{ |
26
|
20 |
|
$this->searchPath = $searchPath; |
27
|
|
|
} |
28
|
|
|
|
29
|
5 |
|
public function resolve(string $ident, iterable $options = null): Svg |
30
|
|
|
{ |
31
|
5 |
|
return new Svg($this->findPath($ident), $options); |
32
|
|
|
} |
33
|
|
|
|
34
|
8 |
|
protected function findPath(string $ident): \SplFileInfo |
35
|
|
|
{ |
36
|
8 |
|
foreach ($this->searchPath as $basePath) { |
37
|
|
|
/** @var \SplFileInfo|null $basePath */ |
38
|
8 |
|
$fullPath = Path::join((string) ($basePath ?? ''), $ident); |
39
|
8 |
|
if (!Path::hasExtension($fullPath, 'svg')) { |
40
|
6 |
|
$fullPath .= '.svg'; // Add svg extension |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
$realPath = realpath($fullPath); |
44
|
8 |
|
if ($realPath && is_file($realPath)) { |
45
|
5 |
|
return new \SplFileInfo($fullPath); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
throw new LoaderException($ident, new \ReflectionClass(__CLASS__), (string) $this->searchPath); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|