Completed
Push — master ( 114577...9e4c6d )
by Klochok
05:40
created

RenderController::renderTwig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hiqdev\yii2\modules\pages\controllers;
4
5
use hiqdev\yii2\modules\pages\models\AbstractPage;
6
use hiqdev\yii2\modules\pages\models\PagesIndex;
7
use Yii;
8
use yii\helpers\Html;
9
use yii\web\NotFoundHttpException;
10
use yii\base\InvalidConfigException;
11
12
class RenderController extends \yii\web\Controller
13
{
14
    public function getViewPath()
15
    {
16
        return Yii::$app->getViewPath();
17
    }
18
19
    /**
20
     * Index action.
21
     * @param string $page
22
     * @return string rendered page
23
     */
24
    public function actionIndex($page = null)
25
    {
26
        if (!$page) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $page of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
27
            $page = 'index';
28
        }
29
30
        $path = $this->module->find($page);
31
32
        if ($path === null) {
33
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
34
        }
35
36
        $meta = $this->module->getMetadata($page);
37
38
        if ($meta['type'] === 'dir') {
39
            $index = PagesIndex::createFromDir($path);
40
41
            return $this->render('@hiqdev/com/views/site/index', ['dataProvider' => $index->getDataProvider()]);
42
        } else {
43
            $page = AbstractPage::createFromFile($path);
44
45
            return $this->renderPage($page);
46
        }
47
    }
48
49
    public function renderPhp($path)
50
    {
51
        $content = $this->renderFile($this->module->localPath($path));
52
        return $this->renderContent($content);
53
    }
54
55
    public function renderPage($page, array $params = [])
56
    {
57
        if ($page->layout) {
58
            $this->layout = $page->layout;
59
        }
60
61
        if ($page->title) {
62
            $this->view->title = Html::encode($page->title);
63
        }
64
65
        $this->view->params = $page->getData();
66
67
        return $this->renderContent($page->render($params));
68
    }
69
70
}
71