|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\View\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Jasny\ViewInterface; |
|
6
|
|
|
use Jasny\View\PluginInterface; |
|
7
|
|
|
use Jasny\View\Twig as TwigView; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Add the official Twig extension and Jansy Twig extensions. |
|
11
|
|
|
* |
|
12
|
|
|
* @link https://github.com/twigphp/Twig-extensions |
|
13
|
|
|
* @link https://github.com/jasny/twig-extensions |
|
14
|
|
|
*/ |
|
15
|
|
|
class DefaultTwigExtensions implements PluginInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Extension class names |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
public $extensions = [ |
|
23
|
|
|
'Twig_Extensions_Extension_Array', |
|
24
|
|
|
'Twig_Extensions_Extension_Date', |
|
25
|
|
|
'Twig_Extensions_Extension_I18n', |
|
26
|
|
|
'Twig_Extensions_Extension_Intl', |
|
27
|
|
|
'Twig_Extensions_Extension_Text', |
|
28
|
|
|
'Jasny\Twig\DateExtension', |
|
29
|
|
|
'Jasny\Twig\PcreExtension', |
|
30
|
|
|
'Jasny\Twig\TextExtension', |
|
31
|
|
|
'Jasny\Twig\ArrayExtension' |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Called when the plugin is added to the view. |
|
36
|
|
|
* |
|
37
|
|
|
* @param ViewInterface $view |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function onAdd(ViewInterface $view) |
|
40
|
|
|
{ |
|
41
|
2 |
|
if (!$view instanceof TwigView) { |
|
42
|
1 |
|
throw new \InvalidArgumentException("This plugin only works with a Twig view"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
foreach ($this->extensions as $class) { |
|
46
|
1 |
|
if (class_exists($class)) { |
|
47
|
1 |
|
$view->getTwig()->addExtension(new $class()); |
|
48
|
1 |
|
} |
|
49
|
1 |
|
} |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Called when view renders a template. |
|
54
|
|
|
* |
|
55
|
|
|
* @param ViewInterface $view |
|
56
|
|
|
* @param string $template Template filename |
|
57
|
|
|
* @param array $context |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function onRender(ViewInterface $view, $template, array $context) |
|
60
|
|
|
{ |
|
61
|
1 |
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|