Auto-configure the post factory

References #347
This commit is contained in:
Oliver Davies 2021-01-13 23:58:11 +00:00
parent 43455d5ae1
commit 322d948c1e
2 changed files with 36 additions and 3 deletions

View file

@ -7,6 +7,3 @@ services:
Drupal\opdavies_blog\Service\PostPusher\PostPusher:
class: Drupal\opdavies_blog\Service\PostPusher\NullPostPusher
Drupal\opdavies_blog_test\Factory\PostFactory:
autowire: true

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_blog_test;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Finder\Finder;
final class OpdaviesBlogTestServiceProvider implements ServiceProviderInterface {
public function register(ContainerBuilder $container): void {
foreach (['Factory'] as $directory) {
$files = Finder::create()
->in(__DIR__ . '/' . $directory)
->files()
->name('*.php');
foreach ($files as $file) {
$class = 'Drupal\opdavies_blog_test\\' . $directory . '\\' .
str_replace('/', '\\', substr($file->getRelativePathname(), 0, -4));
if ($container->hasDefinition($class)) {
continue;
}
$definition = new Definition($class);
$definition->setAutowired(TRUE);
$container->setDefinition($class, $definition);
}
}
}
}