Skip to content

Commit

Permalink
[Config] added a guard against circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jun 5, 2011
1 parent 87fefca commit a98046d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Exception;

/**
* Exception class for when a circular reference is detected when importing resources.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FileLoaderImportCircularReferenceException extends FileLoaderImportException
{
public function __construct(array $resources, $code = null, $previous = null)
{
$message = sprintf('Circular reference detected in "%s" ("%s" > "%s").', $this->varToString($resources[0]), implode('" > "', $resources), $resources[0]);

call_user_func('Exception::__construct', $message, $code, $previous);
}
}
Expand Up @@ -35,7 +35,7 @@ public function __construct($resource, $sourceResource, $code = null, $previous
parent::__construct($message, $code, $previous);
}

private function varToString($var)
protected function varToString($var)
{
if (is_object($var)) {
return sprintf('[object](%s)', get_class($var));
Expand All @@ -58,6 +58,6 @@ private function varToString($var)
return 'null';
}

return str_replace("\n", '', var_export((string) $var, true));
return (string) $var;
}
}
18 changes: 16 additions & 2 deletions src/Symfony/Component/Config/Loader/FileLoader.php
Expand Up @@ -13,6 +13,7 @@

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

/**
* FileLoader is the abstract class used by all built-in loaders that are file based.
Expand All @@ -21,6 +22,8 @@
*/
abstract class FileLoader extends Loader
{
static protected $loading = array();

protected $locator;

private $currentDir;
Expand All @@ -46,7 +49,7 @@ public function getLocator()
}

/**
* Adds definitions and parameters from a resource.
* Imports a resource.
*
* @param mixed $resource A Resource
* @param string $type The resource type
Expand All @@ -64,7 +67,18 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
$resource = $this->locator->locate($resource, $this->currentDir);
}

return $loader->load($resource);
if (isset(self::$loading[$resource])) {
throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
}
self::$loading[$resource] = true;

$ret = $loader->load($resource);

unset(self::$loading[$resource]);

return $ret;
} catch (FileLoaderImportCircularReferenceException $e) {
throw $e;
} catch (\Exception $e) {
if (!$ignoreErrors) {
// prevent embedded imports from nesting multiple exceptions
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Loader/Loader.php
Expand Up @@ -41,7 +41,7 @@ public function setResolver(LoaderResolver $resolver)
}

/**
* Adds definitions and parameters from a resource.
* Imports a resource.
*
* @param mixed $resource A Resource
* @param string $type The resource type
Expand Down

0 comments on commit a98046d

Please sign in to comment.