View::render()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 54
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 6
eloc 32
c 4
b 0
f 1
nc 8
nop 0
dl 0
loc 54
ccs 0
cts 33
cp 0
crap 42
rs 8.7857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Mvc;
12
13
/**
14
 * View class
15
 *
16
 * This class manages views and parameters
17
 */
18
class View
19
{
20
    use \Drone\Util\ParamTrait;
21
22
    /**
23
     * Regex for identifiers
24
     *
25
     * @var string
26
     */
27
    const IDENTIFIER_REGEX = '[_a-zA-Z][_a-zA-Z0-9]*[.]?[_a-zA-Z0-9]+';
28
29
    /**
30
     * View name
31
     *
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * The path where views are located.
38
     *
39
     * @var string
40
     */
41
    protected $path;
42
43
    /**
44
     * The path rendered views are located
45
     *
46
     * @var string
47
     */
48
    protected $cache;
49
50
    /**
51
     * Returns the view name
52
     *
53
     * @return string
54
     */
55
    public function getName()
56
    {
57
        return $this->name;
58
    }
59
60
    /**
61
     * Returns the path
62
     *
63
     * @return string
64
     */
65
    public function getPath()
66
    {
67
        return $this->path;
68
    }
69
70
    /**
71
     * Returns the cache path
72
     *
73
     * @return string
74
     */
75
    public function getCache()
76
    {
77
        return $this->cache;
78
    }
79
80
    /**
81
     * Sets the view name
82
     *
83
     * @param string
84
     * @param mixed $name
85
     *
86
     * @return null
87
     */
88
    public function setName($name)
89
    {
90
        $this->name = $name;
91
    }
92
93
    /**
94
     * Sets the path
95
     *
96
     * @param string
97
     * @param mixed $path
98
     *
99
     * @return string
100
     */
101 1
    public function setPath($path)
102
    {
103 1
        $this->path = $path;
104 1
    }
105
106
    /**
107
     * Sets the cache path
108
     *
109
     * @param string
110
     * @param mixed $cache
111
     *
112
     * @return string
113
     */
114
    public function setCache($cache)
115
    {
116
        $this->cache = $cache;
117
    }
118
119
    /**
120
     * Constructor
121
     *
122
     * @param string $name
123
     * @param array  $parameters
124
     */
125 1
    public function __construct($name, array $parameters = [])
126
    {
127 1
        $this->name  = $name;
128 1
        $this->setParams($parameters);
129 1
    }
130
131
    /**
132
     * Gets the contents of view
133
     *
134
     *
135
     * @param null|mixed $file
136
     * @throws Exception\ViewNotFoundException
137
     * @return  string
138
     */
139 1
    public function getContents($file = null)
140
    {
141 1
        $_view = $this->path . DIRECTORY_SEPARATOR .
142 1
            str_replace('.', DIRECTORY_SEPARATOR, !is_null($file) ? $file : $this->name) . '.phtml';
143
144 1
        $contents = file_get_contents($_view);
145
146 1
        if ($contents === false) {
147
            throw new Exception\ViewGetContentsErrorException("The view '" .$this->name. "' does not exists");
148
        }
149
150 1
        return $contents;
151
    }
152
153
    /**
154
     * Loads the view
155
     *
156
     * @return  null
157
     */
158
    public function render()
159
    {
160
        if (count($this->getParams())) {
161
            extract($this->getParams(), EXTR_SKIP);
162
        }
163
164
        $viewFile = $this->getFile();
165
        $view = file_get_contents($viewFile);
166
167
        if (preg_match("/@extends\('(" .self::IDENTIFIER_REGEX. ")'\)/", $view, $matches) === 1) {
168
            $extendsStm = array_shift($matches);
169
            $viewIdentifier = array_shift($matches);
170
171
            $view = str_replace($extendsStm, file_get_contents($this->getFile($viewIdentifier)), $view);
172
173
            while (preg_match("/@section\('(" .self::IDENTIFIER_REGEX. ")'\)/", $view, $sectionOpening) === 1) {
174
                $sectionStm = array_shift($sectionOpening);
175
                $sectionIdentifier = array_shift($sectionOpening);
176
                $sectionFirstIndex = strpos($view, $sectionStm);
177
178
                if (preg_match("/@endsection\('(" . $sectionIdentifier . ")'\)/", $view, $sectionEnding) !== 1) {
179
                    // no ending statment
180
                    $view = str_replace($sectionStm, '', $view);
181
                    continue;
182
                }
183
184
                $endsectionStm        = array_shift($sectionEnding);
185
                $endsectionIdentifier = array_shift($sectionEnding);
186
                $endsectionFirstIndex = strpos($view, $endsectionStm);
187
188
                $sectionContent = substr(
189
                    $view,
190
                    $sectionFirstIndex,
191
                    $endsectionFirstIndex - $sectionFirstIndex + strlen($endsectionStm)
192
                );
193
194
                $sectionContentTrimmed = str_replace($sectionStm, '', $sectionContent);
195
                $sectionContentTrimmed = str_replace($endsectionStm, '', $sectionContentTrimmed);
196
197
                $view = str_replace($sectionContent, '', $view);
198
                $view = str_replace("@yield('$sectionIdentifier')", $sectionContentTrimmed, $view);
199
            }
200
        }
201
202
        // replace no match yields
203
        while (preg_match("/@yield\('(" .self::IDENTIFIER_REGEX. ")'\)/", $view, $matches) === 1) {
204
            $view = preg_replace("/@yield\('(" .self::IDENTIFIER_REGEX. ")'\)/", '', $view);
205
        }
206
207
        $uid = uniqid() . time();
208
209
        file_put_contents($this->cache . DIRECTORY_SEPARATOR . $uid, $view);
210
        include $this->cache . DIRECTORY_SEPARATOR . $uid;
211
        unlink($this->cache . DIRECTORY_SEPARATOR . $uid);
212
    }
213
214
    /**
215
     * Gets the file path
216
     *
217
     * @param string $file
218
     *
219
     * @return  string
220
     */
221
    private function getFile($file = null)
222
    {
223
        $_view = $this->path . DIRECTORY_SEPARATOR .
224
            str_replace('.', DIRECTORY_SEPARATOR, !is_null($file) ? $file : $this->name) . '.phtml';
225
226
        if (!file_exists($_view)) {
227
            throw new Exception\ViewNotFoundException("The view '" .$this->name. "' does not exists");
228
        }
229
230
        return $_view;
231
    }
232
}
233