PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/RoutingDebugger/RoutingDebugger.php

https://github.com/bazo/Mokuji
PHP | 135 lines | 70 code | 24 blank | 41 comment | 8 complexity | 60b1368ef60dbf899af0c76cb5851460 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /*use Nette\Object; */
  3. /*use Nette\Debug; */
  4. /*use Nette\Environment; */
  5. /*use Nette\Application\IRouter; */
  6. /*use Nette\Application\MultiRouter; */
  7. /*use Nette\Application\SimpleRouter; */
  8. /*use Nette\Application\Route; */
  9. /*use Nette\Templates\Template; */
  10. /*use Nette\Web\IHttpRequest; */
  11. /**
  12. * Routing debugger for Nette Framework.
  13. *
  14. * @author David Grudl
  15. * @copyright Copyright (c) 2009 David Grudl
  16. * @license http://nettephp.com/license Nette license
  17. * @link http://nettephp.com
  18. * @version $Id: RoutingDebugger.php 212 2009-02-13 03:52:50Z david@grudl.com $
  19. */
  20. class RoutingDebugger extends Object
  21. {
  22. /** @var Nette\Application\IRouter */
  23. private $router;
  24. /** @var Nette\Web\IHttpRequest */
  25. private $httpRequest;
  26. /** @var Nette\Templates\Template */
  27. private $template;
  28. /** @var bool */
  29. private static $enabled = FALSE;
  30. /**
  31. * Dispatch an HTTP request to a routing debugger. Please don't call directly.
  32. */
  33. public static function run()
  34. {
  35. if (!self::$enabled || Environment::getMode('production')) {
  36. return;
  37. }
  38. self::$enabled = FALSE;
  39. $debugger = new self(Environment::getApplication()->getRouter(), Environment::getHttpRequest());
  40. $debugger->paint();
  41. }
  42. public function __construct(IRouter $router, IHttpRequest $httpRequest)
  43. {
  44. $this->router = $router;
  45. $this->httpRequest = $httpRequest;
  46. }
  47. /**
  48. * Dispatch an HTTP request to a routing debugger.
  49. */
  50. public static function enable()
  51. {
  52. register_shutdown_function(array(__CLASS__, 'run'));
  53. self::$enabled = TRUE;
  54. }
  55. /**
  56. * Disables profiler.
  57. * @return void
  58. */
  59. public static function disable()
  60. {
  61. self::$enabled = FALSE;
  62. }
  63. /**
  64. * Renders debuger output.
  65. * @return void
  66. */
  67. public function paint()
  68. {
  69. foreach (headers_list() as $header) {
  70. if (strncasecmp($header, 'Content-Type:', 13) === 0) {
  71. if (substr($header, 14, 9) === 'text/html') {
  72. break;
  73. }
  74. return;
  75. }
  76. }
  77. $this->template = new Template;
  78. $this->template->setFile(dirname(__FILE__) . '/RoutingDebugger.phtml');
  79. $this->template->routers = array();
  80. $this->analyse($this->router);
  81. $this->template->render();
  82. }
  83. /**
  84. * Analyses simple route.
  85. * @param Nette\Application\IRouter
  86. * @return void
  87. */
  88. private function analyse($router)
  89. {
  90. if ($router instanceof MultiRouter) {
  91. foreach ($router as $subRouter) {
  92. $this->analyse($subRouter);
  93. }
  94. return;
  95. }
  96. $appRequest = $router->match($this->httpRequest);
  97. $matched = $appRequest === NULL ? 'no' : 'may';
  98. if ($appRequest !== NULL && !isset($this->template->router)) {
  99. $this->template->router = get_class($router) . ($router instanceof Route ? ' "' . $router->mask . '"' : '');
  100. $this->template->presenter = $appRequest->getPresenterName();
  101. $this->template->params = $appRequest->getParams();
  102. $matched = 'yes';
  103. }
  104. $this->template->routers[] = array(
  105. 'matched' => $matched,
  106. 'class' => get_class($router),
  107. 'defaults' => $router instanceof Route || $router instanceof SimpleRouter ? $router->getDefaults() : array(),
  108. 'mask' => $router instanceof Route ? $router->getMask() : NULL,
  109. );
  110. }
  111. }