Module.php 2.94 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
use Yii;
use yii\base\View;

Qiang Xue committed
13 14 15 16 17 18
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Module extends \yii\base\Module
{
Qiang Xue committed
19 20 21 22 23 24 25 26 27
	/**
	 * @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
28
	public $controllerNamespace = 'yii\debug\controllers';
Qiang Xue committed
29 30 31 32
	/**
	 * @var LogTarget
	 */
	public $logTarget;
Qiang Xue committed
33 34 35 36
	/**
	 * @var array|Panel[]
	 */
	public $panels = array();
Qiang Xue committed
37 38 39 40
	/**
	 * @var string the directory storing the debugger data files. This can be specified using a path alias.
	 */
	public $dataPath = '@runtime/debug';
Qiang Xue committed
41
	public $historySize = 50;
42
	public $enabled = true;
Qiang Xue committed
43 44 45 46

	public function init()
	{
		parent::init();
47 48 49
		if (!$this->enabled) {
			return;
		}
Qiang Xue committed
50
		$this->dataPath = Yii::getAlias($this->dataPath);
Qiang Xue committed
51 52 53
		$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
		Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));

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

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

		$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
74 75 76 77
	}

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

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