DefaultController.php 3.15 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\controllers;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\web\Controller;
12
use yii\web\NotFoundHttpException;
13
use yii\debug\models\search\Debug;
Qiang Xue committed
14 15

/**
16 17
 * Debugger controller
 *
Qiang Xue committed
18 19 20 21 22
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class DefaultController extends Controller
{
23 24 25
	/**
	 * @inheritdoc
	 */
Qiang Xue committed
26
	public $layout = 'main';
Qiang Xue committed
27
	/**
28
	 * @var \yii\debug\Module
Qiang Xue committed
29 30 31 32 33 34
	 */
	public $module;
	/**
	 * @var array the summary data (e.g. URL, time)
	 */
	public $summary;
Qiang Xue committed
35

36 37 38
	/**
	 * @inheritdoc
	 */
39 40 41 42 43 44 45 46 47
	public function actions()
	{
		$actions = [];
		foreach($this->module->panels as $panel) {
			$actions = array_merge($actions, $panel->actions);
		}
		return $actions;
	}

Qiang Xue committed
48 49
	public function actionIndex()
	{
50 51 52 53 54 55 56
		$searchModel = new Debug();
		$dataProvider = $searchModel->search($_GET, $this->getManifest());

		return $this->render('index', [
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel,
		]);
Qiang Xue committed
57 58 59
	}

	public function actionView($tag = null, $panel = null)
Qiang Xue committed
60
	{
Qiang Xue committed
61 62
		if ($tag === null) {
			$tags = array_keys($this->getManifest());
Qiang Xue committed
63
			$tag = reset($tags);
Qiang Xue committed
64
		}
Qiang Xue committed
65
		$this->loadData($tag);
Qiang Xue committed
66 67 68
		if (isset($this->module->panels[$panel])) {
			$activePanel = $this->module->panels[$panel];
		} else {
Qiang Xue committed
69
			$activePanel = $this->module->panels['request'];
Qiang Xue committed
70
		}
Alexander Makarov committed
71
		return $this->render('view', [
Qiang Xue committed
72
			'tag' => $tag,
Qiang Xue committed
73
			'summary' => $this->summary,
Qiang Xue committed
74
			'manifest' => $this->getManifest(),
Qiang Xue committed
75 76
			'panels' => $this->module->panels,
			'activePanel' => $activePanel,
Alexander Makarov committed
77
		]);
Qiang Xue committed
78
	}
Qiang Xue committed
79 80

	public function actionToolbar($tag)
Qiang Xue committed
81
	{
Qiang Xue committed
82
		$this->loadData($tag, 5);
Alexander Makarov committed
83
		return $this->renderPartial('toolbar', [
Qiang Xue committed
84
			'tag' => $tag,
Qiang Xue committed
85
			'panels' => $this->module->panels,
86
			'position' => 'bottom',
Alexander Makarov committed
87
		]);
Qiang Xue committed
88 89
	}

Qiang Xue committed
90 91 92 93 94
	public function actionPhpinfo()
	{
		phpinfo();
	}

Qiang Xue committed
95 96
	private $_manifest;

97
	protected function getManifest($forceReload = false)
Qiang Xue committed
98
	{
99
		if ($this->_manifest === null || $forceReload) {
100
			if ($forceReload) {
101 102
				clearstatcache();
			}
Qiang Xue committed
103
			$indexFile = $this->module->dataPath . '/index.data';
Qiang Xue committed
104
			if (is_file($indexFile)) {
105
				$this->_manifest = array_reverse(unserialize(file_get_contents($indexFile)), true);
Qiang Xue committed
106
			} else {
Alexander Makarov committed
107
				$this->_manifest = [];
Qiang Xue committed
108 109 110 111 112
			}
		}
		return $this->_manifest;
	}

113
	public function loadData($tag, $maxRetry = 0)
Qiang Xue committed
114
	{
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		// retry loading debug data because the debug data is logged in shutdown function
		// which may be delayed in some environment if xdebug is enabled.
		// See: https://github.com/yiisoft/yii2/issues/1504
		for ($retry = 0; $retry <= $maxRetry; ++$retry) {
			$manifest = $this->getManifest($retry > 0);
			if (isset($manifest[$tag])) {
				$dataFile = $this->module->dataPath . "/$tag.data";
				$data = unserialize(file_get_contents($dataFile));
				foreach ($this->module->panels as $id => $panel) {
					if (isset($data[$id])) {
						$panel->tag = $tag;
						$panel->load($data[$id]);
					} else {
						// remove the panel since it has not received any data
						unset($this->module->panels[$id]);
					}
Qiang Xue committed
131
				}
132 133
				$this->summary = $data['summary'];
				return;
Qiang Xue committed
134
			}
Qiang Xue committed
135
			sleep(1);
Qiang Xue committed
136
		}
137 138

		throw new NotFoundHttpException("Unable to find debug data tagged with '$tag'.");
Qiang Xue committed
139
	}
resurtm committed
140
}