|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\WebFrontend\DependencyInjection\Compiler; |
| 6 | + |
| 7 | +use ReflectionException; |
| 8 | +use ReflectionClass; |
| 9 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 10 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 11 | + |
| 12 | +class RegisterEndpointsPass implements CompilerPassInterface |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @SuppressWarnings("CyclomaticComplexity") |
| 16 | + * @SuppressWarnings("NPathComplexity") |
| 17 | + */ |
| 18 | + public function process(ContainerBuilder $container): void |
| 19 | + { |
| 20 | + $projectDir = $container->hasParameter('kernel.application_dir') |
| 21 | + ? $container->getParameter('kernel.application_dir') |
| 22 | + : $container->getParameter('kernel.project_dir'); |
| 23 | + |
| 24 | + if (! is_string($projectDir)) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + $endpointDirectory = rtrim($projectDir, '/') . '/vendor/tatevikgr/rest-api-client/src/Endpoint'; |
| 29 | + if (! is_dir($endpointDirectory)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + $endpointFiles = glob($endpointDirectory . '/*.php') ?: []; |
| 34 | + |
| 35 | + foreach ($endpointFiles as $endpointFile) { |
| 36 | + $className = 'PhpList\\RestApiClient\\Endpoint\\' . basename($endpointFile, '.php'); |
| 37 | + |
| 38 | + if ($container->hasDefinition($className) || $container->hasAlias($className)) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + if (! class_exists($className)) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $reflection = new ReflectionClass($className); |
| 48 | + if (! $reflection->isInstantiable()) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + } catch (ReflectionException) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + $container |
| 56 | + ->autowire($className, $className) |
| 57 | + ->setAutowired(true) |
| 58 | + ->setAutoconfigured(true) |
| 59 | + ->setPublic(false); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments