DefaultController.php 3.6 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
	public function actions()
	{
		$actions = [];
Tobias Munk committed
42
		foreach ($this->module->panels as $panel) {
43 44 45 46 47
			$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());

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

58
		return $this->render('index', [
Tobias Munk committed
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
	}

Mark committed
96 97
	public function actionDownloadMail($file)
	{
Mark committed
98
		$filePath = Yii::getAlias($this->module->panels['mail']->mailPath) . '/' . basename($file);
Mark committed
99

Mark committed
100
		if ((mb_strpos($file, '\\') !== false || mb_strpos($file, '/') !== false) || !is_file($filePath)) {
Mark committed
101 102 103 104 105
			throw new NotFoundHttpException('Mail file not found');
		}

		Yii::$app->response->sendFile($filePath);
	}
Qiang Xue committed
106

Qiang Xue committed
107 108
	private $_manifest;

109
	protected function getManifest($forceReload = false)
Qiang Xue committed
110
	{
111
		if ($this->_manifest === null || $forceReload) {
112
			if ($forceReload) {
113 114
				clearstatcache();
			}
Qiang Xue committed
115
			$indexFile = $this->module->dataPath . '/index.data';
Qiang Xue committed
116
			if (is_file($indexFile)) {
117
				$this->_manifest = array_reverse(unserialize(file_get_contents($indexFile)), true);
Qiang Xue committed
118
			} else {
Alexander Makarov committed
119
				$this->_manifest = [];
Qiang Xue committed
120 121 122 123 124
			}
		}
		return $this->_manifest;
	}

125
	public function loadData($tag, $maxRetry = 0)
Qiang Xue committed
126
	{
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		// 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
143
				}
144 145
				$this->summary = $data['summary'];
				return;
Qiang Xue committed
146
			}
Qiang Xue committed
147
			sleep(1);
Qiang Xue committed
148
		}
149 150

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