DefaultController.php 3.29 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
		$searchModel = new Debug();
		$dataProvider = $searchModel->search($_GET, $this->getManifest());

53 54 55 56 57
        // load latest request
        $tags = array_keys($this->getManifest());
        $tag = reset($tags);
        $this->loadData($tag);

58
		return $this->render('index', [
59
            'panels' => $this->module->panels,
60 61 62
			'dataProvider' => $dataProvider,
			'searchModel' => $searchModel,
		]);
Qiang Xue committed
63 64 65
	}

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

	public function actionToolbar($tag)
Qiang Xue committed
87
	{
Qiang Xue committed
88
		$this->loadData($tag, 5);
Alexander Makarov committed
89
		return $this->renderPartial('toolbar', [
Qiang Xue committed
90
			'tag' => $tag,
Qiang Xue committed
91
			'panels' => $this->module->panels,
92
			'position' => 'bottom',
Alexander Makarov committed
93
		]);
Qiang Xue committed
94 95
	}

Qiang Xue committed
96

Qiang Xue committed
97 98
	private $_manifest;

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

115
	public function loadData($tag, $maxRetry = 0)
Qiang Xue committed
116
	{
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		// 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
133
				}
134 135
				$this->summary = $data['summary'];
				return;
Qiang Xue committed
136
			}
Qiang Xue committed
137
			sleep(1);
Qiang Xue committed
138
		}
139 140

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