Skip to content

Commit cddf68c

Browse files
committed
[Config] [DependencyInjection] [Routing] Added option ignore_not_found for imported config files
1 parent 5440d67 commit cddf68c

24 files changed

+174
-18
lines changed

UPGRADE-4.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Console
1010
-------
1111

1212
* Deprecated finding hidden commands using an abbreviation, use the full name instead
13+
* Passing a boolean to argument `$ignoreErrors` of `FileLoader::import()` is deprecated, pass an one of the FileLoader::IGNORE_* constants instead
1314

1415
Debug
1516
-----

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Config
2727
* Removed `FileLoaderLoadException`, use `LoaderLoadException` instead.
2828
* Using environment variables with `cannotBeEmpty()` if the value is validated with `validate()` will throw an exception.
2929
* Removed the `root()` method in `TreeBuilder`, pass the root node information to the constructor instead
30+
* Argument `$ignoreErrors` of `FileLoader::import()` now expects one of the FileLoader::IGNORE_* constants instead of `bool`
3031

3132
Console
3233
-------

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added option `ignore_errors: not_found` for imported config files
8+
* deprecated passing a boolean to argument `$ignoreErrors` of `FileLoader::import()`, pass one of the FileLoader::IGNORE_* constants instead
9+
410
4.3.0
511
-----
612

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
*/
2626
abstract class FileLoader extends Loader
2727
{
28+
const IGNORE_NONE = 0;
29+
const IGNORE_FILE_NOT_FOUND = 1;
30+
const IGNORE_ALL = 256;
31+
2832
protected static $loading = [];
2933

3034
protected $locator;
@@ -61,7 +65,7 @@ public function getLocator()
6165
*
6266
* @param mixed $resource A Resource
6367
* @param string|null $type The resource type or null if unknown
64-
* @param bool $ignoreErrors Whether to ignore import errors or not
68+
* @param int $ignoreErrors How to ignore import errors
6569
* @param string|null $sourceResource The original resource importing the new resource
6670
*
6771
* @return mixed
@@ -70,8 +74,14 @@ public function getLocator()
7074
* @throws FileLoaderImportCircularReferenceException
7175
* @throws FileLocatorFileNotFoundException
7276
*/
73-
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
77+
public function import($resource, $type = null, $ignoreErrors = self::IGNORE_NONE, $sourceResource = null)
7478
{
79+
if (!\is_int($ignoreErrors)) {
80+
@trigger_error(sprintf('Passing a boolean to argument "$ignoreErrors" of "%s()" is deprecated since Symfony 4.4, pass one of the FileLoader::IGNORE_* constants instead.', __METHOD__), E_USER_DEPRECATED);
81+
82+
$ignoreErrors = $ignoreErrors ? self::IGNORE_ALL : self::IGNORE_NONE;
83+
}
84+
7585
if (\is_string($resource) && \strlen($resource) !== $i = strcspn($resource, '*?{[')) {
7686
$ret = [];
7787
$isSubpath = 0 !== $i && false !== strpos(substr($resource, 0, $i), '/');
@@ -93,7 +103,7 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
93103
/**
94104
* @internal
95105
*/
96-
protected function glob(string $pattern, bool $recursive, &$resource = null, bool $ignoreErrors = false, bool $forExclusion = false, array $excluded = [])
106+
protected function glob(string $pattern, bool $recursive, &$resource = null, int $ignoreErrors = self::IGNORE_NONE, bool $forExclusion = false, array $excluded = [])
97107
{
98108
if (\strlen($pattern) === $i = strcspn($pattern, '*?{[')) {
99109
$prefix = $pattern;
@@ -109,7 +119,7 @@ protected function glob(string $pattern, bool $recursive, &$resource = null, boo
109119
try {
110120
$prefix = $this->locator->locate($prefix, $this->currentDir, true);
111121
} catch (FileLocatorFileNotFoundException $e) {
112-
if (!$ignoreErrors) {
122+
if (self::IGNORE_ALL !== $ignoreErrors) {
113123
throw $e;
114124
}
115125

@@ -125,7 +135,7 @@ protected function glob(string $pattern, bool $recursive, &$resource = null, boo
125135
yield from $resource;
126136
}
127137

128-
private function doImport($resource, string $type = null, bool $ignoreErrors = false, $sourceResource = null)
138+
private function doImport($resource, string $type = null, int $ignoreErrors = self::IGNORE_NONE, $sourceResource = null)
129139
{
130140
try {
131141
$loader = $this->resolve($resource, $type);
@@ -156,8 +166,17 @@ private function doImport($resource, string $type = null, bool $ignoreErrors = f
156166
return $ret;
157167
} catch (FileLoaderImportCircularReferenceException $e) {
158168
throw $e;
169+
} catch (FileLocatorFileNotFoundException $e) {
170+
if (self::IGNORE_ALL === $ignoreErrors) {
171+
return;
172+
}
173+
if (self::IGNORE_FILE_NOT_FOUND === $ignoreErrors) {
174+
return;
175+
}
176+
177+
throw new LoaderLoadException($resource, $sourceResource, null, $e, $type);
159178
} catch (\Exception $e) {
160-
if (!$ignoreErrors) {
179+
if (self::IGNORE_ALL !== $ignoreErrors) {
161180
// prevent embedded imports from nesting multiple exceptions
162181
if ($e instanceof LoaderLoadException) {
163182
throw $e;

src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Config\Loader\FileLoader;
1415
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1516
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1617
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
@@ -60,7 +61,7 @@ final public function extension(string $namespace, array $config)
6061
$this->container->loadFromExtension($namespace, static::processValue($config));
6162
}
6263

63-
final public function import(string $resource, string $type = null, bool $ignoreErrors = false)
64+
final public function import(string $resource, string $type = null, $ignoreErrors = FileLoader::IGNORE_NONE)
6465
{
6566
$this->loader->setCurrentDir(\dirname($this->path));
6667
$this->loader->import($resource, $type, $ignoreErrors, $this->file);

src/Symfony/Component/DependencyInjection/Loader/DirectoryLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function load($file, $type = null)
3535

3636
$this->setCurrentDir($path);
3737

38-
$this->import($dir, null, false, $path);
38+
$this->import($dir, null, parent::IGNORE_NONE, $path);
3939
}
4040
}
4141
}

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,22 @@ private function parseImports(\DOMDocument $xml, string $file)
105105
$defaultDirectory = \dirname($file);
106106
foreach ($imports as $import) {
107107
$this->setCurrentDir($defaultDirectory);
108-
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
108+
109+
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, $this->parseIgnoreErrors(XmlUtils::phpize($import->getAttribute('ignore-errors'))), $file);
110+
}
111+
}
112+
113+
private function parseIgnoreErrors($ignoreErrorsInput): int
114+
{
115+
if (true === $ignoreErrorsInput) {
116+
return parent::IGNORE_ALL;
117+
}
118+
119+
if ('not_found' === $ignoreErrorsInput) {
120+
return parent::IGNORE_FILE_NOT_FOUND;
109121
}
122+
123+
return parent::IGNORE_NONE;
110124
}
111125

112126
private function parseDefinitions(\DOMDocument $xml, string $file, array $defaults)

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,22 @@ private function parseImports(array $content, string $file)
190190
}
191191

192192
$this->setCurrentDir($defaultDirectory);
193-
$this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
193+
194+
$this->import($import['resource'], $import['type'] ?? null, $this->parseIgnoreErrors($import), $file);
195+
}
196+
}
197+
198+
private function parseIgnoreErrors(array $import): int
199+
{
200+
if (empty($import['ignore_errors']) || !($import['ignore_errors'])) {
201+
return parent::IGNORE_NONE;
202+
}
203+
204+
if ('not_found' === $import['ignore_errors']) {
205+
return parent::IGNORE_FILE_NOT_FOUND;
194206
}
207+
208+
return parent::IGNORE_ALL;
195209
}
196210

197211
private function parseDefinitions(array $content, string $file)

src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]]></xsd:documentation>
7979
</xsd:annotation>
8080
<xsd:attribute name="resource" type="xsd:string" use="required" />
81-
<xsd:attribute name="ignore-errors" type="boolean" />
81+
<xsd:attribute name="ignore-errors" type="ignore_errors" />
8282
<xsd:attribute name="type" type="xsd:string" />
8383
</xsd:complexType>
8484

@@ -280,6 +280,12 @@
280280
</xsd:restriction>
281281
</xsd:simpleType>
282282

283+
<xsd:simpleType name="ignore_errors">
284+
<xsd:restriction base="xsd:string">
285+
<xsd:pattern value="(%.+%|true|false|not_found)" />
286+
</xsd:restriction>
287+
</xsd:simpleType>
288+
283289
<xsd:simpleType name="boolean">
284290
<xsd:restriction base="xsd:string">
285291
<xsd:pattern value="(%.+%|true|false)" />
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<imports>
7+
<import resource="foo_fake.xml" ignore-errors="not_found" />
8+
</imports>
9+
</container>

0 commit comments

Comments
 (0)