Module.php 2.34 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 array|Panel[]
	 */
	public $panels = array();
Qiang Xue committed
34 35 36 37

	public function init()
	{
		parent::init();
Qiang Xue committed
38 39 40 41 42 43 44

		foreach (array_merge($this->corePanels(), $this->panels) as $id => $config) {
			$config['id'] = $id;
			$this->panels[$id] = Yii::createObject($config);
		}

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

	public function beforeAction($action)
	{
		Yii::$app->getView()->off(View::EVENT_END_BODY, array($this, 'renderToolbar'));
Qiang Xue committed
51
		unset(Yii::$app->getLog()->targets['debug']);
Qiang Xue committed
52 53 54 55 56 57 58 59

		$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
60 61 62 63 64 65 66
	}

	public function renderToolbar($event)
	{
		/** @var View $view */
		$id = 'yii-debug-toolbar';
		$url = Yii::$app->getUrlManager()->createUrl('debug/default/toolbar', array(
Qiang Xue committed
67
			'tag' => Yii::$app->getLog()->getTag(),
Qiang Xue committed
68 69 70 71 72 73 74 75 76
		));
		$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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

	protected function corePanels()
	{
		return array(
			'config' => array(
				'class' => 'yii\debug\panels\ConfigPanel',
			),
			'request' => array(
				'class' => 'yii\debug\panels\RequestPanel',
			),
			'log' => array(
				'class' => 'yii\debug\panels\LogPanel',
			),
		);
	}
resurtm committed
92
}