Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autowiring in ServiceMap #721

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/Drupal/DrupalAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,6 @@ public function register(Container $container): void
continue;
}
foreach ($yaml['services'] as $serviceId => $serviceDefinition) {
// Check if this is an alias shortcut.
// @link https://symfony.com/doc/4.4/service_container/alias_private.html#aliasing
if (is_string($serviceDefinition)) {
$serviceDefinition = [
'alias' => str_replace('@', '', $serviceDefinition),
];
}
// Prevent \Nette\DI\ContainerBuilder::completeStatement from array_walk_recursive into the arguments
// and thinking these are real services for PHPStan's container.
if (isset($serviceDefinition['arguments']) && is_array($serviceDefinition['arguments'])) {
Expand Down
30 changes: 26 additions & 4 deletions src/Drupal/ServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public function setDrupalServices(array $drupalServices): void
$decorators = [];

foreach ($drupalServices as $serviceId => $serviceDefinition) {
// @todo this duplicates \Symfony\Component\DependencyInjection\Loader\YamlFileLoader::parseDefinition()
// Can we re-use the YamlFileLoader instead of this manual parsing?
if (is_string($serviceDefinition) && strpos($serviceDefinition, '@') === 0) {
$serviceDefinition = [
'alias' => substr($serviceDefinition, 1)
];
}
if (is_null($serviceDefinition)) {
$serviceDefinition = [];
}

if (!is_array($serviceDefinition)) {
continue;
}

if (isset($serviceDefinition['alias'], $drupalServices[$serviceDefinition['alias']])) {
$serviceDefinition = $drupalServices[$serviceDefinition['alias']];
}
Expand All @@ -40,6 +55,10 @@ public function setDrupalServices(array $drupalServices): void
}

// @todo support factories
if (isset($serviceDefinition['factory'])) {
continue;
}

if (!isset($serviceDefinition['class'])) {
if (class_exists($serviceId)) {
$serviceDefinition['class'] = $serviceId;
Expand All @@ -60,11 +79,14 @@ public function setDrupalServices(array $drupalServices): void
}

foreach ($decorators as $decorated_service_id => $services) {
foreach ($services as $dcorating_service_id) {
if (!isset(self::$services[$decorated_service_id])) {
if (!isset(self::$services[$decorated_service_id])) {
continue;
}
foreach ($services as $decorating_service_id) {
if (!isset(self::$services[$decorating_service_id])) {
continue;
}
self::$services[$decorated_service_id]->addDecorator(self::$services[$dcorating_service_id]);
self::$services[$decorated_service_id]->addDecorator(self::$services[$decorating_service_id]);
}
}
}
Expand All @@ -73,7 +95,7 @@ private function resolveParentDefinition(string $parentId, array $serviceDefinit
{
$parentDefinition = $drupalServices[$parentId] ?? [];
if ([] === $parentDefinition) {
return $serviceDefinition;
return $parentDefinition;
}

if (isset($parentDefinition['parent'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autowire: true

Drupal\phpstan_fixtures\Foo:
Drupal\phpstan_fixtures\BarInterface: '@Drupal\phpstan_fixtures\Bar'
Drupal\phpstan_fixtures\Bar:
decorates: Drupal\phpstan_fixtures\Foo
14 changes: 14 additions & 0 deletions tests/fixtures/drupal/modules/phpstan_fixtures/src/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Drupal\phpstan_fixtures;

final class Bar implements BarInterface
{

public function __construct(Foo $inner)
{
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Drupal\phpstan_fixtures;

interface BarInterface
{

}
7 changes: 7 additions & 0 deletions tests/fixtures/drupal/modules/phpstan_fixtures/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Drupal\phpstan_fixtures;

final class Foo {

}
14 changes: 14 additions & 0 deletions tests/src/ServiceMapFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class ServiceMapFactoryTest extends TestCase
*/
public function testFactory(string $id, callable $validator): void
{
require_once __DIR__ . '/data/bug-693.php';

$service = new ServiceMap();
$service->setDrupalServices([
'entity_type.manager' => [
Expand Down Expand Up @@ -112,6 +114,11 @@ public function testFactory(string $id, callable $validator): void
'decorates' => 'unknown',
'class' => 'Drupal\service_map\Override',
],
'Bug693\Foo' => null,
'Bug693\BarInterface' => '@Bug693\Bar',
'Bug693\Bar' => [
'decorates' => 'Bug693\Foo'
],
]);
$validator($service->getService($id));
}
Expand Down Expand Up @@ -241,6 +248,13 @@ function (DrupalServiceDefinition $service): void {
self::assertArrayHasKey('service_map.decorates_decorating_base', $child_decorators);
}
];
yield [
'Bug693\Foo',
function (DrupalServiceDefinition $service): void {
self::assertCount(1, $service->getDecorators());
self::assertArrayHasKey('Bug693\\Bar', $service->getDecorators());
}
];
}

}
18 changes: 18 additions & 0 deletions tests/src/data/bug-693.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bug693;

final class Foo
{

}

interface BarInterface
{

}

final class Bar implements BarInterface
{

}
Loading