Bläddra i källkod

refactor the stylesheets and javascript options

Thomas Rabaix 11 år sedan
förälder
incheckning
b4858a41f4

+ 9 - 44
Admin/Pool.php

@@ -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;
     }
 
     /**
@@ -293,40 +291,6 @@ class Pool
         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
      */
@@ -344,16 +308,17 @@ class Pool
     }
 
     /**
-     * @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;
     }
 }

+ 1 - 1
DependencyInjection/Configuration.php

@@ -177,7 +177,7 @@ class Configuration implements ConfigurationInterface
                 ->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',

+ 4 - 2
DependencyInjection/SonataAdminExtension.php

@@ -41,7 +41,7 @@ class SonataAdminExtension extends Extension
         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
             );
         }
@@ -77,11 +77,13 @@ BOOM
         $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']);

+ 1 - 1
Resources/doc/reference/configuration.rst

@@ -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

+ 4 - 4
Resources/views/standard_layout.html.twig

@@ -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 %}
@@ -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 %}

+ 5 - 0
Tests/Admin/PoolTest.php

@@ -234,6 +234,11 @@ class PoolTest extends \PHPUnit_Framework_TestCase
         $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
      */