Completed
Push — master ( 816e5a...57fb58 )
by Restu
11:24
created

View::template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
1
<?php
2
namespace JayaCode\Framework\Core\View;
3
4
use JayaCode\Framework\Core\View\Converter\BasicConvert;
5
use JayaCode\Framework\Core\View\Converter\Converter;
6
use JayaCode\Framework\Core\View\Template\BasicTemplate;
7
use JayaCode\Framework\Core\View\Template\Template;
8
use JayaCode\Framework\Core\View\VariableCollector\BasicVariableCollector;
9
use JayaCode\Framework\Core\View\VariableCollector\VariableCollector;
10
11
/**
12
 * Class View
13
 * @property VariableCollector vars
14
 * @package JayaCode\Framework\Core\View
15
 */
16
class View
17
{
18
    /**
19
     * @var VariableCollector
20
     */
21
    protected $varCollector;
22
23
    /**
24
     * @var array
25
     */
26
    protected $options = array();
27
28
    /**
29 6
     * @var Converter
30
     */
31 6
    protected $converter;
32
33 6
    /**
34
     * View constructor.
35
     * @param string $viewDir
36 6
     * @param $vars
37
     * @param array $options
38
     * @throws \Exception
39 6
     */
40
    public function __construct($viewDir = "/", $vars = null, $options = array())
41 6
    {
42
43 6
        $this->options = $options + [
44 6
            "extension" => ".vj",
45
            "variableCollectionClass" => BasicVariableCollector::class,
46
            "templateClass" => BasicTemplate::class,
47
            "converter" => BasicConvert::class,
48
            "cacheDir" => null
49 6
        ];
50
51 6
        $this->varCollector = new $this->options["variableCollectionClass"]();
52 6
        $this->converter = new $this->options["converter"]();
53
54 6
        Template::$extension = $this->options["extension"];
55 6
56
        $this->setViewDir($viewDir);
57
        if ($vars) {
58
            $this->varCollector->add($vars);
59
        }
60
    }
61
62
    /**
63
     * @param mixed $viewDir
64
     * @throws \Exception
65
     */
66
    public function setViewDir($viewDir)
67
    {
68
        if (!is_dir($viewDir)) {
69
            throw new \Exception("not found directory {$viewDir}");
70
        }
71
72
        $this->options['viewDir'] = rtrim($viewDir, "/") . "/";
73
    }
74
75
    /**
76
     * @param $name
77
     * @return VariableCollector|null
78
     */
79
    public function __get($name)
80
    {
81
        return ($name == "vars")?$this->varCollector:null;
82
    }
83
84
    /**
85
     * @param $fileTemplate
86
     * @return Template
87
     */
88
    public function template($fileTemplate)
89
    {
90
        $fileTemplateReal = str_replace(".", "/", $fileTemplate);
91
        $fileTemplateReal .= $this->options["extension"];
92
93
        return new $this->options["templateClass"]($this->options["viewDir"], $fileTemplateReal,
94
                                                    $this->varCollector, $this->converter, $this->options['cacheDir']);
95
    }
96
}
97