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
This commit is contained in:
commit
1f9f87b177
@ -14,6 +14,8 @@ namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Registers event listeners and subscribers to the available doctrine connections.
|
||||
@ -77,7 +79,7 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
|
||||
uasort($subscribers, $sortFunc);
|
||||
foreach ($subscribers as $id => $instance) {
|
||||
if ($container->getDefinition($id)->isAbstract()) {
|
||||
throw new \InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event subscriber.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event subscriber.', $id));
|
||||
}
|
||||
|
||||
$em->addMethodCall('addEventSubscriber', array(new Reference($id)));
|
||||
@ -93,7 +95,7 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
|
||||
uasort($listeners, $sortFunc);
|
||||
foreach ($listeners as $id => $instance) {
|
||||
if ($container->getDefinition($id)->isAbstract()) {
|
||||
throw new \InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event listener.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The abstract service "%s" cannot be tagged as a doctrine event listener.', $id));
|
||||
}
|
||||
|
||||
$em->addMethodCall('addEventListener', array(
|
||||
@ -116,7 +118,7 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
|
||||
foreach ($instances as $instance) {
|
||||
if ($isListener) {
|
||||
if (!isset($instance['event'])) {
|
||||
throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
|
||||
throw new InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id));
|
||||
}
|
||||
$instance['event'] = array($instance['event']);
|
||||
|
||||
@ -128,7 +130,7 @@ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
|
||||
$cons = isset($instance['connection']) ? array($instance['connection']) : $allCons;
|
||||
foreach ($cons as $con) {
|
||||
if (!isset($grouped[$con])) {
|
||||
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))));
|
||||
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))));
|
||||
}
|
||||
|
||||
if ($isListener && isset($grouped[$con][$id])) {
|
||||
|
@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Base class for the doctrine bundles to provide a compiler pass class that
|
||||
@ -231,7 +232,7 @@ abstract class RegisterMappingsPass implements CompilerPassInterface
|
||||
}
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf(
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'Could not find the manager name parameter in the container. Tried the following parameter names: "%s"',
|
||||
implode('", "', $this->managerParameters)
|
||||
));
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* AddConsoleCommandPass.
|
||||
@ -30,16 +31,16 @@ class AddConsoleCommandPass implements CompilerPassInterface
|
||||
$definition = $container->getDefinition($id);
|
||||
|
||||
if ($definition->isAbstract()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
|
||||
}
|
||||
|
||||
$class = $container->getParameterBag()->resolveValue($definition->getClass());
|
||||
if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) {
|
||||
if (!class_exists($class, false)) {
|
||||
throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
|
||||
}
|
||||
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
|
||||
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;
|
||||
|
@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
@ -73,7 +74,7 @@ class CachePoolPass implements CompilerPassInterface
|
||||
unset($tags[0][$attr]);
|
||||
}
|
||||
if (!empty($tags[0])) {
|
||||
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]))));
|
||||
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]))));
|
||||
}
|
||||
|
||||
if (null !== $clearer) {
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Adds all services with the tags "form.type" and "form.type_guesser" as
|
||||
@ -36,7 +37,7 @@ class FormPass implements CompilerPassInterface
|
||||
foreach ($container->findTaggedServiceIds('form.type') as $serviceId => $tag) {
|
||||
$serviceDefinition = $container->getDefinition($serviceId);
|
||||
if (!$serviceDefinition->isPublic()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form types are lazy-loaded.', $serviceId));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must be public as form types are lazy-loaded.', $serviceId));
|
||||
}
|
||||
|
||||
// Support type access by FQCN
|
||||
@ -50,13 +51,13 @@ class FormPass implements CompilerPassInterface
|
||||
foreach ($container->findTaggedServiceIds('form.type_extension') as $serviceId => $tag) {
|
||||
$serviceDefinition = $container->getDefinition($serviceId);
|
||||
if (!$serviceDefinition->isPublic()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must be public as form type extensions are lazy-loaded.', $serviceId));
|
||||
}
|
||||
|
||||
if (isset($tag[0]['extended_type'])) {
|
||||
$extendedType = $tag[0]['extended_type'];
|
||||
} else {
|
||||
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));
|
||||
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));
|
||||
}
|
||||
|
||||
$typeExtensions[$extendedType][] = $serviceId;
|
||||
@ -69,7 +70,7 @@ class FormPass implements CompilerPassInterface
|
||||
foreach ($guessers as $serviceId) {
|
||||
$serviceDefinition = $container->getDefinition($serviceId);
|
||||
if (!$serviceDefinition->isPublic()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as form type guessers are lazy-loaded.', $serviceId));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must be public as form type guessers are lazy-loaded.', $serviceId));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Adds tagged data_collector services to profiler service.
|
||||
@ -38,7 +39,7 @@ class ProfilerPass implements CompilerPassInterface
|
||||
|
||||
if (isset($attributes[0]['template'])) {
|
||||
if (!isset($attributes[0]['id'])) {
|
||||
throw new \InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
|
||||
throw new InvalidArgumentException(sprintf('Data collector service "%s" must have an id attribute in order to specify a template', $id));
|
||||
}
|
||||
$template = array($attributes[0]['id'], $attributes[0]['template']);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as
|
||||
@ -35,14 +36,14 @@ class SerializerPass implements CompilerPassInterface
|
||||
$normalizers = $this->findAndSortTaggedServices('serializer.normalizer', $container);
|
||||
|
||||
if (empty($normalizers)) {
|
||||
throw new \RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service');
|
||||
throw new RuntimeException('You must tag at least one service as "serializer.normalizer" to use the Serializer service');
|
||||
}
|
||||
$container->getDefinition('serializer')->replaceArgument(0, $normalizers);
|
||||
|
||||
// Looks for all the services tagged "serializer.encoders" and adds them to the Serializer service
|
||||
$encoders = $this->findAndSortTaggedServices('serializer.encoder', $container);
|
||||
if (empty($encoders)) {
|
||||
throw new \RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service');
|
||||
throw new RuntimeException('You must tag at least one service as "serializer.encoder" to use the Serializer service');
|
||||
}
|
||||
$container->getDefinition('serializer')->replaceArgument(1, $encoders);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Adds tagged translation.extractor services to translation extractor.
|
||||
@ -30,7 +31,7 @@ class TranslationExtractorPass implements CompilerPassInterface
|
||||
|
||||
foreach ($container->findTaggedServiceIds('translation.extractor') as $id => $attributes) {
|
||||
if (!isset($attributes[0]['alias'])) {
|
||||
throw new \RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
|
||||
throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
|
||||
}
|
||||
|
||||
$definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id)));
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\EventDispatcher\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Compiler pass to register tagged services for an event dispatcher.
|
||||
@ -59,18 +60,18 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
|
||||
$def = $container->getDefinition($id);
|
||||
if (!$def->isPublic()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
|
||||
}
|
||||
|
||||
if ($def->isAbstract()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
|
||||
}
|
||||
|
||||
foreach ($events as $event) {
|
||||
$priority = isset($event['priority']) ? $event['priority'] : 0;
|
||||
|
||||
if (!isset($event['event'])) {
|
||||
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
|
||||
throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
|
||||
}
|
||||
|
||||
if (!isset($event['method'])) {
|
||||
@ -88,11 +89,11 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
|
||||
$def = $container->getDefinition($id);
|
||||
if (!$def->isPublic()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
|
||||
}
|
||||
|
||||
if ($def->isAbstract()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
|
||||
}
|
||||
|
||||
// We must assume that the class value has been correctly filled, even if the service is created by a factory
|
||||
@ -101,10 +102,10 @@ class RegisterListenersPass implements CompilerPassInterface
|
||||
|
||||
if (!is_subclass_of($class, $interface)) {
|
||||
if (!class_exists($class, false)) {
|
||||
throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||
}
|
||||
|
||||
$definition->addMethodCall('addSubscriberService', array($id, $class));
|
||||
|
@ -13,6 +13,7 @@ namespace Symfony\Component\HttpKernel\DependencyInjection;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Adds services tagged kernel.fragment_renderer as HTTP content rendering strategies.
|
||||
@ -44,11 +45,11 @@ class FragmentRendererPass implements CompilerPassInterface
|
||||
foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) {
|
||||
$def = $container->getDefinition($id);
|
||||
if (!$def->isPublic()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
|
||||
}
|
||||
|
||||
if ($def->isAbstract()) {
|
||||
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
|
||||
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
|
||||
}
|
||||
|
||||
$class = $container->getParameterBag()->resolveValue($def->getClass());
|
||||
@ -56,10 +57,10 @@ class FragmentRendererPass implements CompilerPassInterface
|
||||
|
||||
if (!is_subclass_of($class, $interface)) {
|
||||
if (!class_exists($class, false)) {
|
||||
throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
|
Reference in New Issue
Block a user