1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
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 Translation\Bundle\Twig; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
15
|
|
|
use Translation\Bundle\EditInPlace\ActivatorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Override the `trans` functions `is_safe` option to allow HTML output from the |
19
|
|
|
* translator. This extension is used by for the EditInPlace feature. |
20
|
|
|
* |
21
|
|
|
* @author Damien Alexandre <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
final class EditInPlaceExtension extends \Symfony\Bridge\Twig\Extension\TranslationExtension |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ActivatorInterface |
27
|
|
|
*/ |
28
|
|
|
private $activator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var RequestStack |
32
|
|
|
*/ |
33
|
|
|
private $requestStack; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
5 |
|
public function getFilters() |
39
|
|
|
{ |
40
|
|
|
return [ |
41
|
5 |
|
new \Twig_SimpleFilter('trans', [$this, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]), |
42
|
5 |
|
new \Twig_SimpleFilter('transchoice', [$this, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]), |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Escape output if the EditInPlace is disabled. |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
2 |
|
public function isSafe($node) |
52
|
|
|
{ |
53
|
2 |
|
return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ActivatorInterface $activator |
58
|
|
|
*/ |
59
|
8 |
|
public function setActivator(ActivatorInterface $activator) |
60
|
|
|
{ |
61
|
8 |
|
$this->activator = $activator; |
62
|
8 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param RequestStack $requestStack |
66
|
|
|
*/ |
67
|
8 |
|
public function setRequestStack(RequestStack $requestStack) |
68
|
|
|
{ |
69
|
8 |
|
$this->requestStack = $requestStack; |
70
|
8 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getName() |
76
|
|
|
{ |
77
|
|
|
return self::class; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|