Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScreenshotsController
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 screenshots
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
110
 screenshotsJson
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 screenshot
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Olz\Controller;
4
5use Olz\Utils\EnvUtils;
6use Psr\Log\LoggerInterface;
7use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8use Symfony\Component\HttpFoundation\BinaryFileResponse;
9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\Response;
11use Symfony\Component\Routing\Annotation\Route;
12
13class ScreenshotsController extends AbstractController {
14    #[Route('/screenshots')]
15    public function screenshots(
16        Request $request,
17        LoggerInterface $logger,
18        EnvUtils $envUtils,
19    ): Response {
20        $code_href = $envUtils->getCodeHref();
21        $main_href = 'https://olzimmerberg.ch/';
22
23        $out = <<<'ZZZZZZZZZZ'
24            <style>
25            body { margin: 0; }
26            #root .pair { border: 10px solid black; }
27            #root.main { background-color: red; }
28            #root.local { background-color: green; }
29            #root.main .pair { border-color: red; }
30            #root.local .pair { border-color: green; }
31            #root .main { float: left; }
32            #root .local { float: left; }
33            #root.main .local { margin-left:-10000px; }
34            #root.local .main { margin-left:-10000px; }
35            #root .after-pair { clear: both; }
36            </style>
37            <script>
38            let mode = 'local';
39            window.setInterval(() => {
40                mode = (mode === 'local' ? 'main' : 'local');
41                document.getElementById('root').className = mode;
42            }, 1000);
43            </script>
44            ZZZZZZZZZZ;
45
46        $screenshot_paths = [];
47
48        $generated_dir = "{$envUtils->getCodePath()}/screenshots/generated";
49        $generated_contents = scandir($generated_dir);
50        foreach ($generated_contents as $screenshot_path) {
51            if ($screenshot_path[0] != '.') {
52                $screenshot_paths[] = $screenshot_path;
53            }
54        }
55
56        $main_index = json_decode(
57            @file_get_contents("{$main_href}screenshots.json") ?: '',
58            true
59        );
60        if ($main_index === null) {
61            $out .= '<div>No JSON screenshot index on main</div>';
62        } elseif (!isset($main_index['screenshot_paths'])) {
63            $out .= '<div>Invalid JSON screenshot index on main</div>';
64        } else {
65            $main_paths = $main_index['screenshot_paths'];
66            foreach ($main_paths as $main_path) {
67                if (array_search($main_path, $screenshot_paths) === false) {
68                    $screenshot_paths[] = $main_path;
69                }
70            }
71        }
72
73        sort($screenshot_paths);
74
75        $out .= <<<'ZZZZZZZZZZ'
76            <div id='root' class='local'>
77            ZZZZZZZZZZ;
78        foreach ($screenshot_paths as $screenshot_path) {
79            $has_screenshot_id = preg_match('/^([a-z0-9\-\_]+)\.png$/', $screenshot_path, $matches);
80            $screenshot_id = $has_screenshot_id ? " id='{$matches[1]}'" : "";
81            $enc_screenshot_path = json_encode($screenshot_path);
82            $out .= <<<ZZZZZZZZZZ
83                <div class='pair'>
84                <h2{$screenshot_id}>
85                    <input type='checkbox' onchange='load(this, {$enc_screenshot_path})'/>
86                    {$screenshot_path}
87                </h2>
88                <img class='local' id='local-{$screenshot_path}' />
89                <img class='main' id='main-{$screenshot_path}' />
90                <div class='after-pair'></div>
91                </div>
92                ZZZZZZZZZZ;
93        }
94        $out .= <<<ZZZZZZZZZZ
95            </div>
96            <script>
97            function load(elem, screenshotPath) {
98                const localElem = document.getElementById('local-'+screenshotPath);
99                const mainElem = document.getElementById('main-'+screenshotPath);
100                if (elem.checked) {
101                    localElem.src = '{$code_href}screenshots/generated/' + screenshotPath;
102                    mainElem.src = '{$main_href}screenshots/generated/' + screenshotPath;
103                    localElem.style.display = 'block';
104                    mainElem.style.display = 'block';
105                } else {
106                    localElem.style.display = 'none';
107                    mainElem.style.display = 'none';
108                }
109            }
110            </script>
111            ZZZZZZZZZZ;
112
113        return new Response($out);
114    }
115
116    #[Route('/screenshots.json')]
117    public function screenshotsJson(
118        Request $request,
119        LoggerInterface $logger,
120        EnvUtils $envUtils,
121    ): Response {
122        $generated_dir = "{$envUtils->getCodePath()}screenshots/generated";
123        $generated_contents = scandir($generated_dir);
124        $screenshot_paths = [];
125        foreach ($generated_contents as $screenshot_path) {
126            if ($screenshot_path[0] != '.') {
127                $screenshot_paths[] = $screenshot_path;
128            }
129        }
130        return new Response(json_encode(['screenshot_paths' => $screenshot_paths]) ?: '');
131    }
132
133    #[Route('/screenshots/generated/{name}.png')]
134    public function screenshot(
135        Request $request,
136        LoggerInterface $logger,
137        EnvUtils $envUtils,
138        string $name,
139    ): Response {
140        $path = "{$envUtils->getCodePath()}screenshots/generated/{$name}.png";
141        return new BinaryFileResponse($path);
142    }
143}