Skip to content

[AsseticBundle] fixed formula caching system #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Feb 25, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ protected function createDirectoryResourceDefinition($bundle, $dir, $engine)
$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)
Expand Down
31 changes: 16 additions & 15 deletions src/Symfony/Bundle/AsseticBundle/Factory/DirectoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
61 changes: 54 additions & 7 deletions src/Symfony/Bundle/AsseticBundle/Factory/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,80 @@

namespace Symfony\Bundle\AsseticBundle\Factory;

use Assetic\Factory\Resource\ResourceInterface;
use Assetic\Factory\Resource\FileResourceInterface;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\Templating\Loader\LoaderInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;

/**
* A file resource.
*
* @author Kris Wallsmith <kris.wallsmith@symfony-project.com>
*/
class FileResource implements ResourceInterface
class FileResource implements FileResourceInterface
{
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();
}

public function getPath()
{
return $this->path;
}

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.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Bundle/AsseticBundle/Routing/AsseticLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

namespace Symfony\Bundle\AsseticBundle\Routing;

use Assetic\AssetManager;
use Assetic\Factory\LazyAssetManager;
use Assetic\Factory\Resource\FileResourceInterface;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

Expand All @@ -37,14 +39,28 @@ class AsseticLoader extends Loader
{
protected $am;

public function __construct(AssetManager $am)
public function __construct(LazyAssetManager $am)
{
$this->am = $am;
}

public function load($resource, $type = null)
{
$routes = new RouteCollection();

// resources
foreach ($this->am->getResources() as $resource) {
if (!$resource instanceof \Traversable) {
$resource = array($resource);
}
foreach ($resource as $r) {
if ($r instanceof FileResourceInterface) {
$routes->addResource(new FileResource($r->getPath()));
}
}
}

// routes
foreach ($this->am->getNames() as $name) {
$asset = $this->am->get($name);

Expand Down