Skip to content

Commit

Permalink
[Config] renamed FileLoaderImportException to FileLoaderLoadException…
Browse files Browse the repository at this point in the history
… and replaced some \InvalidArgumentException with this new exception class
  • Loading branch information
fabpot committed Jun 20, 2011
1 parent 38fa4e6 commit 01ecaa4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
Expand Up @@ -16,7 +16,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FileLoaderImportCircularReferenceException extends FileLoaderImportException
class FileLoaderImportCircularReferenceException extends FileLoaderLoadException
{
public function __construct(array $resources, $code = null, $previous = null)
{
Expand Down
Expand Up @@ -12,22 +12,22 @@
namespace Symfony\Component\Config\Exception;

/**
* Exception class for when a resource cannot be imported.
* Exception class for when a resource cannot be loaded or imported.
*
* @author Ryan Weaver <ryan@thatsquality.com>
*/
class FileLoaderImportException extends \Exception
class FileLoaderLoadException extends \Exception
{
/**
* @param string $resource The resource that could not be imported
* @param string $sourceResource The original resource importing the new resource
* @param integer $code The error code
* @param Exception $previous A previous exception
*/
public function __construct($resource, $sourceResource, $code = null, $previous = null)
public function __construct($resource, $sourceResource = null, $code = null, $previous = null)
{
if (null === $sourceResource) {
$message = sprintf('Cannot import resource "%s".', $this->varToString($resource));
$message = sprintf('Cannot load resource "%s".', $this->varToString($resource));
} else {
$message = sprintf('Cannot import resource "%s" from "%s".', $this->varToString($resource), $this->varToString($sourceResource));
}
Expand All @@ -38,7 +38,7 @@ public function __construct($resource, $sourceResource, $code = null, $previous
protected function varToString($var)
{
if (is_object($var)) {
return sprintf('[object](%s)', get_class($var));
return sprintf('Object(%s)', get_class($var));
}

if (is_array($var)) {
Expand All @@ -47,17 +47,25 @@ protected function varToString($var)
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
}

return sprintf("[array](%s)", implode(', ', $a));
return sprintf("Array(%s)", implode(', ', $a));
}

if (is_resource($var)) {
return '[resource]';
return sprintf('Resource(%s)', get_resource_type($var));
}

if (null === $var) {
return 'null';
}

if (false === $var) {
return 'false';
}

if (true === $var) {
return 'true';
}

return (string) $var;
}
}
8 changes: 4 additions & 4 deletions src/Symfony/Component/Config/Loader/DelegatingLoader.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Config\Loader;

use Symfony\Component\Config\Exception\FileLoaderLoadException;

/**
* DelegatingLoader delegates loading to other loaders using a loader resolver.
*
Expand Down Expand Up @@ -41,10 +43,8 @@ public function __construct(LoaderResolverInterface $resolver)
*/
public function load($resource, $type = null)
{
$loader = $this->resolver->resolve($resource, $type);

if (false === $loader) {
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
if (false === $loader = $this->resolver->resolve($resource, $type)) {
throw new FileLoaderLoadException($resource);
}

return $loader->load($resource, $type);
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Config/Loader/FileLoader.php
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Config\Loader;

use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Exception\FileLoaderImportException;
use Symfony\Component\Config\Exception\FileLoaderLoadException;
use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;

/**
Expand Down Expand Up @@ -82,11 +82,11 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
} catch (\Exception $e) {
if (!$ignoreErrors) {
// prevent embedded imports from nesting multiple exceptions
if ($e instanceof FileLoaderImportException) {
if ($e instanceof FileLoaderLoadException) {
throw $e;
}

throw new FileLoaderImportException($resource, $sourceResource, null, $e);
throw new FileLoaderLoadException($resource, $sourceResource, null, $e);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Config/Loader/Loader.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Config\Loader;

use Symfony\Component\Config\Exception\FileLoaderLoadException;

/**
* Loader is the abstract class used by all built-in loaders.
*
Expand Down Expand Up @@ -63,18 +65,16 @@ public function import($resource, $type = null)
*/
public function resolve($resource, $type = null)
{

if ($this->supports($resource, $type)) {
return $this;
}

$loader = null === $this->resolver ? false : $this->resolver->resolve($resource, $type);

if (false === $loader) {
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
throw new FileLoaderLoadException($resource);
}

return $loader;
}

}
Expand Up @@ -73,7 +73,7 @@ public function testLoad()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException Symfony\Component\Config\Exception\FileLoaderLoadException
*/
public function testLoadThrowsAnExceptionIfTheResourceCannotBeLoaded()
{
Expand Down
7 changes: 4 additions & 3 deletions tests/Symfony/Tests/Component/Config/Loader/LoaderTest.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Exception\FileLoaderLoadException;

class LoaderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -49,9 +50,9 @@ public function testResolve()
$loader->setResolver($resolver);
try {
$loader->resolve(new \stdClass());
$this->fail('->resolve() throws an \InvalidArgumentException if the resource cannot be loaded');
} catch (\Exception $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e, '->resolve() throws an \InvalidArgumentException if the resource cannot be loaded');
$this->fail('->resolve() throws a FileLoaderLoadException if the resource cannot be loaded');
} catch (FileLoaderLoadException $e) {
$this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderLoadException', $e, '->resolve() throws a FileLoaderLoadException if the resource cannot be loaded');
}
}
}
Expand Down

0 comments on commit 01ecaa4

Please sign in to comment.