PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Application/Diagnostics/RoutingPanel.php

https://github.com/stekycz/nette
PHP | 125 lines | 68 code | 27 blank | 30 comment | 4 complexity | 196804144b31c208ee7ee695ffb57a15 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6. *
  7. * For the full copyright and license information, please view
  8. * the file license.txt that was distributed with this source code.
  9. */
  10. namespace Nette\Application\Diagnostics;
  11. use Nette,
  12. Nette\Application\Routers,
  13. Nette\Application\UI\Presenter, // templates
  14. Nette\Diagnostics\Dumper;
  15. /**
  16. * Routing debugger for Debug Bar.
  17. *
  18. * @author David Grudl
  19. */
  20. class RoutingPanel extends Nette\Object implements Nette\Diagnostics\IBarPanel
  21. {
  22. /** @var Nette\Application\IRouter */
  23. private $router;
  24. /** @var Nette\Http\IRequest */
  25. private $httpRequest;
  26. /** @var array */
  27. private $routers = array();
  28. /** @var Nette\Application\Request */
  29. private $request;
  30. public static function initializePanel(Nette\Application\Application $application)
  31. {
  32. Nette\Diagnostics\Debugger::$blueScreen->addPanel(function($e) use ($application) {
  33. return $e ? NULL : array(
  34. 'tab' => 'Nette Application',
  35. 'panel' => '<h3>Requests</h3>' . Dumper::toHtml($application->getRequests())
  36. . '<h3>Presenter</h3>' . Dumper::toHtml($application->getPresenter())
  37. );
  38. });
  39. }
  40. public function __construct(Nette\Application\IRouter $router, Nette\Http\IRequest $httpRequest)
  41. {
  42. $this->router = $router;
  43. $this->httpRequest = $httpRequest;
  44. }
  45. /**
  46. * Renders tab.
  47. * @return string
  48. */
  49. public function getTab()
  50. {
  51. $this->analyse($this->router);
  52. ob_start();
  53. require __DIR__ . '/templates/RoutingPanel.tab.phtml';
  54. return ob_get_clean();
  55. }
  56. /**
  57. * Renders panel.
  58. * @return string
  59. */
  60. public function getPanel()
  61. {
  62. ob_start();
  63. require __DIR__ . '/templates/RoutingPanel.panel.phtml';
  64. return ob_get_clean();
  65. }
  66. /**
  67. * Analyses simple route.
  68. * @param Nette\Application\IRouter
  69. * @return void
  70. */
  71. private function analyse($router, $module = '')
  72. {
  73. if ($router instanceof Routers\RouteList) {
  74. foreach ($router as $subRouter) {
  75. $this->analyse($subRouter, $module . $router->getModule());
  76. }
  77. return;
  78. }
  79. $matched = 'no';
  80. $request = $router->match($this->httpRequest);
  81. if ($request) {
  82. $request->setPresenterName($module . $request->getPresenterName());
  83. $matched = 'may';
  84. if (empty($this->request)) {
  85. $this->request = $request;
  86. $matched = 'yes';
  87. }
  88. }
  89. $this->routers[] = array(
  90. 'matched' => $matched,
  91. 'class' => get_class($router),
  92. 'defaults' => $router instanceof Routers\Route || $router instanceof Routers\SimpleRouter ? $router->getDefaults() : array(),
  93. 'mask' => $router instanceof Routers\Route ? $router->getMask() : NULL,
  94. 'request' => $request,
  95. 'module' => rtrim($module, ':')
  96. );
  97. }
  98. }