Repository navigation
Expand file tree
/
Copy pathApiPlatformCompilerPass.php
More file actions
94 lines (76 loc) · 3.82 KB
/
Copy pathApiPlatformCompilerPass.php
File metadata and controls
94 lines (76 loc) · 3.82 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
* This file is part of the Silverback API Components Bundle Project
*
* (c) Daniel West <daniel@silverback.is>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silverback\ApiComponentsBundle\DependencyInjection\CompilerPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Configuration as ApiPlatformConfiguration;
use Silverback\ApiComponentsBundle\Helper\Collection\CollectionPopulator;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* @author Daniel West <daniel@silverback.is>
*/
class ApiPlatformCompilerPass implements CompilerPassInterface
{
private const string HTTP_CACHE_FLUSHER = 'silverback.api_components.http_cache.flusher';
private const string INVALIDATION_CLIENT_PREFIX = 'api_platform.invalidation_http_client.';
private const string EXCEPTION_TO_STATUS = 'api_platform.exception_to_status';
private const string SKOLEM_IRI_CONVERTER = 'api_platform.symfony.iri_converter.skolem';
public function process(ContainerBuilder $container): void
{
$itemsPerPageParameterName = $container->getParameter('api_platform.collection.pagination.items_per_page_parameter_name');
$container->findDefinition(CollectionPopulator::class)->setArgument('$itemsPerPageParameterName', $itemsPerPageParameterName);
if ($container->hasAlias('api_platform.http_cache.purger')) {
// we have implemented fully custom logic
$container->removeDefinition('api_platform.doctrine.listener.http_cache.purge');
} else {
$container->removeDefinition('silverback.api_components.http_cache.purger');
}
$this->configureHttpCacheFlusher($container);
$this->appendApiPlatformDefaultExceptionStatuses($container);
$this->resetSkolemIriConverter($container);
$apiPlatformMercurePublishListener = 'api_platform.doctrine.orm.listener.mercure.publish';
if ($container->hasDefinition($apiPlatformMercurePublishListener)) {
// we have implemented fully custom logic
$container->removeDefinition($apiPlatformMercurePublishListener);
}
}
private function appendApiPlatformDefaultExceptionStatuses(ContainerBuilder $container): void
{
/** @var ArrayNode $tree */
$tree = (new ApiPlatformConfiguration())->getConfigTreeBuilder()->buildTree();
$defaults = $tree->getChildren()['exception_to_status']->getDefaultValue();
$container->setParameter(self::EXCEPTION_TO_STATUS, $container->getParameter(self::EXCEPTION_TO_STATUS) + $defaults);
}
private function resetSkolemIriConverter(ContainerBuilder $container): void
{
if (!$container->hasDefinition(self::SKOLEM_IRI_CONVERTER)) {
return;
}
$definition = $container->getDefinition(self::SKOLEM_IRI_CONVERTER);
if (!$definition->hasTag('kernel.reset')) {
$definition->addTag('kernel.reset', ['method' => 'reset']);
}
}
private function configureHttpCacheFlusher(ContainerBuilder $container): void
{
if (!$container->hasDefinition(self::HTTP_CACHE_FLUSHER)) {
return;
}
$invalidationClients = [];
foreach ($container->getDefinitions() as $id => $definition) {
if (!str_starts_with($id, self::INVALIDATION_CLIENT_PREFIX)) {
continue;
}
$invalidationClients[] = ['url' => $definition->getArgument(1), 'client' => new Reference($id)];
}
$container->getDefinition(self::HTTP_CACHE_FLUSHER)->setArgument('$invalidationClients', $invalidationClients);
}
}