DefaultController.php 3.08 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 18 19 20

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

31 32 33 34 35 36 37 38 39
	public function actions()
	{
		$actions = [];
		foreach($this->module->panels as $panel) {
			$actions = array_merge($actions, $panel->actions);
		}
		return $actions;
	}

Qiang Xue committed
40 41
	public function actionIndex()
	{
42 43 44 45 46 47 48
		$searchModel = new Debug();
		$dataProvider = $searchModel->search($_GET, $this->getManifest());

		return $this->render('index', [
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel,
		]);
Qiang Xue committed
49 50 51
	}

	public function actionView($tag = null, $panel = null)
Qiang Xue committed
52
	{
Qiang Xue committed
53 54
		if ($tag === null) {
			$tags = array_keys($this->getManifest());
Qiang Xue committed
55
			$tag = reset($tags);
Qiang Xue committed
56
		}
Qiang Xue committed
57
		$this->loadData($tag);
Qiang Xue committed
58 59 60
		if (isset($this->module->panels[$panel])) {
			$activePanel = $this->module->panels[$panel];
		} else {
Qiang Xue committed
61
			$activePanel = $this->module->panels['request'];
Qiang Xue committed
62
		}
Alexander Makarov committed
63
		return $this->render('view', [
Qiang Xue committed
64
			'tag' => $tag,
Qiang Xue committed
65
			'summary' => $this->summary,
Qiang Xue committed
66
			'manifest' => $this->getManifest(),
Qiang Xue committed
67 68
			'panels' => $this->module->panels,
			'activePanel' => $activePanel,
Alexander Makarov committed
69
		]);
Qiang Xue committed
70
	}
Qiang Xue committed
71 72

	public function actionToolbar($tag)
Qiang Xue committed
73
	{
Qiang Xue committed
74
		$this->loadData($tag, 5);
Alexander Makarov committed
75
		return $this->renderPartial('toolbar', [
Qiang Xue committed
76
			'tag' => $tag,
Qiang Xue committed
77
			'panels' => $this->module->panels,
78
			'position' => 'bottom',
Alexander Makarov committed
79
		]);
Qiang Xue committed
80 81
	}

Qiang Xue committed
82 83 84 85 86
	public function actionPhpinfo()
	{
		phpinfo();
	}

Qiang Xue committed
87 88
	private $_manifest;

89
	protected function getManifest($forceReload = false)
Qiang Xue committed
90
	{
91
		if ($this->_manifest === null || $forceReload) {
92
			if ($forceReload) {
93 94
				clearstatcache();
			}
Qiang Xue committed
95
			$indexFile = $this->module->dataPath . '/index.data';
Qiang Xue committed
96
			if (is_file($indexFile)) {
97
				$this->_manifest = array_reverse(unserialize(file_get_contents($indexFile)), true);
Qiang Xue committed
98
			} else {
Alexander Makarov committed
99
				$this->_manifest = [];
Qiang Xue committed
100 101 102 103 104
			}
		}
		return $this->_manifest;
	}

105
	public function loadData($tag, $maxRetry = 0)
Qiang Xue committed
106
	{
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		// 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
123
				}
124 125
				$this->summary = $data['summary'];
				return;
Qiang Xue committed
126
			}
Qiang Xue committed
127
			sleep(1);
Qiang Xue committed
128
		}
129 130

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