Completed
Pull Request — master (#410)
by Anton
06:09
created

View::setTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 3
cts 3
cp 1
crap 1
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
     * @throws \Bluz\Common\Exception\CommonException
88
     */
89 41
    public function __construct()
90
    {
91
        // initial default helper path
92 41
        $this->addHelperPath(__DIR__ . '/Helper/');
93 41
    }
94
95
    /**
96
     * Render like string
97
     *
98
     * @return string
99
     */
100 2
    public function __toString()
101
    {
102 2
        ob_start();
103
        try {
104 2
            if (!file_exists($this->path . '/' . $this->template)
105 2
                || !is_file($this->path . '/' . $this->template)) {
106
                throw new ViewException("Template '{$this->template}' not found");
107
            }
108 2
            extract($this->container, EXTR_SKIP);
109 2
            require $this->path . '/' . $this->template;
110
        } catch (\Exception $e) {
111
            // clean output
112
            ob_end_clean();
113
            // save error to log
114
            Logger::error($e->getMessage());
115
            // nothing to output
116
            return '';
117
        }
118 2
        return ob_get_clean();
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     *
124
     * @param  string $path
125
     * @return void
126
     */
127 4
    public function setPath($path)
128
    {
129 4
        $this->path = $path;
130 4
    }
131
132
    /**
133
     * {@inheritdoc}
134
     *
135
     * @param  string $file
136
     * @return void
137
     */
138 2
    public function setTemplate($file)
139
    {
140 2
        $this->template = $file;
141 2
    }
142
143
    /**
144
     * Add partial path for use inside partial and partialLoop helpers
145
     *
146
     * @param  string $path
147
     * @return View
148
     */
149 29
    public function addPartialPath($path)
150
    {
151 29
        $this->partialPath[] = $path;
152 29
        return $this;
153
    }
154
}
155