You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.5 KiB
123 lines
3.5 KiB
12 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
|
namespace yii\debug;
|
||
|
|
||
12 years ago
|
use Yii;
|
||
11 years ago
|
use yii\base\Application;
|
||
12 years ago
|
use yii\base\View;
|
||
12 years ago
|
use yii\web\HttpException;
|
||
12 years ago
|
|
||
12 years ago
|
/**
|
||
11 years ago
|
* The Yii Debug Module provides the debug toolbar and debugger
|
||
|
*
|
||
12 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
|
* @since 2.0
|
||
|
*/
|
||
|
class Module extends \yii\base\Module
|
||
|
{
|
||
12 years ago
|
/**
|
||
|
* @var array the list of IPs that are allowed to access this module.
|
||
|
* Each array element represents a single IP filter which can be either an IP address
|
||
|
* or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
|
||
11 years ago
|
* The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
|
||
12 years ago
|
* by localhost.
|
||
|
*/
|
||
11 years ago
|
public $allowedIPs = ['127.0.0.1', '::1'];
|
||
12 years ago
|
/**
|
||
|
* @var string the namespace that controller classes are in.
|
||
|
*/
|
||
12 years ago
|
public $controllerNamespace = 'yii\debug\controllers';
|
||
12 years ago
|
/**
|
||
12 years ago
|
* @var LogTarget
|
||
|
*/
|
||
|
public $logTarget;
|
||
|
/**
|
||
12 years ago
|
* @var array|Panel[]
|
||
|
*/
|
||
11 years ago
|
public $panels = [];
|
||
12 years ago
|
/**
|
||
|
* @var string the directory storing the debugger data files. This can be specified using a path alias.
|
||
|
*/
|
||
|
public $dataPath = '@runtime/debug';
|
||
12 years ago
|
/**
|
||
|
* @var integer the maximum number of debug data files to keep. If there are more files generated,
|
||
|
* the oldest ones will be removed.
|
||
|
*/
|
||
12 years ago
|
public $historySize = 50;
|
||
12 years ago
|
|
||
12 years ago
|
|
||
|
public function init()
|
||
|
{
|
||
|
parent::init();
|
||
12 years ago
|
$this->dataPath = Yii::getAlias($this->dataPath);
|
||
12 years ago
|
$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
|
||
11 years ago
|
// do not initialize view component before application is ready (needed when debug in preload)
|
||
11 years ago
|
Yii::$app->on(Application::EVENT_BEFORE_ACTION, function() {
|
||
11 years ago
|
Yii::$app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
|
||
11 years ago
|
});
|
||
12 years ago
|
|
||
12 years ago
|
foreach (array_merge($this->corePanels(), $this->panels) as $id => $config) {
|
||
12 years ago
|
$config['module'] = $this;
|
||
12 years ago
|
$config['id'] = $id;
|
||
12 years ago
|
$this->panels[$id] = Yii::createObject($config);
|
||
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
public function beforeAction($action)
|
||
|
{
|
||
11 years ago
|
Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']);
|
||
12 years ago
|
unset(Yii::$app->getLog()->targets['debug']);
|
||
12 years ago
|
$this->logTarget = null;
|
||
12 years ago
|
|
||
12 years ago
|
if ($this->checkAccess($action)) {
|
||
12 years ago
|
return parent::beforeAction($action);
|
||
12 years ago
|
} elseif ($action->id === 'toolbar') {
|
||
|
return false;
|
||
|
} else {
|
||
|
throw new HttpException(403, 'You are not allowed to access this page.');
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
|
public function renderToolbar($event)
|
||
|
{
|
||
12 years ago
|
if (!$this->checkAccess()) {
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
$url = Yii::$app->getUrlManager()->createUrl($this->id . '/default/toolbar', [
|
||
12 years ago
|
'tag' => $this->logTarget->tag,
|
||
11 years ago
|
]);
|
||
12 years ago
|
echo '<div id="yii-debug-toolbar" data-url="' . $url . '" style="display:none"></div>';
|
||
12 years ago
|
/** @var View $view */
|
||
|
$view = $event->sender;
|
||
12 years ago
|
echo '<style>' . $view->renderPhpFile(__DIR__ . '/views/default/toolbar.css') . '</style>';
|
||
|
echo '<script>' . $view->renderPhpFile(__DIR__ . '/views/default/toolbar.js') . '</script>';
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
protected function checkAccess()
|
||
|
{
|
||
|
$ip = Yii::$app->getRequest()->getUserIP();
|
||
|
foreach ($this->allowedIPs as $filter) {
|
||
|
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
12 years ago
|
protected function corePanels()
|
||
|
{
|
||
11 years ago
|
return [
|
||
|
'config' => ['class' => 'yii\debug\panels\ConfigPanel'],
|
||
|
'request' => ['class' => 'yii\debug\panels\RequestPanel'],
|
||
|
'log' => ['class' => 'yii\debug\panels\LogPanel'],
|
||
|
'profiling' => ['class' => 'yii\debug\panels\ProfilingPanel'],
|
||
|
'db' => ['class' => 'yii\debug\panels\DbPanel'],
|
||
|
];
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|