PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Application/Routers/SimpleRouter.php

https://github.com/premiumcombination/nts
PHP | 146 lines | 78 code | 30 blank | 38 comment | 12 complexity | 5552fd34a338616e73e93c65180b5ca2 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004, 2011 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\Routers;
  11. use Nette,
  12. Nette\Application;
  13. /**
  14. * The bidirectional route for trivial routing via query parameters.
  15. *
  16. * @author David Grudl
  17. */
  18. class SimpleRouter extends Nette\Object implements Application\IRouter
  19. {
  20. const PRESENTER_KEY = 'presenter';
  21. const MODULE_KEY = 'module';
  22. /** @var string */
  23. private $module = '';
  24. /** @var array */
  25. private $defaults;
  26. /** @var int */
  27. private $flags;
  28. /**
  29. * @param array default values
  30. * @param int flags
  31. */
  32. public function __construct($defaults = array(), $flags = 0)
  33. {
  34. if (is_string($defaults)) {
  35. $a = strrpos($defaults, ':');
  36. if (!$a) {
  37. throw new Nette\InvalidArgumentException("Argument must be array or string in format Presenter:action, '$defaults' given.");
  38. }
  39. $defaults = array(
  40. self::PRESENTER_KEY => substr($defaults, 0, $a),
  41. 'action' => $a === strlen($defaults) - 1 ? Application\UI\Presenter::DEFAULT_ACTION : substr($defaults, $a + 1),
  42. );
  43. }
  44. if (isset($defaults[self::MODULE_KEY])) {
  45. $this->module = $defaults[self::MODULE_KEY] . ':';
  46. unset($defaults[self::MODULE_KEY]);
  47. }
  48. $this->defaults = $defaults;
  49. $this->flags = $flags;
  50. }
  51. /**
  52. * Maps HTTP request to a Request object.
  53. * @param Nette\Http\IRequest
  54. * @return Nette\Application\Request|NULL
  55. */
  56. public function match(Nette\Http\IRequest $httpRequest)
  57. {
  58. if ($httpRequest->getUrl()->getPathInfo() !== '') {
  59. return NULL;
  60. }
  61. // combine with precedence: get, (post,) defaults
  62. $params = $httpRequest->getQuery();
  63. $params += $this->defaults;
  64. if (!isset($params[self::PRESENTER_KEY])) {
  65. throw new Nette\InvalidStateException('Missing presenter.');
  66. }
  67. $presenter = $this->module . $params[self::PRESENTER_KEY];
  68. unset($params[self::PRESENTER_KEY]);
  69. return new Application\Request(
  70. $presenter,
  71. $httpRequest->getMethod(),
  72. $params,
  73. $httpRequest->getPost(),
  74. $httpRequest->getFiles(),
  75. array(Application\Request::SECURED => $httpRequest->isSecured())
  76. );
  77. }
  78. /**
  79. * Constructs absolute URL from Request object.
  80. * @param Nette\Application\Request
  81. * @param Nette\Http\Url
  82. * @return string|NULL
  83. */
  84. public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
  85. {
  86. $params = $appRequest->getParams();
  87. // presenter name
  88. $presenter = $appRequest->getPresenterName();
  89. if (strncasecmp($presenter, $this->module, strlen($this->module)) === 0) {
  90. $params[self::PRESENTER_KEY] = substr($presenter, strlen($this->module));
  91. } else {
  92. return NULL;
  93. }
  94. // remove default values; NULL values are retain
  95. foreach ($this->defaults as $key => $value) {
  96. if (isset($params[$key]) && $params[$key] == $value) { // intentionally ==
  97. unset($params[$key]);
  98. }
  99. }
  100. $url = ($this->flags & self::SECURED ? 'https://' : 'http://') . $refUrl->getAuthority() . $refUrl->getPath();
  101. $sep = ini_get('arg_separator.input');
  102. $query = http_build_query($params, '', $sep ? $sep[0] : '&');
  103. if ($query != '') { // intentionally ==
  104. $url .= '?' . $query;
  105. }
  106. return $url;
  107. }
  108. /**
  109. * Returns default values.
  110. * @return array
  111. */
  112. public function getDefaults()
  113. {
  114. return $this->defaults;
  115. }
  116. }