1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://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: $name = $appRequest->getPresenterName();
44: if (strncmp($name, 'Nette:', 6)) {
45: $appRequest->setPresenterName($this->module . $name);
46: }
47: return $appRequest;
48: }
49: }
50: return NULL;
51: }
52:
53:
54: /**
55: * Constructs absolute URL from Request object.
56: * @return string|NULL
57: */
58: public function constructUrl(Nette\Application\Request $appRequest, Nette\Http\Url $refUrl)
59: {
60: if ($this->cachedRoutes === NULL) {
61: $routes = array();
62: $routes['*'] = array();
63:
64: foreach ($this as $route) {
65: $presenter = $route instanceof Route ? $route->getTargetPresenter() : NULL;
66:
67: if ($presenter === FALSE) {
68: continue;
69: }
70:
71: if (is_string($presenter)) {
72: $presenter = strtolower($presenter);
73: if (!isset($routes[$presenter])) {
74: $routes[$presenter] = $routes['*'];
75: }
76: $routes[$presenter][] = $route;
77:
78: } else {
79: foreach ($routes as $id => $foo) {
80: $routes[$id][] = $route;
81: }
82: }
83: }
84:
85: $this->cachedRoutes = $routes;
86: }
87:
88: if ($this->module) {
89: if (strncasecmp($tmp = $appRequest->getPresenterName(), $this->module, strlen($this->module)) === 0) {
90: $appRequest = clone $appRequest;
91: $appRequest->setPresenterName(substr($tmp, strlen($this->module)));
92: } else {
93: return NULL;
94: }
95: }
96:
97: $presenter = strtolower($appRequest->getPresenterName());
98: if (!isset($this->cachedRoutes[$presenter])) {
99: $presenter = '*';
100: }
101:
102: foreach ($this->cachedRoutes[$presenter] as $route) {
103: $url = $route->constructUrl($appRequest, $refUrl);
104: if ($url !== NULL) {
105: return $url;
106: }
107: }
108:
109: return NULL;
110: }
111:
112:
113: /**
114: * Adds the router.
115: * @param mixed
116: * @param Nette\Application\IRouter
117: * @return void
118: */
119: public function offsetSet($index, $route)
120: {
121: if (!$route instanceof Nette\Application\IRouter) {
122: throw new Nette\InvalidArgumentException('Argument must be IRouter descendant.');
123: }
124: parent::offsetSet($index, $route);
125: }
126:
127:
128: /**
129: * @return string
130: */
131: public function getModule()
132: {
133: return $this->module;
134: }
135:
136: }
137: