1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Application;
13:
14: use Nette;
15:
16:
17:
18: 19: 20: 21: 22:
23: class MultiRouter extends Nette\Collections\ArrayList implements IRouter
24: {
25:
26: private $cachedRoutes;
27:
28:
29:
30: public function __construct()
31: {
32: parent::__construct(NULL, 'Nette\Application\IRouter');
33: }
34:
35:
36:
37: 38: 39: 40: 41:
42: public function match(Nette\Web\IHttpRequest $httpRequest)
43: {
44: foreach ($this as $route) {
45: $appRequest = $route->match($httpRequest);
46: if ($appRequest !== NULL) {
47: return $appRequest;
48: }
49: }
50: return NULL;
51: }
52:
53:
54:
55: 56: 57: 58: 59: 60:
61: public function constructUrl(PresenterRequest $appRequest, Nette\Web\IHttpRequest $httpRequest)
62: {
63: if ($this->cachedRoutes === NULL) {
64: $routes = array();
65: $routes['*'] = array();
66:
67: foreach ($this as $route) {
68: $presenter = $route instanceof Route ? $route->getTargetPresenter() : NULL;
69:
70: if ($presenter === FALSE) continue;
71:
72: if (is_string($presenter)) {
73: $presenter = strtolower($presenter);
74: if (!isset($routes[$presenter])) {
75: $routes[$presenter] = $routes['*'];
76: }
77: $routes[$presenter][] = $route;
78:
79: } else {
80: foreach ($routes as $id => $foo) {
81: $routes[$id][] = $route;
82: }
83: }
84: }
85:
86: $this->cachedRoutes = $routes;
87: }
88:
89: $presenter = strtolower($appRequest->getPresenterName());
90: if (!isset($this->cachedRoutes[$presenter])) $presenter = '*';
91:
92: foreach ($this->cachedRoutes[$presenter] as $route) {
93: $uri = $route->constructUrl($appRequest, $httpRequest);
94: if ($uri !== NULL) {
95: return $uri;
96: }
97: }
98:
99: return NULL;
100: }
101:
102: }
103: