Completed
Push — master ( ff2953...ed4ef2 )
by Cheren
02:18
created

AppView::partial()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Core
10
 * @license   MIT
11
 * @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link      https://github.com/CakeCMS/Core".
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\View;
17
18
use Core\Plugin;
19
use Cake\View\View;
20
use JBZoo\Utils\FS;
21
use Cake\Event\Event;
22
23
/**
24
 * Class AppView
25
 *
26
 * @package Core\View
27
 * @property \Core\View\Helper\AssetsHelper $Assets
28
 */
29
class AppView extends View
30
{
31
32
    /**
33
     * Initialization hook method.
34
     *
35
     * Properties like $helpers etc. cannot be initialized statically in your custom
36
     * view class as they are overwritten by values from controller in constructor.
37
     * So this method allows you to manipulate them as required after view instance
38
     * is constructed.
39
     *
40
     * @return void
41
     */
42
    public function initialize()
43
    {
44
        parent::initialize();
45
        Plugin::manifestEvent('View.initialize', $this);
46
    }
47
48
    /**
49
     * Is called before each view file is rendered. This includes elements, views, parent views and layouts.
50
     *
51
     * @param Event $event
52
     * @param string $viewFile
53
     * @return void
54
     */
55
    public function beforeRenderFile(Event $event, $viewFile)
56
    {
57
        Plugin::manifestEvent('View.beforeRenderFile', $this, $event, $viewFile);
58
    }
59
60
    /**
61
     * Is called after each view file is rendered. This includes elements, views, parent views and layouts.
62
     * A callback can modify and return $content to change how the rendered content will be displayed in the browser.
63
     *
64
     * @param Event $event
65
     * @param string $viewFile
66
     * @param string $content
67
     * @return void
68
     */
69
    public function afterRenderFile(Event $event, $viewFile, $content)
70
    {
71
        Plugin::manifestEvent('View.afterRenderFile', $this, $event, $viewFile, $content);
72
    }
73
74
    /**
75
     * Is called after the controller’s beforeRender method but before the controller renders view and layout.
76
     * Receives the file being rendered as an argument.
77
     *
78
     * @param Event $event
79
     * @param string $viewFile
80
     * @return void
81
     */
82
    public function beforeRender(Event $event, $viewFile)
83
    {
84
        Plugin::manifestEvent('View.beforeRender', $this, $event, $viewFile);
85
    }
86
87
    /**
88
     * Is called after the view has been rendered but before layout rendering has started.
89
     *
90
     * @param Event $event
91
     * @param string $viewFile
92
     * @return void
93
     */
94
    public function afterRender(Event $event, $viewFile)
95
    {
96
        Plugin::manifestEvent('View.afterRender', $this, $event, $viewFile);
97
    }
98
99
    /**
100
     * Is called before layout rendering starts. Receives the layout filename as an argument.
101
     *
102
     * @param Event $event
103
     * @param string $layoutFile
104
     * @return void
105
     */
106
    public function beforeLayout(Event $event, $layoutFile)
107
    {
108
        Plugin::manifestEvent('View.beforeLayout', $this, $event, $layoutFile);
109
    }
110
111
    /**
112
     * Is called after layout rendering is complete. Receives the layout filename as an argument.
113
     *
114
     * @param Event $event
115
     * @param string $layoutFile
116
     * @return void
117
     */
118
    public function afterLayout(Event $event, $layoutFile)
119
    {
120
        Plugin::manifestEvent('View.beforeLayout', $this, $event, $layoutFile);
121
    }
122
123
    /**
124
     * Render layout partial.
125
     *
126
     * @param string $name
127
     * @param array $data
128
     * @return null|string
129
     */
130
    public function partial($name, array $data = [])
131
    {
132
        $file = $this->_getLayoutPartialPath($name);
133
134
        if (FS::isFile($file)) {
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->_getLayoutPartialPath($name) on line 132 can also be of type false; however, JBZoo\Utils\FS::isFile() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
135
            return $this->_render($file, $data);
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->_getLayoutPartialPath($name) on line 132 can also be of type false; however, Cake\View\View::_render() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * Finds an partial filename, returns false on failure.
143
     *
144
     * @param string $name
145
     * @return bool|string
146
     */
147
    protected function _getLayoutPartialPath($name)
148
    {
149
        list($plugin, $name) = $this->pluginSplit($name);
150
151
        $paths       = $this->_paths($plugin);
152
        $layoutPaths = $this->_getSubPaths('Layout' . DS . 'Partial');
153
154
        foreach ($paths as $path) {
155
            foreach ($layoutPaths as $layoutPath) {
156
                $partial = $path . $layoutPath . DS . $name . $this->_ext;
157
                if (FS::isFile($partial)) {
158
                    return $partial;
159
                }
160
            }
161
        }
162
163
        return false;
164
    }
165
}
166