Source for file MultiRouter.php

Documentation is available at MultiRouter.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Application
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../../Application/IRouter.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../../Collections/ArrayList.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * The router broker.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Application
  34. 34:  */
  35. 35: class MultiRouter extends ArrayList implements IRouter
  36. 36: {
  37. 37:     /** @var array {@link MultiRouter::constructUrl()} */
  38. 38:     private $cachedRoutes;
  39. 39:  
  40. 40:  
  41. 41:  
  42. 42:     public function __construct()
  43. 43:     {
  44. 44:         parent::__construct(NULL'IRouter');
  45. 45:     }
  46. 46:  
  47. 47:  
  48. 48:  
  49. 49:     /**
  50. 50:      * Maps HTTP request to a PresenterRequest object.
  51. 51:      * @param  IHttpRequest 
  52. 52:      * @return PresenterRequest|NULL
  53. 53:      */
  54. 54:     public function match(IHttpRequest $httpRequest)
  55. 55:     {
  56. 56:         foreach ($this as $route{
  57. 57:             $appRequest $route->match($httpRequest);
  58. 58:             if ($appRequest !== NULL{
  59. 59:                 return $appRequest;
  60. 60:             }
  61. 61:         }
  62. 62:         return NULL;
  63. 63:     }
  64. 64:  
  65. 65:  
  66. 66:  
  67. 67:     /**
  68. 68:      * Constructs absolute URL from PresenterRequest object.
  69. 69:      * @param  IHttpRequest 
  70. 70:      * @param  PresenterRequest 
  71. 71:      * @return string|NULL
  72. 72:      */
  73. 73:     public function constructUrl(PresenterRequest $appRequestIHttpRequest $httpRequest)
  74. 74:     {
  75. 75:         if ($this->cachedRoutes === NULL{
  76. 76:             $routes array();
  77. 77:             $routes['*'array();
  78. 78:  
  79. 79:             foreach ($this as $route{
  80. 80:                 $presenter $route instanceof Route $route->getTargetPresenter(NULL;
  81. 81:  
  82. 82:                 if ($presenter === FALSEcontinue;
  83. 83:  
  84. 84:                 if (is_string($presenter)) {
  85. 85:                     $presenter strtolower($presenter);
  86. 86:                     if (!isset($routes[$presenter])) {
  87. 87:                         $routes[$presenter$routes['*'];
  88. 88:                     }
  89. 89:                     $routes[$presenter][$route;
  90. 90:  
  91. 91:                 else {
  92. 92:                     foreach ($routes as $id => $foo{
  93. 93:                         $routes[$id][$route;
  94. 94:                     }
  95. 95:                 }
  96. 96:             }
  97. 97:  
  98. 98:             $this->cachedRoutes $routes;
  99. 99:         }
  100. 100:  
  101. 101:         $presenter strtolower($appRequest->getPresenterName());
  102. 102:         if (!isset($this->cachedRoutes[$presenter])) $presenter '*';
  103. 103:  
  104. 104:         foreach ($this->cachedRoutes[$presenteras $route{
  105. 105:             $uri $route->constructUrl($appRequest$httpRequest);
  106. 106:             if ($uri !== NULL{
  107. 107:                 return $uri;
  108. 108:             }
  109. 109:         }
  110. 110:  
  111. 111:         return NULL;
  112. 112:     }
  113. 113: