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:
Oliver Davies 2021-01-13 23:10:38 +00:00
parent f6791516c5
commit 53ab326415
9 changed files with 183 additions and 26 deletions

View file

@ -140,6 +140,7 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/zendframework/zend-escaper" />
<excludeFolder url="file://$MODULE_DIR$/vendor/zendframework/zend-feed" />
<excludeFolder url="file://$MODULE_DIR$/vendor/zendframework/zend-stdlib" />
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/config" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

View file

@ -149,9 +149,12 @@
<path value="$PROJECT_DIR$/vendor/beberlei/assert" />
<path value="$PROJECT_DIR$/vendor/topfloor/composer-cleanup-vcs-dirs" />
<path value="$PROJECT_DIR$/vendor/tightenco/collect" />
<path value="$PROJECT_DIR$/vendor/symfony/config" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.4" />
<component name="PhpProjectSharedConfiguration" php_language_level="8.0">
<option name="suggestChangeDefaultLanguageLevel" value="false" />
</component>
<component name="PhpStan">
<PhpStan_settings>
<PhpStanConfiguration tool_path="$PROJECT_DIR$/vendor/bin/phpstan" />

View file

@ -41,6 +41,7 @@
"drupal/stage_file_proxy": "^1.0",
"drush/drush": "^10",
"nesbot/carbon": "^2.33",
"symfony/config": "^3.4",
"tightenco/collect": "^8.17"
},
"require-dev": {

78
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "cda1968001f7e8b3c87b26f9eae2d89d",
"content-hash": "65f1f71774508070ca5c65a6f52e7fd8",
"packages": [
{
"name": "asm89/stack-cors",
@ -5673,6 +5673,82 @@
],
"time": "2020-03-15T09:38:08+00:00"
},
{
"name": "symfony/config",
"version": "v3.4.47",
"source": {
"type": "git",
"url": "https://github.com/symfony/config.git",
"reference": "bc6b3fd3930d4b53a60b42fe2ed6fc466b75f03f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/config/zipball/bc6b3fd3930d4b53a60b42fe2ed6fc466b75f03f",
"reference": "bc6b3fd3930d4b53a60b42fe2ed6fc466b75f03f",
"shasum": ""
},
"require": {
"php": "^5.5.9|>=7.0.8",
"symfony/filesystem": "~2.8|~3.0|~4.0",
"symfony/polyfill-ctype": "~1.8"
},
"conflict": {
"symfony/dependency-injection": "<3.3",
"symfony/finder": "<3.3"
},
"require-dev": {
"symfony/dependency-injection": "~3.3|~4.0",
"symfony/event-dispatcher": "~3.3|~4.0",
"symfony/finder": "~3.3|~4.0",
"symfony/yaml": "~3.0|~4.0"
},
"suggest": {
"symfony/yaml": "To use the yaml reference dumper"
},
"type": "library",
"autoload": {
"psr-4": {
"Symfony\\Component\\Config\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Config Component",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/config/tree/v3.4.47"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2020-10-24T10:57:07+00:00"
},
{
"name": "symfony/console",
"version": "v3.4.41",

View file

@ -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

View 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);
}
}
}
}

View file

@ -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

View file

@ -1,13 +1,10 @@
services:
Drupal\Component\Datetime\TimeInterface:
alias: datetime.time
Drupal\Core\Entity\EntityTypeManagerInterface:
alias: entity_type.manager
Drupal\opdavies_talks\EventSubscriber\UpdateTalkNodeBeforeSave:
tags:
- { name: event_subscriber }
Drupal\opdavies_talks\Repository\TalkRepository:
autowire: true
Drupal\opdavies_talks\Service\TalkCounter:
autowire: true
Drupal\opdavies_talks\Service\TalkDateUpdater:
autowire: true

View file

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Drupal\opdavies_talks;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Finder\Finder;
final class OpdaviesTalksServiceProvider 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_talks\\' . $directory . '\\' .
str_replace('/', '\\', substr($file->getRelativePathname(), 0, -4));
if ($container->hasDefinition($class)) {
continue;
}
$definition = new Definition($class);
$definition->setAutowired(TRUE);
$container->setDefinition($class, $definition);
}
}
}
}