]> Raphaël G. Git Repositories - packbundle/commitdiff
Add bundle configuration system
authorRaphaël Gertz <git@rapsys.eu>
Tue, 3 Apr 2018 04:32:08 +0000 (06:32 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Tue, 3 Apr 2018 04:32:08 +0000 (06:32 +0200)
DependencyInjection/Configuration.php
DependencyInjection/RapsysPackExtension.php

index 255359878c3cfc85730ed8040ea7237dcd4600bf..2e64eed62cb4b5b8678cb2e1a8293aac3ee69b94 100644 (file)
@@ -4,38 +4,87 @@ namespace Rapsys\PackBundle\DependencyInjection;
 
 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
 use Symfony\Component\Config\Definition\ConfigurationInterface;
+use Symfony\Component\Process\ExecutableFinder;
 
 /**
  * This is the class that validates and merges configuration from your app/config files.
  *
  * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
  */
-class Configuration implements ConfigurationInterface
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function getConfigTreeBuilder()
-    {
-        $treeBuilder = new TreeBuilder();
-        $rootNode = $treeBuilder->root('rapsys_pack');
+class Configuration implements ConfigurationInterface {
+       //Constructor required to derivate prefix from kernel.project_dir
+       public function __construct(string $projectDir) {
+               $this->projectDir = $projectDir;
+       }
 
-        // Here you should define the parameters that are allowed to
-        // configure your bundle. See the documentation linked above for
-        // more information on that topic.
+       /**
+        * {@inheritdoc}
+        */
+       public function getConfigTreeBuilder() {
+               $treeBuilder = new TreeBuilder();
+               $finder = new ExecutableFinder();
 
-       //TODO: see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php for default value and description
-       //TODO: see http://symfony.com/doc/current/components/config/definition.html
-       //TODO: use bin/console config:dump-reference to dump class infos
-       $rootNode
-               ->children()
-                       ->scalarNode('jpack')->end()
-                       ->scalarNode('cpack')->end()
-                       ->scalarNode('prefix')->end()
-                       ->scalarNode('scheme')->end()
-                       ->integerNode('timeout')->end()
-               ->end();
+               #TODO: see how we deal with asset url generation: see Rapsys/PackBundle/Twig/PackTokenParser.php +243
+               #framework:
+               #    assets:
+               #        packages:
+               #            rapsys_pack:
+               #                base_path: '/'
+               #                version: ~
+               #
+               ## Force cache disable to regenerate resources each time
+               ##twig:
+               ##    cache: ~
 
-        return $treeBuilder;
-    }
+               //Here we define the parameters that are allowed to configure the bundle.
+               //TODO: see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php for default value and description
+               //TODO: see http://symfony.com/doc/current/components/config/definition.html
+               //TODO: see https://github.com/symfony/assetic-bundle/blob/master/DependencyInjection/Configuration.php#L63
+               //TODO: use bin/console config:dump-reference to dump class infos
+               $treeBuilder
+                       //Parameters
+                       ->root('parameters')
+                               ->children()
+                                       ->arrayNode('rapsys_pack')
+                                               ->children()
+                                                       ->scalarNode('coutput')->defaultValue('css/*.pack.css')->end()
+                                                       ->scalarNode('joutput')->defaultValue('js/*.pack.js')->end()
+                                                       ->scalarNode('ioutput')->defaultValue('img/*.pack.jpg')->end()
+                                                       ->arrayNode('cfilter')
+                                                               ->treatNullLike(array())
+                                                               ->scalarPrototype()->end()
+                                                               ->defaultValue(array('Rapsys\PackBundle\Twig\Filter\CPackFilter'))
+                                                       ->end()
+                                                       ->arrayNode('jfilter')
+                                                               ->treatNullLike(array())
+                                                               ->scalarPrototype()->end()
+                                                               ->defaultValue(array('Rapsys\PackBundle\Twig\Filter\JPackFilter'))
+                                                       ->end()
+                                                       ->arrayNode('ifilter')
+                                                               ->treatNullLike(array())
+                                                               ->scalarPrototype()->end()
+                                                               ->defaultValue(array('Rapsys\PackBundle\Twig\Filter\IPackFilter'))
+                                                       ->end()
+                                                       ->scalarNode('prefix')->defaultValue($this->projectDir)->end()
+                                                       ->scalarNode('scheme')->defaultValue('https://')->end()
+                                                       ->integerNode('timeout')->min(0)->defaultValue((int)ini_get('default_socket_timeout'))->end()
+                                                       ->scalarNode('agent')->defaultValue(ini_get('user_agent'))->end()
+                                                       ->integerNode('redirect')->min(1)->defaultValue(20)->end()
+                                               ->end()
+                                       ->end()
+                                       ->arrayNode('rapsys_pack_cpackfilter')
+                                               ->children()
+                                                       ->scalarNode('bin')->defaultValue(function () use ($finder) { return $finder->find('cpack', '/usr/local/bin/cpack'); })->end()
+                                               ->end()
+                                       ->end()
+                                       ->arrayNode('rapsys_pack_jpackfilter')
+                                               ->children()
+                                                       ->scalarNode('bin')->defaultValue(function () use ($finder) { return $finder->find('jpack', '/usr/local/bin/jpack'); })->end()
+                                               ->end()
+                                       ->end()
+                               ->end()
+                       ->end();
+
+               return $treeBuilder;
+       }
 }
index b200abd3a04137f4c94fcda57dd9e2a1de41abe1..9bcd117bd526b627386362f9cd201eb50f69728c 100644 (file)
@@ -12,17 +12,19 @@ use Symfony\Component\DependencyInjection\Loader;
  *
  * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
  */
-class RapsysPackExtension extends Extension
-{
-    /**
-     * {@inheritdoc}
-     */
-    public function load(array $configs, ContainerBuilder $container)
-    {
-        $configuration = new Configuration();
-        $config = $this->processConfiguration($configuration, $configs);
+class RapsysPackExtension extends Extension {
+       /**
+        * {@inheritdoc}
+        */
+       public function load(array $configs, ContainerBuilder $container) {
+               $configuration = new Configuration($container->getParameter('kernel.project_dir').'/web/');
+               $config = $this->processConfiguration($configuration, $configs);
 
-        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
-        $loader->load('services.yml');
-    }
+               $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+               $loader->load('services.yml');
+       }
+
+       public function getConfiguration(array $configs, ContainerBuilder $container) {
+               return new Configuration($container->getParameter('kernel.project_dir').'/web/');
+       }
 }