Completed
Push — master ( dd5665...b0b2c3 )
by Cheren
02:12
created

AppView::_findViewByRequest()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 0
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\Core\App;
20
use Cake\View\View;
21
use JBZoo\Utils\FS;
22
23
/**
24
 * Class AppView
25
 *
26
 * @package Core\View
27
 * @property \Core\View\Helper\AssetsHelper $Assets
28
 * @property \Core\View\Helper\DocumentHelper $Document
29
 * @property \Core\View\Helper\FormHelper $Form
30
 * @property \Core\View\Helper\UrlHelper $Url
31
 * @property \Core\View\Helper\LessHelper $Less
32
 * @property \Core\View\Helper\HtmlHelper $Html
33
 * @property \Core\View\Helper\NavHelper $Nav
34
 * @property \Core\View\Helper\FilterHelper $Filter
35
 */
36
class AppView extends View
37
{
38
39
    const VIEW_FORM = 'form';
40
41
    /**
42
     * Controller form actions.
43
     *
44
     * @var array
45
     */
46
    protected $_formActions = ['edit', 'add'];
47
48
    /**
49
     * Initialization hook method.
50
     *
51
     * Properties like $helpers etc. cannot be initialized statically in your custom
52
     * view class as they are overwritten by values from controller in constructor.
53
     * So this method allows you to manipulate them as required after view instance
54
     * is constructed.
55
     *
56
     * @return void
57
     */
58
    public function initialize()
59
    {
60
        parent::initialize();
61
        Plugin::manifestEvent('View.initialize', $this);
62
    }
63
64
    /**
65
     * Render layout partial.
66
     *
67
     * @param string $name
68
     * @param array $data
69
     * @return null|string
70
     */
71
    public function partial($name, array $data = [])
72
    {
73
        $file = $this->_getLayoutPartialPath($name);
74
75
        if (FS::isFile($file)) {
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->_getLayoutPartialPath($name) on line 73 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...
76
            return $this->_render($file, $data);
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->_getLayoutPartialPath($name) on line 73 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...
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * Renders view for given template file and layout.
84
     *
85
     * @param null|string $view
86
     * @param null|string $layout
87
     * @return null|string
88
     */
89
    public function render($view = null, $layout = null)
90
    {
91
        $view = $this->_getFormView($view);
92
        return parent::render($view, $layout);
93
    }
94
95
    /**
96
     * Find form view by request.
97
     *
98
     * @return string|null
99
     */
100
    protected function _findViewByRequest()
101
    {
102
        $paths = App::path('Template', $this->plugin);
103
104
        $action      = (string) $this->request->param('action');
105
        $controller  = $this->request->param('controller');
106
        $viewFile    = $action . $this->_ext;
107
        $viewSubPath = $this->_getSubPaths($controller);
108
109
        foreach ($paths as $path) {
110
            foreach ($viewSubPath as $subPath) {
111
                $full = $path . $subPath . DS . $viewFile;
112
                if (FS::isFile($full)) {
113
                    return $action;
114
                }
115
116
                $formView = $path . $subPath . DS . self::VIEW_FORM . $this->_ext;
117
                if (FS::isFile($formView)) {
118
                    return self::VIEW_FORM;
119
                }
120
            }
121
        }
122
123
        return null;
124
    }
125
126
    /**
127
     * Get current form view.
128
     *
129
     * @param null|string $view
130
     * @return null
131
     */
132
    protected function _getFormView($view = null)
133
    {
134
        if (is_null($view) && in_array($this->request->param('action'), $this->_formActions)) {
135
            $view = $this->_findViewByRequest();
136
        }
137
138
        return $view;
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