Completed
Push — master ( d12d51...b647e5 )
by Edson
01:35
created

Engine::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
use Exception;
6
7
class Engine extends Content
8
{
9
    private $config;
10
    private $data = [];
11
12 2
    private $file;
0 ignored issues
show
introduced by
The private property $file is not used, and could be removed.
Loading history...
13
    private $content;
0 ignored issues
show
introduced by
The private property $content is not used, and could be removed.
Loading history...
14 2
15
    /**
16
     * @param array $config
17 2
     */
18 2
    public function config(array $config): void
19
    {
20 2
        $this->config = $config;
21
    }
22 2
23
    /**
24 2
     * @param string $view
25
     * @param array $data
26 2
     * @return string
27
     */
28 2
    public function render(string $view, array $data = []): string
29
    {
30 2
        try {
31 2
            $content = $this->handle($this->getContent($view, $this->config));
32 2
        } catch (Exception $e) {
33 2
            die($e->getMessage());
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
34
        }
35
36 2
        $this->data = array_merge($this->data, $data);
37
38
        $fname = getcwd() . '/' . $this->config['cache_dir'] . '/' . $view . '.phtml';
39 2
40
        $file = new File($fname);
41 2
42 2
        if ($this->config['environment'] == 'production') {
43 2
            $file->open();
44 2
        } elseif ($this->config['environment'] == 'development') {
45 2
            $this->setCache($file, $content);
46 2
        }
47
48 2
        $content = $file->read($this->data);
49
50
        $file->close();
51
52
        return $content;
53
    }
54
55
    public function getContent(string $view, array $config): string
56
    {
57
        return parent::getContent($view, $config);
58
    }
59
60
    private function handle($content)
61
    {
62
        $content = new Inheritance($content, $this->config);
63
        $content = new IncludeTpl($content, $this->config);
64
        $content = new ForeachTpl($content);
65
        $content = new IfTpl($content);
66
        $content = new FuncTpl($content);
67
        $content = new VariableTpl($content);
68
69
        return $content;
70
    }
71
72
    private function setCache(File $file, $content): void
73
    {
74
        $file->create();
75
        $file->write($content);
76
    }
77
}
78