[DependencyInjection] added a way to ignore errors when importing a non-existent file (useful when you want to include an optional service file)

This commit is contained in:
Fabien Potencier 2010-08-24 16:36:29 +02:00
parent b1e79963b1
commit a432417ab9
5 changed files with 16 additions and 16 deletions

View File

@ -45,15 +45,21 @@ abstract class FileLoader extends Loader
*
* @param mixed $resource A Resource
*/
public function import($resource)
public function import($resource, $ignoreErrors = false)
{
$loader = $this->resolve($resource);
try {
$loader = $this->resolve($resource);
if ($loader instanceof FileLoader && null !== $this->currentDir) {
$resource = $this->getAbsolutePath($resource, $this->currentDir);
if ($loader instanceof FileLoader && null !== $this->currentDir) {
$resource = $this->getAbsolutePath($resource, $this->currentDir);
}
$loader->load($resource);
} catch (\Exception $e) {
if (!$ignoreErrors) {
throw $e;
}
}
$loader->load($resource);
}
/**

View File

@ -82,7 +82,7 @@ class XmlFileLoader extends FileLoader
foreach ($xml->imports->import as $import) {
$this->currentDir = dirname($file);
$this->import((string) $import['resource']);
$this->import((string) $import['resource'], (Boolean) $import->getAttributeAsPhp('ignore-errors'));
}
}

View File

@ -81,7 +81,7 @@ class YamlFileLoader extends FileLoader
foreach ($content['imports'] as $import) {
$this->currentDir = dirname($file);
$this->import($import['resource']);
$this->import($import['resource'], isset($import['ignore_errors']) ? (Boolean) $import['ignore_errors'] : false);
}
}

View File

@ -61,13 +61,7 @@
]]></xsd:documentation>
</xsd:annotation>
<xsd:attribute name="resource" type="xsd:string" use="required" />
<xsd:attribute name="class" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
The PHP class able to load the resource. If not defined, the loader uses the current loader.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="ignore-errors" type="boolean" />
</xsd:complexType>
<xsd:complexType name="configurator">

View File

@ -6,7 +6,7 @@
<imports>
<import resource="services2.xml" />
<import resource="services3.xml" />
<import resource="../ini/parameters.ini" class="Symfony\Component\DependencyInjection\Loader\IniFileLoader" />
<import resource="../ini/parameters.ini" />
<import resource="../ini/parameters2.ini" />
<import resource="../yaml/services13.yml" />
</imports>