Browse Source

[AsseticBundle] separated read and write paths to facilitate writing assets to a CDN via a custom stream wrapper

Kris Wallsmith 14 năm trước cách đây
mục cha
commit
5683c6e6e1

+ 9 - 2
src/Symfony/Bundle/AsseticBundle/CacheWarmer/AssetWriterCacheWarmer.php

@@ -14,25 +14,32 @@ namespace Symfony\Bundle\AsseticBundle\CacheWarmer;
 use Assetic\AssetManager;
 use Assetic\AssetWriter;
 use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
+use Symfony\Component\EventDispatcher\Event;
+use Symfony\Component\EventDispatcher\EventDispatcher;
 
 class AssetWriterCacheWarmer extends CacheWarmer
 {
     protected $am;
     protected $writer;
+    protected $dispatcher;
 
-    public function __construct(AssetManager $am, AssetWriter $writer)
+    public function __construct(AssetManager $am, AssetWriter $writer, EventDispatcher $dispatcher)
     {
         $this->am = $am;
         $this->writer = $writer;
+        $this->dispatcher = $dispatcher;
     }
 
     public function warmUp($cacheDir)
     {
+        // notify an event so custom stream wrappers can be registered lazily
+        $this->dispatcher->notify(new Event(null, 'assetic.write'));
+
         $this->writer->writeManagerAssets($this->am);
     }
 
     public function isOptional()
     {
-        return false;
+        return true;
     }
 }

+ 16 - 12
src/Symfony/Bundle/AsseticBundle/Command/DumpCommand.php

@@ -18,6 +18,7 @@ use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\EventDispatcher\Event;
 
 /**
  * Dumps assets to the filesystem.
@@ -31,25 +32,28 @@ class DumpCommand extends Command
         $this
             ->setName('assetic:dump')
             ->setDescription('Dumps all assets to the filesystem')
-            ->addArgument('base_dir', InputArgument::OPTIONAL, 'The base directory')
+            ->addArgument('write_to', InputArgument::OPTIONAL, 'Override the configured asset root')
             ->addOption('watch', null, InputOption::VALUE_NONE, 'Check for changes every second')
         ;
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        if (!$baseDir = $input->getArgument('base_dir')) {
-            $baseDir = $this->container->getParameter('assetic.document_root');
+        if (!$basePath = $input->getArgument('write_to')) {
+            $basePath = $this->container->getParameter('assetic.write_to');
         }
 
         $am = $this->container->get('assetic.asset_manager');
 
+        // notify an event so custom stream wrappers can be registered lazily
+        $this->container->get('event_dispatcher')->notify(new Event(null, 'assetic.write', array('path' => $basePath)));
+
         if ($input->getOption('watch')) {
-            return $this->watch($am, $baseDir, $output, $this->container->getParameter('kernel.debug'));
+            return $this->watch($am, $basePath, $output, $this->container->getParameter('kernel.debug'));
         }
 
         foreach ($am->getNames() as $name) {
-            $this->dumpAsset($am->get($name), $baseDir, $output);
+            $this->dumpAsset($am->get($name), $basePath, $output);
         }
     }
 
@@ -60,17 +64,17 @@ class DumpCommand extends Command
      * manager for changes.
      *
      * @param LazyAssetManager $am      The asset manager
-     * @param string           $baseDir The base directory to write to
+     * @param string           $basePath The base directory to write to
      * @param OutputInterface  $output  The command output
      * @param Boolean          $debug   Debug mode
      */
-    protected function watch(LazyAssetManager $am, $baseDir, OutputInterface $output, $debug = false)
+    protected function watch(LazyAssetManager $am, $basePath, OutputInterface $output, $debug = false)
     {
         $refl = new \ReflectionClass('Assetic\\AssetManager');
         $prop = $refl->getProperty('assets');
         $prop->setAccessible(true);
 
-        $cache = sys_get_temp_dir().'/assetic_watch_'.substr(sha1($baseDir), 0, 7);
+        $cache = sys_get_temp_dir().'/assetic_watch_'.substr(sha1($basePath), 0, 7);
         if (file_exists($cache)) {
             $previously = unserialize(file_get_contents($cache));
         } else {
@@ -82,7 +86,7 @@ class DumpCommand extends Command
             try {
                 foreach ($am->getNames() as $name) {
                     if ($asset = $this->checkAsset($am, $name, $previously)) {
-                        $this->dumpAsset($asset, $baseDir, $output);
+                        $this->dumpAsset($asset, $basePath, $output);
                     }
                 }
 
@@ -135,14 +139,14 @@ class DumpCommand extends Command
      * Writes an asset.
      *
      * @param AssetInterface  $asset   An asset
-     * @param string          $baseDir The base directory to write to
+     * @param string          $basePath The base directory to write to
      * @param OutputInterface $output  The command output
      *
      * @throws RuntimeException If there is a problem writing the asset
      */
-    protected function dumpAsset(AssetInterface $asset, $baseDir, OutputInterface $output)
+    protected function dumpAsset(AssetInterface $asset, $basePath, OutputInterface $output)
     {
-        $target = rtrim($baseDir, '/') . '/' . $asset->getTargetUrl();
+        $target = rtrim($basePath, '/') . '/' . $asset->getTargetUrl();
         if (!is_dir($dir = dirname($target))) {
             $output->writeln('<info>[dir+]</info> '.$dir);
             if (false === @mkdir($dir)) {

+ 6 - 3
src/Symfony/Bundle/AsseticBundle/DependencyInjection/AsseticExtension.php

@@ -37,7 +37,8 @@ class AsseticExtension extends Extension
      *     <assetic:config
      *         debug="true"
      *         use-controller="true"
-     *         document-root="/path/to/document/root"
+     *         read-from="/path/to/web"
+     *         write-to="s3://mybucket"
      *         closure="/path/to/google_closure/compiler.jar"
      *         yui="/path/to/yuicompressor.jar"
      *         default-javascripts-output="js/build/*.js"
@@ -49,7 +50,8 @@ class AsseticExtension extends Extension
      *     assetic:
      *         debug: true
      *         use_controller: true
-     *         document_root: /path/to/document/root
+     *         read_from: /path/to/web
+     *         write_to: s3://mybucket
      *         closure: /path/to/google_closure/compiler.jar
      *         yui: /path/to/yuicompressor.jar
      *         default_javascripts_output: js/build/*.js
@@ -71,7 +73,8 @@ class AsseticExtension extends Extension
 
         $container->setParameter('assetic.debug', $config['debug']);
         $container->setParameter('assetic.use_controller', $config['use_controller']);
-        $container->setParameter('assetic.document_root', $config['document_root']);
+        $container->setParameter('assetic.read_from', $config['read_from']);
+        $container->setParameter('assetic.write_to', $config['write_to']);
         $container->setParameter('assetic.default_javascripts_output', $config['default_javascripts_output']);
         $container->setParameter('assetic.default_stylesheets_output', $config['default_stylesheets_output']);
 

+ 2 - 1
src/Symfony/Bundle/AsseticBundle/DependencyInjection/Configuration.php

@@ -37,7 +37,8 @@ class Configuration
         $rootNode
             ->booleanNode('debug')->defaultValue($kernelDebug)->end()
             ->booleanNode('use_controller')->defaultValue($kernelDebug)->end()
-            ->scalarNode('document_root')->defaultValue('%kernel.root_dir%/../web')->end()
+            ->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
+            ->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
             ->scalarNode('closure')->end()
             ->scalarNode('yui')->end()
             ->scalarNode('default_javascripts_output')->defaultValue('js/*.js')->end()

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

@@ -14,9 +14,10 @@
             <tag name="kernel.cache_warmer" />
             <argument type="service" id="assetic.asset_manager" />
             <argument type="service" id="assetic.asset_writer" />
+            <argument type="service" id="event_dispatcher" />
         </service>
         <service id="assetic.asset_writer" class="%assetic.asset_writer.class%" public="false">
-            <argument>%assetic.document_root%</argument>
+            <argument>%assetic.write_to%</argument>
         </service>
     </services>
 </container>

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

@@ -45,7 +45,7 @@
         </service>
         <service id="assetic.asset_factory" class="%assetic.asset_factory.class%" public="false">
             <argument type="service" id="kernel" />
-            <argument>%assetic.document_root%</argument>
+            <argument>%assetic.read_from%</argument>
             <argument>%assetic.debug%</argument>
             <call method="setFilterManager"><argument type="service" id="assetic.filter_manager" /></call>
         </service>
@@ -56,7 +56,7 @@
         </service>
         <service id="assetic.filter.less" class="%assetic.filter.less.class%">
             <tag name="assetic.filter" alias="less" />
-            <argument>%assetic.document_root%</argument>
+            <argument>%assetic.read_from%</argument>
             <argument>%assetic.node_bin%</argument>
             <argument>%assetic.node_paths%</argument>
         </service>
@@ -73,7 +73,7 @@
         </service>
         <service id="assetic.filter.sprockets" class="%assetic.filter.sprockets.class%">
             <tag name="assetic.filter" alias="sprockets" />
-            <argument>%assetic.document_root%</argument>
+            <argument>%assetic.read_from%</argument>
             <argument>%assetic.sprocketize_bin%</argument>
         </service>
         <service id="assetic.filter.coffee" class="%assetic.filter.coffee.class%">
@@ -83,7 +83,7 @@
         </service>
         <service id="assetic.filter.stylus" class="%assetic.filter.stylus.class%">
             <tag name="assetic.filter" alias="stylus" />
-            <argument>%assetic.document_root%</argument>
+            <argument>%assetic.read_from%</argument>
             <argument>%assetic.node_bin%</argument>
             <argument>%assetic.node_paths%</argument>
         </service>

+ 2 - 1
src/Symfony/Bundle/AsseticBundle/Resources/config/schema/assetic-1.0.xsd

@@ -9,7 +9,8 @@
         <xsd:complexType name="config">
             <xsd:attribute name="debug" type="xsd:string" />
             <xsd:attribute name="use-controller" type="xsd:string" />
-            <xsd:attribute name="document-root" type="xsd:string" />
+            <xsd:attribute name="read-from" type="xsd:string" />
+            <xsd:attribute name="write-to" type="xsd:string" />
             <xsd:attribute name="closure" type="xsd:string" />
             <xsd:attribute name="yui" type="xsd:string" />
             <xsd:attribute name="default-javascripts-output" type="xsd:string" />

+ 0 - 19
src/Symfony/Bundle/AsseticBundle/Tests/DependencyInjection/AsseticExtensionTest.php

@@ -92,25 +92,6 @@ class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
         $this->assertSaneContainer($this->getDumpedContainer());
     }
 
-    /**
-     * @dataProvider getDocumentRootKeys
-     */
-    public function testDocumentRoot($key)
-    {
-        $extension = new AsseticExtension();
-        $extension->load(array(array($key => '/path/to/web')), $this->container);
-
-        $this->assertEquals('/path/to/web', $this->container->getParameter('assetic.document_root'), '"'.$key.'" sets document root');
-    }
-
-    public function getDocumentRootKeys()
-    {
-        return array(
-            array('document_root'),
-            array('document-root'),
-        );
-    }
-
     /**
      * @dataProvider getUseControllerKeys
      */

+ 1 - 1
src/Symfony/Bundle/AsseticBundle/Tests/Resources/config/config.yml

@@ -18,4 +18,4 @@ twig:
 
 assetic:
     use_controller: true
-    document_root:  "%kernel.root_dir%/web"
+    read_from:      "%kernel.root_dir%/web"