Skip to content

Commit 0564e74

Browse files
committed
updated versionlog
1 parent 9a98f87 commit 0564e74

File tree

4 files changed

+63
-41
lines changed

4 files changed

+63
-41
lines changed

VERSIONLOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
## 0.6.8
2+
* Updated components.
3+
* Refactoring.
4+
* Improved tests.
5+
* Added additional uri cleanup.
26

37
## 0.6.7 2025-02-06
48
## 0.6.6 2025-01-27
@@ -7,7 +11,7 @@
711
## 0.6.4
812
## 0.6.3 2024-11-27
913
* Updated route signatures.
10-
* Added logging
14+
* Added logging.
1115

1216
## 0.6.2 2024-11-27
1317
* Updated dependencies.

src/Routing/Router.php

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ public function registerFilter( string $Name, Filter $Filter ): void
3030
}
3131

3232
/**
33-
* @param string $Name
33+
* @param string $routeName
3434
* @return Filter
3535
* @throws \Exception
3636
*/
3737

38-
public function getFilter( string $Name ) : Filter
38+
public function getFilter( string $routeName ) : Filter
3939
{
4040
$Filter = null;
4141

42-
if( array_key_exists( $Name, $this->_FilterRegistry ) )
42+
if( array_key_exists( $routeName, $this->_FilterRegistry ) )
4343
{
44-
$Filter = $this->_FilterRegistry[ $Name ];
44+
$Filter = $this->_FilterRegistry[ $routeName ];
4545
}
4646
else
4747
{
48-
throw new \Exception( "Filter $Name not registered." );
48+
throw new \Exception( "Filter $routeName not registered." );
4949
}
5050

5151
return $Filter;
@@ -140,6 +140,21 @@ protected function isRouteWithParams( RouteMap $Route ) : bool
140140
*/
141141
protected function processRoute( RouteMap $Route, $Uri ) : ?array
142142
{
143+
if( !$Uri )
144+
{
145+
$Uri = '/';
146+
}
147+
else if( $Uri[ 0 ] != '/' )
148+
{
149+
$Uri = '/' . $Uri;
150+
}
151+
152+
if( strlen( $Uri ) > 1 && $Uri[ strlen( $Uri ) - 1 ] == "/" )
153+
{
154+
$string = new NString( $Uri );
155+
$Uri = $string->left( $string->length() - 1 );
156+
}
157+
143158
// Does route have parameters?
144159
145160
if( $this->isRouteWithParams( $Route ) )
@@ -155,15 +170,6 @@ protected function processRoute( RouteMap $Route, $Uri ) : ?array
155170
}
156171
else
157172
{
158-
if( !$Uri )
159-
{
160-
$Uri = '/';
161-
}
162-
else if( $Uri[ 0 ] != '/' )
163-
{
164-
$Uri = '/' . $Uri;
165-
}
166-
167173
if( $Route->Path == $Uri )
168174
{
169175
return [];
@@ -179,6 +185,7 @@ protected function processRoute( RouteMap $Route, $Uri ) : ?array
179185
* @return array
180186
* @throws \Exception
181187
*/
188+
182189
protected function processRouteWithParameters( RouteMap $Route, string $Uri ) : array
183190
{
184191
$Details = $Route->parseParams();
@@ -192,9 +199,10 @@ protected function processRouteWithParameters( RouteMap $Route, string $Uri ) :
192199
* @param array $Details
193200
* @return array
194201
*/
202+
195203
protected function extractRouteParams( string $Uri, array $Details ) : array
196204
{
197-
if( $Uri && $Uri[ 0 ] == '/' )
205+
if( $Uri && $Uri[ 0 ] == '/' )
198206
{
199207
$String = new NString( $Uri );
200208
$Uri = $String->right( $String->length() - 1 );
@@ -213,12 +221,10 @@ protected function extractRouteParams( string $Uri, array $Details ) : array
213221
}
214222

215223
$action = $Details[ $iOffset ][ 'action' ];
216-
if( $action )
224+
225+
if( $action && $action != $Part )
217226
{
218-
if( $action != $Part )
219-
{
220-
return [];
221-
}
227+
return [];
222228
}
223229
else
224230
{
@@ -296,15 +302,7 @@ public function getRoute( int $Method, string $Uri ) : ?RouteMap
296302
{
297303
if( $Params )
298304
{
299-
if( is_array( $Params ) )
300-
{
301-
$Route->Parameters = $Params;
302-
}
303-
else
304-
{
305-
$Route->Parameters = [];
306-
}
307-
305+
$Route->Parameters = $Params;
308306
return $Route;
309307
}
310308
}
@@ -374,7 +372,9 @@ function run( array $Argv = [] ) : mixed
374372
$Type = $Argv[ 'type' ];
375373
}
376374

377-
$Route = $this->getRoute( RequestMethod::getType( $Type ), $Argv[ 'route' ] );
375+
$Uri = $Argv[ 'route' ];
376+
377+
$Route = $this->getRoute( RequestMethod::getType( $Type ), $Uri );
378378

379379
if( !$Route )
380380
{
@@ -394,14 +394,7 @@ function run( array $Argv = [] ) : mixed
394394

395395
if( array_key_exists( 'extra', $Argv ) )
396396
{
397-
if( is_array( $Route->Parameters ) )
398-
{
399-
$Route->Parameters = array_merge( $Route->Parameters, $Argv[ 'extra' ] );
400-
}
401-
else
402-
{
403-
$Route->Parameters = $Argv[ 'extra' ];
404-
}
397+
$Route->Parameters = array_merge( $Route->Parameters, $Argv[ 'extra' ] );
405398
}
406399

407400
$Route->Parameters = array_merge( $Route->Parameters, $Route->Payload );

tests/unit/FilterTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function( Routing\RouteMap $Route ) use ( &$Filter, &$Name )
3232
'/test',
3333
function(){},
3434
'PreFilter'
35-
);
35+
);
3636

3737
$Route = $this->Router->getRoute(
3838
Routing\RequestMethod::GET,
@@ -47,6 +47,15 @@ function(){},
4747
$this->assertEquals( '/test', $Name );
4848
}
4949

50+
public function testRouteNoFilter()
51+
{
52+
$Filter = false;
53+
$Name = '';
54+
55+
$this>$this->expectException(\Exception::class);
56+
$this->Router->getFilter( 'NoFilter' );
57+
}
58+
5059
public function testRoutePostFilter()
5160
{
5261
$Filter = false;

tests/unit/RouteTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ public function testPut()
7575
);
7676
}
7777

78+
public function testMissingMethodType()
79+
{
80+
Routing\Route::get(
81+
'/get/:id',
82+
function(){ return 'get'; }
83+
)->setName( 'test.get' );
84+
85+
$this->expectException( \Exception::class );
86+
87+
$Route = Routing\Router::getInstance()->run(
88+
[
89+
'route' => '/get/1'
90+
]
91+
);
92+
}
93+
7894
public function testGet()
7995
{
8096
Routing\Route::get(
@@ -85,7 +101,7 @@ function(){ return 'get'; }
85101

86102
$Route = Routing\Router::getInstance()->getRoute(
87103
Routing\RequestMethod::GET,
88-
'/get/1'
104+
'/get/1/'
89105
);
90106

91107
$this->assertEquals(

0 commit comments

Comments
 (0)