Skip to content

Commit 53cee6e

Browse files
committed
Use DI exceptions in components compiler passes
1 parent 4250842 commit 53cee6e

File tree

6 files changed

+18
-12
lines changed

6 files changed

+18
-12
lines changed

DependencyInjection/Compiler/AddConsoleCommandPass.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617

1718
/**
1819
* AddConsoleCommandPass.
@@ -30,16 +31,16 @@ public function process(ContainerBuilder $container)
3031
$definition = $container->getDefinition($id);
3132

3233
if ($definition->isAbstract()) {
33-
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
34+
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
3435
}
3536

3637
$class = $container->getParameterBag()->resolveValue($definition->getClass());
3738
if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) {
3839
if (!class_exists($class, false)) {
39-
throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
40+
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
4041
}
4142

42-
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
43+
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
4344
}
4445
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
4546
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;

DependencyInjection/Compiler/CachePoolPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Definition;
1818
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1919
use Symfony\Component\DependencyInjection\Reference;
20+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2021

2122
/**
2223
* @author Nicolas Grekas <p@tchwork.com>
@@ -73,7 +74,7 @@ public function process(ContainerBuilder $container)
7374
unset($tags[0][$attr]);
7475
}
7576
if (!empty($tags[0])) {
76-
throw new \InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0]))));
77+
throw new InvalidArgumentException(sprintf('Invalid "cache.pool" tag for service "%s": accepted attributes are "clearer", "provider", "namespace" and "default_lifetime", found "%s".', $id, implode('", "', array_keys($tags[0]))));
7778
}
7879

7980
if (null !== $clearer) {

DependencyInjection/Compiler/FormPass.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1617

1718
/**
1819
* Adds all services with the tags "form.type" and "form.type_guesser" as
@@ -36,7 +37,7 @@ public function process(ContainerBuilder $container)
3637
foreach ($container->findTaggedServiceIds('form.type') as $serviceId => $tag) {
3738
$serviceDefinition = $container->getDefinition($serviceId);
3839
if (!$serviceDefinition->isPublic()) {
39-
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form types are lazy-loaded.', $serviceId));
40+
throw new InvalidArgumentException(sprintf('The service "%s" must be public as form types are lazy-loaded.', $serviceId));
4041
}
4142

4243
// Support type access by FQCN
@@ -50,13 +51,13 @@ public function process(ContainerBuilder $container)
5051
foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
5152
$serviceDefinition = $container->getDefinition($serviceId);
5253
if (!$serviceDefinition->isPublic()) {
53-
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId));
54+
throw new InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId));
5455
}
5556

5657
if (isset($tag[0]['extended_type'])) {
5758
$extendedType = $tag[0]['extended_type'];
5859
} else {
59-
throw new \InvalidArgumentException(sprintf('Tagged form type extension must have the extended type configured using the extended_type/extended-type attribute, none was configured for the "%s" service.', $serviceId));
60+
throw new InvalidArgumentException(sprintf('Tagged form type extension must have the extended type configured using the extended_type/extended-type attribute, none was configured for the "%s" service.', $serviceId));
6061
}
6162

6263
$typeExtensions[$extendedType][] = $serviceId;
@@ -69,7 +70,7 @@ public function process(ContainerBuilder $container)
6970
foreach ($guessers as $serviceId) {
7071
$serviceDefinition = $container->getDefinition($serviceId);
7172
if (!$serviceDefinition->isPublic()) {
72-
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type guessers are lazy-loaded.', $serviceId));
73+
throw new InvalidArgumentException(sprintf('The service "%s" must be public as form type guessers are lazy-loaded.', $serviceId));
7374
}
7475
}
7576

DependencyInjection/Compiler/ProfilerPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Reference;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1718

1819
/**
1920
* Adds tagged data_collector services to profiler service.
@@ -38,7 +39,7 @@ public function process(ContainerBuilder $container)
3839

3940
if (isset($attributes[0]['template'])) {
4041
if (!isset($attributes[0]['id'])) {
41-
throw new \InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
42+
throw new InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
4243
}
4344
$template = array($attributes[0]['id'], $attributes[0]['template']);
4445
}

DependencyInjection/Compiler/SerializerPass.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1718

1819
/**
1920
* Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
@@ -35,14 +36,14 @@ public function process(ContainerBuilder $container)
3536
$normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container);
3637

3738
if (empty($normalizers)) {
38-
throw new \RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service');
39+
throw new RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service');
3940
}
4041
$container->getDefinition('serializer')->replaceArgument(0, $normalizers);
4142

4243
// Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service
4344
$encoders = $this->findAndSortTaggedServices('serializer.encoder', $container);
4445
if (empty($encoders)) {
45-
throw new \RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service');
46+
throw new RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service');
4647
}
4748
$container->getDefinition('serializer')->replaceArgument(1, $encoders);
4849
}

DependencyInjection/Compiler/TranslationExtractorPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Reference;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1718

1819
/**
1920
* Adds tagged translation.extractor services to translation extractor.
@@ -30,7 +31,7 @@ public function process(ContainerBuilder $container)
3031

3132
foreach ($container->findTaggedServiceIds('translation.extractor') as $id => $attributes) {
3233
if (!isset($attributes[0]['alias'])) {
33-
throw new \RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
34+
throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
3435
}
3536

3637
$definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id)));

0 commit comments

Comments
 (0)