Completed
Push — master ( 649b4a...cfcee7 )
by Karel
02:05 queued 11s
created

EditInPlaceExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Translation\Bundle\Twig\TranslationExtension.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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