Module.php 2.82 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\debug;

Qiang Xue committed
10 11 12 13
use Yii;
use yii\base\View;
use yii\helpers\Html;

Qiang Xue committed
14 15 16 17 18 19
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Module extends \yii\base\Module
{
Qiang Xue committed
20 21 22 23 24 25 26 27 28
	/**
	 * @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');

Qiang Xue committed
29
	public $controllerNamespace = 'yii\debug\controllers';
Qiang Xue committed
30 31 32 33
	/**
	 * @var LogTarget
	 */
	public $logTarget;
Qiang Xue committed
34 35 36 37
	/**
	 * @var array|Panel[]
	 */
	public $panels = array();
Qiang Xue committed
38 39 40 41
	/**
	 * @var string the directory storing the debugger data files. This can be specified using a path alias.
	 */
	public $dataPath = '@runtime/debug';
Qiang Xue committed
42
	public $historySize = 50;
Qiang Xue committed
43 44 45 46

	public function init()
	{
		parent::init();
Qiang Xue committed
47

Qiang Xue committed
48
		$this->dataPath = Yii::getAlias($this->dataPath);
Qiang Xue committed
49 50 51
		$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
		Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));

Qiang Xue committed
52
		foreach (array_merge($this->corePanels(), $this->panels) as $id => $config) {
Qiang Xue committed
53
			$config['module'] = $this;
Qiang Xue committed
54 55
			$this->panels[$id] = Yii::createObject($config);
		}
Qiang Xue committed
56 57 58 59 60
	}

	public function beforeAction($action)
	{
		Yii::$app->getView()->off(View::EVENT_END_BODY, array($this, 'renderToolbar'));
Qiang Xue committed
61
		unset(Yii::$app->getLog()->targets['debug']);
Qiang Xue committed
62
		$this->logTarget = null;
Qiang Xue committed
63 64 65 66 67 68 69 70

		$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;
Qiang Xue committed
71 72 73 74 75 76
	}

	public function renderToolbar($event)
	{
		/** @var View $view */
		$id = 'yii-debug-toolbar';
Qiang Xue committed
77
		$tag = $this->logTarget->tag;
Qiang Xue committed
78
		$url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array(
Qiang Xue committed
79
			'tag' => $tag,
Qiang Xue committed
80 81 82 83 84 85 86 87 88
		));
		$view = $event->sender;
		$view->registerJs("yii.debug.load('$id', '$url');");
		$view->registerAssetBundle('yii/debug');
		echo Html::tag('div', '', array(
			'id' => $id,
			'style' => 'display: none',
		));
	}
Qiang Xue committed
89 90 91 92

	protected function corePanels()
	{
		return array(
Qiang Xue committed
93 94 95
			'config' => array(
				'class' => 'yii\debug\panels\ConfigPanel',
			),
Qiang Xue committed
96 97 98 99 100 101
			'request' => array(
				'class' => 'yii\debug\panels\RequestPanel',
			),
			'log' => array(
				'class' => 'yii\debug\panels\LogPanel',
			),
Qiang Xue committed
102 103 104 105 106 107
			'profiling' => array(
				'class' => 'yii\debug\panels\ProfilingPanel',
			),
			'db' => array(
				'class' => 'yii\debug\panels\DbPanel',
			),
Qiang Xue committed
108 109
		);
	}
resurtm committed
110
}