Skip to content

Commit

Permalink
[DependencyInjection] added a way to ignore errors when importing a n…
Browse files Browse the repository at this point in the history
…on-existent file (useful when you want to include an optional service file)
  • Loading branch information
fabpot committed Aug 24, 2010
1 parent b1e7996 commit a432417
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
18 changes: 12 additions & 6 deletions src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ public function __construct(ContainerBuilder $container, $paths = array())
*
* @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);
$loader->load($resource);
} catch (\Exception $e) {
if (!$ignoreErrors) {
throw $e;
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected function parseImports($xml, $file)

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'));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function parseImports($content, $file)

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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit a432417

Please sign in to comment.