Completed
Pull Request — master (#425)
by Anton
05:10
created

View::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\View;
12
13
use Bluz\Application\Application;
14
use Bluz\Auth\AbstractRowEntity;
15
use Bluz\Common\Container;
16
use Bluz\Common\Helper;
17
use Bluz\Common\Options;
18
use Bluz\Proxy\Logger;
19
use Bluz\Response\ResponseTrait;
20
21
/**
22
 * View - simple template engine with native PHP syntax
23
 *
24
 * @package  Bluz\View
25
 * @author   Anton Shevchuk
26
 * @author   ErgallM
27
 * @link     https://github.com/bluzphp/framework/wiki/View
28
 *
29
 * @method string ahref(string $text, mixed $href, array $attributes = [])
30
 * @method string api(string $module, string $method, $params = [])
31
 * @method string attributes(array $attributes = [])
32
 * @method string baseUrl(string $file = null)
33
 * @method string checkbox($name, $value = null, $checked = false, array $attributes = [])
34
 * @method string|bool controller(string $controller = null)
35
 * @method string|View dispatch($module, $controller, $params = [])
36
 * @method string exception(\Exception $exception)
37
 * @method string gravatar($email, $size = 80, $default = 'mm', $rate = 'g')
38
 * @method bool hasModule(string $module)
39
 * @method string|null headScript(string $src = null, array $attributes = [])
40
 * @method string|null headScriptBlock(string $code = null)
41
 * @method string|null headStyle(string $href = null, string $media = 'all')
42
 * @method string|bool module(string $module = null)
43
 * @method string partial($__template, $__params = [])
44
 * @method string partialLoop($template, $data = [], $params = [])
45
 * @method string radio($name, $value = null, $checked = false, array $attributes = [])
46
 * @method string redactor($selector, array $settings = [])
47
 * @method string script(string $src, array $attributes = [])
48
 * @method string scriptBlock(string $code)
49
 * @method string select($name, array $options = [], $selected = null, array $attributes = [])
50
 * @method string style(string $href, $media = 'all')
51
 * @method string styleBlock(string $code, $media = 'all')
52
 * @method string|null url(string $module, string $controller, array $params = [], bool $checkAccess = false)
53
 * @method AbstractRowEntity|null user()
54
 * @method void widget($module, $widget, $params = [])
55
 */
56
class View implements ViewInterface, \JsonSerializable
57
{
58
    use Container\Container;
59
    use Container\JsonSerialize;
60
    use Container\MagicAccess;
61
    use Options;
62
    use Helper;
63
    use ResponseTrait;
64
65
    /**
66
     * @var string base url
67
     */
68
    protected $baseUrl;
69
70
    /**
71
     * @var string path to template
72
     */
73
    protected $path;
74
75
    /**
76
     * @var array paths to partial
77
     */
78
    protected $partialPath = [];
79
80
    /**
81
     * @var string template name
82
     */
83
    protected $template;
84
85
    /**
86
     * Create view instance, initial default helper path
87
     *
88
     * @throws \Bluz\Common\Exception\CommonException
89
     */
90 44
    public function __construct()
91
    {
92
        // initial default helper path
93 44
        $this->addHelperPath(__DIR__ . '/Helper/');
94 44
    }
95
96
    /**
97
     * Render like string
98
     *
99
     * @return string
100
     */
101 2
    public function __toString()
102
    {
103 2
        ob_start();
104
        try {
105 2
            if (!file_exists($this->path . DIRECTORY_SEPARATOR . $this->template)
106 2
                || !is_file($this->path . DIRECTORY_SEPARATOR . $this->template)
107
            ) {
108
                throw new ViewException("Template `{$this->template}` not found");
109
            }
110 2
            extract($this->container, EXTR_SKIP);
111 2
            require $this->path . DIRECTORY_SEPARATOR . $this->template;
112
        } catch (\Exception $e) {
113
            // clean output
114
            ob_end_clean();
115
            // save error to log
116
            Logger::error($e->getMessage());
117
            // nothing to output
118
            return '';
119
        }
120 2
        return ob_get_clean();
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     *
126
     * @return string
127
     */
128
    public function getPath()
129
    {
130
        return $this->path;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     *
136
     * @param  string $path
137
     *
138
     * @return void
139
     */
140 5
    public function setPath($path)
141
    {
142 5
        $this->path = $path;
143 5
    }
144
145
    /**
146
     * {@inheritdoc}
147
     *
148
     * @return string
149
     */
150 1
    public function getTemplate()
151
    {
152 1
        return $this->template;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     *
158
     * @param  string $file
159
     *
160
     * @return void
161
     */
162 4
    public function setTemplate($file)
163
    {
164 4
        $this->template = $file;
165 4
    }
166
167
    /**
168
     * Add partial path for use inside partial and partialLoop helpers
169
     *
170
     * @param  string $path
171
     *
172
     * @return void
173
     */
174 31
    public function addPartialPath($path)
175
    {
176 31
        $this->partialPath[] = $path;
177 31
    }
178
}
179