BasicTemplate::getContentParent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
namespace JayaCode\Framework\Core\View\Template;
3
4
/**
5
 * Class BasicTemplate
6
 * @package JayaCode\Framework\Core\View\Template
7
 */
8
class BasicTemplate extends Template
9
{
10
    /**
11
     * @var
12
     */
13
    protected $parent;
14
15
    /**
16
     * @return string
17
     */
18 3
    public function buildScript()
19
    {
20 3
        $fileCache = $this->cacheDir.str_replace(["/", "."], "_", $this->fileTemplate);
21 3
        if ($this->cacheDir !== null && file_exists($fileCache) && empty($this->contentParent)) {
22
            return $this->script = file_get_contents($fileCache);
23
        }
24
25 3
        $this->script = $this->converter->build(file_get_contents($this->locTemplate));
26
27 3
        if ($this->cacheDir !== null && empty($this->contentParent)) {
28
            file_put_contents($fileCache, $this->script);
29
        }
30 3
    }
31
32
    /**
33
     * @return mixed|string
34
     */
35 3
    public function render()
36
    {
37 3
        if ($this->script === null) {
38 3
            $this->buildScript();
39 3
        }
40
41 3
        $varArray = $this->vars->all();
42 3
        extract($varArray);
43
        
44 3
        ob_start();
45 3
        eval("?>" . $this->script);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
46 3
        $result = ob_get_contents();
47 3
        ob_end_clean();
48
        
49 3
        if ($this->parent) {
50 3
            $fileParent = str_replace(".", "/", $this->parent);
51 3
            $fileParent = $fileParent.Template::$extension;
52 3
            $parent = new BasicTemplate($this->locView, $fileParent, $this->variableCollector, $this->converter);
53 3
            $parent->setContentParent($this->contentParent);
54 3
            return $parent->render();
55
        }
56 3
        return $result;
57
    }
58
59
    /**
60
     * @param $name
61
     */
62 3
    public function setParent($name)
63
    {
64 3
        $this->parent = $name;
65 3
    }
66
67
    /**
68
     * @var array
69
     */
70
    protected $contentParent = [];
71
72
    /**
73
     * @return array
74
     */
75
    public function getContentParent()
76
    {
77
        return $this->contentParent;
78
    }
79
80
    /**
81
     * @param array $contentParent
82
     */
83 3
    public function setContentParent($contentParent)
84
    {
85 3
        $this->contentParent = $contentParent;
86 3
    }
87
}
88