ソースを参照

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

Fabien Potencier 14 年 前
コミット
a432417ab9

+ 12 - 6
src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

@@ -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);
+            $loader->load($resource);
+        } catch (\Exception $e) {
+            if (!$ignoreErrors) {
+                throw $e;
+            }
+        }
     }
 
     /**

+ 1 - 1
src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

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

+ 1 - 1
src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

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

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

@@ -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">

+ 1 - 1
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/xml/services4.xml

@@ -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>