Skip to content

Commit 591b3c5

Browse files
committed
Validate services
1 parent f1f94a8 commit 591b3c5

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

src/Helper/Drupal/ServiceInfo.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getServiceClasses(): array {
7474
/**
7575
* Gets service definition.
7676
*
77-
* @psalm-return array<string, string>|null
77+
* @psalm-return array{class: class-string}|null
7878
*/
7979
public function getServiceDefinition(string $service_id): ?array {
8080
$serialized_definitions = $this->getSerializedDefinitions();
@@ -91,6 +91,7 @@ public function getServiceDefinition(string $service_id): ?array {
9191
* @todo Add extended description.
9292
*/
9393
public function getServiceMeta(string $service_id): array {
94+
/** @psalm-suppress UnresolvableInclude */
9495
$dumped_meta = require Application::ROOT . '/resources/service-meta.php';
9596
// Most used core services are described statically.
9697
if (\array_key_exists($service_id, $dumped_meta)) {
@@ -99,10 +100,11 @@ public function getServiceMeta(string $service_id): array {
99100
// For services from contrib and custom modules we build meta on demand.
100101
elseif ($definition = $this->getServiceDefinition($service_id)) {
101102
$class = $definition['class'];
103+
/** @psalm-var class-string $interface */
102104
$interface = $class . 'Interface';
103105
$meta = [
104106
'name' => Utils::camelize($service_id, FALSE),
105-
'type' => \is_subclass_of($class, $interface) ? $interface : $class
107+
'type' => \is_subclass_of($class, $interface) ? $interface : $class,
106108
];
107109
}
108110
else {
@@ -115,6 +117,7 @@ public function getServiceMeta(string $service_id): array {
115117
$type_parts = \explode('\\', $meta['type']);
116118
$meta['short_type'] = \end($type_parts);
117119

120+
\ksort($meta);
118121
return $meta;
119122
}
120123

src/InputOutput/Interviewer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use DrupalCodeGenerator\Validator\Required;
1515
use DrupalCodeGenerator\Validator\RequiredClassName;
1616
use DrupalCodeGenerator\Validator\RequiredMachineName;
17+
use DrupalCodeGenerator\Validator\ServiceExists;
1718
use DrupalCodeGenerator\Validator\ServiceName;
1819
use Symfony\Component\Console\Question\ChoiceQuestion;
1920
use Symfony\Component\Console\Question\Question;
@@ -173,9 +174,13 @@ public function askServices(bool $default = TRUE, array $forced_services = []):
173174

174175
if ($this->io->confirm('Would you like to inject dependencies?', $default)) {
175176
$service_ids = $this->serviceInfo->getServicesIds();
177+
$validator = new Chained(
178+
new Optional(new ServiceName()),
179+
new Optional(new ServiceExists($this->serviceInfo)),
180+
);
176181
while (TRUE) {
177182
$question = new Question('Type the service name or use arrows up/down. Press enter to continue');
178-
$question->setValidator(new Optional(new ServiceName()));
183+
$question->setValidator($validator);
179184
$question->setAutocompleterValues($service_ids);
180185
$service = $this->io->askQuestion($question);
181186
if (!$service) {
@@ -187,7 +192,7 @@ public function askServices(bool $default = TRUE, array $forced_services = []):
187192

188193
$definitions = [];
189194
foreach (\array_unique($services) as $service_id) {
190-
$definitions[$service_id] = $this->getServiceDefinition($service_id);
195+
$definitions[$service_id] = $this->serviceInfo->getServiceMeta($service_id);
191196
}
192197
return $definitions;
193198
}

templates/_lib/di.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use {{ service.type }};
1212

1313
{% macro signature(services) %}
1414
{% for service in services %}
15-
private readonly {{ service.short_type }} ${{ service.name|camelize(false) }},{{ loop.last ? '' : constant('PHP_EOL') }}
15+
private readonly {{ service.short_type }} ${{ service.name }},{{ loop.last ? '' : constant('PHP_EOL') }}
1616
{%- endfor %}
1717
{% endmacro %}
1818

tests/functional/Helper/Drupal/ServiceInfoTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public function testGetServiceMeta(string $id, string $name, string $type, strin
169169

170170
$expected = [
171171
'name' => $name,
172-
'type' => $type,
173172
'short_type' => $short_type,
173+
'type' => $type,
174174
];
175175
self::assertSame($expected, $meta);
176176
}
@@ -189,10 +189,10 @@ public function serviceMetaProvider(): array {
189189
];
190190

191191
$data[] = [
192-
'logger.channel.user',
193-
'loggerChannelUser',
194-
'Drupal\Core\Logger\LoggerChannelInterface',
195-
'LoggerChannelInterface',
192+
'views.views_data_helper',
193+
'viewsViewsDataHelper',
194+
'Drupal\views\ViewsDataHelper',
195+
'ViewsDataHelper',
196196
NULL,
197197
];
198198

@@ -201,7 +201,7 @@ public function serviceMetaProvider(): array {
201201
'render_placeholder_generator',
202202
'Drupal\Core\EventSubscriber\AuthenticationSubscriber',
203203
'AuthenticationSubscriber',
204-
new \LogicException('Service "not.exists" does not exist.')
204+
new \LogicException('Service "not.exists" does not exist.'),
205205
];
206206

207207
return $data;

tests/functional/InputOutput/InterviewerTest.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,9 @@ public function testAskServices(): void {
504504
$answer = $interviewer->askServices();
505505
$expected_answer = [
506506
'entity_type.manager' => [
507-
'type' => 'Drupal\Core\Entity\EntityTypeManagerInterface',
508-
'name' => 'entity_type_manager',
509-
'description' => 'The entity type manager.',
507+
'name' => 'entityTypeManager',
510508
'short_type' => 'EntityTypeManagerInterface',
509+
'type' => 'Drupal\Core\Entity\EntityTypeManagerInterface',
511510
],
512511
];
513512
self::assertSame($expected_answer, $answer);
@@ -538,22 +537,16 @@ public function testAskServices(): void {
538537
// -- Non-existing service..
539538
$this->setStream("\nmissing\n\n");
540539
$answer = $interviewer->askServices();
541-
$expected_answer = [
542-
'missing' => [
543-
'name' => 'missing',
544-
'type' => 'Drupal\example\ExampleInterface',
545-
'description' => 'The missing service.',
546-
'short_type' => 'ExampleInterface',
547-
],
548-
];
540+
$expected_answer = [];
549541
self::assertSame($expected_answer, $answer);
550542

551543
$expected_output = <<< 'TXT'
552544
553545
Would you like to inject dependencies? [Yes]:
554546
555547
Type the service name or use arrows up/down. Press enter to continue:
556-
548+
➤ Service does not exists.
549+
557550
Type the service name or use arrows up/down. Press enter to continue:
558551
559552
TXT;

0 commit comments

Comments
 (0)