Skip to content

Commit

Permalink
Make DI container more similar between API and CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Feb 4, 2025
1 parent cd0991e commit 2529e06
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 49 deletions.
29 changes: 1 addition & 28 deletions src/Infrastructure/API/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@

namespace HexagonalPlayground\Infrastructure\API;

use HexagonalPlayground\Application\ServiceProvider as ApplicationServiceProvider;
use HexagonalPlayground\Infrastructure\API\Event\RequestEvent;
use HexagonalPlayground\Infrastructure\API\Event\ResponseEvent;
use HexagonalPlayground\Infrastructure\API\ServiceProvider as ApiServiceProvider;
use HexagonalPlayground\Infrastructure\API\GraphQL\RouteProvider as GraphQLRouteProvider;
use HexagonalPlayground\Infrastructure\API\GraphQL\ServiceProvider as GraphQLServiceProvider;
use HexagonalPlayground\Infrastructure\API\Health\RouteProvider as HealthRouteProvider;
use HexagonalPlayground\Infrastructure\API\Health\ServiceProvider as HealthServiceProvider;
use HexagonalPlayground\Infrastructure\API\Logos\RouteProvider as LogosRouteProvider;
use HexagonalPlayground\Infrastructure\API\Logos\ServiceProvider as LogosServiceProvider;
use HexagonalPlayground\Infrastructure\API\Metrics\RouteProvider as MetricsRouteProvider;
use HexagonalPlayground\Infrastructure\API\Metrics\ServiceProvider as MetricsServiceProvider;
use HexagonalPlayground\Infrastructure\API\Security\AuthenticationMiddleware;
use HexagonalPlayground\Infrastructure\API\Security\RateLimitMiddleware;
use HexagonalPlayground\Infrastructure\API\Security\ServiceProvider as SecurityServiceProvider;
use HexagonalPlayground\Infrastructure\ContainerBuilder;
use HexagonalPlayground\Infrastructure\Email\MailServiceProvider;
use HexagonalPlayground\Infrastructure\Filesystem\ServiceProvider as FilesystemServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\ORM\DoctrineServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\EventServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\Read\ReadRepositoryProvider;
use Iterator;
use Middlewares\TrailingSlash;
use Psr\Container\ContainerInterface;
Expand All @@ -39,22 +27,7 @@ class Application extends App
{
public function __construct()
{
$serviceProviders = [
new HealthServiceProvider(),
new ApplicationServiceProvider(),
new DoctrineServiceProvider(),
new ReadRepositoryProvider(),
new SecurityServiceProvider(),
new MailServiceProvider(),
new EventServiceProvider(),
new GraphQLServiceProvider(),
new LogosServiceProvider(),
new ApiServiceProvider(),
new FilesystemServiceProvider(),
new MetricsServiceProvider()
];

$container = ContainerBuilder::build($serviceProviders);
$container = ContainerBuilder::build(new ServiceProvider());

parent::__construct($container->get(ResponseFactoryInterface::class), $container);

Expand Down
19 changes: 1 addition & 18 deletions src/Infrastructure/CLI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
use Doctrine\ORM\Tools\Console\EntityManagerProvider;
use HexagonalPlayground\Application\Security\AuthContext;
use HexagonalPlayground\Application\ServiceProvider as ApplicationServiceProvider;
use HexagonalPlayground\Domain\User;
use HexagonalPlayground\Infrastructure\CLI\ServiceProvider as CliServiceProvider;
use HexagonalPlayground\Infrastructure\ContainerBuilder;
use HexagonalPlayground\Infrastructure\Email\MailServiceProvider;
use HexagonalPlayground\Infrastructure\Filesystem\ServiceProvider as FilesystemServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\EventServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\ORM\DoctrineServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\Read\ReadRepositoryProvider;
use Iterator;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -32,17 +25,7 @@ class Application extends \Symfony\Component\Console\Application

public function __construct()
{
$serviceProviders = [
new ApplicationServiceProvider(),
new DoctrineServiceProvider(),
new ReadRepositoryProvider(),
new MailServiceProvider(),
new EventServiceProvider(),
new CliServiceProvider(),
new FilesystemServiceProvider()
];

$this->container = ContainerBuilder::build($serviceProviders);
$this->container = ContainerBuilder::build(new ServiceProvider());

$user = new User(
'cli',
Expand Down
40 changes: 37 additions & 3 deletions src/Infrastructure/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@
namespace HexagonalPlayground\Infrastructure;

use DI;
use Iterator;
use HexagonalPlayground\Application\ServiceProvider as ApplicationServiceProvider;
use HexagonalPlayground\Infrastructure\API\GraphQL\ServiceProvider as GraphQLServiceProvider;
use HexagonalPlayground\Infrastructure\API\Health\ServiceProvider as HealthServiceProvider;
use HexagonalPlayground\Infrastructure\API\Logos\ServiceProvider as LogosServiceProvider;
use HexagonalPlayground\Infrastructure\API\Metrics\ServiceProvider as MetricsServiceProvider;
use HexagonalPlayground\Infrastructure\API\Security\ServiceProvider as SecurityServiceProvider;
use HexagonalPlayground\Infrastructure\Filesystem\ServiceProvider as FilesystemServiceProvider;
use HexagonalPlayground\Infrastructure\Email\MailServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\ORM\DoctrineServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\EventServiceProvider;
use HexagonalPlayground\Infrastructure\Persistence\Read\ReadRepositoryProvider;
use HexagonalPlayground\Application\ServiceProviderInterface;
use Psr\Container\ContainerInterface;

class ContainerBuilder
{
/**
* @param array|ServiceProviderInterface[] $serviceProviders
* @param ServiceProviderInterface $additionalServices
* @return ContainerInterface
*/
public static function build(array $serviceProviders): ContainerInterface
public static function build(ServiceProviderInterface $additionalServices): ContainerInterface
{
$params = [
'app.home' => getenv('APP_HOME') ?: realpath(__DIR__ . '/../..'),
Expand All @@ -30,10 +42,32 @@ public static function build(array $serviceProviders): ContainerInterface
Config::class => $config
]);

foreach ($serviceProviders as $provider) {
foreach (self::getServiceProviders() as $provider) {
$builder->addDefinitions($provider->getDefinitions());
}

$builder->addDefinitions($additionalServices->getDefinitions());

return $builder->build();
}

/**
* Returns an iterator for common service providers
*
* @return ServiceProviderInterface[]
*/
private static function getServiceProviders(): Iterator
{
yield new ApplicationServiceProvider();
yield new DoctrineServiceProvider();
yield new EventServiceProvider();
yield new FilesystemServiceProvider();
yield new GraphQLServiceProvider();
yield new HealthServiceProvider();
yield new LogosServiceProvider();
yield new MailServiceProvider();
yield new MetricsServiceProvider();
yield new ReadRepositoryProvider();
yield new SecurityServiceProvider();
}
}

0 comments on commit 2529e06

Please sign in to comment.