8000 minor #19875 Use DI exceptions in components compiler passes (chalasr) · symfony/symfony@1f9f87b · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f9f87b

Browse files
committed
minor #19875 Use DI exceptions in components compiler passes (chalasr)
This PR was merged into the 3.2-dev branch. Discussion ---------- Use DI exceptions in components compiler passes | Q | A | ------------- | --- | Branch? | master | License | MIT Commits ------- f2e30bc Use DI exceptions in components compiler passes
2 parents a2a442b + f2e30bc commit 1f9f87b

File tree

10 files changed

+39
-28
lines changed

10 files changed

+39
-28
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Reference;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1719

1820
/**
1921
* Registers event listeners and subscribers to the available doctrine connections.
@@ -77,7 +79,7 @@ public function process(ContainerBuilder $container)
7779
uasort($subscribers, $sortFunc);
7880
foreach ($subscribers as $id => $instance) {
7981
if ($container->getDefinition($id)->isAbstract()) {
80-
throw new \InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event subscriber.', $id));
82+
throw new InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event subscriber.', $id));
8183
}
8284

8385
$em->addMethodCall('addEventSubscriber', array(new Reference($id)));
@@ -93,7 +95,7 @@ public function process(ContainerBuilder $container)
9395
uasort($listeners, $sortFunc);
9496
foreach ($listeners as $id => $instance) {
9597
if ($container->getDefinition($id)->isAbstract()) {
96-
throw new \InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event listener.', $id));
98+
throw new InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event listener.', $id));
9799
}
98100

99101
$em->addMethodCall('addEventListener', array(
@@ -116,7 +118,7 @@ private function groupByConnection(array $services, $isListener = false)
116118
foreach ($instances as $instance) {
117119
if ($isListener) {
118120
if (!isset($instance['event'])) {
119-
throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
121+
throw new InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
120122
}
121123
$instance['event'] = array($instance['event']);
122124

@@ -128,7 +130,7 @@ private function groupByConnection(array $services, $isListener = false)
128130
$cons = isset($instance['connection']) ? array($instance['connection']) : $allCons;
129131
foreach ($cons as $con) {
130132
if (!isset($grouped[$con])) {
131-
throw new \RuntimeException(sprintf('The Doctrine connection "%s" referenced in service "%s" does not exist. Available connections names: %s', $con, $id, implode(', ', array_keys($this->connections))));
133+
throw new RuntimeException(sprintf('The Doctrine connection "%s" referenced in service "%s" does not exist. Available connections names: %s', $con, $id, implode(', ', array_keys($this->connections))));
132134
}
133135

134136
if ($isListener && isset($grouped[$con][$id])) {

src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterMappingsPass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Definition;
1616
use Symfony\Component\DependencyInjection\Reference;
1717
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1819

1920
/**
2021
* Base class for the doctrine bundles to provide a compiler pass class that
@@ -231,7 +232,7 @@ private function getManagerName(ContainerBuilder $container)
231232
}
232233
}
233234

234-
throw new \InvalidArgumentException(sprintf(
235+
throw new InvalidArgumentException(sprintf(
235236
'Could not find the manager name parameter in the container. Tried the following parameter names: "%s"',
236237
implode('", "', $this->managerParameters)
237238
));

src/Symfony/Bundle/FrameworkBundle/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;

src/Symfony/Bundle/FrameworkBundle/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) {

src/Symfony/Bundle/FrameworkBundle/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

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Bundle/FrameworkBundle/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)));

src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php

Lines changed: 8 additions & 7 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
* Compiler pass to register tagged services for an event dispatcher.
@@ -59,18 +60,18 @@ public function process(ContainerBuilder $container)
5960
foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
6061
$def = $container->getDefinition($id);
6162
if (!$def->isPublic()) {
62-
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
63+
throw new InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
6364
}
6465

6566
if ($def->isAbstract()) {
66-
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
67+
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
6768
}
6869

6970
foreach ($events as $event) {
7071
$priority = isset($event['priority']) ? $event['priority'] : 0;
7172

7273
if (!isset($event['event'])) {
73-
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
74+
throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
7475
}
7576

7677
if (!isset($event['method'])) {
@@ -88,11 +89,11 @@ public function process(ContainerBuilder $container)
8889
foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
8990
$def = $container->getDefinition($id);
9091
if (!$def->isPublic()) {
91-
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
92+
throw new InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
9293
}
9394

9495
if ($def->isAbstract()) {
95-
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
96+
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
9697
}
9798

9899
// We must assume that the class value has been correctly filled, even if the service is created by a factory
@@ -101,10 +102,10 @@ public function process(ContainerBuilder $container)
101102

102103
if (!is_subclass_of($class, $interface)) {
103104
if (!class_exists($class, false)) {
104-
throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
105+
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
105106
}
106107

107-
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
108+
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
108109
}
109110

110111
$definition->addMethodCall('addSubscriberService', array($id, $class));

src/Symfony/Component/HttpKernel/DependencyInjection/FragmentRendererPass.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 services tagged kernel.fragment_renderer as HTTP content rendering strategies.
@@ -44,22 +45,22 @@ public function process(ContainerBuilder $container)
4445
foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) {
4546
$def = $container->getDefinition($id);
4647
if (!$def->isPublic()) {
47-
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
48+
throw new InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
4849
}
4950

5051
if ($def->isAbstract()) {
51-
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
52+
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
5253
}
5354

5455
$class = $container->getParameterBag()->resolveValue($def->getClass());
5556
$interface = 'Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface';
5657

5758
if (!is_subclass_of($class, $interface)) {
5859
if (!class_exists($class, false)) {
59-
throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
60+
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
6061
}
6162

62-
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
63+
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
6364
}
6465

6566
foreach ($tags as $tag) {

0 commit comments

Comments
 (0)
0