Completed
Push — master ( 02a364...d559ac )
by Cheren
04:35
created

DocumentHelper::beforeRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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\Helper;
17
18
use Core\Plugin;
19
use JBZoo\Utils\Str;
20
use Cake\Event\Event;
21
use Cake\Core\Configure;
22
23
/**
24
 * Class DocumentHelper
25
 *
26
 * @package Core\View\Helper
27
 * @property \Core\View\Helper\HtmlHelper $Html
28
 * @property \Core\View\Helper\AssetsHelper $Assets
29
 */
30
class DocumentHelper extends AppHelper
31
{
32
33
    /**
34
     * Init vars.
35
     *
36
     * @var string
37
     */
38
    public $dir;
39
    public $eol;
40
    public $tab;
41
    public $locale;
42
    public $charset;
43
44
    /**
45
     * Uses helpers.
46
     *
47
     * @var array
48
     */
49
    public $helpers = [
50
        'Core.Html',
51
        'Core.Assets',
52
    ];
53
54
    /**
55
     * Constructor hook method.
56
     *
57
     * @param array $config
58
     */
59
    public function initialize(array $config)
60
    {
61
        parent::initialize($config);
62
63
        $this->dir     = Configure::read('Cms.docDir');
64
        $this->locale  = Configure::read('App.defaultLocale');
65
        $this->charset = Str::low(Configure::read('App.encoding'));
66
        $this->eol     = (Configure::read('debug')) ? PHP_EOL : '';
67
        $this->tab     = (Configure::read('debug')) ? Configure::read('Cms.lineTab') : '';
68
    }
69
70
    /**
71
     * Create head for layout.
72
     *
73
     * @return string
74
     */
75
    public function head()
76
    {
77
        $output = [
78
            'meta' => $this->_View->fetch('meta'),
79
            'assets' => $this->assets('css'),
80
            'fetch_css' => $this->_View->fetch('css'),
81
            'fetch_css_bottom' => $this->_View->fetch('css_bottom'),
82
        ];
83
84
        return implode('', $output);
85
    }
86
87
    /**
88
     * Get assets fot layout render.
89
     *
90
     * @param string $type
91
     * @return string
92
     */
93
    public function assets($type = 'css')
94
    {
95
        $output = [];
96
        $assets = $this->Assets->getAssets($type);
97
        foreach ($assets as $asset) {
0 ignored issues
show
Bug introduced by
The expression $assets of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
98
            $output[] = $asset['output'];
99
        }
100
101
        return implode($this->eol, $output);
102
    }
103
104
    /**
105
     * Site language.
106
     *
107
     * @param bool|true $isLang
108
     * @return string
109
     * @throws \Exception
110
     */
111
    public function lang($isLang = true)
112
    {
113
        list($lang, $region) = explode('_', $this->locale);
114
        return ($isLang) ? Str::low($lang) : Str::low($region);
115
    }
116
117
    /**
118
     * Creates a link to an external resource and handles basic meta tags.
119
     *
120
     * @param array $rows
121
     * @param null $block
122
     * @return null|string
123
     */
124
    public function meta(array $rows, $block = null)
125
    {
126
        $output = [];
127
        foreach ($rows as $row) {
128
            $output[] = trim($row);
129
        }
130
131
        $output = implode($this->eol, $output) . $this->eol;
132
133
        if ($block !== null) {
134
            $this->_View->append($block, $output);
135
            return null;
136
        }
137
138
        return $output;
139
    }
140
141
    /**
142
     * Create html 5 document type.
143
     *
144
     * @return string
145
     * @throws \Exception
146
     */
147
    public function type()
148
    {
149
        $lang = $this->lang();
150
        $html = [
151
            '<!doctype html>',
152
            '<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7 ie6" '
153
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
154
            '<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" '
155
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
156
            '<!--[if IE 8]><html class="no-js lt-ie9 ie8" '
157
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
158
            '<!--[if gt IE 8]><!--><html class="no-js" xmlns="http://www.w3.org/1999/xhtml" '
159
            . 'lang="' . $lang . '" dir="' . $this->dir . '" '
160
            . 'prefix="og: http://ogp.me/ns#" '
161
            . '> <!--<![endif]-->',
162
        ];
163
        return implode($this->eol, $html) . $this->eol;
164
    }
165
166
    /**
167
     * Is called before each view file is rendered. This includes elements, views, parent views and layouts.
168
     *
169
     * @param Event $event
170
     * @param string $viewFile
171
     * @return void
172
     */
173
    public function beforeRenderFile(Event $event, $viewFile)
174
    {
175
        Plugin::manifestEvent('View.beforeRenderFile', $this->_View, $event, $viewFile);
176
    }
177
178
    /**
179
     * Is called after each view file is rendered. This includes elements, views, parent views and layouts.
180
     * A callback can modify and return $content to change how the rendered content will be displayed in the browser.
181
     *
182
     * @param Event $event
183
     * @param string $viewFile
184
     * @param string $content
185
     * @return void
186
     */
187
    public function afterRenderFile(Event $event, $viewFile, $content)
188
    {
189
        Plugin::manifestEvent('View.afterRenderFile', $this->_View, $event, $viewFile, $content);
190
    }
191
192
    /**
193
     * Is called after the controller’s beforeRender method but before the controller renders view and layout.
194
     * Receives the file being rendered as an argument.
195
     *
196
     * @param Event $event
197
     * @param string $viewFile
198
     * @return void
199
     */
200
    public function beforeRender(Event $event, $viewFile)
201
    {
202
        $this->Assets->loadPluginAssets();
203
        Plugin::manifestEvent('View.beforeRender', $this->_View, $event, $viewFile);
204
    }
205
206
    /**
207
     * Is called after the view has been rendered but before layout rendering has started.
208
     *
209
     * @param Event $event
210
     * @param string $viewFile
211
     * @return void
212
     */
213
    public function afterRender(Event $event, $viewFile)
214
    {
215
        $this->_setupMetaData();
216
        Plugin::manifestEvent('View.afterRender', $this->_View, $event, $viewFile);
217
    }
218
219
    /**
220
     * Is called before layout rendering starts. Receives the layout filename as an argument.
221
     *
222
     * @param Event $event
223
     * @param string $layoutFile
224
     * @return void
225
     */
226
    public function beforeLayout(Event $event, $layoutFile)
227
    {
228
        Plugin::manifestEvent('View.beforeLayout', $this->_View, $event, $layoutFile);
229
    }
230
231
    /**
232
     * Is called after layout rendering is complete. Receives the layout filename as an argument.
233
     *
234
     * @param Event $event
235
     * @param string $layoutFile
236
     * @return void
237
     */
238
    public function afterLayout(Event $event, $layoutFile)
239
    {
240
        Plugin::manifestEvent('View.beforeLayout', $this->_View, $event, $layoutFile);
241
    }
242
243
    /**
244
     * Setup view meta data.
245
     *
246
     * @return void
247
     */
248
    protected function _setupMetaData()
249
    {
250
        $this->_assignMeta('page_title')
251
            ->_assignMeta('meta_keywords')
252
            ->_assignMeta('meta_description');
253
    }
254
255
    /**
256
     * Assign data from view vars.
257
     *
258
     * @param string $key
259
     * @return $this
260
     */
261
    protected function _assignMeta($key)
262
    {
263
        if (isset($this->_View->viewVars[$key])) {
264
            $this->_View->assign($key, $this->_View->viewVars[$key]);
265
        }
266
267
        return $this;
268
    }
269
}
270