1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\DataProvider\StateProvider; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Metadata\Operation; |
17
|
|
|
use ApiPlatform\State\ProviderInterface; |
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\Route; |
19
|
|
|
use Silverback\ApiComponentsBundle\Repository\Core\RouteRepository; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Daniel West <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class RouteStateProvider implements ProviderInterface |
25
|
|
|
{ |
26
|
|
|
private const ALREADY_CALLED = 'ROUTE_DATA_PROVIDER_ALREADY_CALLED'; |
27
|
|
|
|
28
|
|
|
private RouteRepository $routeRepository; |
29
|
|
|
private ProviderInterface $defaultProvider; |
30
|
|
|
|
31
|
|
|
public function __construct(RouteRepository $routeRepository, ProviderInterface $defaultProvider) |
32
|
|
|
{ |
33
|
|
|
$this->routeRepository = $routeRepository; |
34
|
|
|
$this->defaultProvider = $defaultProvider; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null |
38
|
|
|
{ |
39
|
|
|
$id = $uriVariables['id']; |
40
|
|
|
|
41
|
|
|
$context[self::ALREADY_CALLED] = true; |
42
|
|
|
if (!\is_string($id)) { |
43
|
|
|
return $this->defaultProvider->provide($operation, $uriVariables, $context); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->routeRepository->findOneByIdOrPath($id); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function supports(string $resourceClass, array $uriVariables = [], ?string $operationName = null, array $context = []): bool |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
/** @var Operation */ |
52
|
|
|
$operation = $context['operation']; |
53
|
|
|
|
54
|
|
|
return Route::class === $resourceClass && !$operation->isCollection() && !isset($context[self::ALREADY_CALLED]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.