1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of TwigView. |
6
|
|
|
* |
7
|
|
|
** (c) 2014 Cees-Jan Kiewiet |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WyriHaximus\TwigView\Event; |
14
|
|
|
|
15
|
|
|
use Cake\Core\Configure; |
16
|
|
|
use Cake\Event\EventListenerInterface; |
17
|
|
|
use Jasny\Twig\ArrayExtension; |
18
|
|
|
use Jasny\Twig\DateExtension; |
19
|
|
|
use Jasny\Twig\PcreExtension; |
20
|
|
|
use Jasny\Twig\TextExtension; |
21
|
|
|
use Twig\Extension\DebugExtension; |
22
|
|
|
use Twig\Extension\StringLoaderExtension; |
23
|
|
|
use Twig\Extra\Markdown\MarkdownExtension; |
24
|
|
|
use Twig\Extra\Markdown\MarkdownInterface; |
25
|
|
|
use Twig\Extra\Markdown\MarkdownRuntime; |
26
|
|
|
use Twig\RuntimeLoader\RuntimeLoaderInterface; |
27
|
|
|
use WyriHaximus\TwigView\Lib\Twig\Extension; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class ExtensionsListener. |
31
|
|
|
* @package WyriHaximus\TwigView\Event |
32
|
|
|
*/ |
33
|
|
|
final class ExtensionsListener implements EventListenerInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Return implemented events. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
14 |
|
public function implementedEvents(): array |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
14 |
|
ConstructEvent::EVENT => 'construct', |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Event handler. |
49
|
|
|
* |
50
|
|
|
* @param \WyriHaximus\TwigView\Event\ConstructEvent $event Event. |
51
|
|
|
*/ |
52
|
5 |
|
public function construct(ConstructEvent $event) |
53
|
|
|
{ |
54
|
5 |
|
if ($event->getTwig()->hasExtension(StringLoaderExtension::class)) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Twig core extensions |
59
|
5 |
|
$event->getTwig()->addExtension(new StringLoaderExtension()); |
60
|
5 |
|
$event->getTwig()->addExtension(new DebugExtension()); |
61
|
|
|
|
62
|
|
|
// CakePHP bridging extensions |
63
|
5 |
|
$event->getTwig()->addExtension(new Extension\I18n()); |
64
|
5 |
|
$event->getTwig()->addExtension(new Extension\Time()); |
65
|
5 |
|
$event->getTwig()->addExtension(new Extension\Basic()); |
66
|
5 |
|
$event->getTwig()->addExtension(new Extension\Number()); |
67
|
5 |
|
$event->getTwig()->addExtension(new Extension\Utils()); |
68
|
5 |
|
$event->getTwig()->addExtension(new Extension\Arrays()); |
69
|
5 |
|
$event->getTwig()->addExtension(new Extension\Strings()); |
70
|
5 |
|
$event->getTwig()->addExtension(new Extension\Inflector()); |
71
|
|
|
|
72
|
|
|
if ( |
73
|
5 |
|
!Configure::check('WyriHaximus.TwigView.flags.potentialDangerous') || |
74
|
|
|
( |
75
|
|
|
Configure::check('WyriHaximus.TwigView.flags.potentialDangerous') && |
76
|
5 |
|
Configure::read('WyriHaximus.TwigView.flags.potentialDangerous') === true |
77
|
|
|
) |
78
|
|
|
) { |
79
|
5 |
|
$event->getTwig()->addExtension(new Extension\PotentialDangerous()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Markdown extension |
83
|
|
|
if ( |
84
|
5 |
|
Configure::check('WyriHaximus.TwigView.markdown.engine') && |
85
|
5 |
|
Configure::read('WyriHaximus.TwigView.markdown.engine') instanceof MarkdownInterface |
86
|
|
|
) { |
87
|
1 |
|
$engine = Configure::read('WyriHaximus.TwigView.markdown.engine'); |
88
|
1 |
|
$event->getTwig()->addExtension(new MarkdownExtension()); |
89
|
|
|
|
90
|
|
|
$event->getTwig()->addRuntimeLoader(new class ($engine) implements RuntimeLoaderInterface { |
91
|
|
|
/** |
92
|
|
|
* @var \Twig\Extra\Markdown\MarkdownInterface |
93
|
|
|
*/ |
94
|
|
|
private $engine; |
95
|
|
|
|
96
|
1 |
|
public function __construct(MarkdownInterface $engine) |
97
|
|
|
{ |
98
|
1 |
|
$this->engine = $engine; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
public function load($class) |
102
|
|
|
{ |
103
|
|
|
if ($class === MarkdownRuntime::class) { |
104
|
|
|
return new MarkdownRuntime($this->engine); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// jasny/twig-extensions |
111
|
|
|
$event->getTwig()->addExtension(new DateExtension()); |
112
|
|
|
$event->getTwig()->addExtension(new PcreExtension()); |
113
|
|
|
$event->getTwig()->addExtension(new TextExtension()); |
114
|
|
|
$event->getTwig()->addExtension(new ArrayExtension()); |
115
|
|
|
// @codingStandardsIgnoreEnd |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|