Completed
Push — master ( 83a9fe...bdaaf9 )
by Tobias
07:57
created

ResponseListener::onKernelResponse()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 1
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\EditInPlace;
13
14
use Symfony\Component\Asset\Packages;
15
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16
use Symfony\Component\Routing\Router;
17
18
/**
19
 * Adds Javascript/CSS files to the Response if the Activator returns true.
20
 *
21
 * @author Damien Alexandre <[email protected]>
22
 */
23
class ResponseListener
24
{
25
    const HTML = <<<'HTML'
26
<!-- TranslationBundle -->
27
<link rel="stylesheet" type="text/css" href="%s">
28
29
<script type="text/javascript" src="%s"></script>
30
<script type="text/javascript" src="%s"></script>
31
32
<script type="text/javascript">
33
window.onload = function() {
34
    TranslationBundleEditInPlace("%s");
35
}
36
</script>
37
<!-- /TranslationBundle -->
38
HTML;
39
40
    /**
41
     * @var ActivatorInterface
42
     */
43
    private $activator;
44
45
    /**
46
     * @var Router
47
     */
48
    private $router;
49
50
    /**
51
     * @var Packages
52
     */
53
    private $packages;
54
55
    /**
56
     * @var string
57
     */
58
    private $configName;
59
60
    public function __construct(ActivatorInterface $activator, Router $router, Packages $packages, $configName = 'default')
61
    {
62
        $this->activator = $activator;
63
        $this->router = $router;
64
        $this->packages = $packages;
65
        $this->configName = $configName;
66
    }
67
68
    public function onKernelResponse(FilterResponseEvent $event)
69
    {
70
        $request = $event->getRequest();
71
72
        if ($this->activator->checkRequest($request)) {
73
            $content = $event->getResponse()->getContent();
74
75
            // Clean the content for malformed tags in attributes or encoded tags
76
            $content = preg_replace("@=\\s*[\"']\\s*(<x-trans.+<\\/x-trans>)\\s*[\"']@mi", "=\"🚫 Can't be translated here. 🚫\"", $content);
77
            $content = preg_replace('@&lt;x-trans.+data-key=&quot;([^&]+)&quot;.+&lt;\\/x-trans&gt;@mi', '🚫 $1 🚫', $content);
78
79
            $html = sprintf(
80
                self::HTML,
81
                $this->packages->getUrl('bundles/translation/css/content-tools.min.css'),
82
                $this->packages->getUrl('bundles/translation/js/content-tools.min.js'),
83
                $this->packages->getUrl('bundles/translation/js/editInPlace.js'),
84
85
                $this->router->generate('translation_edit_in_place_update', [
86
                    'configName' => $this->configName,
87
                    'locale' => $event->getRequest()->getLocale(),
88
                ])
89
            );
90
            $content = str_replace('</body>', $html."\n".'</body>', $content);
91
92
            $response = $event->getResponse();
93
94
            // Remove the cache because we do not want the modified page to be cached
95
            $response->headers->set('cache-control', 'no-cache, no-store, must-revalidate');
96
            $response->headers->set('pragma', 'no-cache');
97
            $response->headers->set('expires', '0');
98
99
            $event->getResponse()->setContent($content);
100
        }
101
    }
102
}
103