Completed
Push — master ( f806ab...8def13 )
by Anton
9s
created

View::__invoke()   A

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 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
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
/**
10
 * @namespace
11
 */
12
namespace Bluz\View;
13
14
use Bluz\Application\Application;
15
use Bluz\Auth\AbstractRowEntity;
16
use Bluz\Common\Container;
17
use Bluz\Common\Helper;
18
use Bluz\Common\Options;
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|null headScript(string $script = null)
38
 * @method string|null headStyle(string $style = null, $media = 'all')
39
 * @method string|bool module(string $module = null)
40
 * @method string partial($__template, $__params = array())
41
 * @method string partialLoop($template, $data = [], $params = [])
42
 * @method string radio($name, $value = null, $checked = false, array $attributes = [])
43
 * @method string redactor($selector, array $settings = [])
44
 * @method string script(string $script)
45
 * @method string select($name, array $options = [], $selected = null, array $attributes = [])
46
 * @method string style(string $style, $media = 'all')
47
 * @method string|null url(string $module, string $controller, array $params = [], bool $checkAccess = false)
48
 * @method AbstractRowEntity|null user()
49
 * @method void widget($module, $widget, $params = [])
50
 */
51
class View implements ViewInterface, \JsonSerializable
52
{
53
    use Container\Container;
54
    use Container\JsonSerialize;
55
    use Container\MagicAccess;
56
    use Options;
57
    use Helper;
58
    use ResponseTrait;
59
60
    /**
61
     * Constants for define positions
62
     */
63
    const POS_PREPEND = 'prepend';
64
    const POS_REPLACE = 'replace';
65
    const POS_APPEND = 'append';
66
67
    /**
68
     * @var string base url
69
     */
70
    protected $baseUrl;
71
72
    /**
73
     * @var string path to template
74
     */
75
    protected $path;
76
77
    /**
78
     * @var array paths to partial
79
     */
80
    protected $partialPath = [];
81
82
    /**
83
     * @var string template name
84
     */
85
    protected $template;
86
87
    /**
88
     * Create view instance, initial default helper path
89
     */
90 41
    public function __construct()
91
    {
92
        // initial default helper path
93 41
        $this->addHelperPath(dirname(__FILE__) . '/Helper/');
94 41
    }
95
96
    /**
97
     * List of packed properties
98
     *
99
     * @return string[]
100
     */
101 1
    public function __sleep()
102
    {
103 1
        return ['baseUrl', 'container', 'helpersPath', 'path', 'partialPath', 'template'];
104
    }
105
106
    /**
107
     * Render like string
108
     *
109
     * @return string
110
     */
111 2
    public function __toString()
112
    {
113 2
        ob_start();
114
        try {
115 2
            if (!file_exists($this->path . '/' . $this->template)
116 2
                || !is_file($this->path . '/' . $this->template)) {
117
                throw new ViewException("Template '{$this->template}' not found");
118
            }
119 2
            extract($this->container);
120 2
            require $this->path . '/' . $this->template;
121
        } catch (\Exception $e) {
122
            // clean output
123
            ob_end_clean();
124
            // @codeCoverageIgnoreStart
125
            if (Application::getInstance()->isDebug()) {
126
                return $e->getMessage() ."\n<br/>". $e->getTraceAsString();
127
            }
128
            // @codeCoverageIgnoreEnd
129
            // nothing for production
130
            return '';
131
        }
132 2
        return ob_get_clean();
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @param  string $path
139
     * @return void
140
     */
141 4
    public function setPath($path)
142
    {
143 4
        $this->path = $path;
144 4
    }
145
146
    /**
147
     * {@inheritdoc}
148
     *
149
     * @param  string $file
150
     * @return void
151
     */
152 2
    public function setTemplate($file)
153
    {
154 2
        $this->template = $file;
155 2
    }
156
157
    /**
158
     * Add partial path for use inside partial and partialLoop helpers
159
     *
160
     * @param  string $path
161
     * @return View
162
     */
163 29
    public function addPartialPath($path)
164
    {
165 29
        $this->partialPath[] = $path;
166 29
        return $this;
167
    }
168
}
169