Completed
Branch master (a1edd4)
by
unknown
54:09
created

Module   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getViewPath() 0 4 1
B bootstrap() 0 31 3
B renderToolbar() 0 28 4
1
<?php
2
/**
3
 * This file is part of the fangface/yii2-concord package
4
 *
5
 * For the full copyright and license information, please view
6
 * the file LICENSE.md that was distributed with this source code.
7
 *
8
 * @package fangface/yii2-concord
9
 * @author Fangface <[email protected]>
10
 * @copyright Copyright (c) 2014 Fangface <[email protected]>
11
 * @license https://github.com/fangface/yii2-concord/blob/master/LICENSE.md MIT License
12
 *
13
 */
14
15
namespace fangface\debug;
16
17
use fangface\web\View;
18
use Yii;
19
use yii\base\Application;
20
use yii\base\BootstrapInterface;
21
use yii\base\ViewContextInterface;
22
use yii\debug\LogTarget;
23
use yii\helpers\Html;
24
use yii\helpers\Url;
25
26
/**
27
 * Extends Yii Debug Module to suppoer ajax debug requests updating the debug toolbar
28
 */
29
class Module extends \yii\debug\Module implements BootstrapInterface, ViewContextInterface
30
{
31
    /**
32
     * Override view path to point back to yii2-debug
33
     * {@inheritDoc}
34
     * @see \yii\base\Module::getViewPath()
35
     */
36
    public function getViewPath()
37
    {
38
        return Yii::getAlias(Yii::getAlias('@base') . '/vendor/yiisoft/yii2-debug/views');
0 ignored issues
show
Bug Compatibility introduced by
The expression \Yii::getAlias(\Yii::get...oft/yii2-debug/views'); of type string|boolean adds the type boolean to the return on line 38 which is incompatible with the return type declared by the interface yii\base\ViewContextInterface::getViewPath of type string.
Loading history...
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function bootstrap($app)
45
    {
46
        $this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
47
48
        if (Yii::$app->getRequest()->getIsAjax()) {
49
            if (YII_DEBUG_AJAX) {
50
                // delay attaching event handler to the view component after it is fully configured
51
                $app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
52
                    $app->getView()->on(View::EVENT_END_AJAX_DEBUG, [$this, 'renderToolbar']);
53
                });
54
            }
55
        } else {
56
            // delay attaching event handler to the view component after it is fully configured
57
            $app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
58
                $app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
59
            });
60
        }
61
62
        $app->getUrlManager()->addRules([
63
            [
64
                'class' => 'yii\web\UrlRule',
65
                'route' => $this->id,
66
                'pattern' => $this->id,
67
            ],
68
            [
69
                'class' => 'yii\web\UrlRule',
70
                'route' => $this->id . '/<controller>/<action>',
71
                'pattern' => $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>',
72
            ]
73
        ], false);
74
    }
75
76
    /**
77
     * Renders mini-toolbar at the end of page body.
78
     *
79
     * @param \yii\base\Event $event
80
     */
81
    public function renderToolbar($event)
82
    {
83
        if (!$this->checkAccess()) {
84
            return;
85
        }
86
87
        $url = Url::toRoute(['/' . $this->id . '/default/toolbar',
88
            'tag' => $this->logTarget->tag,
89
        ]);
90
91
        if (!Yii::$app->getRequest()->getIsAjax()) {
92
            echo '<div id="yii-debug-toolbar-wrapper">';
93
        }
94
95
        echo '<div id="yii-debug-toolbar" data-url="' . Html::encode($url) . '" style="display:none" class="yii-debug-toolbar-bottom"></div>';
96
97
        /* @var $view View */
98
        $view = $event->sender;
99
100
        // echo is used in order to support cases where asset manager is not available
101
        echo '<style>' . $view->renderPhpFile(Yii::getAlias('@base/vendor/yiisoft/yii2-debug/assets/toolbar.css')) . '</style>';
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias('@base/ve...ug/assets/toolbar.css') targeting yii\BaseYii::getAlias() can also be of type boolean; however, yii\base\View::renderPhpFile() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
102
        echo '<script>' . $view->renderPhpFile(Yii::getAlias('@base/vendor/yiisoft/yii2-debug/assets/toolbar.js')) . '</script>';
0 ignored issues
show
Bug introduced by
It seems like \Yii::getAlias('@base/ve...bug/assets/toolbar.js') targeting yii\BaseYii::getAlias() can also be of type boolean; however, yii\base\View::renderPhpFile() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
104
        if (!Yii::$app->getRequest()->getIsAjax()) {
105
            echo '</div>';
106
        }
107
108
    }
109
110
}
111