-
-
Notifications
You must be signed in to change notification settings - Fork 844
/
MainControllerResourceMetadataCollectionFactory.php
61 lines (50 loc) · 1.93 KB
/
MainControllerResourceMetadataCollectionFactory.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
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Metadata\Resource\Factory;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
/**
* @author Antoine Bluchet <soyuka@gmail.com>
*/
final class MainControllerResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
use OperationDefaultsTrait;
public function __construct(private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, private ?bool $useSymfonyEvents = false)
{
}
/**
* {@inheritdoc}
*/
public function create(string $resourceClass): ResourceMetadataCollection
{
$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
if ($this->decorated) {
$resourceMetadataCollection = $this->decorated->create($resourceClass);
}
foreach ($resourceMetadataCollection as $i => $resource) {
/** @var ApiResource $resource */
$operations = $resource->getOperations() ?? new Operations();
foreach ($resource->getOperations() as $key => $operation) {
if ($operation->getRouteName() || $operation->getController()) {
continue;
}
if (false === $this->useSymfonyEvents) {
$operation = $operation->withController('api_platform.symfony.main_controller');
$operations->add($key, $operation);
}
}
$resource = $resource->withOperations($operations);
$resourceMetadataCollection[$i] = $resource;
}
return $resourceMetadataCollection;
}
}