Ver Fonte

[AsseticBundle] fixed formula caching system

Kris Wallsmith há 14 anos atrás
pai
commit
05055d443c

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

@@ -135,11 +135,10 @@ class AsseticExtension extends Extension
         $definition = new Definition('%assetic.directory_resource.class%');
 
         $definition
-            ->addArgument(new Reference('templating.name_parser'))
             ->addArgument(new Reference('templating.loader'))
             ->addArgument($bundle)
             ->addArgument($dir)
-            ->addArgument('/\.'.$engine.'$/')
+            ->addArgument('/^[^.]+\.[^.]+\.'.$engine.'$/')
             ->addTag('assetic.templating.'.$engine)
             ->addTag('assetic.formula_resource', array('loader' => $engine))
             ->setPublic(false)

+ 16 - 15
src/Symfony/Bundle/AsseticBundle/Factory/DirectoryResource.php

@@ -12,37 +12,38 @@
 namespace Symfony\Bundle\AsseticBundle\Factory;
 
 use Assetic\Factory\Resource\DirectoryResource as BaseDirectoryResource;
-use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser;
 use Symfony\Component\Templating\Loader\LoaderInterface;
 
 /**
- * A directory resource that creates Symfony2 resources.
+ * A directory resource that creates Symfony2 templating resources.
  *
  * @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
  */
 class DirectoryResource extends BaseDirectoryResource
 {
-    protected $parser;
     protected $loader;
     protected $bundle;
-    protected $baseDirLength;
-
-    public function __construct(TemplateNameParser $parser, LoaderInterface $loader, $bundle, $baseDir, $pattern = null)
+    protected $path;
+
+    /**
+     * Constructor.
+     *
+     * @param LoaderInterface $loader  The templating loader
+     * @param string          $bundle  The current bundle name
+     * @param string          $path    The directory path
+     * @param string          $pattern A regex pattern for file basenames
+     */
+    public function __construct(LoaderInterface $loader, $bundle, $path, $pattern = null)
     {
-        $this->parser = $parser;
         $this->loader = $loader;
         $this->bundle = $bundle;
+        $this->path = rtrim($path, '/').'/';
 
-        $this->baseDirLength = strlen(rtrim($baseDir, '/')) + 1;
-
-        parent::__construct($baseDir, $pattern);
+        parent::__construct($path, $pattern);
     }
 
-    protected function createResource($path)
+    public function getIterator()
     {
-        $template = $this->parser->parseFromFilename(substr($path, $this->baseDirLength));
-        $template->set('bundle', $this->bundle);
-
-        return new FileResource($this->loader, $template);
+        return new DirectoryResourceIterator($this->loader, $this->bundle, $this->path, $this->getInnerIterator());
     }
 }

+ 45 - 0
src/Symfony/Bundle/AsseticBundle/Factory/DirectoryResourceIterator.php

@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+namespace Symfony\Bundle\AsseticBundle\Factory;
+
+use Symfony\Component\Templating\Loader\LoaderInterface;
+
+class DirectoryResourceIterator extends \RecursiveIteratorIterator
+{
+    protected $loader;
+    protected $bundle;
+    protected $path;
+
+    /**
+     * Constructor.
+     *
+     * @param LoaderInterface   $loader   The templating loader
+     * @param string            $bundle   The current bundle name
+     * @param string            $path     The directory
+     * @param RecursiveIterator $iterator The inner iterator
+     */
+    public function __construct(LoaderInterface $loader, $bundle, $path, \RecursiveIterator $iterator)
+    {
+        $this->loader = $loader;
+        $this->bundle = $bundle;
+        $this->path = $path;
+
+        parent::__construct($iterator);
+    }
+
+    public function current()
+    {
+        $file = parent::current();
+
+        return new FileResource($this->loader, $this->bundle, $this->path, $file->getPathname());
+    }
+}

+ 47 - 5
src/Symfony/Bundle/AsseticBundle/Factory/FileResource.php

@@ -12,8 +12,8 @@
 namespace Symfony\Bundle\AsseticBundle\Factory;
 
 use Assetic\Factory\Resource\ResourceInterface;
+use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
 use Symfony\Component\Templating\Loader\LoaderInterface;
-use Symfony\Component\Templating\TemplateReferenceInterface;
 
 /**
  * A file resource.
@@ -23,21 +23,63 @@ use Symfony\Component\Templating\TemplateReferenceInterface;
 class FileResource implements ResourceInterface
 {
     protected $loader;
+    protected $parser;
+    protected $bundle;
+    protected $baseDir;
+    protected $path;
+
     protected $template;
 
-    public function __construct(LoaderInterface $loader, TemplateReferenceInterface $template)
+    /**
+     * Constructor.
+     *
+     * @param LoaderInterface $loader  The templating loader
+     * @param string          $bundle  The current bundle name
+     * @param string          $baseDir The directory
+     * @param string          $path    The file path
+     */
+    public function __construct(LoaderInterface $loader, $bundle, $baseDir, $path)
     {
         $this->loader = $loader;
-        $this->template = $template;
+        $this->bundle = $bundle;
+        $this->baseDir = $baseDir;
+        $this->path = $path;
     }
 
     public function isFresh($timestamp)
     {
-        return $this->loader->isFresh($this->template, $timestamp);
+        return $this->loader->isFresh($this->getTemplate(), $timestamp);
     }
 
     public function getContent()
     {
-        return $this->loader->load($this->template)->getContent();
+        return $this->loader->load($this->getTemplate())->getContent();
+    }
+
+    protected function getTemplate()
+    {
+        if (null === $this->template) {
+            $this->template = self::createTemplateReference($this->bundle, substr($this->path, strlen($this->baseDir)));
+        }
+
+        return $this->template;
+    }
+
+    static private function createTemplateReference($bundle, $file)
+    {
+        $parts = explode('/', strtr($file, '\\', '/'));
+        $elements = explode('.', array_pop($parts));
+
+        return new TemplateReference($bundle, implode('/', $parts), $elements[0], $elements[1], $elements[2]);
+    }
+
+    public function __sleep()
+    {
+        return array('path');
+    }
+
+    public function __wakeup()
+    {
+        throw new \Exception(__CLASS__.' cannot be unserialized.');
     }
 }

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

@@ -24,6 +24,7 @@
             <tag name="assetic.templating.php" />
             <argument type="service" id="assetic.php_formula_loader.real" />
             <argument type="service" id="assetic.config_cache" />
+            <argument>%kernel.debug%</argument>
         </service>
         <service id="assetic.php_formula_loader.real" class="%assetic.php_formula_loader.class%" public="false">
             <tag name="assetic.templating.php" />

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

@@ -25,6 +25,7 @@
             <tag name="assetic.templating.twig" />
             <argument type="service" id="assetic.twig_formula_loader.real" />
             <argument type="service" id="assetic.config_cache" />
+            <argument>%kernel.debug%</argument>
         </service>
         <service id="assetic.twig_formula_loader.real" class="%assetic.twig_formula_loader.class%" public="false">
             <tag name="assetic.templating.twig" />