1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\GridBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\GridBundle\Doctrine\ORM\Driver as DoctrineORMDriver; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
16
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
17
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Configuration implements ConfigurationInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function getConfigTreeBuilder() |
28
|
|
|
{ |
29
|
|
|
$treeBuilder = new TreeBuilder(); |
30
|
|
|
$rootNode = $treeBuilder->root('sylius_grid'); |
31
|
|
|
|
32
|
|
|
$this->addDriversSection($rootNode); |
33
|
|
|
$this->addTemplatesSection($rootNode); |
34
|
|
|
$this->addGridsSection($rootNode); |
35
|
|
|
|
36
|
|
|
return $treeBuilder; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function addDriversSection(ArrayNodeDefinition $node) |
40
|
|
|
{ |
41
|
|
|
// determine which drivers are distributed with this bundle |
42
|
|
|
$driverDir = __DIR__ . '/../Resources/config/driver'; |
43
|
|
|
$iterator = new \RecursiveDirectoryIterator($driverDir); |
44
|
|
|
foreach (new \RecursiveIteratorIterator($iterator) as $file) { |
45
|
|
|
if ($file->getExtension() !== 'xml') { |
46
|
|
|
continue; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// we use the parent directory name in addition to the filename to |
50
|
|
|
// determine the name of the driver (e.g. doctrine/orm) |
51
|
|
|
$validDrivers[] = substr($file->getPathname(), 1 + strlen($driverDir), -4); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$node |
55
|
|
|
->children() |
56
|
|
|
->arrayNode('drivers') |
57
|
|
|
->info('Enable drivers which are distributed with this bundle') |
58
|
|
|
->validate() |
59
|
|
|
->ifTrue(function ($value) use ($validDrivers) { |
|
|
|
|
60
|
|
|
return 0 !== count(array_diff($value, $validDrivers)); |
61
|
|
|
}) |
62
|
|
|
->thenInvalid(sprintf('Invalid driver specified in %%s, valid drivers: ["%s"]', implode('", "', $validDrivers))) |
63
|
|
|
->end() |
64
|
|
|
->defaultValue(['doctrine/orm']) |
65
|
|
|
->prototype('scalar')->end() |
66
|
|
|
->end() |
67
|
|
|
->end(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ArrayNodeDefinition $node |
72
|
|
|
*/ |
73
|
|
|
private function addTemplatesSection(ArrayNodeDefinition $node) |
74
|
|
|
{ |
75
|
|
|
$node |
76
|
|
|
->children() |
77
|
|
|
->arrayNode('templates') |
78
|
|
|
->children() |
79
|
|
|
->arrayNode('filter') |
80
|
|
|
->useAttributeAsKey('name') |
81
|
|
|
->prototype('scalar')->end() |
82
|
|
|
->end() |
83
|
|
|
->arrayNode('action') |
84
|
|
|
->useAttributeAsKey('name') |
85
|
|
|
->prototype('scalar')->end() |
86
|
|
|
->end() |
87
|
|
|
->end() |
88
|
|
|
->end() |
89
|
|
|
->end() |
90
|
|
|
; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param ArrayNodeDefinition $node |
95
|
|
|
*/ |
96
|
|
|
private function addGridsSection(ArrayNodeDefinition $node) |
97
|
|
|
{ |
98
|
|
|
$node |
99
|
|
|
->children() |
100
|
|
|
->arrayNode('grids') |
101
|
|
|
->useAttributeAsKey('code') |
102
|
|
|
->prototype('array') |
103
|
|
|
->children() |
104
|
|
|
->arrayNode('driver') |
105
|
|
|
->addDefaultsIfNotSet() |
106
|
|
|
->children() |
107
|
|
|
->scalarNode('name')->defaultValue(DoctrineORMDriver::NAME)->end() |
108
|
|
|
->arrayNode('options') |
109
|
|
|
->prototype('variable')->end() |
110
|
|
|
->defaultValue([]) |
111
|
|
|
->end() |
112
|
|
|
->end() |
113
|
|
|
->end() |
114
|
|
|
->arrayNode('sorting') |
115
|
|
|
->prototype('scalar')->end() |
116
|
|
|
->end() |
117
|
|
|
->arrayNode('fields') |
118
|
|
|
->useAttributeAsKey('name') |
119
|
|
|
->prototype('array') |
120
|
|
|
->children() |
121
|
|
|
->scalarNode('type')->isRequired()->cannotBeEmpty()->end() |
122
|
|
|
->scalarNode('label')->cannotBeEmpty()->end() |
123
|
|
|
->scalarNode('path')->cannotBeEmpty()->end() |
124
|
|
|
->arrayNode('options') |
125
|
|
|
->prototype('variable')->end() |
126
|
|
|
->end() |
127
|
|
|
->end() |
128
|
|
|
->end() |
129
|
|
|
->end() |
130
|
|
|
->arrayNode('filters') |
131
|
|
|
->useAttributeAsKey('name') |
132
|
|
|
->prototype('array') |
133
|
|
|
->children() |
134
|
|
|
->scalarNode('type')->isRequired()->cannotBeEmpty()->end() |
135
|
|
|
->scalarNode('label')->cannotBeEmpty()->end() |
136
|
|
|
->arrayNode('options') |
137
|
|
|
->prototype('variable')->end() |
138
|
|
|
->end() |
139
|
|
|
->end() |
140
|
|
|
->end() |
141
|
|
|
->end() |
142
|
|
|
->arrayNode('actions') |
143
|
|
|
->useAttributeAsKey('name') |
144
|
|
|
->prototype('array') |
145
|
|
|
->useAttributeAsKey('name') |
146
|
|
|
->prototype('array') |
147
|
|
|
->children() |
148
|
|
|
->scalarNode('type')->isRequired()->end() |
149
|
|
|
->scalarNode('label')->end() |
150
|
|
|
->arrayNode('options') |
151
|
|
|
->prototype('variable')->end() |
152
|
|
|
->end() |
153
|
|
|
->end() |
154
|
|
|
->end() |
155
|
|
|
->end() |
156
|
|
|
->end() |
157
|
|
|
->end() |
158
|
|
|
->end() |
159
|
|
|
->end() |
160
|
|
|
->end() |
161
|
|
|
; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.