Completed
Pull Request — master (#393)
by Anton
04:53
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

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