YiiDebugToolbar.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * YiiDebugToolbar class file.
  4. *
  5. * @author Sergey Malyshev <malyshev.php@gmail.com>
  6. */
  7. Yii::import('yii-debug-toolbar.panels.*');
  8. Yii::import('yii-debug-toolbar.widgets.*');
  9. /**
  10. * YiiDebugToolbar represents an ...
  11. *
  12. * Description of YiiDebugToolbar
  13. *
  14. * @author Sergey Malyshev <malyshev.php@gmail.com>
  15. * @author Igor Golovanov <igor.golovanov@gmail.com>
  16. * @version $Id$
  17. * @package YiiDebugToolbar
  18. * @since 1.1.7
  19. */
  20. class YiiDebugToolbar extends CWidget
  21. {
  22. /**
  23. * The URL of assets.
  24. *
  25. * @var string
  26. */
  27. private $_assetsUrl;
  28. /**
  29. * Panels.
  30. *
  31. * @var array
  32. */
  33. private $_panels;
  34. /**
  35. * Setup toolbar panels.
  36. *
  37. * @param array $panels Panels.
  38. * @return YiiDebugToolbar
  39. */
  40. public function setPanels(array $panels = array())
  41. {
  42. $this->_panels = $panels;
  43. return $this;
  44. }
  45. /**
  46. * Get toolbar panels.
  47. *
  48. * @return array
  49. */
  50. public function getPanels()
  51. {
  52. if(null === $this->_panels)
  53. {
  54. $this->_panels = array();
  55. }
  56. return $this->_panels;
  57. }
  58. /**
  59. * Get the URL of assets.
  60. * The base URL that contains all published asset files of yii-debug-toolbar.
  61. * @return string
  62. */
  63. public function getAssetsUrl()
  64. {
  65. if (null === $this->_assetsUrl && 'cli' !== php_sapi_name()) {
  66. $linkAssets = Yii::app()->getAssetManager()->linkAssets;
  67. $this->_assetsUrl = Yii::app()
  68. ->getAssetManager()
  69. ->publish(dirname(__FILE__) . '/assets', false, -1, YII_DEBUG and !$linkAssets);
  70. }
  71. return $this->_assetsUrl;
  72. }
  73. public function getLogs()
  74. {
  75. return $this->owner->logs;
  76. }
  77. /**
  78. * Set the URL of assets.
  79. * The base URL that contains all published asset files of yii-debug-toolbar.
  80. *
  81. * @param string $value
  82. */
  83. public function setAssetsUrl($value)
  84. {
  85. $this->_assetsUrl = $value;
  86. }
  87. /**
  88. * Initialization.
  89. */
  90. public function init()
  91. {
  92. if (false === ($this->owner instanceof CLogRoute)) {
  93. throw new CException(YiiDebug::t('YiiDebugToolbar owner must be instance of CLogRoute'));
  94. }
  95. $this->createPanels()
  96. ->registerClientScripts();
  97. }
  98. /**
  99. * Run.
  100. */
  101. public function run()
  102. {
  103. $this->render('main', array(
  104. 'panels' => $this->getPanels()
  105. ));
  106. }
  107. /**
  108. * Register client scripts.
  109. *
  110. * @return YiiDebugToolbar
  111. */
  112. private function registerClientScripts()
  113. {
  114. $cs = Yii::app()->getClientScript()
  115. ->registerCoreScript('jquery');
  116. $cs->registerCssFile($this->assetsUrl . '/main_old.css');
  117. $cs->registerCssFile($this->assetsUrl . '/main.css');
  118. $cs->registerScriptFile($this->assetsUrl . '/main.js',
  119. CClientScript::POS_END);
  120. return $this;
  121. }
  122. /**
  123. * Create panels.
  124. *
  125. * @return YiiDebugToolbar
  126. */
  127. private function createPanels()
  128. {
  129. foreach ($this->getPanels() as $id => $config) {
  130. if (!is_object($config)) {
  131. if (is_string($config)) {
  132. $config = array('class' => $config);
  133. }
  134. if(is_array($config)) {
  135. if (is_array($config) && !isset($config['class'])) {
  136. $config['class'] = $id;
  137. }
  138. if (isset($config['enabled']) && false === $config['enabled']) {
  139. unset($this->_panels[$id]);
  140. continue;
  141. } else if (isset($config['enabled']) && true === $config['enabled']) {
  142. unset($config['enabled']);
  143. }
  144. }
  145. $panel = Yii::createComponent($config, $this);
  146. if (false === ($panel instanceof YiiDebugToolbarPanelInterface)) {
  147. throw new CException(Yii::t('yii-debug-toolbar',
  148. 'The %class% class must be compatible with YiiDebugToolbarPanelInterface', array(
  149. '%class%' => get_class($panel)
  150. )));
  151. }
  152. $panel->init();
  153. $this->_panels[$id] = $panel;
  154. }
  155. }
  156. return $this;
  157. }
  158. }
  159. /**
  160. * YiiDebugToolbarPanelInterface
  161. *
  162. * @author Sergey Malyshev <malyshev.php@gmail.com>
  163. * @author Igor Golovanov <igor.golovanov@gmail.com>
  164. * @version $Id$
  165. * @package YiiDebugToolbar
  166. * @since 1.1.7
  167. */
  168. interface YiiDebugToolbarPanelInterface
  169. {
  170. /**
  171. * Get the title of menu.
  172. *
  173. * @return string
  174. */
  175. function getMenuTitle();
  176. /**
  177. * Get the subtitle of menu.
  178. *
  179. * @return string
  180. */
  181. function getMenuSubTitle();
  182. /**
  183. * Get the title.
  184. *
  185. * @return string
  186. */
  187. function getTitle();
  188. /**
  189. * Get the subtitle.
  190. *
  191. * @return string
  192. */
  193. function getSubTitle();
  194. }