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