[DependencyInjection] Use component-specific SPL exceptions

This replaces existing use of core SPL exceptions with the equivalent classes defined within the component. Although method documentation has been changed, this change should be BC since the component-specific SPL exceptions extend their core counterpart.

This commit purposely omits any changes to the PhpDumper, which throws several core SPL exceptions.
This commit is contained in:
Jeremy Mikola 2011-12-04 15:51:22 -08:00
parent cf2ca9b196
commit 123f514e06
20 changed files with 125 additions and 97 deletions

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* This pass validates each definition individually only taking the information
@ -33,14 +34,14 @@ class CheckDefinitionValidityPass implements CompilerPassInterface
* Processes the ContainerBuilder to validate the Definition.
*
* @param ContainerBuilder $container
* @throws \RuntimeException When the Definition is invalid
* @throws RuntimeException When the Definition is invalid
*/
public function process(ContainerBuilder $container)
{
foreach ($container->getDefinitions() as $id => $definition) {
// synthetic service is public
if ($definition->isSynthetic() && !$definition->isPublic()) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'A synthetic service ("%s") must be public.',
$id
));
@ -48,7 +49,7 @@ class CheckDefinitionValidityPass implements CompilerPassInterface
// synthetic service has non-prototype scope
if ($definition->isSynthetic() && ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope()) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'A synthetic service ("%s") cannot be of scope "prototype".',
$id
));
@ -57,14 +58,14 @@ class CheckDefinitionValidityPass implements CompilerPassInterface
// non-synthetic, non-abstract service has class
if (!$definition->isAbstract() && !$definition->isSynthetic() && !$definition->getClass()) {
if ($definition->getFactoryClass() || $definition->getFactoryService()) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'Please add the class to service "%s" even if it is constructed by a factory '
.'since we might need to add method calls based on compile-time checks.',
$id
));
}
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'The definition for "%s" has no class. If you intend to inject '
.'this service dynamically at runtime, please mark it as synthetic=true. '
.'If this is an abstract definition solely used by child definitions, '

View File

@ -11,12 +11,13 @@
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\ScopeWideningInjectionException;
use Symfony\Component\DependencyInjection\Exception\ScopeCrossingInjectionException;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ScopeCrossingInjectionException;
use Symfony\Component\DependencyInjection\Exception\ScopeWideningInjectionException;
/**
* Checks the validity of references
@ -85,7 +86,7 @@ class CheckReferenceValidityPass implements CompilerPassInterface
* Validates an array of References.
*
* @param array $arguments An array of Reference objects
* @throws \RuntimeException when there is a reference to an abstract definition.
* @throws RuntimeException when there is a reference to an abstract definition.
*/
private function validateReferences(array $arguments)
{
@ -96,7 +97,7 @@ class CheckReferenceValidityPass implements CompilerPassInterface
$targetDefinition = $this->getDefinition((string) $argument);
if (null !== $targetDefinition && $targetDefinition->isAbstract()) {
throw new \RuntimeException(sprintf(
throw new RuntimeException(sprintf(
'The definition "%s" has a reference to an abstract definition "%s". '
.'Abstract definitions cannot be the target of references.',
$this->currentId,

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* Compiler Pass Configuration
*
@ -95,7 +97,7 @@ class PassConfig
*
* @param CompilerPassInterface $pass A Compiler pass
* @param string $type The pass type
* @throws \InvalidArgumentException when a pass type doesn't exist
* @throws InvalidArgumentException when a pass type doesn't exist
*
* @api
*/
@ -103,7 +105,7 @@ class PassConfig
{
$property = $type.'Passes';
if (!isset($this->$property)) {
throw new \InvalidArgumentException(sprintf('Invalid type "%s".', $type));
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
}
$passes = &$this->$property;

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* A pass that might be run repeatedly.
@ -32,7 +33,7 @@ class RepeatedPass implements CompilerPassInterface
{
foreach ($passes as $pass) {
if (!$pass instanceof RepeatablePassInterface) {
throw new \InvalidArgumentException('$passes must be an array of RepeatablePassInterface.');
throw new InvalidArgumentException('$passes must be an array of RepeatablePassInterface.');
}
$pass->setRepeatedPass($this);

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* This replaces all DefinitionDecorator instances with their equivalent fully
@ -60,7 +61,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
private function resolveDefinition($id, DefinitionDecorator $definition)
{
if (!$this->container->hasDefinition($parent = $definition->getParent())) {
throw new \RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
throw new RuntimeException(sprintf('The parent definition "%s" defined for definition "%s" does not exist.', $parent, $id));
}
$parentDef = $this->container->getDefinition($parent);
@ -116,7 +117,7 @@ class ResolveDefinitionTemplatesPass implements CompilerPassInterface
}
if (0 !== strpos($k, 'index_')) {
throw new \RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
throw new RuntimeException(sprintf('Invalid argument key "%s" found.', $k));
}
$index = (integer) substr($k, strlen('index_'));

View File

@ -14,6 +14,7 @@ namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* Emulates the invalid behavior if the reference is not found within the
@ -46,7 +47,7 @@ class ResolveInvalidReferencesPass implements CompilerPassInterface
foreach ($definition->getMethodCalls() as $call) {
try {
$calls[] = array($call[0], $this->processArguments($call[1], true));
} catch (\RuntimeException $ignore) {
} catch (RuntimeException $ignore) {
// this call is simply removed
}
}
@ -57,7 +58,7 @@ class ResolveInvalidReferencesPass implements CompilerPassInterface
try {
$value = $this->processArguments(array($value), true);
$properties[$name] = reset($value);
} catch (\RuntimeException $ignore) {
} catch (RuntimeException $ignore) {
// ignore property
}
}
@ -89,7 +90,7 @@ class ResolveInvalidReferencesPass implements CompilerPassInterface
$arguments[$k] = null;
} else if (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
if ($inMethodCall) {
throw new \RuntimeException('Method shouldn\'t be called.');
throw new RuntimeException('Method shouldn\'t be called.');
}
$arguments[$k] = null;

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* This is a directed graph of your services.
*
@ -51,7 +53,7 @@ class ServiceReferenceGraph
public function getNode($id)
{
if (!isset($this->nodes[$id])) {
throw new \InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
throw new InvalidArgumentException(sprintf('There is no node with id "%s".', $id));
}
return $this->nodes[$id];

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
@ -135,7 +137,7 @@ class Container implements ContainerInterface
*
* @return mixed The parameter value
*
* @throws \InvalidArgumentException if the parameter is not defined
* @throws InvalidArgumentException if the parameter is not defined
*
* @api
*/
@ -183,14 +185,14 @@ class Container implements ContainerInterface
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
{
if (self::SCOPE_PROTOTYPE === $scope) {
throw new \InvalidArgumentException('You cannot set services of scope "prototype".');
throw new InvalidArgumentException('You cannot set services of scope "prototype".');
}
$id = strtolower($id);
if (self::SCOPE_CONTAINER !== $scope) {
if (!isset($this->scopedServices[$scope])) {
throw new \RuntimeException('You cannot set services of inactive scopes.');
throw new RuntimeException('You cannot set services of inactive scopes.');
}
$this->scopedServices[$scope][$id] = $service;
@ -226,7 +228,7 @@ class Container implements ContainerInterface
*
* @return object The associated service
*
* @throws \InvalidArgumentException if the service is not defined
* @throws InvalidArgumentException if the service is not defined
*
* @see Reference
*
@ -292,11 +294,11 @@ class Container implements ContainerInterface
public function enterScope($name)
{
if (!isset($this->scopes[$name])) {
throw new \InvalidArgumentException(sprintf('The scope "%s" does not exist.', $name));
throw new InvalidArgumentException(sprintf('The scope "%s" does not exist.', $name));
}
if (self::SCOPE_CONTAINER !== $this->scopes[$name] && !isset($this->scopedServices[$this->scopes[$name]])) {
throw new \RuntimeException(sprintf('The parent scope "%s" must be active when entering this scope.', $this->scopes[$name]));
throw new RuntimeException(sprintf('The parent scope "%s" must be active when entering this scope.', $this->scopes[$name]));
}
// check if a scope of this name is already active, if so we need to
@ -330,14 +332,14 @@ class Container implements ContainerInterface
* scope.
*
* @param string $name The name of the scope to leave
* @throws \InvalidArgumentException if the scope is not active
* @throws InvalidArgumentException if the scope is not active
*
* @api
*/
public function leaveScope($name)
{
if (!isset($this->scopedServices[$name])) {
throw new \InvalidArgumentException(sprintf('The scope "%s" is not active.', $name));
throw new InvalidArgumentException(sprintf('The scope "%s" is not active.', $name));
}
// remove all services of this scope, or any of its child scopes from
@ -377,13 +379,13 @@ class Container implements ContainerInterface
$parentScope = $scope->getParentName();
if (self::SCOPE_CONTAINER === $name || self::SCOPE_PROTOTYPE === $name) {
throw new \InvalidArgumentException(sprintf('The scope "%s" is reserved.', $name));
throw new InvalidArgumentException(sprintf('The scope "%s" is reserved.', $name));
}
if (isset($this->scopes[$name])) {
throw new \InvalidArgumentException(sprintf('A scope with name "%s" already exists.', $name));
throw new InvalidArgumentException(sprintf('A scope with name "%s" already exists.', $name));
}
if (self::SCOPE_CONTAINER !== $parentScope && !isset($this->scopes[$parentScope])) {
throw new \InvalidArgumentException(sprintf('The parent scope "%s" does not exist, or is invalid.', $parentScope));
throw new InvalidArgumentException(sprintf('The parent scope "%s" does not exist, or is invalid.', $parentScope));
}
$this->scopes[$name] = $parentScope;

View File

@ -14,6 +14,10 @@ namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\ResourceInterface;
@ -70,7 +74,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
return $this->extensionsByNs[$name];
}
throw new \LogicException(sprintf('Container extension "%s" is not registered', $name));
throw new LogicException(sprintf('Container extension "%s" is not registered', $name));
}
/**
@ -161,7 +165,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
public function loadFromExtension($extension, array $values = array())
{
if (true === $this->isFrozen()) {
throw new \LogicException('Cannot load from an extension on a frozen container.');
throw new LogicException('Cannot load from an extension on a frozen container.');
}
$namespace = $this->getExtension($extension)->getAlias();
@ -253,14 +257,14 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* @param object $service The service instance
* @param string $scope The scope
*
* @throws BadMethodCallException
* @throws BadMethodCallException When this ContainerBuilder is frozen
*
* @api
*/
public function set($id, $service, $scope = self::SCOPE_CONTAINER)
{
if ($this->isFrozen()) {
throw new \BadMethodCallException('Setting service on a frozen container is not allowed');
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
}
$id = strtolower($id);
@ -306,8 +310,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return object The associated service
*
* @throws \InvalidArgumentException if the service is not defined
* @throws \LogicException if the service has a circular reference to itself
* @throws InvalidArgumentException if the service is not defined
* @throws LogicException if the service has a circular reference to itself
*
* @see Reference
*
@ -319,9 +323,9 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
try {
return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
if (isset($this->loading[$id])) {
throw new \LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
}
if (!$this->hasDefinition($id) && isset($this->aliases[$id])) {
@ -330,7 +334,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
try {
$definition = $this->getDefinition($id);
} catch (\InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
@ -367,14 +371,14 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
* constructor.
*
* @param ContainerBuilder $container The ContainerBuilder instance to merge.
* @throws \LogicException when this ContainerBuilder is frozen
* @throws LogicException when this ContainerBuilder is frozen
*
* @api
*/
public function merge(ContainerBuilder $container)
{
if (true === $this->isFrozen()) {
throw new \LogicException('Cannot merge on a frozen container.');
throw new LogicException('Cannot merge on a frozen container.');
}
$this->addDefinitions($container->getDefinitions());
@ -497,11 +501,11 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
if (is_string($id)) {
$id = new Alias($id);
} else if (!$id instanceof Alias) {
throw new \InvalidArgumentException('$id must be a string, or an Alias object.');
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
}
if ($alias === strtolower($id)) {
throw new \InvalidArgumentException('An alias can not reference itself, got a circular reference on "'.$alias.'".');
throw new InvalidArgumentException('An alias can not reference itself, got a circular reference on "'.$alias.'".');
}
unset($this->definitions[$alias]);
@ -554,7 +558,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return string The aliased service identifier
*
* @throws \InvalidArgumentException if the alias does not exist
* @throws InvalidArgumentException if the alias does not exist
*
* @api
*/
@ -563,7 +567,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$id = strtolower($id);
if (!$this->hasAlias($id)) {
throw new \InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
}
return $this->aliases[$id];
@ -639,7 +643,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
public function setDefinition($id, Definition $definition)
{
if ($this->isFrozen()) {
throw new \BadMethodCallException('Adding definition to a frozen container is not allowed');
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
}
$id = strtolower($id);
@ -670,7 +674,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return Definition A Definition instance
*
* @throws \InvalidArgumentException if the service definition does not exist
* @throws InvalidArgumentException if the service definition does not exist
*
* @api
*/
@ -679,7 +683,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$id = strtolower($id);
if (!$this->hasDefinition($id)) {
throw new \InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
}
return $this->definitions[$id];
@ -694,7 +698,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return Definition A Definition instance
*
* @throws \InvalidArgumentException if the service definition does not exist
* @throws InvalidArgumentException if the service definition does not exist
*
* @api
*/
@ -715,7 +719,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
*
* @return object The service described by the service definition
*
* @throws \InvalidArgumentException When configure callable is not callable
* @throws InvalidArgumentException When configure callable is not callable
*/
private function createService(Definition $definition, $id)
{
@ -731,7 +735,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
} elseif (null !== $definition->getFactoryService()) {
$factory = $this->get($this->getParameterBag()->resolveValue($definition->getFactoryService()));
} else {
throw new \RuntimeException('Cannot create service from factory method without a factory service or factory class.');
throw new RuntimeException('Cannot create service from factory method without a factory service or factory class.');
}
$service = call_user_func_array(array($factory, $definition->getFactoryMethod()), $arguments);
@ -743,7 +747,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
if (self::SCOPE_PROTOTYPE !== $scope = $definition->getScope()) {
if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
throw new \RuntimeException('You tried to create a service of an inactive scope.');
throw new RuntimeException('You tried to create a service of an inactive scope.');
}
$this->services[$lowerId = strtolower($id)] = $service;
@ -782,7 +786,7 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
}
if (!is_callable($callable)) {
throw new \InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
}
call_user_func($callable, $service);

View File

@ -46,7 +46,7 @@ interface ContainerInterface
*
* @return object The associated service
*
* @throws \InvalidArgumentException if the service is not defined
* @throws InvalidArgumentException if the service is not defined
*
* @see Reference
*
@ -72,7 +72,7 @@ interface ContainerInterface
*
* @return mixed The parameter value
*
* @throws \InvalidArgumentException if the parameter is not defined
* @throws InvalidArgumentException if the parameter is not defined
*
* @api
*/

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
/**
* Definition represents a service definition.
@ -245,7 +246,7 @@ class Definition
public function replaceArgument($index, $argument)
{
if ($index < 0 || $index > count($this->arguments) - 1) {
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
}
$this->arguments[$index] = $argument;
@ -277,7 +278,7 @@ class Definition
public function getArgument($index)
{
if ($index < 0 || $index > count($this->arguments) - 1) {
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
}
return $this->arguments[$index];

View File

@ -11,6 +11,9 @@
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
/**
* This definition decorates another definition.
*
@ -167,7 +170,7 @@ class DefinitionDecorator extends Definition
$lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
if ($index < 0 || $index > $lastIndex) {
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
}
return $this->arguments[$index];
@ -185,14 +188,14 @@ class DefinitionDecorator extends Definition
* @param mixed $value
*
* @return DefinitionDecorator the current instance
* @throws \InvalidArgumentException when $index isn't an integer
* @throws InvalidArgumentException when $index isn't an integer
*
* @api
*/
public function replaceArgument($index, $value)
{
if (!is_int($index)) {
throw new \InvalidArgumentException('$index must be an integer.');
throw new InvalidArgumentException('$index must be an integer.');
}
$this->arguments['index_'.$index] = $value;

View File

@ -11,7 +11,6 @@
namespace Symfony\Component\DependencyInjection\Dumper;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Variable;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@ -19,6 +18,9 @@ use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
/**
* PhpDumper dumps a service container as a PHP class.
@ -232,7 +234,7 @@ class PhpDumper extends Dumper
} elseif (null !== $sDefinition->getFactoryService()) {
$code .= sprintf(" \$%s = %s->%s(%s);\n", $name, $this->getServiceCall($sDefinition->getFactoryService()), $sDefinition->getFactoryMethod(), implode(', ', $arguments));
} else {
throw new \RuntimeException('Factory service or factory class must be defined in service definition for '.$id);
throw new RuntimeException('Factory service or factory class must be defined in service definition for '.$id);
}
} elseif (false !== strpos($class, '$')) {
$code .= sprintf(" \$class = %s;\n \$%s = new \$class(%s);\n", $class, $name, implode(', ', $arguments));
@ -276,15 +278,15 @@ class PhpDumper extends Dumper
* @param Definition $definition
* @return string
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
* @throws InvalidArgumentException
* @throws RuntimeException
*/
private function addServiceInstance($id, $definition)
{
$class = $this->dumpValue($definition->getClass());
if (0 === strpos($class, "'") && !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
}
$arguments = array();
@ -316,7 +318,7 @@ class PhpDumper extends Dumper
} elseif (null !== $definition->getFactoryService()) {
$code = sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
} else {
throw new \RuntimeException('Factory method requires a factory service or factory class in service definition for '.$id);
throw new RuntimeException('Factory method requires a factory service or factory class in service definition for '.$id);
}
} elseif (false !== strpos($class, '$')) {
$code = sprintf(" \$class = %s;\n $return{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
@ -796,11 +798,11 @@ EOF;
if (is_array($value)) {
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
} elseif ($value instanceof Variable) {
throw new \InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain variable references. Variable "%s" found in "%s".', $value, $path.'/'.$key));
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain variable references. Variable "%s" found in "%s".', $value, $path.'/'.$key));
} elseif ($value instanceof Definition) {
throw new \InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain service definitions. Definition for "%s" found in "%s".', $value->getClass(), $path.'/'.$key));
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain service definitions. Definition for "%s" found in "%s".', $value->getClass(), $path.'/'.$key));
} elseif ($value instanceof Reference) {
throw new \InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").', $value, $path.'/'.$key));
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain references to other services (reference to service "%s" found in "%s").', $value, $path.'/'.$key));
} else {
$value = var_export($value, true);
}
@ -969,10 +971,10 @@ EOF;
return $this->dumpValue($this->definitionVariables->offsetGet($value), $interpolate);
}
if (count($value->getMethodCalls()) > 0) {
throw new \RuntimeException('Cannot dump definitions which have method calls.');
throw new RuntimeException('Cannot dump definitions which have method calls.');
}
if (null !== $value->getConfigurator()) {
throw new \RuntimeException('Cannot dump definitions which have a configurator.');
throw new RuntimeException('Cannot dump definitions which have a configurator.');
}
$arguments = array();
@ -982,7 +984,7 @@ EOF;
$class = $this->dumpValue($value->getClass());
if (false !== strpos($class, '$')) {
throw new \RuntimeException('Cannot dump definitions which have a variable class name.');
throw new RuntimeException('Cannot dump definitions which have a variable class name.');
}
if (null !== $value->getFactoryMethod()) {
@ -991,7 +993,7 @@ EOF;
} elseif (null !== $value->getFactoryService()) {
return sprintf("%s->%s(%s)", $this->getServiceCall($value->getFactoryService()), $value->getFactoryMethod(), implode(', ', $arguments));
} else {
throw new \RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
throw new RuntimeException('Cannot dump definitions which have factory method without factory service or factory class.');
}
}
@ -1026,7 +1028,7 @@ EOF;
return $code;
}
} elseif (is_object($value) || is_resource($value)) {
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
} else {
return var_export($value, true);
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* XmlDumper dumps a service container as an XML string.
@ -277,7 +278,7 @@ class XmlDumper extends Dumper
* Converts php types to xml types.
*
* @param mixed $value Value to convert
* @throws \RuntimeException When trying to dump object or resource
* @throws RuntimeException When trying to dump object or resource
*/
static public function phpToXml($value)
{
@ -291,7 +292,7 @@ class XmlDumper extends Dumper
case is_object($value) && $value instanceof Parameter:
return '%'.$value.'%';
case is_object($value) || is_resource($value):
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
default:
return (string) $value;
}

View File

@ -15,6 +15,7 @@ use Symfony\Component\Yaml\Yaml;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* YamlDumper dumps a service container as a YAML string.
@ -175,7 +176,7 @@ class YamlDumper extends Dumper
* Dumps the value to YAML format
*
* @param mixed $value
* @throws \RuntimeException When trying to dump object or resource
* @throws RuntimeException When trying to dump object or resource
*/
private function dumpValue($value)
{
@ -191,7 +192,7 @@ class YamlDumper extends Dumper
} elseif (is_object($value) && $value instanceof Parameter) {
return $this->getParameterCall((string) $value);
} elseif (is_object($value) || is_resource($value)) {
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}
return $value;

View File

@ -28,7 +28,7 @@ interface ExtensionInterface
* @param array $config An array of configuration values
* @param ContainerBuilder $container A ContainerBuilder instance
*
* @throws \InvalidArgumentException When provided tag is not defined in this extension
* @throws InvalidArgumentException When provided tag is not defined in this extension
*
* @api
*/

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* IniFileLoader loads parameters from INI files.
@ -26,7 +27,7 @@ class IniFileLoader extends FileLoader
* @param mixed $file The resource
* @param string $type The resource type
*
* @throws \InvalidArgumentException When ini file is not valid
* @throws InvalidArgumentException When ini file is not valid
*/
public function load($file, $type = null)
{
@ -36,7 +37,7 @@ class IniFileLoader extends FileLoader
$result = parse_ini_file($path, true);
if (false === $result || array() === $result) {
throw new \InvalidArgumentException(sprintf('The "%s" file is not valid.', $file));
throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $file));
}
if (isset($result['parameters']) && is_array($result['parameters'])) {

View File

@ -12,13 +12,14 @@
namespace Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\SimpleXMLElement;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
/**
* XmlFileLoader loads XML files service definitions.
@ -198,14 +199,14 @@ class XmlFileLoader extends FileLoader
* Parses a XML file.
*
* @param string $file Path to a file
* @throws \InvalidArgumentException When loading of XML file returns error
* @throws InvalidArgumentException When loading of XML file returns error
*/
private function parseFile($file)
{
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
if (!$dom->load($file, defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0)) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
throw new InvalidArgumentException(implode("\n", $this->getXmlErrors()));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
@ -290,8 +291,8 @@ class XmlFileLoader extends FileLoader
* @param \DOMDocument $dom
* @param string $file
*
* @throws \RuntimeException When extension references a non-existent XSD file
* @throws \InvalidArgumentException When xml doesn't validate its xsd schema
* @throws RuntimeException When extension references a non-existent XSD file
* @throws InvalidArgumentException When xml doesn't validate its xsd schema
*/
private function validateSchema(\DOMDocument $dom, $file)
{
@ -308,7 +309,7 @@ class XmlFileLoader extends FileLoader
$path = str_replace($extension->getNamespace(), str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);
if (!is_file($path)) {
throw new \RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s"', get_class($extension), $path));
throw new RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s"', get_class($extension), $path));
}
$schemaLocations[$items[$i]] = $path;
@ -353,7 +354,7 @@ EOF
@unlink($tmpfile);
}
if (!$valid) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
throw new InvalidArgumentException(implode("\n", $this->getXmlErrors()));
}
libxml_use_internal_errors($current);
}
@ -364,7 +365,7 @@ EOF
* @param \DOMDocument $dom
* @param string $file
*
* @throws \InvalidArgumentException When non valid tag are found or no extension are found
* @throws InvalidArgumentException When no extension is found corresponding to a tag
*/
private function validateExtensions(\DOMDocument $dom, $file)
{
@ -376,7 +377,7 @@ EOF
// can it be handled by an extension?
if (!$this->container->hasExtension($node->namespaceURI)) {
$extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getNamespace(); }, $this->container->getExtensions()));
throw new \InvalidArgumentException(sprintf(
throw new InvalidArgumentException(sprintf(
'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
$node->tagName,
$file,

View File

@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Yaml\Yaml;
@ -198,12 +199,12 @@ class YamlFileLoader extends FileLoader
if (isset($service['tags'])) {
if (!is_array($service['tags'])) {
throw new \InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s.', $id, $file));
}
foreach ($service['tags'] as $tag) {
if (!isset($tag['name'])) {
throw new \InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.', $id, $file));
}
$name = $tag['name'];
@ -234,7 +235,7 @@ class YamlFileLoader extends FileLoader
* @param string $file
* @return array
*
* @throws \InvalidArgumentException When service file is not valid
* @throws InvalidArgumentException When service file is not valid
*/
private function validate($content, $file)
{
@ -243,7 +244,7 @@ class YamlFileLoader extends FileLoader
}
if (!is_array($content)) {
throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
}
foreach (array_keys($content) as $namespace) {
@ -253,7 +254,7 @@ class YamlFileLoader extends FileLoader
if (!$this->container->hasExtension($namespace)) {
$extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
throw new \InvalidArgumentException(sprintf(
throw new InvalidArgumentException(sprintf(
'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
$namespace,
$file,

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\DependencyInjection\ParameterBag;
use Symfony\Component\DependencyInjection\Exception\LogicException;
/**
*
* @author Fabien Potencier <fabien@symfony.com>
@ -44,7 +46,7 @@ class FrozenParameterBag extends ParameterBag
*/
public function clear()
{
throw new \LogicException('Impossible to call clear() on a frozen ParameterBag.');
throw new LogicException('Impossible to call clear() on a frozen ParameterBag.');
}
/**
@ -54,7 +56,7 @@ class FrozenParameterBag extends ParameterBag
*/
public function add(array $parameters)
{
throw new \LogicException('Impossible to call add() on a frozen ParameterBag.');
throw new LogicException('Impossible to call add() on a frozen ParameterBag.');
}
/**
@ -64,6 +66,6 @@ class FrozenParameterBag extends ParameterBag
*/
public function set($name, $value)
{
throw new \LogicException('Impossible to call set() on a frozen ParameterBag.');
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
}
}