Skip to content

Commit

Permalink
[AsseticBundle] added support for parameter replacement in asset inputs
Browse files Browse the repository at this point in the history
    {% javascripts '%kernel.root_dir%/Resources/css/main.css' %}
    ...
    {% endjavascripts %}
  • Loading branch information
kriswallsmith committed Jun 1, 2011
1 parent af84cfe commit 3e68eb6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Symfony/Bundle/AsseticBundle/Factory/AssetFactory.php
Expand Up @@ -13,6 +13,7 @@

use Assetic\Factory\AssetFactory as BaseAssetFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;

/**
Expand All @@ -24,31 +25,36 @@ class AssetFactory extends BaseAssetFactory
{
private $kernel;
private $container;
private $parameterBag;

/**
* 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
* @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 ParameterBagInterface $parameterBag The container parameter bag
* @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)
public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
{
$this->kernel = $kernel;
$this->container = $container;
$this->parameterBag = $parameterBag;

parent::__construct($baseDir, $debug);
}

/**
* Adds support for bundle notation file and glob assets.
* Adds support for bundle notation file and glob assets and parameter placeholders.
*
* FIXME: This is a naive implementation of globs in that it doesn't
* attempt to support bundle inheritance within the glob pattern itself.
*/
protected function parseInput($input, array $options = array())
{
$input = $this->parameterBag->resolveValue($input);

// expand bundle notation
if ('@' == $input[0] && false !== strpos($input, '/')) {
// use the bundle path as this asset's root
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Bundle/AsseticBundle/Resources/config/assetic.xml
Expand Up @@ -35,6 +35,7 @@
<service id="assetic.asset_factory" class="%assetic.asset_factory.class%" public="false">
<argument type="service" id="kernel" />
<argument type="service" id="service_container" />
<argument type="service" id="assetic.parameter_bag" />
<argument>%assetic.read_from%</argument>
<argument>%assetic.debug%</argument>
</service>
Expand All @@ -60,5 +61,11 @@
<argument /> <!-- pattern -->
<argument /> <!-- filter -->
</service>

<service id="assetic.parameter_bag" class="Symfony\Component\DependencyInjection\ParameterBag\ParameterBag" public="false">
<argument type="service" id="assetic.parameters" />
</service>

<service id="assetic.parameters" class="stdClass" factory-service="service_container" factory-method="getDefaultParameters" public="false" />

This comment has been minimized.

Copy link
@kriswallsmith

kriswallsmith Jun 1, 2011

Author Contributor

I'm tricking the container into giving me a populated parameter bag here (notice getDefaultParameters() is protected) so I can perform parameter replacement. Should we provide a better way to do this? Maybe have the dumped container lazily create the parameter bag? Right now getParameterBag() returns null.

This comment has been minimized.

Copy link
@fabpot

fabpot Jun 1, 2011

Member

Why not use $this->container->getParameterBag()->resolveValue('...');?

This comment has been minimized.

Copy link
@kriswallsmith

kriswallsmith Jun 1, 2011

Author Contributor

I've done that in #1184.

</services>
</container>
Expand Up @@ -27,14 +27,18 @@ protected function setUp()

$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface');
$this->container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$this->factory = new AssetFactory($this->kernel, $this->container, '/path/to/web');
$this->parameterBag = $this->getMock('Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBagInterface');
$this->factory = new AssetFactory($this->kernel, $this->container, $this->parameterBag, '/path/to/web');
}

public function testBundleNotation()
{
$input = '@MyBundle/Resources/css/main.css';
$bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');

$this->parameterBag->expects($this->once())
->method('resolveValue')
->will($this->returnCallback(function($v) { return $v; }));
$this->kernel->expects($this->once())
->method('getBundle')
->with('MyBundle')
Expand All @@ -61,6 +65,9 @@ public function testBundleGlobNotation($input)
{
$bundle = $this->getMock('Symfony\\Component\\HttpKernel\\Bundle\\BundleInterface');

$this->parameterBag->expects($this->once())
->method('resolveValue')
->will($this->returnCallback(function($v) { return $v; }));
$this->kernel->expects($this->once())
->method('getBundle')
->with('MyBundle')
Expand Down

0 comments on commit 3e68eb6

Please sign in to comment.