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\Bridge\Twig\Extension\TranslationExtension; |
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
16
|
|
|
use Translation\Bundle\EditInPlace\ActivatorInterface; |
17
|
|
|
use Twig\Extension\AbstractExtension; |
18
|
|
|
use Twig\TwigFilter; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Override the `trans` functions `is_safe` option to allow HTML output from the |
22
|
|
|
* translator. This extension is used by for the EditInPlace feature. |
23
|
|
|
* |
24
|
|
|
* @author Damien Alexandre <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class EditInPlaceExtension extends AbstractExtension |
27
|
|
|
{ |
28
|
|
|
private $extension; |
29
|
|
|
private $requestStack; |
30
|
|
|
private $activator; |
31
|
|
|
|
32
|
|
|
public function __construct(TranslationExtension $extension, RequestStack $requestStack, ActivatorInterface $activator) |
33
|
|
|
{ |
34
|
|
|
$this->extension = $extension; |
35
|
|
|
$this->requestStack = $requestStack; |
36
|
|
|
$this->activator = $activator; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function getFilters(): array |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
new TwigFilter('trans', [$this->extension, 'trans'], ['is_safe_callback' => [$this, 'isSafe']]), |
46
|
|
|
new TwigFilter('transchoice', [$this->extension, 'transchoice'], ['is_safe_callback' => [$this, 'isSafe']]), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Escape output if the EditInPlace is disabled. |
52
|
|
|
*/ |
53
|
|
|
public function isSafe($node): array |
54
|
|
|
{ |
55
|
|
|
return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : []; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getName(): string |
62
|
|
|
{ |
63
|
|
|
return self::class; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: