Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
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 / 52
0.00% covered (danger)
0.00%
0 / 3
272
0.00% covered (danger)
0.00%
0 / 1
 screenshots
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
132
 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        $content = '';
57        try {
58            $content = file_get_contents("{$main_href}screenshots.json") ?: '';
59        } catch (\Throwable $th) {
60            // ignore
61        }
62        $main_index = json_decode($content, true);
63        if ($main_index === null) {
64            $out .= "<div>No JSON screenshot index on main: {$content}</div>";
65        } elseif (!isset($main_index['screenshot_paths'])) {
66            $out .= "<div>Invalid JSON screenshot index on main: {$content}</div>";
67        } else {
68            $main_paths = $main_index['screenshot_paths'];
69            foreach ($main_paths as $main_path) {
70                if (array_search($main_path, $screenshot_paths) === false) {
71                    $screenshot_paths[] = $main_path;
72                }
73            }
74        }
75
76        sort($screenshot_paths);
77
78        $out .= <<<'ZZZZZZZZZZ'
79            <div id='root' class='local'>
80            ZZZZZZZZZZ;
81        foreach ($screenshot_paths as $screenshot_path) {
82            $has_screenshot_id = preg_match('/^([a-z0-9\-\_]+)\.png$/', $screenshot_path, $matches);
83            $screenshot_id = $has_screenshot_id ? " id='{$matches[1]}'" : "";
84            $enc_screenshot_path = json_encode($screenshot_path);
85            $out .= <<<ZZZZZZZZZZ
86                <div class='pair'>
87                <h2{$screenshot_id}>
88                    <input type='checkbox' onchange='load(this, {$enc_screenshot_path})'/>
89                    {$screenshot_path}
90                </h2>
91                <img class='local' id='local-{$screenshot_path}' />
92                <img class='main' id='main-{$screenshot_path}' />
93                <div class='after-pair'></div>
94                </div>
95                ZZZZZZZZZZ;
96        }
97        $out .= <<<ZZZZZZZZZZ
98            </div>
99            <script>
100            function load(elem, screenshotPath) {
101                const localElem = document.getElementById('local-'+screenshotPath);
102                const mainElem = document.getElementById('main-'+screenshotPath);
103                if (elem.checked) {
104                    localElem.src = '{$code_href}screenshots/generated/' + screenshotPath;
105                    mainElem.src = '{$main_href}screenshots/generated/' + screenshotPath;
106                    localElem.style.display = 'block';
107                    mainElem.style.display = 'block';
108                } else {
109                    localElem.style.display = 'none';
110                    mainElem.style.display = 'none';
111                }
112            }
113            </script>
114            ZZZZZZZZZZ;
115
116        return new Response($out);
117    }
118
119    #[Route('/screenshots.json')]
120    public function screenshotsJson(
121        Request $request,
122        LoggerInterface $logger,
123        EnvUtils $envUtils,
124    ): Response {
125        $generated_dir = "{$envUtils->getCodePath()}screenshots/generated";
126        $generated_contents = scandir($generated_dir);
127        $screenshot_paths = [];
128        foreach ($generated_contents as $screenshot_path) {
129            if ($screenshot_path[0] != '.') {
130                $screenshot_paths[] = $screenshot_path;
131            }
132        }
133        return new Response(json_encode(['screenshot_paths' => $screenshot_paths]) ?: '');
134    }
135
136    #[Route('/screenshots/generated/{name}.png')]
137    public function screenshot(
138        Request $request,
139        LoggerInterface $logger,
140        EnvUtils $envUtils,
141        string $name,
142    ): Response {
143        $path = "{$envUtils->getCodePath()}screenshots/generated/{$name}.png";
144        return new BinaryFileResponse($path);
145    }
146}