Browse Source

[AsseticBundle] injected container into factory for better stability

Kris Wallsmith 14 years ago
parent
commit
57251712e8

+ 31 - 2
src/Symfony/Bundle/AsseticBundle/Factory/AssetFactory.php

@@ -12,6 +12,7 @@
 namespace Symfony\Bundle\AsseticBundle\Factory;
 
 use Assetic\Factory\AssetFactory as BaseAssetFactory;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpKernel\KernelInterface;
 
 /**
@@ -21,11 +22,21 @@ use Symfony\Component\HttpKernel\KernelInterface;
  */
 class AssetFactory extends BaseAssetFactory
 {
-    protected $kernel;
+    private $kernel;
+    private $container;
 
-    public function __construct(KernelInterface $kernel, $baseDir, $debug = false)
+    /**
+     * Constructor.
+     *
+     * @param KernelInterface    $kernel    The kernel is used to parse bundle notation
+     * @param ContainerInterface $container The container is used to load the managers lazily, thus avoiding a circular dependency
+     * @param string             $baseDir   The base directory for relative inputs
+     * @param Boolean            $debug     The current debug mode
+     */
+    public function __construct(KernelInterface $kernel, ContainerInterface $container, $baseDir, $debug = false)
     {
         $this->kernel = $kernel;
+        $this->container = $container;
 
         parent::__construct($baseDir, $debug);
     }
@@ -51,4 +62,22 @@ class AssetFactory extends BaseAssetFactory
 
         return parent::parseInput($input);
     }
+
+    protected function createAssetReference($name)
+    {
+        if (!$this->getAssetManager()) {
+            $this->setAssetManager($this->container->get('assetic.asset_manager'));
+        }
+
+        return parent::createAssetReference($name);
+    }
+
+    protected function getFilter($name)
+    {
+        if (!$this->getFilterManager()) {
+            $this->setFilterManager($this->container->get('assetic.filter_manager'));
+        }
+
+        return parent::getFilter($name);
+    }
 }

+ 1 - 1
src/Symfony/Bundle/AsseticBundle/Resources/config/assetic.xml

@@ -31,9 +31,9 @@
 
         <service id="assetic.asset_factory" class="%assetic.asset_factory.class%" public="false">
             <argument type="service" id="kernel" />
+            <argument type="service" id="service_container" />
             <argument>%assetic.read_from%</argument>
             <argument>%assetic.debug%</argument>
-            <call method="setFilterManager"><argument type="service" id="assetic.filter_manager" /></call>
         </service>
 
         <service id="assetic.config_cache" class="%assetic.config_cache.class%" public="false">

+ 3 - 1
src/Symfony/Bundle/AsseticBundle/Tests/Factory/AssetFactoryTest.php

@@ -17,6 +17,7 @@ class AssetFactoryTest extends \PHPUnit_Framework_TestCase
 {
     protected $kernel;
     protected $factory;
+    protected $container;
 
     protected function setUp()
     {
@@ -25,7 +26,8 @@ class AssetFactoryTest extends \PHPUnit_Framework_TestCase
         }
 
         $this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
-        $this->factory = new AssetFactory($this->kernel, '/path/to/web');
+        $this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
+        $this->factory = new AssetFactory($this->kernel, $this->container, '/path/to/web');
     }
 
     public function testBundleNotation()