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.

109 lines
2.9 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\debug;
11 years ago
use Yii;
use yii\base\View;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Module extends \yii\base\Module
{
11 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.
* The default value is `array('127.0.0.1', '::1')`, which means the module can only be accessed
* by localhost.
*/
public $allowedIPs = array('127.0.0.1', '::1');
public $controllerNamespace = 'yii\debug\controllers';
11 years ago
/**
11 years ago
* @var LogTarget
*/
public $logTarget;
/**
11 years ago
* @var array|Panel[]
*/
public $panels = array();
11 years ago
/**
* @var string the directory storing the debugger data files. This can be specified using a path alias.
*/
public $dataPath = '@runtime/debug';
11 years ago
public $historySize = 50;
public $enabled = true;
11 years ago
public function init()
{
parent::init();
if (!$this->enabled) {
return;
}
11 years ago
$this->dataPath = Yii::getAlias($this->dataPath);
11 years ago
$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));
11 years ago
foreach (array_merge($this->corePanels(), $this->panels) as $id => $config) {
11 years ago
$config['module'] = $this;
$config['id'] = $id;
11 years ago
$this->panels[$id] = Yii::createObject($config);
}
11 years ago
}
public function beforeAction($action)
{
Yii::$app->getView()->off(View::EVENT_END_BODY, array($this, 'renderToolbar'));
11 years ago
unset(Yii::$app->getLog()->targets['debug']);
11 years ago
$this->logTarget = null;
11 years ago
$ip = Yii::$app->getRequest()->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return parent::beforeAction($action);
}
}
return false;
11 years ago
}
public function renderToolbar($event)
{
$url = Yii::$app->getUrlManager()->createUrl($this->id . '/default/toolbar', array(
'tag' => $this->logTarget->tag,
11 years ago
));
echo '<div id="yii-debug-toolbar" data-url="' . $url . '" style="display:none"></div>';
/** @var View $view */
$view = $event->sender;
echo '<style>' . $view->renderPhpFile(__DIR__ . '/views/default/toolbar.css') . '</style>';
echo '<script>' . $view->renderPhpFile(__DIR__ . '/views/default/toolbar.js') . '</script>';
11 years ago
}
11 years ago
protected function corePanels()
{
return array(
11 years ago
'config' => array(
'class' => 'yii\debug\panels\ConfigPanel',
),
11 years ago
'request' => array(
'class' => 'yii\debug\panels\RequestPanel',
),
'log' => array(
'class' => 'yii\debug\panels\LogPanel',
),
11 years ago
'profiling' => array(
'class' => 'yii\debug\panels\ProfilingPanel',
),
'db' => array(
'class' => 'yii\debug\panels\DbPanel',
),
11 years ago
);
}
11 years ago
}