1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Application\Routers;
9:
10: use Nette;
11:
12:
13: /**
14: * The router broker.
15: *
16: * @author David Grudl
17: * @property-read string $module
18: */
19: class RouteList extends Nette\ArrayList implements Nette\Application\IRouter
20: {
21: /** @var array */
22: private $cachedRoutes;
23:
24: /** @var string */
25: private $module;
26:
27:
28: public function __construct($module = NULL)
29: {
30: $this->module = $module ? $module . ':' : '';
31: }
32:
33:
34: /**
35: * Maps HTTP request to a Request object.
36: * @return Nette\Application\Request|NULL
37: */
38: public function match(Nette\Http\IRequest $httpRequest)
39: {
40: foreach ($this as $route) {
41: $appRequest = $route->match($httpRequest);
42: if ($appRequest !== NULL) {
43: $appRequest->setPresenterName($this->module . $appRequest->getPresenterName());
44: return $appRequest;
45: }
46: }
47: return NULL;
48: }
49:
50:
51: /**
52: * Constructs absolute URL from Request object.
53: * @return string|NULL
54: */
55: public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl)
56: {
57: if ($this->cachedRoutes === NULL) {
58: $routes = array();
59: $routes['*'] = array();
60:
61: foreach ($this as $route) {
62: $presenter = $route instanceof Route ? $route->getTargetPresenter() : NULL;
63:
64: if ($presenter === FALSE) {
65: continue;
66: }
67:
68: if (is_string($presenter)) {
69: $presenter = strtolower($presenter);
70: if (!isset($routes[$presenter])) {
71: $routes[$presenter] = $routes['*'];
72: }
73: $routes[$presenter][] = $route;
74:
75: } else {
76: foreach ($routes as $id => $foo) {
77: $routes[$id][] = $route;
78: }
79: }
80: }
81:
82: $this->cachedRoutes = $routes;
83: }
84:
85: if ($this->module) {
86: if (strncasecmp($tmp = $appRequest->getPresenterName(), $this->module, strlen($this->module)) === 0) {
87: $appRequest = clone $appRequest;
88: $appRequest->setPresenterName(substr($tmp, strlen($this->module)));
89: } else {
90: return NULL;
91: }
92: }
93:
94: $presenter = strtolower($appRequest->getPresenterName());
95: if (!isset($this->cachedRoutes[$presenter])) {
96: $presenter = '*';
97: }
98:
99: foreach ($this->cachedRoutes[$presenter] as $route) {
100: $url = $route->constructUrl($appRequest, $refUrl);
101: if ($url !== NULL) {
102: return $url;
103: }
104: }
105:
106: return NULL;
107: }
108:
109:
110: /**
111: * Adds the router.
112: * @param mixed
113: * @param Nette\Application\IRouter
114: * @return void
115: */
116: public function offsetSet($index, $route)
117: {
118: if (!$route instanceof Nette\Application\IRouter) {
119: throw new Nette\InvalidArgumentException('Argument must be IRouter descendant.');
120: }
121: parent::offsetSet($index, $route);
122: }
123:
124:
125: /**
126: * @return string
127: */
128: public function getModule()
129: {
130: return $this->module;
131: }
132:
133: }
134: