forked from api-platform/laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiPlatformMiddleware.php
49 lines (40 loc) · 1.49 KB
/
ApiPlatformMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Laravel;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactory;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class ApiPlatformMiddleware
{
public function __construct(
protected OperationMetadataFactory $operationMetadataFactory,
) {
}
/**
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, \Closure $next, ?string $operationName = null): Response
{
$operation = null;
if ($operationName) {
$request->attributes->set('_api_operation', $operation = $this->operationMetadataFactory->create($operationName));
}
if (!($format = $request->route('_format')) && $operation instanceof HttpOperation && str_ends_with($operation->getUriTemplate(), '{._format}')) {
$matches = [];
if (preg_match('/\.[a-zA-Z]+$/', $request->getPathInfo(), $matches)) {
$format = $matches[0];
}
}
$request->attributes->set('_format', $format ? substr($format, 1, \strlen($format) - 1) : '');
return $next($request);
}
}