Skip to content

Commit 2b3120d

Browse files
committed
RegisterEndpointsPass
1 parent 1675d4e commit 2b3120d

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

config/services.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ services:
2727
PhpList\RestApiClient\Client:
2828
$baseUrl: '%api_base_url%'
2929

30-
PhpList\RestApiClient\Endpoint\:
31-
resource: '../vendor/tatevikgr/rest-api-client/src/Endpoint/'
32-
autowire: true
33-
autoconfigure: true
34-
3530
PhpList\WebFrontend\EventListener\ApiSessionListener:
3631
tags:
3732
- { name: kernel.event_subscriber }
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

src/PhpListFrontendBundle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44

55
namespace PhpList\WebFrontend;
66

7+
use PhpList\WebFrontend\DependencyInjection\Compiler\RegisterEndpointsPass;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
79
use Symfony\Component\HttpKernel\Bundle\Bundle;
810

911
class PhpListFrontendBundle extends Bundle
1012
{
13+
public function build(ContainerBuilder $container): void
14+
{
15+
parent::build($container);
16+
17+
$container->addCompilerPass(new RegisterEndpointsPass());
18+
}
1119
}

0 commit comments

Comments
 (0)