Skip to content

Commit

Permalink
refactor the stylesheets and javascript options
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Apr 8, 2014
1 parent 1476bbf commit b4858a4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 52 deletions.
53 changes: 9 additions & 44 deletions Admin/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ class Pool
protected $options;

/**
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param string $title
* @param string $logoTitle
* @param array $options
* @param array $assets
* @param ContainerInterface $container
* @param string $title
* @param string $logoTitle
* @param array $options
*/
public function __construct(ContainerInterface $container, $title, $logoTitle, $options = array(), $assets = array())
public function __construct(ContainerInterface $container, $title, $logoTitle, $options = array())
{
$this->container = $container;
$this->title = $title;
$this->titleLogo = $logoTitle;
$this->options = $options;
$this->assets = $assets;
}

/**
Expand Down Expand Up @@ -293,40 +291,6 @@ public function getTemplate($name)
return null;
}

/**
* @param array $assets
*
* @return void
*/
public function setAssets(array $assets)
{
$this->assets = $assets;
}

/**
* @return array
*/
public function getAssets()
{
return $this->assets;
}

/**
* @return array
*/
public function getCss()
{
return $this->assets['css'];
}

/**
* @return array
*/
public function getJavascripts()
{
return $this->assets['javascripts'];
}

/**
* @return string
*/
Expand All @@ -344,16 +308,17 @@ public function getTitle()
}

/**
* @param $name
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getOption($name)
public function getOption($name, $default = null)
{
if (isset($this->options[$name])) {
return $this->options[$name];
}

return null;
return $default;
}
}
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function getConfigTreeBuilder()
->arrayNode('assets')
->addDefaultsIfNotSet()
->children()
->arrayNode('css')
->arrayNode('stylesheets')
->defaultValue(array(
'bundles/sonataadmin/vendor/bootstrap/dist/css/bootstrap.min.css',
'bundles/sonataadmin/vendor/AdminLTE/css/font-awesome.min.css',
Expand Down
6 changes: 4 additions & 2 deletions DependencyInjection/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function load(array $configs, ContainerBuilder $container)
if (!isset($bundles['SonataCoreBundle'])) {
throw new \RuntimeException(<<<BOOM
Boom! you are living on the edge ;) The AdminBundle requires the CoreBundle!
Please add ``"sonata-project/core-bundle": "~2.2@dev"`` into your composer.json file and add the SonataCoreBundle into the AppKernel');
Please add ``"sonata-project/core-bundle": "~2.2"`` into your composer.json file and add the SonataCoreBundle into the AppKernel');
BOOM
);
}
Expand Down Expand Up @@ -77,11 +77,13 @@ public function load(array $configs, ContainerBuilder $container)
$processor = new Processor();
$config = $processor->processConfiguration($configuration, $configs);

$config['options']['javascripts'] = $config['assets']['javascripts'];
$config['options']['stylesheets'] = $config['assets']['stylesheets'];

$pool = $container->getDefinition('sonata.admin.pool');
$pool->replaceArgument(1, $config['title']);
$pool->replaceArgument(2, $config['title_logo']);
$pool->replaceArgument(3, $config['options']);
$pool->replaceArgument(4, $config['assets']);

$container->setParameter('sonata.admin.configuration.templates', $config['templates']);
$container->setParameter('sonata.admin.configuration.admin_services', $config['admin_services']);
Expand Down
2 changes: 1 addition & 1 deletion Resources/doc/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Full Configuration Options
pager_results: SonataAdminBundle:Pager:results.html.twig
assets:
css:
stylesheets:
# Defaults:
- bundles/sonataadmin/admin-lte/css/bootstrap.min.css
Expand Down
8 changes: 4 additions & 4 deletions Resources/views/standard_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ file that was distributed with this source code.

{% block stylesheets %}

{% for css in admin_pool.css %}
<link rel="stylesheet" href="{{ asset(css) }}" />
{% for stylesheet in admin_pool.getOption('stylesheets', []) %}
<link rel="stylesheet" href="{{ asset(stylesheet) }}" />
{% endfor %}

{% endblock %}
Expand All @@ -46,8 +46,8 @@ file that was distributed with this source code.
};
</script>

{% for js in admin_pool.javascripts %}
<script src="{{ asset(js) }}"></script>
{% for javascript in admin_pool.getOption('javascripts', []) %}
<script src="{{ asset(javascript) }}"></script>
{% endfor %}

{% endblock %}
Expand Down
5 changes: 5 additions & 0 deletions Tests/Admin/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ public function testGetOption()
$this->assertEquals(null, $this->pool->getOption('non_existent_option'));
}

public function testOptionDefault()
{
$this->assertEquals(array(), $this->pool->getOption('nonexistantarray', array()));
}

/**
* @return Symfony\Component\DependencyInjection\ContainerInterface - the mock of container interface
*/
Expand Down

0 comments on commit b4858a4

Please sign in to comment.