Passed
Push — master ( 9d2f0c...c82b8d )
by Henri
01:28
created

Viewer::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 2
nop 2
1
<?php
2
3
namespace HnrAzevedo\Viewer;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Server\RequestHandlerInterface;
8
use HnrAzevedo\Http\Uri;
9
10
use HnrAzevedo\Http\Factory;
11
12
final class Viewer implements ViewerInterface
13
{
14
    use Helper;
15
16
    private static Viewer $instance;
17
    private static string $path = '';
18
    private static bool $middleware;
19
    private ServerRequestInterface $serverRequest;
0 ignored issues
show
introduced by
The private property $serverRequest is not used, and could be removed.
Loading history...
20
21
    public static function getInstance(): Viewer
22
    {
23
        self::$instance = (isset(self::$instance)) ? self::$instance : new self();
24
        return self::$instance;
25
    }
26
27
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29
        if(null === $request->getAttribute('viewer')){
30
            throw new \RuntimeException('The path and file parameters for viewing were not defined in the request');
31
        }
32
33
        self::$path = $request->getAttribute('viewer')['path'];
34
35
        $buffer = $this->getInstance()->render($request->getAttribute('viewer')['file'],
36
            (isset($request->getAttribute('viewer')['data'])) ? $request->getAttribute('viewer')['data'] : [] );
37
38
        $request = $request->withBody((new Factory())->createStream($buffer));
39
40
        return $handler->handle($request);
41
    }
42
43
    public static function render(string $file, ?array $data = []): string
44
    {
45
        self::getInstance()->data = $data;
46
        
47
        if(!isset(self::$middleware)){
48
            self::getInstance()->handle($file);
49
            return '';
50
        }
51
52
        return self::getInstance()->getBody($file.'.view.php');
53
    }
54
55
    private function handle(string $file): void
56
    {
57
        self::$middleware = false;
58
        
59
        $serverRequest = (new Factory())->createServerRequest(
60
            $_SERVER['REQUEST_METHOD'], 
61
            new Uri($_SERVER['REQUEST_URI'])
62
        );
63
    
64
        $serverRequest = $serverRequest->withAttribute('viewer',[
65
            'path' => self::$path,
66
            'file' => $file,
67
            'data' => self::getInstance()->data
68
        ]);
69
70
71
        self::getInstance()->process($serverRequest, new class implements RequestHandlerInterface{
72
            public function handle(ServerRequestInterface $request): ResponseInterface
73
            {
74
                echo $request->getBody()->getContents();
75
                return (new Factory())->createResponse(200);
76
            }
77
        });
78
79
    }
80
81
    public static function path(string $path): Viewer
82
    {
83
        self::$path = $path;
84
        return self::getInstance();
85
    }
86
87
    public static function import(string $file): void
88
    {
89
        try{
90
            echo self::getInstance()->getBody($file.'.inc.php');
91
        }catch(\Exception $er){
92
            echo "<div class='view error'>Component error: {$er->getMessage()}</div>";
93
        }
94
    }
95
96
    private function getBody(string $file)
97
    {
98
        $buffer = $this->getInstance()->getOB(self::$path . DIRECTORY_SEPARATOR . $file);
99
        $buffer = $this->getInstance()->getVars($buffer);
100
        $buffer = $this->getInstance()->getVars($buffer, false);
101
        $buffer = $this->getInstance()->removeComments($buffer);
102
        return $buffer;
103
    }
104
105
}
106