1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NMultiRouter extends NArrayList implements IRouter
22: {
23:
24: private $cachedRoutes;
25:
26:
27:
28: public function __construct()
29: {
30: parent::__construct(NULL, 'IRouter');
31: }
32:
33:
34:
35: 36: 37: 38: 39:
40: public function match(IHttpRequest $httpRequest)
41: {
42: foreach ($this as $route) {
43: $appRequest = $route->match($httpRequest);
44: if ($appRequest !== NULL) {
45: return $appRequest;
46: }
47: }
48: return NULL;
49: }
50:
51:
52:
53: 54: 55: 56: 57: 58:
59: public function constructUrl(NPresenterRequest $appRequest, IHttpRequest $httpRequest)
60: {
61: if ($this->cachedRoutes === NULL) {
62: $routes = array();
63: $routes['*'] = array();
64:
65: foreach ($this as $route) {
66: $presenter = $route instanceof NRoute ? $route->getTargetPresenter() : NULL;
67:
68: if ($presenter === FALSE) continue;
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: $presenter = strtolower($appRequest->getPresenterName());
88: if (!isset($this->cachedRoutes[$presenter])) $presenter = '*';
89:
90: foreach ($this->cachedRoutes[$presenter] as $route) {
91: $uri = $route->constructUrl($appRequest, $httpRequest);
92: if ($uri !== NULL) {
93: return $uri;
94: }
95: }
96:
97: return NULL;
98: }
99:
100: }
101: