Skip to content

Commit 8575a21

Browse files
committed
Add MiddlewareAllowAllTrait, implements HTTP method check in middleware
1 parent d54c1e5 commit 8575a21

File tree

5 files changed

+124
-14
lines changed

5 files changed

+124
-14
lines changed

src/Business/Middleware/MiddlewareLocator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,15 @@ public function locate(Request $request): \Traversable
3636

3737
/** @var array<int, HttpMiddlewarePluginInterface[]> $pc */
3838
$pc = [];
39+
$requestMethod = strtolower($request->getMethod());
3940

41+
/** @var HttpMiddlewarePluginInterface $middleware */
4042
foreach ($it as $middleware) {
43+
$methods = array_map('strtolower', $middleware->getRequestMatchMethods());
44+
if (!\in_array($requestMethod, $methods)) {
45+
continue;
46+
}
47+
4148
$middlewareMatch = $middleware->getRequestMatchPath();
4249
$pattern = '/'.preg_replace('/\//', '\/', $middlewareMatch).'/i';
4350

src/Plugin/HttpMiddlewarePluginInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
*/
2121
interface HttpMiddlewarePluginInterface
2222
{
23+
/**
24+
* @return string[]
25+
*/
26+
public function getRequestMatchMethods(): array;
27+
2328
/**
2429
* Match pattern.
2530
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http\Plugin;
15+
16+
trait MiddlewareAllowAllTrait
17+
{
18+
/**
19+
* @return string[]
20+
*/
21+
public function getRequestMatchMethods(): array
22+
{
23+
return ['get', 'post', 'patch', 'put', 'delete'];
24+
}
25+
26+
public function getRequestMatchPath(): string
27+
{
28+
return '^/';
29+
}
30+
31+
public function getMiddlewarePriority(): int
32+
{
33+
return 0;
34+
}
35+
}

tests/Unit/Business/Middleware/MiddlewareLocatorTest.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,26 @@ public function middlewareCollectionConfig(): array
5757
{
5858
return [
5959
// Success
60-
['^/one/two', 3],
61-
['/three', 1],
62-
['^/one', null],
63-
['^/one/(\b[a-z]+)/three', 2],
64-
['^/One/(\b[a-z]+)/ThreE/(\d+)/success', 4],
60+
['^/one/two', 3, ['gEt', 'PoSt']],
61+
['/three', 1, ['get']],
62+
['^/one', null, ['get', 'put']],
63+
['^/one/(\b[a-z]+)/three', 2, ['get']],
64+
['^/One/(\b[a-z]+)/ThreE/(\d+)/success', 4, ['get']],
6565

6666
// will no executed
67-
['^/one$', null],
68-
['/none', null],
69-
['/(\d+)/$', null],
70-
['^/one/(\b[a-z]+)/three/four', 2],
71-
['^/one/two/three/four', null],
72-
['/one/two/three/four/', null],
67+
68+
['^/one/two', 3, ['PoSt']],
69+
['/three', 1, ['put']],
70+
['^/one', null, ['patch']],
71+
['^/one/(\b[a-z]+)/three', 2, ['put']],
72+
['^/One/(\b[a-z]+)/ThreE/(\d+)/success', 4, ['put']],
73+
74+
['^/one$', null, ['get']],
75+
['/none', null, ['get']],
76+
['/(\d+)/$', null, ['get']],
77+
['^/one/(\b[a-z]+)/three/four', 2, ['get']],
78+
['^/one/two/three/four', null, ['get']],
79+
['/one/two/three/four/', null, ['get']],
7380
];
7481
}
7582

@@ -81,7 +88,7 @@ protected function createMiddlewareCollection(Request $request)
8188

8289
foreach ($config as $mc) {
8390
$middlewares[] = $this->createMiddlewareObj(
84-
$request,
91+
$mc[2],
8592
$mc[0],
8693
$mc[1],
8794
);
@@ -90,7 +97,7 @@ protected function createMiddlewareCollection(Request $request)
9097
return new \ArrayObject($middlewares);
9198
}
9299

93-
protected function createMiddlewareObj(Request $request, string $path, int|null $priority)
100+
protected function createMiddlewareObj(array $methods, string $path, int|null $priority)
94101
{
95102
if (!$priority) {
96103
$middleware = $this->createMock(HttpMiddlewarePluginInterface::class);
@@ -99,7 +106,12 @@ protected function createMiddlewareObj(Request $request, string $path, int|null
99106
$middleware->method('getMiddlewarePriority')->willReturn($priority);
100107
}
101108

102-
$middleware->expects($this->once())
109+
$middleware
110+
->expects($this->once())
111+
->method('getRequestMatchMethods')
112+
->willReturn($methods);
113+
114+
$middleware
103115
->method('getRequestMatchPath')
104116
->willReturn($path);
105117

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http\Test\Unit\Plugin;
15+
16+
use Micro\Plugin\Http\Plugin\HttpMiddlewareOrderedPluginInterface;
17+
use Micro\Plugin\Http\Plugin\HttpMiddlewarePluginInterface;
18+
use Micro\Plugin\Http\Plugin\MiddlewareAllowAllTrait;
19+
use PHPUnit\Framework\TestCase;
20+
use Symfony\Component\HttpFoundation\Request;
21+
22+
class MiddlewareAllowAllTraitTest extends TestCase
23+
{
24+
private HttpMiddlewarePluginInterface $plugin;
25+
26+
protected function setUp(): void
27+
{
28+
$this->plugin = new class() implements HttpMiddlewareOrderedPluginInterface {
29+
use MiddlewareAllowAllTrait;
30+
31+
public function processMiddleware(Request $request): void
32+
{
33+
}
34+
};
35+
}
36+
37+
public function testGetRequestMatchMethods()
38+
{
39+
$this->assertEquals(['get', 'post', 'patch', 'put', 'delete'], $this->plugin->getRequestMatchMethods());
40+
}
41+
42+
public function testGetRequestMatchPath()
43+
{
44+
$this->assertEquals('^/', $this->plugin->getRequestMatchPath());
45+
}
46+
47+
public function testGetMiddlewarePriority()
48+
{
49+
$this->assertEquals(0, $this->plugin->getMiddlewarePriority());
50+
}
51+
}

0 commit comments

Comments
 (0)