Completed
Push — master ( 705fb1...b4c0e6 )
by Tobias
06:39
created

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