|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of ocubom/twig-extra-bundle |
|
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\TwigExtraBundle; |
|
13
|
|
|
|
|
14
|
|
|
use Ocubom\Twig\Extension\HtmlExtension; |
|
15
|
|
|
use Ocubom\Twig\Extension\SvgExtension; |
|
16
|
|
|
use Twig\Error\SyntaxError; |
|
17
|
|
|
|
|
18
|
|
|
final class Extensions |
|
19
|
|
|
{ |
|
20
|
|
|
private const EXTENSIONS = [ |
|
21
|
|
|
'html' => [ |
|
22
|
|
|
'name' => 'html', |
|
23
|
|
|
'class' => HtmlExtension::class, |
|
24
|
|
|
'class_name' => 'OcubomHtmlExtension', |
|
25
|
|
|
'package' => 'ocubom/twig-html-extension', |
|
26
|
|
|
'filters' => ['html_attributes', 'html_compress'], |
|
27
|
|
|
'functions' => [], |
|
28
|
|
|
], |
|
29
|
|
|
'svg' => [ |
|
30
|
|
|
'name' => 'svg', |
|
31
|
|
|
'class' => SvgExtension::class, |
|
32
|
|
|
'class_name' => 'OcubomSvgExtension', |
|
33
|
|
|
'package' => 'ocubom/twig-svg-extension', |
|
34
|
|
|
'filters' => ['svg_symbols'], |
|
35
|
|
|
'functions' => [], |
|
36
|
|
|
], |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
2 |
|
public static function getClasses(): array |
|
40
|
|
|
{ |
|
41
|
2 |
|
return array_column(self::EXTENSIONS, 'class', 'name'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function suggestFilter(string $name): bool |
|
45
|
|
|
{ |
|
46
|
|
|
foreach (self::EXTENSIONS as $extension) { |
|
47
|
|
|
if (isset($extension['filters']) && in_array($name, $extension['filters'])) { |
|
48
|
|
|
throw new SyntaxError(sprintf('The "%s" filter is part of the %s, which is not installed/enabled; try running "composer require %s".', $name, $extension['class_name'], $extension['package'])); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return false; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function suggestFunction(string $name): bool |
|
56
|
|
|
{ |
|
57
|
|
|
foreach (self::EXTENSIONS as $extension) { |
|
58
|
|
|
if (isset($extension['functions']) && in_array($name, $extension['functions'])) { |
|
59
|
|
|
throw new SyntaxError(sprintf('The "%s" function is part of the %s, which is not installed/enabled; try running "composer require %s".', $name, $extension['class_name'], $extension['package'])); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|