Completed
Push — master ( 110bba...069124 )
by Edson
01:59
created

Engine::render()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 7
nop 2
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
namespace Sketch\Tpl;
4
5
use Exception;
6
7
/**
8
 * Class Engine
9
 * @package Sketch\Tpl
10
 */
11
class Engine
12
{
13
    /**
14
     * @var array
15
     */
16
    private $data = [];
17
18
    /**
19
     * @param array $config
20
     * @throws Exception
21
     */
22 2
    public function config($config): void
23
    {
24 2
        $expected = ['environment', 'template_dir', 'cache_dir'];
25
26 2
        foreach ($expected as $exp) {
27 2
            if (count($config) == 3) {
28 2
                if (!array_key_exists($exp, $config)) {
29 2
                    throw new Exception("The $exp configuration is expected");
30
                }
31
            } else {
32 2
                throw new Exception("The configuration expected only tree arguments");
33
            }
34
        }
35
36 2
        Tag::setConfig($config);
37 2
    }
38
39
    /**
40
     * @param string $view
41
     * @param array $data
42
     * @return string
43
     */
44 2
    public function render(string $view, array $data = []): string
45
    {
46
        try {
47 2
            $content = $this->handle(Content::getContent($view, Tag::getConfig()));
48
        } catch (Exception $e) { // @codeCoverageIgnore
49
            return $e->getMessage(); // @codeCoverageIgnore
50
        }
51
52 2
        $this->data = array_merge($this->data, $data);
53
54 2
        $dir = Tag::getConfig()['cache_dir'];
55
56 2
        if (!is_dir($dir)) {
57
            mkdir($dir);
58
        }
59
60 2
        $fname = getcwd() . '/' . $dir . '/' . md5($view) . '.phtml';
61
62 2
        $file = new File($fname);
63
64 2
        if (Tag::getConfig()['environment'] == 'production') {
65
            $file->open(); // @codeCoverageIgnore
66 2
        } elseif (Tag::getConfig()['environment'] == 'development') {
67 2
            $this->setCache($file, $content);
68
        }
69
70 2
        $content = $file->read($this->data);
71
72 2
        $file->close();
73
74 2
        return $content;
75
    }
76
77
    /**
78
     * @param $content
79
     * @return string
80
     */
81 2
    private function handle($content)
82
    {
83 2
        Tag::setContent($content);
84
85 2
        $this->registerTag([
86 2
            'Inheritance',
87
            'Include',
88
            'Loop',
89
            'Repeat',
90
            'If',
91
            'Func',
92
            'Eval',
93
            'Variable'
94
        ]);
95
96 2
        return Tag::getContent();
97
    }
98
99
    /**
100
     * @param File $file
101
     * @param $content
102
     */
103 2
    private function setCache(File $file, $content): void
104
    {
105 2
        $file->create();
106 2
        $file->write($content);
107 2
    }
108
109
    /**
110
     * @param array $tags
111
     */
112 2
    private function registerTag(array $tags): void
113
    {
114 2
        foreach ($tags as $tag) {
115 2
            $tag = "\\Sketch\Tpl\\" . ucfirst($tag) . "Tag";
116 2
            new $tag;
117
        }
118 2
    }
119
}
120