Completed
Push — master ( 15cb6d...86e5be )
by Nikolas
03:31
created

DelayedOutputRenderer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 1
A render() 0 13 1
A headElements() 0 19 1
1
<?php namespace rtens\domin\delivery\web\renderers;
2
3
use rtens\domin\delivery\DelayedOutput;
4
use rtens\domin\delivery\web\Element;
5
use rtens\domin\delivery\web\WebRenderer;
6
7
class DelayedOutputRenderer implements WebRenderer {
8
9
    /**
10
     * @param mixed $value
11
     * @return bool
12
     */
13
    public function handles($value) {
14
        return $value instanceof DelayedOutput;
15
    }
16
17
    /**
18
     * @param DelayedOutput $value
19
     * @return DelayedOutput
20
     */
21
    public function render($value) {
22
        header('Content-Encoding: none;');
23
        header('X-Accel-Buffering: no');
24
        ob_end_flush();
25
26
        $value->surroundWith("<pre>", "</pre>");
27
        $value->setPrinter(function ($string) {
28
            echo $string;
29
            flush();
30
            ob_flush();
31
        });
32
        return $value;
33
    }
34
35
    /**
36
     * @param mixed $value
37
     * @return array|Element[]
38
     */
39
    public function headElements($value) {
40
        return [
41
            new Element('script', [], ['
42
                var scrolled = true;
43
44
                var keepScrolling = setInterval(function () {
45
                    scrolled = true;
46
                    window.scrollTo(0,document.body.scrollHeight);
47
                }, 100);
48
49
                window.addEventListener("scroll", function () {
50
                    if (!scrolled) {
51
                        clearInterval(keepScrolling);
52
                    }
53
                    scrolled = false;
54
                });
55
            '])
56
        ];
57
    }
58
}