Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Middleware parameters #715

Open
wants to merge 23 commits into
base: v5-development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e22ea70
Merge pull request #686 from skipperbent/v5-development
skipperbent Nov 21, 2023
fd0237e
Merge pull request #687 from skipperbent/v5-release
skipperbent Nov 21, 2023
36dda20
Merge pull request #688 from skipperbent/v5-development
skipperbent Nov 21, 2023
ca053a6
Merge pull request #689 from skipperbent/v5-release
skipperbent Nov 21, 2023
ef4756c
Merge pull request #690 from skipperbent/v5-development
skipperbent Nov 27, 2023
3c03f08
Merge pull request #692 from skipperbent/v5-development
skipperbent Nov 27, 2023
b23f6a0
Merge pull request #691 from skipperbent/v5-release
skipperbent Nov 27, 2023
d75af21
Merge pull request #693 from skipperbent/v5-development
skipperbent Nov 27, 2023
fdbd72c
Merge pull request #694 from skipperbent/v5-release
skipperbent Nov 27, 2023
14c2199
Merge pull request #695 from skipperbent/v5-development
skipperbent Nov 27, 2023
0b01aa9
Merge pull request #698 from skipperbent/v5-development
skipperbent Nov 29, 2023
2a21524
Merge pull request #699 from skipperbent/v5-development
skipperbent Dec 9, 2023
8923bee
Merge pull request #700 from skipperbent/v5-development
skipperbent Dec 9, 2023
7098f6a
Merge pull request #696 from skipperbent/v5-release
skipperbent Dec 9, 2023
a2843d5
Merge pull request #702 from skipperbent/v5-development
skipperbent Dec 11, 2023
b98d40b
Merge pull request #703 from skipperbent/v5-release
skipperbent Dec 11, 2023
545a5a6
chore(tests): add `DummyMiddlewareWithParam` middleware
michaelthedev Jun 26, 2024
81cf684
chore(tests): add test for `DummyMiddlewareWithParam` middleware
michaelthedev Jun 26, 2024
8f5b956
chore(route): allow comma separated values as Middleware parameters o…
michaelthedev Jun 26, 2024
4b0d67d
chore: change namespace for release purpose
michaelthedev Jun 26, 2024
c09f1c7
chore: composer add author details
michaelthedev Jun 26, 2024
034a71d
chore: composer modify author details
michaelthedev Jul 7, 2024
8915bd5
chore: composer modify details
michaelthedev Jul 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pecee/simple-router",
"description": "Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router.",
"name": "michaelthedev/simple-router",
"description": "Simple, fast PHP router that is easy to get integrated and in almost any project. Heavily inspired by the Laravel router. (Forked from skipperbent/simple-php-router)",
"keywords": [
"router",
"router",
Expand All @@ -24,6 +24,10 @@
{
"name": "Simon Sessingø",
"email": "[email protected]"
},
{
"name": "Michael Arawole",
"email": "[email protected]"
}
],
"require": {
Expand All @@ -45,7 +49,7 @@
},
"autoload": {
"psr-4": {
"Pecee\\": "src/Pecee/"
"LogadApp\\": "src/Pecee/"
}
},
"config": {
Expand Down
13 changes: 12 additions & 1 deletion src/Pecee/SimpleRouter/Route/LoadableRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ public function loadMiddleware(Request $request, Router $router): void
$router->debug('Loading middlewares');

foreach ($this->getMiddlewares() as $middleware) {
$parameters = [];

if (is_object($middleware) === false) {
if (str_contains($middleware, ',')) {
$options = explode(',', $middleware);

//@todo: see efficiency of trim
$middleware = $options[0];

// set the rest of explode as parameters
$parameters = array_slice($options, 1);
}

$middleware = $router->getClassLoader()->loadClass($middleware);
}

Expand All @@ -49,7 +60,7 @@ public function loadMiddleware(Request $request, Router $router): void
$className = get_class($middleware);

$router->debug('Loading middleware "%s"', $className);
$middleware->handle($request);
$middleware->handle($request, ...$parameters);
$router->debug('Finished loading middleware "%s"', $className);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Pecee/SimpleRouter/Dummy/DummyMiddlewareWithParam.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once 'Exception/MiddlewareLoadedException.php';

use Pecee\Http\Request;

class DummyMiddlewareWithParam implements \Pecee\Http\Middleware\IMiddleware
{
public function handle(Request $request, ?string $val1 = null, ?string $val2 = null): void
{
throw new MiddlewareLoadedException('Middleware loaded! Param1: ' . $val1 . ' Param2: ' . $val2);
}

}
12 changes: 12 additions & 0 deletions tests/Pecee/SimpleRouter/MiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

require_once 'Dummy/DummyMiddleware.php';
require_once 'Dummy/DummyMiddlewareWithParam.php';
require_once 'Dummy/DummyController.php';
require_once 'Dummy/Handler/ExceptionHandler.php';

Expand Down Expand Up @@ -32,4 +33,15 @@ public function testNestedMiddlewareDontLoad()
$this->assertTrue(true);
}

public function testMiddlewareWithParameters()
{
$this->expectException(MiddlewareLoadedException::class);

TestRouter::group(['exceptionHandler' => 'ExceptionHandler'], function () {
TestRouter::get('/my/test/url', 'DummyController@method1', ['middleware' => 'DummyMiddlewareWithParam,4,hello']);
});

TestRouter::debug('/my/test/url', 'get');

}
}