|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Application\Configuration; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Configuration\Configuration; |
|
6
|
|
|
use LAG\AdminBundle\Configuration\ConfigurationInterface; |
|
7
|
|
|
use LAG\AdminBundle\Field\Field; |
|
8
|
|
|
use LAG\AdminBundle\Field\Field\StringField; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
10
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Application configuration class. Allow easy configuration manipulation within an Admin. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ApplicationConfiguration extends Configuration implements ConfigurationInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Configure configuration allowed parameters. |
|
20
|
|
|
* |
|
21
|
|
|
* @param OptionsResolver $resolver |
|
22
|
|
|
*/ |
|
23
|
23 |
|
public function configureOptions(OptionsResolver $resolver) |
|
24
|
|
|
{ |
|
25
|
|
|
// enable or disable extra configuration listener |
|
26
|
23 |
|
$resolver->setDefault('enable_extra_configuration', true); |
|
27
|
23 |
|
$resolver->setAllowedTypes('enable_extra_configuration', 'boolean'); |
|
28
|
|
|
|
|
29
|
|
|
// application title |
|
30
|
23 |
|
$resolver->setDefault('title', 'AdminBundle application'); |
|
31
|
23 |
|
$resolver->setAllowedTypes('title', 'string'); |
|
32
|
|
|
|
|
33
|
|
|
// application description |
|
34
|
23 |
|
$resolver->setDefault('description', ''); |
|
35
|
23 |
|
$resolver->setAllowedTypes('description', 'string'); |
|
36
|
|
|
|
|
37
|
|
|
// application locale (en by default) |
|
38
|
23 |
|
$resolver->setDefault('locale', 'en'); |
|
39
|
23 |
|
$resolver->setAllowedTypes('locale', 'string'); |
|
40
|
|
|
|
|
41
|
|
|
// main base template |
|
42
|
|
|
// as bundles are not loaded when reading the configuration, the kernel locateResources will always failed. |
|
43
|
|
|
// So we must not check resource existance here. |
|
44
|
23 |
|
$resolver->setDefault('base_template', 'LAGAdminBundle::admin.layout.html.twig'); |
|
45
|
23 |
|
$resolver->setAllowedTypes('base_template', 'string'); |
|
46
|
|
|
|
|
47
|
|
|
// form block template |
|
48
|
23 |
|
$resolver->setDefault('block_template', 'LAGAdminBundle:Form:fields.html.twig'); |
|
49
|
23 |
|
$resolver->setAllowedTypes('block_template', 'string'); |
|
50
|
|
|
|
|
51
|
|
|
// use bootstrap or not |
|
52
|
23 |
|
$resolver->setDefault('bootstrap', true); |
|
53
|
23 |
|
$resolver->setAllowedTypes('bootstrap', 'boolean'); |
|
54
|
|
|
|
|
55
|
|
|
// general date format |
|
56
|
23 |
|
$resolver->setDefault('date_format', 'Y/m/d'); |
|
57
|
23 |
|
$resolver->setAllowedTypes('date_format', 'string'); |
|
58
|
|
|
|
|
59
|
|
|
// string length before truncation (0 means no truncation) |
|
60
|
23 |
|
$resolver->setDefault('string_length', 0); |
|
61
|
23 |
|
$resolver->setAllowedTypes('string_length', 'integer'); |
|
62
|
|
|
|
|
63
|
23 |
|
$resolver->setDefault('string_length_truncate', '...'); |
|
64
|
23 |
|
$resolver->setAllowedTypes('string_length_truncate', 'string'); |
|
65
|
|
|
|
|
66
|
|
|
// routing configuration (route name pattern and url name pattern) |
|
67
|
23 |
|
$resolver->setDefault('routing', [ |
|
68
|
23 |
|
'url_pattern' => '/{admin}/{action}', |
|
69
|
23 |
|
'name_pattern' => 'lag.admin.{admin}', |
|
70
|
23 |
|
]); |
|
71
|
23 |
|
$resolver->setAllowedTypes('routing', 'array'); |
|
72
|
|
|
$resolver->setNormalizer('routing', function (Options $options, $value) { |
|
73
|
|
|
|
|
74
|
|
|
// url pattern should contain {admin} and {action} token |
|
75
|
23 |
|
$urlPattern = $value['url_pattern']; |
|
76
|
|
|
|
|
77
|
23 |
|
if (strstr($urlPattern, '{admin}') === false) { |
|
78
|
|
|
throw new InvalidOptionsException('Admin routing configuration url pattern should contains {admin} placeholder'); |
|
79
|
|
|
} |
|
80
|
23 |
|
if (strstr($urlPattern, '{action}') === false) { |
|
81
|
|
|
throw new InvalidOptionsException('Admin routing configuration url pattern should contains the {action} placeholder'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// name pattern should contain {admin} token |
|
85
|
23 |
|
$namePattern = $value['name_pattern']; |
|
86
|
|
|
|
|
87
|
23 |
|
if (strstr($namePattern, '{admin}') === false) { |
|
88
|
|
|
throw new InvalidOptionsException('Admin routing configuration pattern name should contains the {admin} placeholder'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
23 |
|
return $value; |
|
92
|
23 |
|
}); |
|
93
|
|
|
|
|
94
|
|
|
// translation configuration |
|
95
|
23 |
|
$resolver->setDefault('translation', [ |
|
96
|
23 |
|
'enabled' => true, |
|
97
|
|
|
'pattern' => 'lag.admin.{key}' |
|
98
|
23 |
|
]); |
|
99
|
23 |
|
$resolver->setAllowedTypes('translation', 'array'); |
|
100
|
|
|
$resolver->setNormalizer('translation', function (Options $options, $value) { |
|
101
|
|
|
|
|
102
|
23 |
|
if (!is_bool($value['enabled'])) { |
|
103
|
|
|
throw new InvalidOptionsException('Admin translation enabled parameter should be a boolean'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
23 |
|
if (!array_key_exists('pattern', $value)) { |
|
107
|
|
|
$value['pattern'] = '{key}'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
23 |
|
if ($value['enabled'] && strstr($value['pattern'], '{key}') === false) { |
|
111
|
|
|
throw new InvalidOptionsException('Admin translation pattern should contains the {key} placeholder'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
23 |
|
return $value; |
|
115
|
23 |
|
}); |
|
116
|
|
|
|
|
117
|
|
|
// maximum number of elements displayed |
|
118
|
23 |
|
$resolver->setDefault('max_per_page', 25); |
|
119
|
23 |
|
$resolver->setAllowedTypes('max_per_page', 'integer'); |
|
120
|
|
|
|
|
121
|
|
|
// admin field type mapping |
|
122
|
|
|
$defaultMapping = [ |
|
123
|
23 |
|
Field::TYPE_STRING => StringField::class, |
|
124
|
23 |
|
Field::TYPE_ARRAY => Field\ArrayField::class, |
|
125
|
23 |
|
Field::TYPE_LINK => Field\Link::class, |
|
126
|
23 |
|
Field::TYPE_DATE => Field\Date::class, |
|
127
|
23 |
|
Field::TYPE_COUNT => Field\Count::class, |
|
128
|
23 |
|
Field::TYPE_ACTION => Field\Action::class, |
|
129
|
23 |
|
Field::TYPE_COLLECTION => Field\Collection::class, |
|
130
|
23 |
|
Field::TYPE_BOOLEAN => Field\Boolean::class, |
|
131
|
23 |
|
]; |
|
132
|
|
|
|
|
133
|
23 |
|
$resolver->setDefault('fields_mapping', $defaultMapping); |
|
134
|
23 |
|
$resolver->setAllowedTypes('fields_mapping', 'array'); |
|
135
|
|
|
$resolver->setNormalizer('fields_mapping', function (Options $options, $value) use ($defaultMapping) { |
|
136
|
|
|
// merge with default mapping to allow override |
|
137
|
23 |
|
$value = array_merge($defaultMapping, $value); |
|
138
|
|
|
|
|
139
|
23 |
|
return $value; |
|
140
|
23 |
|
}); |
|
141
|
|
|
|
|
142
|
|
|
// fields templates mapping |
|
143
|
|
|
$defaultMapping = [ |
|
144
|
23 |
|
Field::TYPE_LINK => 'LAGAdminBundle:Render:link.html.twig', |
|
145
|
23 |
|
]; |
|
146
|
|
|
|
|
147
|
23 |
|
$resolver->setDefault('fields_template_mapping', $defaultMapping); |
|
148
|
23 |
|
$resolver->setAllowedTypes('fields_template_mapping', 'array'); |
|
149
|
23 |
|
$resolver->setNormalizer('fields_template_mapping', function (Options $options, $value) use ($defaultMapping) { |
|
150
|
|
|
// merge with default mapping to allow override |
|
151
|
23 |
|
$value = array_merge($defaultMapping, $value); |
|
152
|
|
|
|
|
153
|
23 |
|
return $value; |
|
154
|
23 |
|
}); |
|
155
|
23 |
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|