Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adds support for asset variables

  • Loading branch information...
commit 52cf0eaa7160cbe33bcf1ed7224c7619ceeebbdc 1 parent da4a46c
@schmittjoh schmittjoh authored
View
52 Command/DumpCommand.php
@@ -11,6 +11,9 @@
namespace Symfony\Bundle\AsseticBundle\Command;
+use Assetic\Util\PathUtils;
+
+use Assetic\AssetWriter;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\LazyAssetManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
@@ -180,31 +183,42 @@ private function dumpAsset($name, OutputInterface $output)
*/
private function doDump(AssetInterface $asset, OutputInterface $output)
{
- $target = rtrim($this->basePath, '/').'/'.str_replace('_controller/', '', $asset->getTargetPath());
- if (!is_dir($dir = dirname($target))) {
- $output->writeln('<info>[dir+]</info> '.$dir);
- if (false === @mkdir($dir, 0777, true)) {
- throw new \RuntimeException('Unable to create directory '.$dir);
+ $writer = new AssetWriter(sys_get_temp_dir(), $this->getContainer()->getParameter('assetic.variables'));
+ $ref = new \ReflectionMethod($writer, 'getCombinations');
+ $ref->setAccessible(true);
+ $combinations = $ref->invoke($writer, $asset->getVars());
+
+ foreach ($combinations as $combination) {
+ $asset->setValues($combination);
+
+ $target = rtrim($this->basePath, '/').'/'.str_replace('_controller/', '',
+ PathUtils::resolvePath($asset->getTargetPath(), $asset->getVars(),
+ $asset->getValues()));
+ if (!is_dir($dir = dirname($target))) {
+ $output->writeln('<info>[dir+]</info> '.$dir);
+ if (false === @mkdir($dir, 0777, true)) {
+ throw new \RuntimeException('Unable to create directory '.$dir);
+ }
}
- }
- $output->writeln('<info>[file+]</info> '.$target);
- if ($this->verbose) {
- if ($asset instanceof \Traversable) {
- foreach ($asset as $leaf) {
- $root = $leaf->getSourceRoot();
- $path = $leaf->getSourcePath();
+ $output->writeln('<info>[file+]</info> '.$target);
+ if ($this->verbose) {
+ if ($asset instanceof \Traversable) {
+ foreach ($asset as $leaf) {
+ $root = $leaf->getSourceRoot();
+ $path = $leaf->getSourcePath();
+ $output->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
+ }
+ } else {
+ $root = $asset->getSourceRoot();
+ $path = $asset->getSourcePath();
$output->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
}
- } else {
- $root = $asset->getSourceRoot();
- $path = $asset->getSourcePath();
- $output->writeln(sprintf(' <comment>%s/%s</comment>', $root ?: '[unknown root]', $path ?: '[unknown path]'));
}
- }
- if (false === @file_put_contents($target, $asset->dump())) {
- throw new \RuntimeException('Unable to write file '.$target);
+ if (false === @file_put_contents($target, $asset->dump())) {
+ throw new \RuntimeException('Unable to write file '.$target);
+ }
}
}
}
View
16 Controller/AsseticController.php
@@ -11,6 +11,8 @@
namespace Symfony\Bundle\AsseticBundle\Controller;
+use Assetic\ValueSupplierInterface;
+
use Assetic\Asset\AssetCache;
use Assetic\Asset\AssetInterface;
use Assetic\Factory\LazyAssetManager;
@@ -32,6 +34,7 @@ class AsseticController
protected $cache;
protected $enableProfiler;
protected $profiler;
+ protected $valueSupplier;
public function __construct(Request $request, LazyAssetManager $am, CacheInterface $cache, $enableProfiler = false, Profiler $profiler = null)
{
@@ -42,6 +45,11 @@ public function __construct(Request $request, LazyAssetManager $am, CacheInterfa
$this->profiler = $profiler;
}
+ public function setValueSupplier(ValueSupplierInterface $supplier)
+ {
+ $this->valueSupplier = $supplier;
+ }
+
public function render($name, $pos = null)
{
if (!$this->enableProfiler && null !== $this->profiler) {
@@ -90,6 +98,14 @@ protected function createResponse()
protected function cachifyAsset(AssetInterface $asset)
{
+ if ($vars = $asset->getVars()) {
+ if (null === $this->valueSupplier) {
+ throw new \RuntimeException(sprintf('You must configure a value supplier if you have assets with variables.'));
+ }
+
+ $asset->setValues(array_intersect_key($this->valueSupplier->getValues(), array_flip($vars)));
+ }
+
return new AssetCache($asset, $this->cache);
}
View
35 DefaultValueSupplier.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Symfony\Bundle\AsseticBundle;
+
+use Assetic\ValueSupplierInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Default Value Supplier.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class DefaultValueSupplier implements ValueSupplierInterface
+{
+ protected $container;
+
+ public function __construct(ContainerInterface $container)
+ {
+ $this->container = $container;
+ }
+
+ public function getValues()
+ {
+ if (!$this->container->isScopeActive('request')) {
+ return array();
+ }
+
+ $request = $this->container->get('request');
+
+ return array(
+ 'locale' => $request->getLocale(),
+ 'env' => $this->container->getParameter('kernel.environment'),
+ );
+ }
+}
View
1  DependencyInjection/AsseticExtension.php
@@ -48,6 +48,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('assetic.enable_profiler', $config['use_controller']['profiler']);
$container->setParameter('assetic.read_from', $config['read_from']);
$container->setParameter('assetic.write_to', $config['write_to']);
+ $container->setParameter('assetic.variables', $config['variables']);
$container->setParameter('assetic.java.bin', $config['java']);
$container->setParameter('assetic.node.bin', $config['node']);
View
11 DependencyInjection/Configuration.php
@@ -68,6 +68,17 @@ public function getConfigTreeBuilder()
->scalarNode('sass')->defaultValue(function() use($finder) { return $finder->find('sass', '/usr/bin/sass'); })->end()
->end()
+ // variables
+ ->fixXmlConfig('variable')
+ ->children()
+ ->arrayNode('variables')
+ ->useAttributeAsKey('name')
+ ->prototype('array')
+ ->prototype('scalar')->end()
+ ->end()
+ ->end()
+ ->end()
+
// bundles
->fixXmlConfig('bundle')
->children()
View
7 Resources/config/assetic.xml
@@ -16,6 +16,7 @@
<parameter key="assetic.directory_resource.class">Symfony\Bundle\AsseticBundle\Factory\Resource\DirectoryResource</parameter>
<parameter key="assetic.filter_manager.class">Symfony\Bundle\AsseticBundle\FilterManager</parameter>
<parameter key="assetic.worker.ensure_filter.class">Assetic\Factory\Worker\EnsureFilterWorker</parameter>
+ <parameter key="assetic.value_supplier.class">Symfony\Bundle\AsseticBundle\DefaultValueSupplier</parameter>
<parameter key="assetic.node.paths" type="collection"></parameter>
<parameter key="assetic.cache_dir">%kernel.cache_dir%/assetic</parameter>
@@ -68,5 +69,11 @@
</service>
<service id="assetic.parameters" class="stdClass" factory-service="service_container" factory-method="getDefaultParameters" public="false" />
+
+ <service id="assetic.value_supplier.default" class="%assetic.value_supplier.class%" public="false">
+ <argument type="service" id="service_container" />
+ </service>
+
+ <service id="assetic.value_supplier" alias="assetic.value_supplier.default" public="false" />
</services>
</container>
View
4 Resources/config/controller.xml
@@ -23,6 +23,10 @@
<argument type="service" id="assetic.cache" />
<argument>%assetic.enable_profiler%</argument>
<argument type="service" id="profiler" on-invalid="null" />
+
+ <call method="setValueSupplier">
+ <argument type="service" id="assetic.value_supplier" on-invalid="ignore" />
+ </call>
</service>
<service id="assetic.cache" class="%assetic.cache.class%" public="false">
<argument>%assetic.cache_dir%/assets</argument>
View
1  Resources/config/templating_twig.xml
@@ -18,6 +18,7 @@
<argument>%assetic.use_controller%</argument>
<argument>%assetic.twig_extension.functions%</argument>
<argument>%assetic.bundles%</argument>
+ <argument type="service" id="assetic.value_supplier" on-invalid="null" />
</service>
<service id="assetic.twig_formula_loader" class="%assetic.cached_formula_loader.class%" public="false">
<tag name="assetic.formula_loader" alias="twig" />
View
32 Tests/Command/DumpCommandTest.php
@@ -68,10 +68,20 @@ protected function setUp()
$this->kernel->expects($this->any())
->method('getContainer')
->will($this->returnValue($this->container));
+
+ $writeTo = $this->writeTo;
$this->container->expects($this->any())
->method('getParameter')
- ->with('assetic.write_to')
- ->will($this->returnValue($this->writeTo));
+ ->will($this->returnCallback(function($p) use($writeTo) {
+ if ('assetic.write_to' === $p) {
+ return $writeTo;
+ } else if ('assetic.variables' === $p) {
+ return array();
+ }
+
+ throw new \RuntimeException(sprintf('Unknown parameter "%s".', $p));
+ }));
+
$this->container->expects($this->once())
->method('get')
->with('assetic.asset_manager')
@@ -122,6 +132,12 @@ public function testDumpOne()
$asset->expects($this->once())
->method('dump')
->will($this->returnValue('/* test_asset */'));
+ $asset->expects($this->any())
+ ->method('getVars')
+ ->will($this->returnValue(array()));
+ $asset->expects($this->any())
+ ->method('getValues')
+ ->will($this->returnValue(array()));
$this->command->run(new ArrayInput(array()), new NullOutput());
@@ -157,12 +173,24 @@ public function testDumpDebug()
$asset->expects($this->once())
->method('getIterator')
->will($this->returnValue(new \ArrayIterator(array($leaf))));
+ $asset->expects($this->any())
+ ->method('getVars')
+ ->will($this->returnValue(array()));
+ $asset->expects($this->any())
+ ->method('getValues')
+ ->will($this->returnValue(array()));
$leaf->expects($this->once())
->method('getTargetPath')
->will($this->returnValue('test_leaf.css'));
$leaf->expects($this->once())
->method('dump')
->will($this->returnValue('/* test_leaf */'));
+ $leaf->expects($this->any())
+ ->method('getVars')
+ ->will($this->returnValue(array()));
+ $leaf->expects($this->any())
+ ->method('getValues')
+ ->will($this->returnValue(array()));
$this->command->run(new ArrayInput(array()), new NullOutput());
View
1  Tests/DependencyInjection/AsseticExtensionTest.php
@@ -207,6 +207,7 @@ private function getDumpedContainer()
$container = new $class();
$container->enterScope('request');
+ $container->set('request', Request::create('/'));
$container->set('kernel', $this->kernel);
return $container;
View
5 Twig/AsseticExtension.php
@@ -11,6 +11,7 @@
namespace Symfony\Bundle\AsseticBundle\Twig;
+use Assetic\ValueSupplierInterface;
use Assetic\Extension\Twig\AsseticExtension as BaseAsseticExtension;
use Assetic\Factory\AssetFactory;
use Symfony\Component\Templating\TemplateNameParserInterface;
@@ -24,9 +25,9 @@ class AsseticExtension extends BaseAsseticExtension
{
private $useController;
- public function __construct(AssetFactory $factory, TemplateNameParserInterface $templateNameParser, $useController = false, $functions = array(), $enabledBundles = array())
+ public function __construct(AssetFactory $factory, TemplateNameParserInterface $templateNameParser, $useController = false, $functions = array(), $enabledBundles = array(), ValueSupplierInterface $valueSupplier = null)
{
- parent::__construct($factory, $functions);
+ parent::__construct($factory, $functions, $valueSupplier);
$this->useController = $useController;
$this->templateNameParser = $templateNameParser;
View
28 Twig/AsseticNode.php
@@ -27,7 +27,7 @@ protected function compileAssetUrl(\Twig_Compiler $compiler, AssetInterface $ass
->raw('isset($context[\'assetic\'][\'use_controller\']) && $context[\'assetic\'][\'use_controller\'] ? ')
->subcompile($this->getPathFunction($name))
->raw(' : ')
- ->subcompile($this->getAssetFunction($asset->getTargetPath()))
+ ->subcompile($this->getAssetFunction(new TargetPathNode($this, $asset, $name)))
;
}
@@ -43,7 +43,7 @@ private function getPathFunction($name)
private function getAssetFunction($path)
{
- $arguments = array(new \Twig_Node_Expression_Constant($path, $this->getLine()));
+ $arguments = array($path);
if ($this->hasAttribute('package')) {
$arguments[] = new \Twig_Node_Expression_Constant($this->getAttribute('package'), $this->getLine());
@@ -57,3 +57,27 @@ private function getAssetFunction($path)
);
}
}
+
+class TargetPathNode extends AsseticNode
+{
+ private $node;
+ private $asset;
+ private $name;
+
+ public function __construct(AsseticNode $node, AssetInterface $asset, $name)
+ {
+ $this->node = $node;
+ $this->asset = $asset;
+ $this->name = $name;
+ }
+
+ public function compile(\Twig_Compiler $compiler)
+ {
+ BaseAsseticNode::compileAssetUrl($compiler, $this->asset, $this->name);
+ }
+
+ public function getLine()
+ {
+ return $this->node->getLine();
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.