Completed
Push — master ( 4dd6a7...a4332f )
by Cheren
02:10
created

DocumentHelper::getBodyClasses()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 4
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\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
     * Get body classes by view data.
72
     *
73
     * @return string
74
     */
75
    public function getBodyClasses()
76
    {
77
        $prefix = ($this->request->param('prefix')) ? 'prefix-' . $this->request->param('prefix') : 'prefix-site';
78
79
        $classes = [
80
            $prefix,
81
            'theme-'    . Str::low($this->_View->theme),
82
            'plugin-'   . Str::low($this->_View->plugin),
83
            'view-'     . Str::low($this->_View->name),
84
            'tmpl-'     . Str::low($this->_View->template),
85
            'layout-'   . Str::low($this->_View->layout),
86
        ];
87
88
        $pass = (array) $this->request->param('pass');
89
        if (count($pass)) {
90
            $classes[] = 'item-id-' . array_shift($pass);
91
        }
92
93
        return implode(' ', $classes);
94
    }
95
96
    /**
97
     * Create head for layout.
98
     *
99
     * @return string
100
     */
101
    public function head()
102
    {
103
        $output = [
104
            'meta' => $this->_View->fetch('meta'),
105
            'assets' => $this->assets('css'),
106
            'fetch_css' => $this->_View->fetch('css'),
107
            'fetch_css_bottom' => $this->_View->fetch('css_bottom'),
108
        ];
109
110
        return implode('', $output);
111
    }
112
113
    /**
114
     * Get assets fot layout render.
115
     *
116
     * @param string $type
117
     * @return string
118
     */
119
    public function assets($type = 'css')
120
    {
121
        $output = [];
122
        $assets = $this->Assets->getAssets($type);
123
        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...
124
            $output[] = $asset['output'];
125
        }
126
127
        return implode($this->eol, $output);
128
    }
129
130
    /**
131
     * Site language.
132
     *
133
     * @param bool|true $isLang
134
     * @return string
135
     * @throws \Exception
136
     */
137
    public function lang($isLang = true)
138
    {
139
        list($lang, $region) = explode('_', $this->locale);
140
        return ($isLang) ? Str::low($lang) : Str::low($region);
141
    }
142
143
    /**
144
     * Creates a link to an external resource and handles basic meta tags.
145
     *
146
     * @param array $rows
147
     * @param null $block
148
     * @return null|string
149
     */
150
    public function meta(array $rows, $block = null)
151
    {
152
        $output = [];
153
        foreach ($rows as $row) {
154
            $output[] = trim($row);
155
        }
156
157
        $output = implode($this->eol, $output) . $this->eol;
158
159
        if ($block !== null) {
160
            $this->_View->append($block, $output);
161
            return null;
162
        }
163
164
        return $output;
165
    }
166
167
    /**
168
     * Create html 5 document type.
169
     *
170
     * @return string
171
     * @throws \Exception
172
     */
173
    public function type()
174
    {
175
        $lang = $this->lang();
176
        $html = [
177
            '<!doctype html>',
178
            '<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7 ie6" '
179
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
180
            '<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8 ie7" '
181
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
182
            '<!--[if IE 8]><html class="no-js lt-ie9 ie8" '
183
            . 'lang="' . $lang . '" dir="' . $this->dir . '"> <![endif]-->',
184
            '<!--[if gt IE 8]><!--><html class="no-js" xmlns="http://www.w3.org/1999/xhtml" '
185
            . 'lang="' . $lang . '" dir="' . $this->dir . '" '
186
            . 'prefix="og: http://ogp.me/ns#" '
187
            . '> <!--<![endif]-->',
188
        ];
189
        return implode($this->eol, $html) . $this->eol;
190
    }
191
192
    /**
193
     * Is called before each view file is rendered. This includes elements, views, parent views and layouts.
194
     *
195
     * @param Event $event
196
     * @param string $viewFile
197
     * @return void
198
     */
199
    public function beforeRenderFile(Event $event, $viewFile)
200
    {
201
        Plugin::manifestEvent('View.beforeRenderFile', $this->_View, $event, $viewFile);
202
    }
203
204
    /**
205
     * Is called after each view file is rendered. This includes elements, views, parent views and layouts.
206
     * A callback can modify and return $content to change how the rendered content will be displayed in the browser.
207
     *
208
     * @param Event $event
209
     * @param string $viewFile
210
     * @param string $content
211
     * @return void
212
     */
213
    public function afterRenderFile(Event $event, $viewFile, $content)
214
    {
215
        Plugin::manifestEvent('View.afterRenderFile', $this->_View, $event, $viewFile, $content);
216
    }
217
218
    /**
219
     * Is called after the controller’s beforeRender method but before the controller renders view and layout.
220
     * Receives the file being rendered as an argument.
221
     *
222
     * @param Event $event
223
     * @param string $viewFile
224
     * @return void
225
     */
226
    public function beforeRender(Event $event, $viewFile)
227
    {
228
        $this->Assets->loadPluginAssets();
229
        Plugin::manifestEvent('View.beforeRender', $this->_View, $event, $viewFile);
230
    }
231
232
    /**
233
     * Is called after the view has been rendered but before layout rendering has started.
234
     *
235
     * @param Event $event
236
     * @param string $viewFile
237
     * @return void
238
     */
239
    public function afterRender(Event $event, $viewFile)
240
    {
241
        $this->_setupMetaData();
242
        Plugin::manifestEvent('View.afterRender', $this->_View, $event, $viewFile);
243
    }
244
245
    /**
246
     * Is called before layout rendering starts. Receives the layout filename as an argument.
247
     *
248
     * @param Event $event
249
     * @param string $layoutFile
250
     * @return void
251
     */
252
    public function beforeLayout(Event $event, $layoutFile)
253
    {
254
        Plugin::manifestEvent('View.beforeLayout', $this->_View, $event, $layoutFile);
255
    }
256
257
    /**
258
     * Is called after layout rendering is complete. Receives the layout filename as an argument.
259
     *
260
     * @param Event $event
261
     * @param string $layoutFile
262
     * @return void
263
     */
264
    public function afterLayout(Event $event, $layoutFile)
265
    {
266
        Plugin::manifestEvent('View.beforeLayout', $this->_View, $event, $layoutFile);
267
    }
268
269
    /**
270
     * Setup view meta data.
271
     *
272
     * @return void
273
     */
274
    protected function _setupMetaData()
275
    {
276
        $this->_assignMeta('page_title')
277
            ->_assignMeta('meta_keywords')
278
            ->_assignMeta('meta_description');
279
    }
280
281
    /**
282
     * Assign data from view vars.
283
     *
284
     * @param string $key
285
     * @return $this
286
     */
287
    protected function _assignMeta($key)
288
    {
289
        if (isset($this->_View->viewVars[$key])) {
290
            $this->_View->assign($key, $this->_View->viewVars[$key]);
291
        }
292
293
        return $this;
294
    }
295
}
296