]> Raphaƫl G. Git Repositories - packbundle/blob - DependencyInjection/Configuration.php
dde458d5c3b1fb84eca837ec22cd7defab2c1869
[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 //The project dir
16 private $projectDir;
17
18 //Constructor required to derivate prefix from kernel.project_dir
19 public function __construct($projectDir) {
20 $this->projectDir = $projectDir;
21 }
22
23 /**
24 * {@inheritdoc}
25 */
26 public function getConfigTreeBuilder() {
27 //Get TreeBuilder object
28 $treeBuilder = new TreeBuilder('rapsys_pack');
29
30 //Get ExecutableFinder object
31 $finder = new ExecutableFinder();
32
33 #TODO: see how we deal with asset url generation: see Rapsys/PackBundle/Twig/PackTokenParser.php +243
34 #framework:
35 # assets:
36 # packages:
37 # rapsys_pack:
38 # base_path: '/'
39 # version: ~
40 #
41 ## Force cache disable to regenerate resources each time
42 ##twig:
43 ## cache: ~
44
45 //TODO: see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php for default value and description
46 //TODO: see http://symfony.com/doc/current/components/config/definition.html
47 //TODO: see https://github.com/symfony/assetic-bundle/blob/master/DependencyInjection/Configuration.php#L63
48 //TODO: use bin/console config:dump-reference to dump class infos
49
50 //The bundle default values
51 $defaults = [
52 'config' => [
53 'prefix' => $this->projectDir,
54 'name' => 'asset_url',
55 'scheme' => 'https://',
56 'timeout' => (int)ini_get('default_socket_timeout'),
57 'agent' => (string)ini_get('user_agent')?:'rapsys_pack/0.0.2',
58 'redirect' => 5
59 ],
60 'output' => [
61 'css' => 'css/*.pack.css',
62 'js' => 'js/*.pack.js',
63 'img' => 'img/*.pack.jpg'
64 ],
65 'filters' => [
66 'css' => [
67 'class' => 'Rapsys\PackBundle\Twig\Filter\CPackFilter',
68 'args' => [
69 $finder->find('cpack', '/usr/local/bin/cpack'),
70 'minify'
71 ]
72 ],
73 'js' => [
74 'class' => 'Rapsys\PackBundle\Twig\Filter\JPackFilter',
75 'args' => [
76 $finder->find('jpack', '/usr/local/bin/jpack'),
77 'best'
78 ]
79 ],
80 'img' => [
81 'class' => 'Rapsys\PackBundle\Twig\Filter\IPackFilter',
82 'args' => []
83 ],
84 ]
85 ];
86
87 //Here we define the parameters that are allowed to configure the bundle.
88 $treeBuilder
89 //Parameters
90 ->getRootNode()
91 #->cannotBeEmpty()
92 ->addDefaultsIfNotSet()
93 ->children()
94 ->arrayNode('config')
95 ->addDefaultsIfNotSet()
96 ->children()
97 ->scalarNode('prefix')->cannotBeEmpty()->defaultValue($defaults['config']['prefix'])->end()
98 ->scalarNode('name')->cannotBeEmpty()->defaultValue($defaults['config']['name'])->end()
99 ->scalarNode('scheme')->cannotBeEmpty()->defaultValue($defaults['config']['scheme'])->end()
100 ->integerNode('timeout')->min(0)->max(300)->defaultValue($defaults['config']['timeout'])->end()
101 ->scalarNode('agent')->cannotBeEmpty()->defaultValue($defaults['config']['agent'])->end()
102 ->integerNode('redirect')->min(1)->max(30)->defaultValue($defaults['config']['redirect'])->end()
103 ->end()
104 ->end()
105 ->arrayNode('output')
106 ->addDefaultsIfNotSet()
107 ->children()
108 ->scalarNode('css')->cannotBeEmpty()->defaultValue($defaults['output']['css'])->end()
109 ->scalarNode('js')->cannotBeEmpty()->defaultValue($defaults['output']['js'])->end()
110 ->scalarNode('img')->cannotBeEmpty()->defaultValue($defaults['output']['img'])->end()
111 ->end()
112 ->end()
113 ->arrayNode('filters')
114 ->addDefaultsIfNotSet()
115 ->children()
116 ->arrayNode('css')
117 #XXX: undocumented, see Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +513
118 ->addDefaultChildrenIfNoneSet()
119 ->arrayPrototype()
120 ->children()
121 ->scalarNode('class')
122 ->isRequired()
123 ->cannotBeEmpty()
124 ->defaultValue($defaults['filters']['css']['class'])
125 ->end()
126 ->arrayNode('args')
127 /*->isRequired()*/
128 ->treatNullLike(array())
129 ->defaultValue($defaults['filters']['css']['args'])
130 ->scalarPrototype()->end()
131 ->end()
132 ->end()
133 ->end()
134 ->end()
135 ->arrayNode('js')
136 #XXX: undocumented, see Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +513
137 ->addDefaultChildrenIfNoneSet()
138 ->arrayPrototype()
139 ->children()
140 ->scalarNode('class')
141 ->isRequired()
142 ->cannotBeEmpty()
143 ->defaultValue($defaults['filters']['js']['class'])
144 ->end()
145 ->arrayNode('args')
146 ->treatNullLike(array())
147 ->defaultValue($defaults['filters']['js']['args'])
148 ->scalarPrototype()->end()
149 ->end()
150 ->end()
151 ->end()
152 ->end()
153 ->arrayNode('img')
154 #XXX: undocumented, see Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +513
155 ->addDefaultChildrenIfNoneSet()
156 ->arrayPrototype()
157 ->children()
158 ->scalarNode('class')
159 ->isRequired()
160 ->cannotBeEmpty()
161 ->defaultValue($defaults['filters']['img']['class'])
162 ->end()
163 ->arrayNode('args')
164 ->treatNullLike(array())
165 ->defaultValue($defaults['filters']['img']['args'])
166 ->scalarPrototype()->end()
167 ->end()
168 ->end()
169 ->end()
170 ->end()
171 ->end()
172 ->end()
173 ->end()
174 ->end();
175
176 return $treeBuilder;
177 }
178 }