@@ -22,68 +22,68 @@ |
||
22 | 22 | */ |
23 | 23 | class Router extends \Slim\Router |
24 | 24 | { |
25 | - /** |
|
26 | - * Build the path for a named route excluding the base path |
|
27 | - * |
|
28 | - * @param string $name Route name |
|
29 | - * @param array $data Named argument replacement data |
|
30 | - * @param array $queryParams Optional query string parameters |
|
31 | - * |
|
32 | - * @return string |
|
33 | - * |
|
34 | - * @throws RuntimeException If named route does not exist |
|
35 | - * @throws InvalidArgumentException If required data not provided |
|
36 | - */ |
|
37 | - public function relativePathFor($name, array $data = [], array $queryParams = []) |
|
38 | - { |
|
39 | - $route = $this->getNamedRoute($name); |
|
40 | - $pattern = $route->getPattern(); |
|
25 | + /** |
|
26 | + * Build the path for a named route excluding the base path |
|
27 | + * |
|
28 | + * @param string $name Route name |
|
29 | + * @param array $data Named argument replacement data |
|
30 | + * @param array $queryParams Optional query string parameters |
|
31 | + * |
|
32 | + * @return string |
|
33 | + * |
|
34 | + * @throws RuntimeException If named route does not exist |
|
35 | + * @throws InvalidArgumentException If required data not provided |
|
36 | + */ |
|
37 | + public function relativePathFor($name, array $data = [], array $queryParams = []) |
|
38 | + { |
|
39 | + $route = $this->getNamedRoute($name); |
|
40 | + $pattern = $route->getPattern(); |
|
41 | 41 | |
42 | - $routeDatas = $this->routeParser->parse($pattern); |
|
43 | - // $routeDatas is an array of all possible routes that can be made. There is |
|
44 | - // one routedata for each optional parameter plus one for no optional parameters. |
|
45 | - // |
|
46 | - // The most specific is last, so we look for that first. |
|
47 | - $routeDatas = array_reverse($routeDatas); |
|
42 | + $routeDatas = $this->routeParser->parse($pattern); |
|
43 | + // $routeDatas is an array of all possible routes that can be made. There is |
|
44 | + // one routedata for each optional parameter plus one for no optional parameters. |
|
45 | + // |
|
46 | + // The most specific is last, so we look for that first. |
|
47 | + $routeDatas = array_reverse($routeDatas); |
|
48 | 48 | |
49 | - $segments = $segmentKeys = []; |
|
50 | - foreach ($routeDatas as $routeData) { |
|
51 | - foreach ($routeData as $item) { |
|
52 | - if (is_string($item)) { |
|
53 | - // this segment is a static string |
|
54 | - $segments[] = $item; |
|
55 | - continue; |
|
56 | - } |
|
49 | + $segments = $segmentKeys = []; |
|
50 | + foreach ($routeDatas as $routeData) { |
|
51 | + foreach ($routeData as $item) { |
|
52 | + if (is_string($item)) { |
|
53 | + // this segment is a static string |
|
54 | + $segments[] = $item; |
|
55 | + continue; |
|
56 | + } |
|
57 | 57 | |
58 | - // This segment has a parameter: first element is the name |
|
59 | - if (!array_key_exists($item[0], $data)) { |
|
60 | - // we don't have a data element for this segment: cancel |
|
61 | - // testing this routeData item, so that we can try a less |
|
62 | - // specific routeData item. |
|
63 | - $segments = []; |
|
64 | - $segmentName = $item[0]; |
|
65 | - break; |
|
66 | - } |
|
67 | - $segments[] = $data[$item[0]]; |
|
68 | - $segmentKeys[$item[0]] = true; |
|
69 | - } |
|
70 | - if (!empty($segments)) { |
|
71 | - // we found all the parameters for this route data, no need to check |
|
72 | - // less specific ones |
|
73 | - break; |
|
74 | - } |
|
75 | - } |
|
58 | + // This segment has a parameter: first element is the name |
|
59 | + if (!array_key_exists($item[0], $data)) { |
|
60 | + // we don't have a data element for this segment: cancel |
|
61 | + // testing this routeData item, so that we can try a less |
|
62 | + // specific routeData item. |
|
63 | + $segments = []; |
|
64 | + $segmentName = $item[0]; |
|
65 | + break; |
|
66 | + } |
|
67 | + $segments[] = $data[$item[0]]; |
|
68 | + $segmentKeys[$item[0]] = true; |
|
69 | + } |
|
70 | + if (!empty($segments)) { |
|
71 | + // we found all the parameters for this route data, no need to check |
|
72 | + // less specific ones |
|
73 | + break; |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - if (empty($segments)) { |
|
78 | - throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName); |
|
79 | - } |
|
80 | - $url = implode('', $segments); |
|
77 | + if (empty($segments)) { |
|
78 | + throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName); |
|
79 | + } |
|
80 | + $url = implode('', $segments); |
|
81 | 81 | |
82 | - $params = array_merge(array_diff_key($data, $segmentKeys), $queryParams); |
|
83 | - if ($params) { |
|
84 | - $url .= '?' . http_build_query($params); |
|
85 | - } |
|
82 | + $params = array_merge(array_diff_key($data, $segmentKeys), $queryParams); |
|
83 | + if ($params) { |
|
84 | + $url .= '?' . http_build_query($params); |
|
85 | + } |
|
86 | 86 | |
87 | - return $url; |
|
88 | - } |
|
87 | + return $url; |
|
88 | + } |
|
89 | 89 | } |
@@ -10,136 +10,136 @@ |
||
10 | 10 | |
11 | 11 | class RouterTest extends \PHPUnit_Framework_TestCase |
12 | 12 | { |
13 | - /** @var Router */ |
|
14 | - protected $router; |
|
15 | - |
|
16 | - public function setUp() |
|
17 | - { |
|
18 | - $this->router = new \Aimeos\Slim\Router; |
|
19 | - } |
|
20 | - |
|
21 | - public function testRelativePathFor() |
|
22 | - { |
|
23 | - $this->router->setBasePath('/base/path'); |
|
24 | - |
|
25 | - $methods = ['GET']; |
|
26 | - $pattern = '/hello/{first:\w+}/{last}'; |
|
27 | - $callable = function ($request, $response, $args) { |
|
28 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
29 | - }; |
|
30 | - $route = $this->router->map($methods, $pattern, $callable); |
|
31 | - $route->setName('foo'); |
|
32 | - |
|
33 | - $this->assertEquals( |
|
34 | - '/hello/josh/lockhart', |
|
35 | - $this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
36 | - ); |
|
37 | - } |
|
38 | - |
|
39 | - public function testPathForWithNoBasePath() |
|
40 | - { |
|
41 | - $this->router->setBasePath(''); |
|
42 | - |
|
43 | - $methods = ['GET']; |
|
44 | - $pattern = '/hello/{first:\w+}/{last}'; |
|
45 | - $callable = function ($request, $response, $args) { |
|
46 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
47 | - }; |
|
48 | - $route = $this->router->map($methods, $pattern, $callable); |
|
49 | - $route->setName('foo'); |
|
50 | - |
|
51 | - $this->assertEquals( |
|
52 | - '/hello/josh/lockhart', |
|
53 | - $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
54 | - ); |
|
55 | - } |
|
56 | - |
|
57 | - public function testPathForWithBasePath() |
|
58 | - { |
|
59 | - $methods = ['GET']; |
|
60 | - $pattern = '/hello/{first:\w+}/{last}'; |
|
61 | - $callable = function ($request, $response, $args) { |
|
62 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
63 | - }; |
|
64 | - $this->router->setBasePath('/base/path'); |
|
65 | - $route = $this->router->map($methods, $pattern, $callable); |
|
66 | - $route->setName('foo'); |
|
67 | - |
|
68 | - $this->assertEquals( |
|
69 | - '/base/path/hello/josh/lockhart', |
|
70 | - $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
71 | - ); |
|
72 | - } |
|
73 | - |
|
74 | - public function testPathForWithOptionalParameters() |
|
75 | - { |
|
76 | - $methods = ['GET']; |
|
77 | - $pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]'; |
|
78 | - $callable = function ($request, $response, $args) { |
|
79 | - return $response; |
|
80 | - }; |
|
81 | - $route = $this->router->map($methods, $pattern, $callable); |
|
82 | - $route->setName('foo'); |
|
83 | - |
|
84 | - $this->assertEquals( |
|
85 | - '/archive/2015', |
|
86 | - $this->router->pathFor('foo', ['year' => '2015']) |
|
87 | - ); |
|
88 | - $this->assertEquals( |
|
89 | - '/archive/2015/07', |
|
90 | - $this->router->pathFor('foo', ['year' => '2015', 'month' => '07']) |
|
91 | - ); |
|
92 | - $this->assertEquals( |
|
93 | - '/archive/2015/07/d/19', |
|
94 | - $this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19']) |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - public function testPathForWithSurplusRouteParameters() |
|
99 | - { |
|
100 | - $methods = ['GET']; |
|
101 | - $pattern = '/hello/{name}'; |
|
102 | - $callable = function ($request, $response, $args) { |
|
103 | - echo sprintf('Hello %s', $args['name']); |
|
104 | - }; |
|
105 | - $route = $this->router->map($methods, $pattern, $callable); |
|
106 | - $route->setName('foo'); |
|
107 | - |
|
108 | - $this->assertEquals( |
|
109 | - '/hello/josh?a=b', |
|
110 | - $this->router->pathFor('foo', ['name' => 'josh', 'a' => 'b']) |
|
111 | - ); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @expectedException \InvalidArgumentException |
|
116 | - */ |
|
117 | - public function testPathForWithMissingSegmentData() |
|
118 | - { |
|
119 | - $methods = ['GET']; |
|
120 | - $pattern = '/hello/{first}/{last}'; |
|
121 | - $callable = function ($request, $response, $args) { |
|
122 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
123 | - }; |
|
124 | - $route = $this->router->map($methods, $pattern, $callable); |
|
125 | - $route->setName('foo'); |
|
126 | - |
|
127 | - $this->router->pathFor('foo', ['last' => 'lockhart']); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @expectedException \RuntimeException |
|
132 | - */ |
|
133 | - public function testPathForRouteNotExists() |
|
134 | - { |
|
135 | - $methods = ['GET']; |
|
136 | - $pattern = '/hello/{first}/{last}'; |
|
137 | - $callable = function ($request, $response, $args) { |
|
138 | - echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
139 | - }; |
|
140 | - $route = $this->router->map($methods, $pattern, $callable); |
|
141 | - $route->setName('foo'); |
|
142 | - |
|
143 | - $this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']); |
|
144 | - } |
|
13 | + /** @var Router */ |
|
14 | + protected $router; |
|
15 | + |
|
16 | + public function setUp() |
|
17 | + { |
|
18 | + $this->router = new \Aimeos\Slim\Router; |
|
19 | + } |
|
20 | + |
|
21 | + public function testRelativePathFor() |
|
22 | + { |
|
23 | + $this->router->setBasePath('/base/path'); |
|
24 | + |
|
25 | + $methods = ['GET']; |
|
26 | + $pattern = '/hello/{first:\w+}/{last}'; |
|
27 | + $callable = function ($request, $response, $args) { |
|
28 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
29 | + }; |
|
30 | + $route = $this->router->map($methods, $pattern, $callable); |
|
31 | + $route->setName('foo'); |
|
32 | + |
|
33 | + $this->assertEquals( |
|
34 | + '/hello/josh/lockhart', |
|
35 | + $this->router->relativePathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
36 | + ); |
|
37 | + } |
|
38 | + |
|
39 | + public function testPathForWithNoBasePath() |
|
40 | + { |
|
41 | + $this->router->setBasePath(''); |
|
42 | + |
|
43 | + $methods = ['GET']; |
|
44 | + $pattern = '/hello/{first:\w+}/{last}'; |
|
45 | + $callable = function ($request, $response, $args) { |
|
46 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
47 | + }; |
|
48 | + $route = $this->router->map($methods, $pattern, $callable); |
|
49 | + $route->setName('foo'); |
|
50 | + |
|
51 | + $this->assertEquals( |
|
52 | + '/hello/josh/lockhart', |
|
53 | + $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
54 | + ); |
|
55 | + } |
|
56 | + |
|
57 | + public function testPathForWithBasePath() |
|
58 | + { |
|
59 | + $methods = ['GET']; |
|
60 | + $pattern = '/hello/{first:\w+}/{last}'; |
|
61 | + $callable = function ($request, $response, $args) { |
|
62 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
63 | + }; |
|
64 | + $this->router->setBasePath('/base/path'); |
|
65 | + $route = $this->router->map($methods, $pattern, $callable); |
|
66 | + $route->setName('foo'); |
|
67 | + |
|
68 | + $this->assertEquals( |
|
69 | + '/base/path/hello/josh/lockhart', |
|
70 | + $this->router->pathFor('foo', ['first' => 'josh', 'last' => 'lockhart']) |
|
71 | + ); |
|
72 | + } |
|
73 | + |
|
74 | + public function testPathForWithOptionalParameters() |
|
75 | + { |
|
76 | + $methods = ['GET']; |
|
77 | + $pattern = '/archive/{year}[/{month:[\d:{2}]}[/d/{day}]]'; |
|
78 | + $callable = function ($request, $response, $args) { |
|
79 | + return $response; |
|
80 | + }; |
|
81 | + $route = $this->router->map($methods, $pattern, $callable); |
|
82 | + $route->setName('foo'); |
|
83 | + |
|
84 | + $this->assertEquals( |
|
85 | + '/archive/2015', |
|
86 | + $this->router->pathFor('foo', ['year' => '2015']) |
|
87 | + ); |
|
88 | + $this->assertEquals( |
|
89 | + '/archive/2015/07', |
|
90 | + $this->router->pathFor('foo', ['year' => '2015', 'month' => '07']) |
|
91 | + ); |
|
92 | + $this->assertEquals( |
|
93 | + '/archive/2015/07/d/19', |
|
94 | + $this->router->pathFor('foo', ['year' => '2015', 'month' => '07', 'day' => '19']) |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + public function testPathForWithSurplusRouteParameters() |
|
99 | + { |
|
100 | + $methods = ['GET']; |
|
101 | + $pattern = '/hello/{name}'; |
|
102 | + $callable = function ($request, $response, $args) { |
|
103 | + echo sprintf('Hello %s', $args['name']); |
|
104 | + }; |
|
105 | + $route = $this->router->map($methods, $pattern, $callable); |
|
106 | + $route->setName('foo'); |
|
107 | + |
|
108 | + $this->assertEquals( |
|
109 | + '/hello/josh?a=b', |
|
110 | + $this->router->pathFor('foo', ['name' => 'josh', 'a' => 'b']) |
|
111 | + ); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @expectedException \InvalidArgumentException |
|
116 | + */ |
|
117 | + public function testPathForWithMissingSegmentData() |
|
118 | + { |
|
119 | + $methods = ['GET']; |
|
120 | + $pattern = '/hello/{first}/{last}'; |
|
121 | + $callable = function ($request, $response, $args) { |
|
122 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
123 | + }; |
|
124 | + $route = $this->router->map($methods, $pattern, $callable); |
|
125 | + $route->setName('foo'); |
|
126 | + |
|
127 | + $this->router->pathFor('foo', ['last' => 'lockhart']); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @expectedException \RuntimeException |
|
132 | + */ |
|
133 | + public function testPathForRouteNotExists() |
|
134 | + { |
|
135 | + $methods = ['GET']; |
|
136 | + $pattern = '/hello/{first}/{last}'; |
|
137 | + $callable = function ($request, $response, $args) { |
|
138 | + echo sprintf('Hello %s %s', $args['first'], $args['last']); |
|
139 | + }; |
|
140 | + $route = $this->router->map($methods, $pattern, $callable); |
|
141 | + $route->setName('foo'); |
|
142 | + |
|
143 | + $this->router->pathFor('bar', ['first' => 'josh', 'last' => 'lockhart']); |
|
144 | + } |
|
145 | 145 | } |