Panel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\debug;
  8. use Yii;
  9. use yii\base\Component;
  10. use yii\helpers\ArrayHelper;
  11. use yii\helpers\Url;
  12. /**
  13. * Panel is a base class for debugger panel classes. It defines how data should be collected,
  14. * what should be displayed at debug toolbar and on debugger details view.
  15. *
  16. * @property string $detail Content that is displayed in debugger detail view. This property is read-only.
  17. * @property string $name Name of the panel. This property is read-only.
  18. * @property string $summary Content that is displayed at debug toolbar. This property is read-only.
  19. * @property string $url URL pointing to panel detail view. This property is read-only.
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @since 2.0
  23. */
  24. class Panel extends Component
  25. {
  26. /**
  27. * @var string panel unique identifier.
  28. * It is set automatically by the container module.
  29. */
  30. public $id;
  31. /**
  32. * @var string request data set identifier.
  33. */
  34. public $tag;
  35. /**
  36. * @var Module
  37. */
  38. public $module;
  39. /**
  40. * @var mixed data associated with panel
  41. */
  42. public $data;
  43. /**
  44. * @var array array of actions to add to the debug modules default controller.
  45. * This array will be merged with all other panels actions property.
  46. * See [[\yii\base\Controller::actions()]] for the format.
  47. */
  48. public $actions = [];
  49. /**
  50. * @var FlattenException|null Error while saving the panel
  51. * @since 2.0.10
  52. */
  53. protected $error;
  54. /**
  55. * @return string name of the panel
  56. */
  57. public function getName()
  58. {
  59. return '';
  60. }
  61. /**
  62. * @return string content that is displayed at debug toolbar
  63. */
  64. public function getSummary()
  65. {
  66. return '';
  67. }
  68. /**
  69. * @return string content that is displayed in debugger detail view
  70. */
  71. public function getDetail()
  72. {
  73. return '';
  74. }
  75. /**
  76. * Saves data to be later used in debugger detail view.
  77. * This method is called on every page where debugger is enabled.
  78. *
  79. * @return mixed data to be saved
  80. */
  81. public function save()
  82. {
  83. return null;
  84. }
  85. /**
  86. * Loads data into the panel
  87. *
  88. * @param mixed $data
  89. */
  90. public function load($data)
  91. {
  92. $this->data = $data;
  93. }
  94. /**
  95. * @param null|array $additionalParams Optional additional parameters to add to the route
  96. * @return string URL pointing to panel detail view
  97. */
  98. public function getUrl($additionalParams = null)
  99. {
  100. $route = [
  101. '/' . $this->module->id . '/default/view',
  102. 'panel' => $this->id,
  103. 'tag' => $this->tag,
  104. ];
  105. if (is_array($additionalParams)) {
  106. $route = ArrayHelper::merge($route, $additionalParams);
  107. }
  108. return Url::toRoute($route);
  109. }
  110. /**
  111. * Returns a trace line
  112. * @param array $options The array with trace
  113. * @return string the trace line
  114. * @since 2.0.7
  115. */
  116. public function getTraceLine($options)
  117. {
  118. if (!isset($options['text'])) {
  119. $options['text'] = "{$options['file']}:{$options['line']}";
  120. }
  121. $traceLine = $this->module->traceLine;
  122. if ($traceLine === false) {
  123. return $options['text'];
  124. }
  125. $options['file'] = str_replace('\\', '/', $options['file']);
  126. $rawLink = $traceLine instanceof \Closure ? $traceLine($options, $this) : $traceLine;
  127. return strtr($rawLink, ['{file}' => $options['file'], '{line}' => $options['line'], '{text}' => $options['text']]);
  128. }
  129. /**
  130. * @param FlattenException $error
  131. * @since 2.0.10
  132. */
  133. public function setError(FlattenException $error)
  134. {
  135. $this->error = $error;
  136. }
  137. /**
  138. * @return FlattenException|null
  139. * @since 2.0.10
  140. */
  141. public function getError()
  142. {
  143. return $this->error;
  144. }
  145. /**
  146. * @return bool
  147. * @since 2.0.10
  148. */
  149. public function hasError()
  150. {
  151. return $this->error !== null;
  152. }
  153. /**
  154. * Checks whether this panel is enabled.
  155. * @return bool whether this panel is enabled.
  156. * @since 2.0.10
  157. */
  158. public function isEnabled()
  159. {
  160. return true;
  161. }
  162. }