]> Raphaƫl G. Git Repositories - packbundle/blob - DependencyInjection/Configuration.php
a9bff3bfa141e77516d996f1e0d734c67d62821d
[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 /**
16 * {@inheritdoc}
17 */
18 public function getConfigTreeBuilder() {
19 //Get TreeBuilder object
20 $treeBuilder = new TreeBuilder('rapsys_pack');
21
22 //Get ExecutableFinder object
23 $finder = new ExecutableFinder();
24
25 /**
26 * XXX: Note about the output schemes
27 *
28 * The output files are written based on the output.<ext> scheme with the * replaced by the hashed path of packed files
29 *
30 * The following service configuration make twig render the output file path with the right '/' basePath prefix:
31 * services:
32 * assets.pack_package:
33 * class: Rapsys\PackBundle\Asset\PathPackage
34 * arguments: [ '/', '@assets.empty_version_strategy', '@assets.context' ]
35 * rapsys_pack.twig.pack_extension:
36 * class: Rapsys\PackBundle\Twig\PackExtension
37 * arguments: [ '@file_locator', '@service_container', '@assets.pack_package' ]
38 * tags: [ twig.extension ]
39 */
40
41 //The bundle default values
42 $defaults = [
43 'config' => [
44 'name' => 'asset_url',
45 'scheme' => 'https://',
46 'timeout' => (int)ini_get('default_socket_timeout'),
47 'agent' => (string)ini_get('user_agent')?:'rapsys_pack/0.1.3',
48 'redirect' => 5
49 ],
50 'output' => [
51 'css' => 'css/*.pack.css',
52 'js' => 'js/*.pack.js',
53 'img' => 'img/*.pack.jpg'
54 ],
55 'filters' => [
56 'css' => [
57 'class' => 'Rapsys\PackBundle\Twig\Filter\CPackFilter',
58 'args' => [
59 $finder->find('cpack', '/usr/local/bin/cpack'),
60 'minify'
61 ]
62 ],
63 'js' => [
64 'class' => 'Rapsys\PackBundle\Twig\Filter\JPackFilter',
65 'args' => [
66 $finder->find('jpack', '/usr/local/bin/jpack'),
67 'best'
68 ]
69 ],
70 'img' => [
71 'class' => 'Rapsys\PackBundle\Twig\Filter\IPackFilter',
72 'args' => []
73 ],
74 ]
75 ];
76
77 //Here we define the parameters that are allowed to configure the bundle.
78 //XXX: see https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php for default value and description
79 //XXX: see http://symfony.com/doc/current/components/config/definition.html
80 //XXX: see https://github.com/symfony/assetic-bundle/blob/master/DependencyInjection/Configuration.php#L63
81 //XXX: see php bin/console config:dump-reference rapsys_pack to dump default config
82 //XXX: see php bin/console debug:config rapsys_pack to dump config
83 $treeBuilder
84 //Parameters
85 ->getRootNode()
86 ->addDefaultsIfNotSet()
87 ->children()
88 ->arrayNode('config')
89 ->addDefaultsIfNotSet()
90 ->children()
91 ->scalarNode('name')->cannotBeEmpty()->defaultValue($defaults['config']['name'])->end()
92 ->scalarNode('scheme')->cannotBeEmpty()->defaultValue($defaults['config']['scheme'])->end()
93 ->integerNode('timeout')->min(0)->max(300)->defaultValue($defaults['config']['timeout'])->end()
94 ->scalarNode('agent')->cannotBeEmpty()->defaultValue($defaults['config']['agent'])->end()
95 ->integerNode('redirect')->min(1)->max(30)->defaultValue($defaults['config']['redirect'])->end()
96 ->end()
97 ->end()
98 ->arrayNode('output')
99 ->addDefaultsIfNotSet()
100 ->children()
101 ->scalarNode('css')->cannotBeEmpty()->defaultValue($defaults['output']['css'])->end()
102 ->scalarNode('js')->cannotBeEmpty()->defaultValue($defaults['output']['js'])->end()
103 ->scalarNode('img')->cannotBeEmpty()->defaultValue($defaults['output']['img'])->end()
104 ->end()
105 ->end()
106 ->arrayNode('filters')
107 ->addDefaultsIfNotSet()
108 ->children()
109 ->arrayNode('css')
110 #XXX: undocumented, see Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +513
111 ->addDefaultChildrenIfNoneSet()
112 ->arrayPrototype()
113 ->children()
114 ->scalarNode('class')
115 ->isRequired()
116 ->cannotBeEmpty()
117 ->defaultValue($defaults['filters']['css']['class'])
118 ->end()
119 ->arrayNode('args')
120 /*->isRequired()*/
121 ->treatNullLike(array())
122 ->defaultValue($defaults['filters']['css']['args'])
123 ->scalarPrototype()->end()
124 ->end()
125 ->end()
126 ->end()
127 ->end()
128 ->arrayNode('js')
129 #XXX: undocumented, see Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +513
130 ->addDefaultChildrenIfNoneSet()
131 ->arrayPrototype()
132 ->children()
133 ->scalarNode('class')
134 ->isRequired()
135 ->cannotBeEmpty()
136 ->defaultValue($defaults['filters']['js']['class'])
137 ->end()
138 ->arrayNode('args')
139 ->treatNullLike(array())
140 ->defaultValue($defaults['filters']['js']['args'])
141 ->scalarPrototype()->end()
142 ->end()
143 ->end()
144 ->end()
145 ->end()
146 ->arrayNode('img')
147 #XXX: undocumented, see Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +513
148 ->addDefaultChildrenIfNoneSet()
149 ->arrayPrototype()
150 ->children()
151 ->scalarNode('class')
152 ->isRequired()
153 ->cannotBeEmpty()
154 ->defaultValue($defaults['filters']['img']['class'])
155 ->end()
156 ->arrayNode('args')
157 ->treatNullLike(array())
158 ->defaultValue($defaults['filters']['img']['args'])
159 ->scalarPrototype()->end()
160 ->end()
161 ->end()
162 ->end()
163 ->end()
164 ->end()
165 ->end()
166 ->end()
167 ->end();
168
169 return $treeBuilder;
170 }
171 }