Module.php 3.4 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
use Yii;
use yii\base\View;
12
use yii\web\HttpException;
Qiang Xue committed
13

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
	/**
	 * @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');
28 29 30
	/**
	 * @var string the namespace that controller classes are in.
	 */
Qiang Xue committed
31
	public $controllerNamespace = 'yii\debug\controllers';
Qiang Xue committed
32 33 34 35
	/**
	 * @var LogTarget
	 */
	public $logTarget;
Qiang Xue committed
36 37 38 39
	/**
	 * @var array|Panel[]
	 */
	public $panels = array();
Qiang Xue committed
40 41 42 43
	/**
	 * @var string the directory storing the debugger data files. This can be specified using a path alias.
	 */
	public $dataPath = '@runtime/debug';
44 45 46 47
	/**
	 * @var integer the maximum number of debug data files to keep. If there are more files generated,
	 * the oldest ones will be removed.
	 */
Qiang Xue committed
48
	public $historySize = 50;
49

Qiang Xue committed
50 51 52 53

	public function init()
	{
		parent::init();
Qiang Xue committed
54
		$this->dataPath = Yii::getAlias($this->dataPath);
Qiang Xue committed
55 56 57
		$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
		Yii::$app->getView()->on(View::EVENT_END_BODY, array($this, 'renderToolbar'));

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

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

71
		if ($this->checkAccess($action)) {
Qiang Xue committed
72
			return parent::beforeAction($action);
73 74 75 76
		} elseif ($action->id === 'toolbar') {
			return false;
		} else {
			throw new HttpException(403, 'You are not allowed to access this page.');
Qiang Xue committed
77
		}
Qiang Xue committed
78 79 80 81
	}

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

95 96 97 98 99 100 101 102 103 104 105
	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;
	}

Qiang Xue committed
106 107 108
	protected function corePanels()
	{
		return array(
Qiang Xue committed
109 110 111
			'config' => array(
				'class' => 'yii\debug\panels\ConfigPanel',
			),
Qiang Xue committed
112 113 114 115 116 117
			'request' => array(
				'class' => 'yii\debug\panels\RequestPanel',
			),
			'log' => array(
				'class' => 'yii\debug\panels\LogPanel',
			),
Qiang Xue committed
118 119 120 121 122 123
			'profiling' => array(
				'class' => 'yii\debug\panels\ProfilingPanel',
			),
			'db' => array(
				'class' => 'yii\debug\panels\DbPanel',
			),
Qiang Xue committed
124 125
		);
	}
resurtm committed
126
}