8000 [Config] added a guard against circular references · liveink/symfony@a98046d · GitHub
[go: up one dir, main page]

Skip to content

Commit a98046d

Browse files
committed
[Config] added a guard against circular references
1 parent 87fefca commit a98046d

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Config\Exception;
13+
14+
/**
15+
* Exception class for when a circular reference is detected when importing resources.
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
class FileLoaderImportCircularReferenceException extends FileLoaderImportException
20+
{
21+
public function __construct(array $resources, $code = null, $previous = null)
22+
{
23+
$message = sprintf('Circular reference detected in "%s" ("%s" > "%s").', $this->varToString($resources[0]), implode('" > "', $resources), $resources[0]);
24+
25+
call_user_func('Exception::__construct', $message, $code, $previous);
26+
}
27+
}

src/Symfony/Component/Config/Exception/FileLoaderImportException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct($resource, $sourceResource, $code = null, $previous
3535
parent::__construct($message, $code, $previous);
3636
}
3737

38-
private function varToString($var)
38+
protected function varToString($var)
3939
{
4040
if (is_object($var)) {
4141
return sprintf('[object](%s)', get_class($var));
@@ -58,6 +58,6 @@ private function varToString($var)
5858
return 'null';
5959
}
6060

61-
return str_replace("\n", '', var_export((string) $var, true));
61+
return (string) $var;
6262
}
6363
}

src/Symfony/Component/Config/Loader/FileLoader.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\FileLocatorInterface;
1515
use Symfony\Component\Config\Exception\FileLoaderImportException;
16+
use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
1617

1718
/**
1819
* FileLoader is the abstract class used by all built-in loaders that are file based.
@@ -21,6 +22,8 @@
2122
*/
2223
abstract class FileLoader extends Loader
2324
{
25+
static protected $loading = array();
26+
2427
protected $locator;
2528

2629
private $currentDir;
@@ -46,7 +49,7 @@ public function getLocator()
4649
}
4750

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

67-
return $loader->load($resource);
70+
if (isset(self::$loading[$resource])) {
71+
throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
72+
}
73+
self::$loading[$resource] = true;
74+
75+
$ret = $loader->load($resource);
76+
77+
unset(self::$loading[$resource]);
78+
79+
return $ret;
80+
} catch (FileLoaderImportCircularReferenceException $e) {
81+
throw $e;
6882
} catch (\Exception $e) {
6983
if (!$ignoreErrors) {
7084
// prevent embedded imports from nesting multiple exceptions

src/Symfony/Component/Config/Loader/Loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function setResolver(LoaderResolver $resolver)
4141
}
4242

4343
/**
44-
* Adds definitions and parameters from a resource.
44+
* Imports a resource.
4545
*
4646
* @param mixed $resource A Resource
4747
* @param string $type The resource type

0 commit comments

Comments
 (0)
0