|
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
|
18 |
|
public function __construct(PathCollection $searchPath) |
|
25
|
|
|
{ |
|
26
|
18 |
|
$this->searchPath = $searchPath; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
public function resolve(string $ident, iterable $options = null): Svg |
|
30
|
|
|
{ |
|
31
|
4 |
|
return new Svg($this->findPath($ident), $options); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
7 |
|
protected function findPath(string $ident): \SplFileInfo |
|
35
|
|
|
{ |
|
36
|
7 |
|
foreach ($this->searchPath as $basePath) { |
|
37
|
|
|
/** @var \SplFileInfo|null $basePath */ |
|
38
|
7 |
|
$fullPath = Path::join((string) ($basePath ?? ''), $ident); |
|
39
|
7 |
|
if (!Path::hasExtension($fullPath, 'svg')) { |
|
40
|
5 |
|
$fullPath .= '.svg'; // Add svg extension |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
7 |
|
$realPath = realpath($fullPath); |
|
44
|
7 |
|
if ($realPath && is_file($realPath)) { |
|
45
|
5 |
|
return new \SplFileInfo($fullPath); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
throw new LoaderException($ident, new \ReflectionClass(__CLASS__), (string) $this->searchPath); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|