Completed
Push — master ( 314e3c...5adb0b )
by Cheren
05:48
created

AppView   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 7
dl 0
loc 159
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewFile() 0 14 3
A initialize() 0 6 1
A partial() 0 10 2
A render() 0 5 1
B _findViewByRequest() 0 25 5
A _getFormView() 0 8 3
A _getLayoutPartialPath() 0 18 4
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\Cms;
19
use Core\Plugin;
20
use Cake\Core\App;
21
use Cake\View\View;
22
use JBZoo\Utils\Arr;
23
use JBZoo\Utils\FS;
24
25
/**
26
 * Class AppView
27
 *
28
 * @package Core\View
29
 * @property \Core\View\Helper\AssetsHelper $Assets
30
 * @property \Core\View\Helper\DocumentHelper $Document
31
 * @property \Core\View\Helper\FormHelper $Form
32
 * @property \Core\View\Helper\UrlHelper $Url
33
 * @property \Core\View\Helper\LessHelper $Less
34
 * @property \Core\View\Helper\HtmlHelper $Html
35
 * @property \Core\View\Helper\NavHelper $Nav
36
 * @property \Core\View\Helper\FilterHelper $Filter
37
 * @property \Core\View\Helper\JsHelper $Js
38
 */
39
class AppView extends View
40
{
41
42
    const VIEW_FORM = 'form';
43
44
    /**
45
     * Hold CMS object.
46
     *
47
     * @var Cms
48
     */
49
    public $cms;
50
51
    /**
52
     * Controller form actions.
53
     *
54
     * @var array
55
     */
56
    protected $_formActions = ['edit', 'add'];
57
58
    /**
59
     * Get view file path.
60
     *
61
     * @param string $name
62
     * @return null|string
63
     */
64
    public function getViewFile($name)
65
    {
66
        list($plugin, $name) = $this->pluginSplit($name);
67
        $return = null;
68
        foreach ($this->_paths($plugin) as $path) {
69
            $viewFile = FS::clean($path . $name . $this->_ext);
70
            if (FS::isFile($viewFile)) {
71
                $return = $this->_checkFilePath($viewFile, $path);
72
                break;
73
            }
74
        }
75
76
        return $return;
77
    }
78
79
    /**
80
     * Initialization hook method.
81
     *
82
     * Properties like $helpers etc. cannot be initialized statically in your custom
83
     * view class as they are overwritten by values from controller in constructor.
84
     * So this method allows you to manipulate them as required after view instance
85
     * is constructed.
86
     *
87
     * @return void
88
     */
89
    public function initialize()
90
    {
91
        parent::initialize();
92
        $this->cms = Cms::getInstance();
93
        Plugin::manifestEvent('View.initialize', $this);
94
    }
95
96
    /**
97
     * Render layout partial.
98
     *
99
     * @param string $name
100
     * @param array $data
101
     * @return null|string
102
     */
103
    public function partial($name, array $data = [])
104
    {
105
        $file = $this->_getLayoutPartialPath($name);
106
107
        if (FS::isFile($file)) {
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->_getLayoutPartialPath($name) on line 105 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...
108
            return $this->_render($file, $data);
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by $this->_getLayoutPartialPath($name) on line 105 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...
109
        }
110
111
        return null;
112
    }
113
114
    /**
115
     * Renders view for given template file and layout.
116
     *
117
     * @param null|string $view
118
     * @param null|string $layout
119
     * @return null|string
120
     */
121
    public function render($view = null, $layout = null)
122
    {
123
        $view = $this->_getFormView($view);
124
        return parent::render($view, $layout);
125
    }
126
127
    /**
128
     * Find form view by request.
129
     *
130
     * @return string|null
131
     */
132
    protected function _findViewByRequest()
133
    {
134
        $paths = App::path('Template', $this->plugin);
135
136
        $action      = (string) $this->request->getParam('action');
137
        $controller  = $this->request->getParam('controller');
138
        $viewFile    = $action . $this->_ext;
139
        $viewSubPath = $this->_getSubPaths($controller);
140
141
        foreach ($paths as $path) {
142
            foreach ($viewSubPath as $subPath) {
143
                $full = $path . $subPath . DS . $viewFile;
144
                if (FS::isFile($full)) {
145
                    return $action;
146
                }
147
148
                $formView = $path . $subPath . DS . self::VIEW_FORM . $this->_ext;
149
                if (FS::isFile($formView)) {
150
                    return self::VIEW_FORM;
151
                }
152
            }
153
        }
154
155
        return null;
156
    }
157
158
    /**
159
     * Get current form view.
160
     *
161
     * @param null|string $view
162
     * @return null
163
     */
164
    protected function _getFormView($view = null)
165
    {
166
        if (empty($view) && Arr::in($this->request->getParam('action'), $this->_formActions)) {
167
            $view = $this->_findViewByRequest();
168
        }
169
170
        return $view;
171
    }
172
173
    /**
174
     * Finds an partial filename, returns false on failure.
175
     *
176
     * @param string $name
177
     * @return bool|string
178
     */
179
    protected function _getLayoutPartialPath($name)
180
    {
181
        list($plugin, $name) = $this->pluginSplit($name);
182
183
        $paths       = $this->_paths($plugin);
184
        $layoutPaths = $this->_getSubPaths('Layout' . DS . 'Partial');
185
186
        foreach ($paths as $path) {
187
            foreach ($layoutPaths as $layoutPath) {
188
                $partial = $path . $layoutPath . DS . $name . $this->_ext;
189
                if (FS::isFile($partial)) {
190
                    return $partial;
191
                }
192
            }
193
        }
194
195
        return false;
196
    }
197
}
198