Auto-configure services and repositories
Enable the auto-configuration of service and repository classes, including support for classes in subdirectories by using `getRelativePathname()` rather than `getFilename()` and making some additional changes to the result. References #347
This commit is contained in:
parent
f6791516c5
commit
53ab326415
9 changed files with 183 additions and 26 deletions
web/modules/custom/blog
|
@ -1,24 +1,25 @@
|
|||
parameters:
|
||||
container.autowiring.strict_mode: true
|
||||
|
||||
services:
|
||||
Drupal\Core\Config\ConfigFactoryInterface:
|
||||
alias: config.factory
|
||||
|
||||
Drupal\Core\Entity\EntityTypeManagerInterface:
|
||||
alias: entity_type.manager
|
||||
|
||||
Drupal\Core\Queue\QueueFactory:
|
||||
alias: queue
|
||||
|
||||
Drupal\opdavies_blog\EventSubscriber\PushPostToSocialMediaOnceItIsPublished:
|
||||
autowire: true
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
|
||||
Drupal\opdavies_blog\EventSubscriber\SortTagsAlphabeticallyWhenPostIsSaved:
|
||||
autowire: true
|
||||
tags:
|
||||
- { name: event_subscriber }
|
||||
|
||||
Drupal\opdavies_blog\Repository\PostRepository:
|
||||
autowire: true
|
||||
|
||||
Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher:
|
||||
autowire: true
|
||||
|
||||
Drupal\opdavies_blog\Service\PostPusher\NullPostPusher:
|
||||
autowire: true
|
||||
|
||||
Drupal\opdavies_blog\Service\PostPusher\PostPusher:
|
||||
alias: Drupal\opdavies_blog\Service\PostPusher\IftttPostPusher
|
||||
|
||||
Drupal\opdavies_blog\Repository\RelatedPostsRepository:
|
||||
autowire: true
|
||||
GuzzleHttp\ClientInterface:
|
||||
alias: http_client
|
||||
|
|
36
web/modules/custom/blog/src/OpdaviesBlogServiceProvider.php
Normal file
36
web/modules/custom/blog/src/OpdaviesBlogServiceProvider.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
final class OpdaviesBlogServiceProvider implements ServiceProviderInterface {
|
||||
|
||||
public function register(ContainerBuilder $container): void {
|
||||
foreach (['Repository', 'Service'] as $directory) {
|
||||
$files = Finder::create()
|
||||
->in(__DIR__ . '/' . $directory)
|
||||
->files()
|
||||
->name('*.php');
|
||||
|
||||
foreach ($files as $file) {
|
||||
$class = 'Drupal\opdavies_blog\\' . $directory . '\\' .
|
||||
str_replace('/', '\\', substr($file->getRelativePathname(), 0, -4));
|
||||
|
||||
if ($container->hasDefinition($class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$definition = new Definition($class);
|
||||
$definition->setAutowired(TRUE);
|
||||
$container->setDefinition($class, $definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,12 @@
|
|||
parameters:
|
||||
container.autowiring.strict_mode: true
|
||||
|
||||
services:
|
||||
Drupal\Core\Entity\EntityTypeManagerInterface:
|
||||
alias: entity_type.manager
|
||||
|
||||
Drupal\opdavies_blog\Service\PostPusher\PostPusher:
|
||||
alias: Drupal\opdavies_blog\Service\PostPusher\NullPostPusher
|
||||
class: Drupal\opdavies_blog\Service\PostPusher\NullPostPusher
|
||||
|
||||
Drupal\opdavies_blog_test\Factory\PostFactory:
|
||||
autowire: true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue