]> Raphaƫl G. Git Repositories - packbundle/blob - DependencyInjection/Configuration.php
Load configuration by default
[packbundle] / DependencyInjection / Configuration.php
1 <?php
2
3 namespace Rapsys\PackBundle\DependencyInjection;
4
5 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6 use Symfony\Component\Config\Definition\ConfigurationInterface;
7 use Symfony\Component\Process\ExecutableFinder;
8
9 /**
10 * This is the class that validates and merges configuration from your app/config files.
11 *
12 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/configuration.html}
13 */
14 class Configuration implements ConfigurationInterface {
15 //Constructor required to derivate prefix from kernel.project_dir
16 public function __construct($projectDir) {
17 $this->projectDir = $projectDir;
18 }
19
20 /**
21 * {@inheritdoc}
22 */
23 public function getConfigTreeBuilder() {
24 //Get TreeBuilder object
25 $treeBuilder = new TreeBuilder();
26
27 //Get ExecutableFinder object
28 $finder = new ExecutableFinder();
29
30 #TODO: see how we deal with asset url generation: see Rapsys/PackBundle/Twig/PackTokenParser.php +243
31 #framework:
32 # assets:
33 # packages:
34 # rapsys_pack:
35 # base_path: '/'
36 # version: ~
37 #
38 ## Force cache disable to regenerate resources each time
39 ##twig:
40 ## cache: ~
41
42 //TODO: see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php for default value and description
43 //TODO: see http://symfony.com/doc/current/components/config/definition.html
44 //TODO: see https://github.com/symfony/assetic-bundle/blob/master/DependencyInjection/Configuration.php#L63
45 //TODO: use bin/console config:dump-reference to dump class infos
46
47 //The bundle default values
48 $defaults = [
49 'config' => [
50 'prefix' => $this->projectDir,
51 'scheme' => 'https://',
52 'timeout' => (int)ini_get('default_socket_timeout'),
53 'agent' => (string)ini_get('user_agent')?:'rapsys_pack/0.0.2',
54 'redirect' => 5
55 ],
56 'output' => [
57 'css' => 'css/*.pack.css',
58 'js' => 'js/*.pack.js',
59 'img' => 'img/*.pack.jpg'
60 ],
61 'filter' => [
62 'css' => [
63 'Rapsys\PackBundle\Twig\Filter\CPackFilter' => [$finder->find('cpack', '/usr/local/bin/cpack')]
64 ],
65 'js' => [
66 'Rapsys\PackBundle\Twig\Filter\JPackFilter' => [$finder->find('jpack', '/usr/local/bin/jpack')]
67 ],
68 'img' => [
69 'Rapsys\PackBundle\Twig\Filter\IPackFilter' => []
70 ],
71 ]
72 ];
73
74 //Here we define the parameters that are allowed to configure the bundle.
75 $treeBuilder
76 //Parameters
77 ->root('parameters')
78 ->isRequired()
79 ->addDefaultsIfNotSet()
80 ->children()
81 ->arrayNode('rapsys_pack')
82 ->isRequired()
83 ->addDefaultsIfNotSet()
84 ->children()
85 ->arrayNode('config')
86 ->isRequired()
87 ->addDefaultsIfNotSet()
88 ->children()
89 ->scalarNode('prefix')->isRequired()->defaultValue($defaults['config']['prefix'])->end()
90 ->scalarNode('scheme')->isRequired()->defaultValue($defaults['config']['scheme'])->end()
91 ->integerNode('timeout')->isRequired()->min(0)->defaultValue($defaults['config']['timeout'])->end()
92 ->scalarNode('agent')->isRequired()->defaultValue($defaults['config']['agent'])->end()
93 ->integerNode('redirect')->isRequired()->min(1)->defaultValue($defaults['config']['redirect'])->end()
94 ->end()
95 ->end()
96 ->arrayNode('output')
97 ->isRequired()
98 ->addDefaultsIfNotSet()
99 ->children()
100 ->scalarNode('css')->isRequired()->defaultValue($defaults['output']['css'])->end()
101 ->scalarNode('js')->isRequired()->defaultValue($defaults['output']['js'])->end()
102 ->scalarNode('img')->isRequired()->defaultValue($defaults['output']['img'])->end()
103 ->end()
104 ->end()
105 ->arrayNode('filter')
106 ->isRequired()
107 ->addDefaultsIfNotSet()
108 ->children()
109 ->arrayNode('css')
110 ->isRequired()
111 ->treatNullLike(array())
112 ->scalarPrototype()->end()
113 ->defaultValue($defaults['filter']['css'])
114 ->end()
115 ->arrayNode('js')
116 ->isRequired()
117 ->treatNullLike(array())
118 ->scalarPrototype()->end()
119 ->defaultValue($defaults['filter']['js'])
120 ->end()
121 ->arrayNode('img')
122 ->isRequired()
123 ->treatNullLike(array())
124 ->scalarPrototype()->end()
125 ->defaultValue($defaults['filter']['img'])
126 ->end()
127 ->end()
128 ->end()
129 ->end()
130 ->end()
131 ->end()
132 ->end();
133
134 return $treeBuilder;
135 }
136 }