Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
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 / 54
0.00% covered (danger)
0.00%
0 / 3
342
0.00% covered (danger)
0.00%
0 / 1
 screenshots
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
182
 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\Common\Controller;
4
5use Olz\Common\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 .local, #root .main { display: none; width: 1000px; height: 900px; border: 1px solid #888; background-color: white; }
34            #root.main .local { margin-left:-10000px; }
35            #root.local .main { margin-left:-10000px; }
36            #root .after-pair { clear: both; }
37            </style>
38            <script>
39            let mode = 'local';
40            window.setInterval(() => {
41                mode = (mode === 'local' ? 'main' : 'local');
42                document.getElementById('root').className = mode;
43            }, 1000);
44            </script>
45            ZZZZZZZZZZ;
46
47        $screenshot_paths = [];
48
49        $generated_dir = "{$envUtils->getCodePath()}/screenshots/generated";
50        $generated_contents = scandir($generated_dir);
51        foreach ($generated_contents as $screenshot_path) {
52            if ($screenshot_path[0] != '.' && str_ends_with($screenshot_path, '.html')) {
53                $screenshot_paths[] = $screenshot_path;
54            }
55        }
56
57        $content = '';
58        try {
59            $content = file_get_contents("{$main_href}screenshots.json") ?: '';
60        } catch (\Throwable $th) {
61            // ignore
62        }
63        $main_index = json_decode($content, true);
64        if ($main_index === null) {
65            $out .= "<div>No JSON screenshot index on main: {$content}</div>";
66        } elseif (!isset($main_index['screenshot_paths'])) {
67            $out .= "<div>Invalid JSON screenshot index on main: {$content}</div>";
68        } else {
69            $main_paths = $main_index['screenshot_paths'];
70            foreach ($main_paths as $main_path) {
71                if (!str_ends_with($main_path, '.html')) {
72                    continue;
73                }
74                if (array_search($main_path, $screenshot_paths) === false) {
75                    $screenshot_paths[] = $main_path;
76                }
77            }
78        }
79
80        sort($screenshot_paths);
81
82        $out .= <<<'ZZZZZZZZZZ'
83            <div id='root' class='local'>
84            ZZZZZZZZZZ;
85        foreach ($screenshot_paths as $screenshot_path) {
86            $has_screenshot_id = preg_match('/^([a-z0-9\-\_]+)\.html$/', $screenshot_path, $matches);
87            $screenshot_id = $has_screenshot_id ? " id='{$matches[1]}'" : "";
88            $enc_screenshot_path = json_encode($screenshot_path);
89            $out .= <<<ZZZZZZZZZZ
90                <div class='pair'>
91                <h2{$screenshot_id}>
92                    <input type='checkbox' onchange='load(this, {$enc_screenshot_path})'/>
93                    {$screenshot_path}
94                </h2>
95                <iframe class='local' id='local-{$screenshot_path}'></iframe>
96                <iframe class='main' id='main-{$screenshot_path}'></iframe>
97                <div class='after-pair'></div>
98                </div>
99                ZZZZZZZZZZ;
100        }
101        $out .= <<<ZZZZZZZZZZ
102            </div>
103            <script>
104            function load(elem, screenshotPath) {
105                const localElem = document.getElementById('local-'+screenshotPath);
106                const mainElem = document.getElementById('main-'+screenshotPath);
107                if (elem.checked) {
108                    localElem.src = '{$code_href}screenshots/generated/' + screenshotPath;
109                    mainElem.src = '{$main_href}screenshots/generated/' + screenshotPath;
110                    localElem.style.display = 'block';
111                    mainElem.style.display = 'block';
112                } else {
113                    localElem.style.display = 'none';
114                    mainElem.style.display = 'none';
115                }
116            }
117            </script>
118            ZZZZZZZZZZ;
119
120        return new Response($out);
121    }
122
123    #[Route('/screenshots.json')]
124    public function screenshotsJson(
125        Request $request,
126        LoggerInterface $logger,
127        EnvUtils $envUtils,
128    ): Response {
129        $generated_dir = "{$envUtils->getCodePath()}screenshots/generated";
130        $generated_contents = scandir($generated_dir);
131        $screenshot_paths = [];
132        foreach ($generated_contents as $screenshot_path) {
133            if ($screenshot_path[0] != '.') {
134                $screenshot_paths[] = $screenshot_path;
135            }
136        }
137        return new Response(json_encode(['screenshot_paths' => $screenshot_paths]) ?: '');
138    }
139
140    #[Route('/screenshots/generated/{name}.html')]
141    public function screenshot(
142        Request $request,
143        LoggerInterface $logger,
144        EnvUtils $envUtils,
145        string $name,
146    ): Response {
147        $path = "{$envUtils->getCodePath()}screenshots/generated/{$name}.html";
148        return new BinaryFileResponse($path);
149    }
150}