Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[AsseticBundle] added config option to limit which bundles formulae a…
…re loaded from
  • Loading branch information
kriswallsmith committed Feb 28, 2011
1 parent 57d0d3f commit d57c665
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 62 deletions.
Expand Up @@ -29,34 +29,6 @@ class AsseticExtension extends Extension
/**
* Loads the configuration.
*
* When the debug flag is true, files in an asset collections will be
* rendered individually.
*
* In XML:
*
* <assetic:config
* debug="true"
* use-controller="true"
* 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"
* default-stylesheets-output="css/build/*.css"
* />
*
* In YAML:
*
* assetic:
* debug: true
* use_controller: true
* 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
* default_stylesheets_output: css/build/*.css
*
* @param array $configs An array of configuration settings
* @param ContainerBuilder $container A ContainerBuilder instance
*/
Expand All @@ -67,9 +39,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('templating_twig.xml');
// $loader->load('templating_php.xml'); // not ready yet

$configuration = new Configuration();
$processor = new Processor();
$config = $processor->process($configuration->getConfigTree($container->getParameter('kernel.debug')), $configs);
$config = self::processConfigs($configs, $container->getParameter('kernel.debug'), array_keys($container->getParameter('kernel.bundles')));

$container->setParameter('assetic.debug', $config['debug']);
$container->setParameter('assetic.use_controller', $config['use_controller']);
Expand Down Expand Up @@ -100,20 +70,36 @@ public function load(array $configs, ContainerBuilder $container)
$container->getDefinition('assetic.filter.less')->addMethodCall('setCompress', array('%assetic.less.compress%'));
}

$this->registerFormulaResources($container);
$this->registerFormulaResources($container, $container->getParameterBag()->resolveValue($config['bundles']));
}

protected function registerFormulaResources(ContainerBuilder $container)
static protected function processConfigs(array $configs, $debug, array $bundles)

This comment has been minimized.

Copy link
@kbond

kbond Jun 3, 2011

Member

@kriswallsmith what is the reason for this method to be static?

This comment has been minimized.

Copy link
@kriswallsmith

kriswallsmith Jun 3, 2011

Author Contributor

I've been making any methods that don't rely on the object state static -- but people think it's strange so I may stop.

This comment has been minimized.

Copy link
@kbond

kbond Jun 3, 2011

Member

I see. I have been working on a way to access an extension's Configuration object (#1099). I ran into some code duplication issues when setting it up for the AsseticBundle because of this static method.

{
// bundle views/ directories
$configuration = new Configuration();
$tree = $configuration->getConfigTree($debug, $bundles);

$processor = new Processor();
return $processor->process($tree, $configs);
}

static protected function registerFormulaResources(ContainerBuilder $container, array $bundles)
{
$map = $container->getParameter('kernel.bundles');

if ($diff = array_diff($bundles, array_keys($map))) {
throw new \InvalidArgumentException(sprintf('The following bundles are not registered: "%s"', implode('", "', $diff)));
}

$am = $container->getDefinition('assetic.asset_manager');
foreach ($container->getParameter('kernel.bundles') as $name => $class) {
$rc = new \ReflectionClass($class);

// bundle views/ directories
foreach ($bundles as $name) {
$rc = new \ReflectionClass($map[$name]);
if (is_dir($dir = dirname($rc->getFileName()).'/Resources/views')) {
foreach (array('twig', 'php') as $engine) {
$container->setDefinition(
'assetic.'.$engine.'_directory_resource.'.$name,
$this->createDirectoryResourceDefinition($name, $dir, $engine)
self::createDirectoryResourceDefinition($name, $dir, $engine)
);
}
}
Expand All @@ -124,7 +110,7 @@ protected function registerFormulaResources(ContainerBuilder $container)
foreach (array('twig', 'php') as $engine) {
$container->setDefinition(
'assetic.'.$engine.'_directory_resource.kernel',
$this->createDirectoryResourceDefinition('', $dir, $engine)
self::createDirectoryResourceDefinition('', $dir, $engine)
);
}
}
Expand All @@ -133,7 +119,7 @@ protected function registerFormulaResources(ContainerBuilder $container)
/**
* @todo decorate an abstract xml definition
*/
protected function createDirectoryResourceDefinition($bundle, $dir, $engine)
static protected function createDirectoryResourceDefinition($bundle, $dir, $engine)
{
$definition = new Definition('%assetic.directory_resource.class%');

Expand Down
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\AsseticBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

/**
Expand All @@ -27,23 +26,37 @@ class Configuration
/**
* Generates the configuration tree.
*
* @return \Symfony\Component\Config\Definition\NodeInterface
* @return Symfony\Component\Config\Definition\NodeInterface The config tree
*/
public function getConfigTree($kernelDebug)
public function getConfigTree($debug, $bundles)
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('assetic', 'array');

$rootNode
->booleanNode('debug')->defaultValue($kernelDebug)->end()
->booleanNode('use_controller')->defaultValue($kernelDebug)->end()
$tree = new TreeBuilder();
$tree->root('assetic', 'array')
->booleanNode('debug')->defaultValue($debug)->end()
->booleanNode('use_controller')->defaultValue($debug)->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()
->scalarNode('default_stylesheets_output')->defaultValue('css/*.css')->end();
->scalarNode('default_stylesheets_output')->defaultValue('css/*.css')->end()
->fixXmlConfig('bundle')
->arrayNode('bundles')
->defaultValue($bundles)
->requiresAtLeastOneElement()
->beforeNormalization()
->ifTrue(function($v){ return !is_array($v); })
->then(function($v){ return array($v); })
->end()
->prototype('scalar')
->beforeNormalization()
->ifTrue(function($v) { return is_array($v) && isset($v['name']); })
->then(function($v){ return $v['name']; })
->end()
->end()
->end()
;

return $treeBuilder->buildTree();
return $tree->buildTree();
}
}
Expand Up @@ -5,16 +5,23 @@
targetNamespace="http://www.symfony-project.org/schema/dic/assetic"
elementFormDefault="qualified">

<xsd:element name="config">
<xsd:complexType name="config">
<xsd:attribute name="debug" type="xsd:string" />
<xsd:attribute name="use-controller" 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" />
<xsd:attribute name="default-stylesheets-output" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="config" type="config" />

<xsd:complexType name="config">
<xsd:sequence>
<xsd:element name="bundle" type="bundle" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="debug" type="xsd:string" />
<xsd:attribute name="use-controller" 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" />
<xsd:attribute name="default-stylesheets-output" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="bundle">
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:schema>

0 comments on commit d57c665

Please sign in to comment.