Completed
Pull Request — master (#357)
by Anton
03:23
created

View::__sleep()   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
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
/**
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
20
/**
21
 * View - simple template engine with native PHP syntax
22
 *
23
 * @package  Bluz\View
24
 * @author   Anton Shevchuk
25
 * @author   ErgallM
26
 * @link     https://github.com/bluzphp/framework/wiki/View
27
 *
28
 * @method string ahref(string $text, mixed $href, array $attributes = [])
29
 * @method string api(string $module, string $method, $params = [])
30
 * @method string attributes(array $attributes = [])
31
 * @method string baseUrl(string $file = null)
32
 * @method string checkbox($name, $value = null, $checked = false, array $attributes = [])
33
 * @method string|bool controller(string $controller = null)
34
 * @method string|View dispatch($module, $controller, $params = [])
35
 * @method string exception(\Exception $exception)
36
 * @method string|null headScript(string $script = null)
37
 * @method string|null headStyle(string $style = null, $media = 'all')
38
 * @method string|bool module(string $module = null)
39
 * @method string partial($__template, $__params = array())
40
 * @method string partialLoop($template, $data = [], $params = [])
41
 * @method string radio($name, $value = null, $checked = false, array $attributes = [])
42
 * @method string redactor($selector, array $settings = [])
43
 * @method string script(string $script)
44
 * @method string select($name, array $options = [], $selected = null, array $attributes = [])
45
 * @method string style(string $style, $media = 'all')
46
 * @method string|null url(string $module, string $controller, array $params = [], bool $checkAccess = false)
47
 * @method AbstractRowEntity|null user()
48
 * @method void widget($module, $widget, $params = [])
49
 */
50
class View implements ViewInterface, \JsonSerializable
51
{
52
    use Container\Container;
53
    use Container\JsonSerialize;
54
    use Container\MagicAccess;
55
    use Options;
56
    use Helper;
57
58
    /**
59
     * Constants for define positions
60
     */
61
    const POS_PREPEND = 'prepend';
62
    const POS_REPLACE = 'replace';
63
    const POS_APPEND = 'append';
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 43
    public function __construct()
89
    {
90
        // initial default helper path
91 43
        $this->addHelperPath(dirname(__FILE__) . '/Helper/');
92 43
    }
93
94
    /**
95
     * List of packed properties
96
     *
97
     * @return string[]
98
     */
99 1
    public function __sleep()
100
    {
101 1
        return ['baseUrl', 'container', 'helpersPath', 'path', 'partialPath', 'template'];
102
    }
103
104
    /**
105
     * View should be callable
106
     *
107
     * @return string
108
     */
109 2
    public function __invoke()
110
    {
111 2
        return $this->render();
112
    }
113
114
    /**
115
     * Render like string
116
     *
117
     * @return string
118
     */
119
    public function __toString()
120
    {
121
        return $this->render();
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     *
127
     * @param  string $path
128
     * @return void
129
     */
130 4
    public function setPath($path)
131
    {
132 4
        $this->path = $path;
133 4
    }
134
135
    /**
136
     * {@inheritdoc}
137
     *
138
     * @param  string $file
139
     * @return void
140
     */
141 2
    public function setTemplate($file)
142
    {
143 2
        $this->template = $file;
144 2
    }
145
146
    /**
147
     * Add partial path for use inside partial and partialLoop helpers
148
     *
149
     * @param  string $path
150
     * @return View
151
     */
152 31
    public function addPartialPath($path)
153
    {
154 31
        $this->partialPath[] = $path;
155 31
        return $this;
156
    }
157
158
    /**
159
     * Render template
160
     *
161
     * @return string
162
     * @throws ViewException
163
     */
164 2
    public function render()
165
    {
166 2
        ob_start();
167
        try {
168 2
            if (!file_exists($this->path . '/' . $this->template)
169 2
                || !is_file($this->path . '/' . $this->template)) {
170
                throw new ViewException("Template '{$this->template}' not found");
171
            }
172 2
            extract($this->container);
173 2
            require $this->path . '/' . $this->template;
174 2
        } catch (\Exception $e) {
175
            // clean output
176
            ob_end_clean();
177
            // @codeCoverageIgnoreStart
178
            if (Application::getInstance()->isDebug()) {
179
                return $e->getMessage() ."\n<br/>". $e->getTraceAsString();
180
            }
181
            // @codeCoverageIgnoreEnd
182
            // nothing for production
183
            return '';
184
        }
185 2
        return ob_get_clean();
186
    }
187
}
188