Skip to content

Commit 419a9b5

Browse files
pulzarraidernicolas-grekas
authored andcommitted
[DependencyInjection] Added option ignore_errors: not_found while importing config files
1 parent ba07cda commit 419a9b5

14 files changed

+140
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function getLocator()
7373
*/
7474
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null/*, $exclude = null*/)
7575
{
76-
if (\func_num_args() < 5 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) {
76+
if (\func_num_args() < 5 && __CLASS__ !== \get_class($this) && 0 !== strpos(\get_class($this), 'Symfony\Component\\') && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) {
7777
@trigger_error(sprintf('The "%s()" method will have a new "$exclude = null" argument in version 5.0, not defining it is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED);
7878
}
7979
$exclude = \func_num_args() >= 5 ? func_get_arg(4) : null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final public function extension(string $namespace, array $config)
6060
$this->container->loadFromExtension($namespace, static::processValue($config));
6161
}
6262

63-
final public function import(string $resource, string $type = null, bool $ignoreErrors = false)
63+
final public function import(string $resource, string $type = null, $ignoreErrors = false)
6464
{
6565
$this->loader->setCurrentDir(\dirname($this->path));
6666
$this->loader->import($resource, $type, $ignoreErrors, $this->file);

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader;
1313

14+
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
15+
use Symfony\Component\Config\Exception\LoaderLoadException;
1416
use Symfony\Component\Config\FileLocatorInterface;
1517
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
1618
use Symfony\Component\Config\Resource\GlobResource;
@@ -41,6 +43,42 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
4143
parent::__construct($locator);
4244
}
4345

46+
/**
47+
* {@inheritdoc}
48+
*
49+
* @param bool|string $ignoreErrors Whether errors should be ignored; pass "not_found" to ignore only when the loaded resource is not found
50+
* @param string|string[]|null $exclude Glob patterns to exclude from the import
51+
*/
52+
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null/*, $exclude = null*/)
53+
{
54+
$args = \func_get_args();
55+
56+
if ($ignoreNotFound = 'not_found' === $ignoreErrors) {
57+
$args[2] = false;
58+
} elseif (!\is_bool($ignoreErrors)) {
59+
@trigger_error(sprintf('Invalid argument $ignoreErrors provided to %s::import(): boolean or "not_found" expected, %s given.', \get_class($this), \gettype($ignoreErrors)), E_USER_DEPRECATED);
60+
$args[2] = (bool) $ignoreErrors;
61+
}
62+
63+
try {
64+
parent::import(...$args);
65+
} catch (LoaderLoadException $e) {
66+
if (!$ignoreNotFound || !($prev = $e->getPrevious()) instanceof FileLocatorFileNotFoundException) {
67+
throw $e;
68+
}
69+
70+
foreach ($prev->getTrace() as $frame) {
71+
if ('import' === ($frame['function'] ?? null) && BaseFileLoader::class === ($frame['class'] ?? null)) {
72+
break;
73+
}
74+
}
75+
76+
if ($args !== $frame['args']) {
77+
throw $e;
78+
}
79+
}
80+
}
81+
4482
/**
4583
* Registers a set of classes as services using PSR-4 for discovery.
4684
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ 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+
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, XmlUtils::phpize($import->getAttribute('ignore-errors')) ?: false, $file);
109109
}
110110
}
111111

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private function parseImports(array $content, string $file)
191191
}
192192

193193
$this->setCurrentDir($defaultDirectory);
194-
$this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
194+
$this->import($import['resource'], $import['type'] ?? null, $import['ignore_errors'] ?? false, $file);
195195
}
196196
}
197197

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

@@ -273,6 +273,12 @@
273273
</xsd:restriction>
274274
</xsd:simpleType>
275275

276+
<xsd:simpleType name="ignore_errors">
277+
<xsd:restriction base="xsd:string">
278+
<xsd:pattern value="(true|false|not_found)" />
279+
</xsd:restriction>
280+
</xsd:simpleType>
281+
276282
<xsd:simpleType name="invalid_sequence">
277283
<xsd:restriction base="xsd:string">
278284
<xsd:enumeration value="null" />
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>
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="nonvalid.xml" ignore-errors="not_found" />
8+
</imports>
9+
</container>
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" />
8+
</imports>
9+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports:
2+
- { resource: foo_fake.yml, ignore_errors: not_found }

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy