Skip to content

Commit 63e9b66

Browse files
committed
Merge pull request #222 from symfony-cmf/cleanup-admin
adjust sonata admin
2 parents 9075b78 + 2cf3fef commit 63e9b66

File tree

8 files changed

+109
-23
lines changed

8 files changed

+109
-23
lines changed

Admin/RouteAdmin.php

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,40 @@ protected function configureListFields(ListMapper $listMapper)
5959
protected function configureFormFields(FormMapper $formMapper)
6060
{
6161
$formMapper
62-
->with('form.group_general')
63-
->add(
64-
'parent',
65-
'doctrine_phpcr_odm_tree',
66-
array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot)
67-
)
68-
->add('name', 'text')
62+
->with('form.group_general', array(
63+
'translation_domain' => 'CmfRoutingBundle',
64+
))
65+
->add(
66+
'parent',
67+
'doctrine_phpcr_odm_tree',
68+
array('choice_list' => array(), 'select_root_node' => true, 'root_node' => $this->routeRoot)
69+
)
70+
->add('name', 'text')
6971
->end();
7072

7173
if (null === $this->getParentFieldDescription()) {
7274
$formMapper
73-
->with('form.group_general')
75+
->with('form.group_general', array(
76+
'translation_domain' => 'CmfRoutingBundle',
77+
))
7478
->add('content', 'doctrine_phpcr_odm_tree', array('choice_list' => array(), 'required' => false, 'root_node' => $this->contentRoot))
7579
->end()
76-
->with('form.group_advanced')
80+
->with('form.group_advanced', array(
81+
'translation_domain' => 'CmfRoutingBundle',
82+
))
7783
->add('variablePattern', 'text', array('required' => false), array('help' => 'form.help_variable_pattern'))
78-
->add('defaults', 'sonata_type_immutable_array', array('keys' => $this->configureFieldsForDefaults()))
79-
->add('options', 'sonata_type_immutable_array', array('keys' => $this->configureFieldsForOptions()), array('help' => 'form.help_options'))
84+
->add(
85+
'defaults',
86+
'sonata_type_immutable_array',
87+
array('keys' => $this->configureFieldsForDefaults($this->getSubject()->getDefaults()))
88+
)
89+
->add(
90+
'options',
91+
'sonata_type_immutable_array',
92+
array(
93+
'keys' => $this->configureFieldsForOptions($this->getSubject()->getOptions())),
94+
array('help' => 'form.help_options')
95+
)
8096
->end()
8197
->end();
8298
}
@@ -110,7 +126,14 @@ public function getExportFormats()
110126
return array();
111127
}
112128

113-
protected function configureFieldsForDefaults()
129+
/**
130+
* Provide default route defaults and extract defaults from $dynamicDefaults.
131+
*
132+
* @param array $dynamicDefaults
133+
*
134+
* @return array Value for sonata_type_immutable_array
135+
*/
136+
protected function configureFieldsForDefaults($dynamicDefaults)
114137
{
115138
$defaults = array(
116139
'_controller' => array('_controller', 'text', array('required' => false)),
@@ -121,14 +144,13 @@ protected function configureFieldsForDefaults()
121144
)),
122145
);
123146

124-
$dynamicDefaults = $this->getSubject()->getDefaults();
125147
foreach ($dynamicDefaults as $name => $value) {
126148
if (!isset($defaults[$name])) {
127149
$defaults[$name] = array($name, 'text', array('required' => false));
128150
}
129151
}
130152

131-
//parse variable pattern and add defaults for it - taken from routecompiler
153+
//parse variable pattern and add defaults for tokens - taken from routecompiler
132154
/** @var $route Route */
133155
$route = $this->subject;
134156
if ($route && $route->getVariablePattern()) {
@@ -141,18 +163,31 @@ protected function configureFieldsForDefaults()
141163
}
142164
}
143165

166+
if ($route && $route->getOption('add_format_pattern')) {
167+
$defaults['_format'] = array('_format', 'text', array('required' => true));
168+
}
169+
if ($route && $route->getOption('add_locale_pattern')) {
170+
$defaults['_locale'] = array('_format', 'text', array('required' => false));
171+
}
172+
144173
return $defaults;
145174
}
146175

147-
protected function configureFieldsForOptions()
176+
/**
177+
* Provide default options and extract options from $dynamicOptions.
178+
*
179+
* @param array $dynamicOptions
180+
*
181+
* @return array Value for sonata_type_immutable_array
182+
*/
183+
protected function configureFieldsForOptions(array $dynamicOptions)
148184
{
149185
$options = array(
150-
array('add_locale_pattern', 'checkbox', array('required' => false, 'label' => 'form.label_add_locale_pattern', 'translation_domain' => $this->translationDomain)),
151-
array('add_format_pattern', 'checkbox', array('required' => false, 'label' => 'form.label_add_format_pattern', 'translation_domain' => $this->translationDomain)),
152-
array('add_trailing_slash', 'checkbox', array('required' => false, 'label' => 'form.label_add_trailing_slash', 'translation_domain' => $this->translationDomain)),
186+
'add_locale_pattern' => array('add_locale_pattern', 'checkbox', array('required' => false, 'label' => 'form.label_add_locale_pattern', 'translation_domain' => 'CmfRoutingBundle')),
187+
'add_format_pattern' => array('add_format_pattern', 'checkbox', array('required' => false, 'label' => 'form.label_add_format_pattern', 'translation_domain' => 'CmfRoutingBundle')),
188+
'add_trailing_slash' => array('add_trailing_slash', 'checkbox', array('required' => false, 'label' => 'form.label_add_trailing_slash', 'translation_domain' => 'CmfRoutingBundle')),
153189
);
154190

155-
$dynamicOptions = $this->getSubject()->getOptions();
156191
foreach ($dynamicOptions as $name => $value) {
157192
if (!isset($options[$name])) {
158193
$options[$name] = array($name, 'text', array('required' => false));

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function getConfigTreeBuilder()
131131
->arrayNode('locales')
132132
->prototype('scalar')->end()
133133
->end()
134-
->scalarNode('limit_candidates')->defaultValue(20)->end()
134+
->integerNode('limit_candidates')->defaultValue(20)->end()
135135
->booleanNode('match_implicit_locale')->defaultValue(true)->end()
136136
->booleanNode('auto_locale_pattern')->defaultValue(false)->end()
137137
->end()

Doctrine/Phpcr/PrefixCandidates.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr;
1313

14+
use PHPCR\Util\PathHelper;
1415
use Doctrine\Common\Persistence\ManagerRegistry;
1516
use Doctrine\ODM\PHPCR\DocumentManager;
1617
use Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder;
@@ -68,7 +69,9 @@ public function isCandidate($name)
6869
{
6970
foreach ($this->getPrefixes() as $prefix) {
7071
// $name is the route document path
71-
if ($name === $prefix || 0 === strpos($name, $prefix . '/')) {
72+
if (($name === $prefix || 0 === strpos($name, $prefix . '/'))
73+
&& PathHelper::assertValidAbsolutePath($name, false, false)
74+
) {
7275
return true;
7376
}
7477
}
@@ -113,6 +116,13 @@ public function getCandidates(Request $request)
113116
}
114117
}
115118

119+
// filter out things like double // or trailing / - this would trigger an exception on the document manager.
120+
foreach ($candidates as $key => $candidate) {
121+
if (! PathHelper::assertValidAbsolutePath($candidate, false, false)) {
122+
unset($candidates[$key]);
123+
}
124+
}
125+
116126
return $candidates;
117127
}
118128

Doctrine/Phpcr/RedirectRoute.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,12 @@ public function getRouteChildren()
260260

261261
return $children;
262262
}
263+
264+
/**
265+
* {@inheritDoc}
266+
*/
267+
protected function isBooleanOption($name)
268+
{
269+
return $name === 'add_trailing_slash' || parent::isBooleanOption($name);
270+
}
263271
}

Doctrine/Phpcr/Route.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,4 +310,12 @@ public function getChildren()
310310
{
311311
return $this->children;
312312
}
313+
314+
/**
315+
* {@inheritDoc}
316+
*/
317+
protected function isBooleanOption($name)
318+
{
319+
return $name === 'add_trailing_slash' || parent::isBooleanOption($name);
320+
}
313321
}

Model/Route.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ public function getOption($name)
180180
if (null === $option && 'compiler_class' === $name) {
181181
return 'Symfony\\Component\\Routing\\RouteCompiler';
182182
}
183+
if ($this->isBooleanOption($name)) {
184+
return (boolean) $option;
185+
}
183186

184187
return $option;
185188
}
@@ -196,10 +199,25 @@ public function getOptions()
196199
if (!array_key_exists('compiler_class', $options)) {
197200
$options['compiler_class'] = 'Symfony\\Component\\Routing\\RouteCompiler';
198201
}
202+
foreach ($options as $key => $value) {
203+
if ($this->isBooleanOption($key)) {
204+
$options[$key] = (boolean) $value;
205+
}
206+
}
199207

200208
return $options;
201209
}
202210

211+
/**
212+
* Helper method to check if an option is a boolean option to allow better forms.
213+
*
214+
* @param string $name
215+
*/
216+
protected function isBooleanOption($name)
217+
{
218+
return in_array($name, array('add_format_pattern', 'add_locale_pattern'));
219+
}
220+
203221
/**
204222
* We need to overwrite this to avoid issues with the legacy code in
205223
* SymfonyRoute.

Tests/Functional/Doctrine/Phpcr/RouteProviderTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,17 @@ public function testGetRouteCollectionForRequestFormat()
9999
$this->assertEquals(null, $root->getDefault('_format'));
100100
}
101101

102+
/**
103+
* The root route will always be found.
104+
*/
102105
public function testGetRouteCollectionForRequestNophpcrUrl()
103106
{
104107
$collection = $this->repository->getRouteCollectionForRequest(Request::create(':///'));
105108
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $collection);
106-
$this->assertCount(0, $collection);
109+
$this->assertCount(1, $collection);
110+
$routes = $collection->all();
111+
list ($key, $route) = each($routes);
112+
$this->assertEquals(self::ROUTE_ROOT, $key);
107113
}
108114

109115
public function testGetRoutesByNames()
@@ -113,6 +119,7 @@ public function testGetRoutesByNames()
113119
$routeNames = array(
114120
self::ROUTE_ROOT . '/testroute/noroute/child',
115121
self::ROUTE_ROOT . '/testroute/noroute',
122+
self::ROUTE_ROOT . '/testroute/', // trailing slash is invalid for phpcr
116123
self::ROUTE_ROOT . '/testroute'
117124
);
118125

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function testSupportsAllConfigFormats()
7171
'uri_filter_regexp' => '',
7272
'route_filters_by_id' => array(),
7373
'locales' => array('en', 'fr'),
74-
'limit_candidates' => true,
74+
'limit_candidates' => 20,
7575
'auto_locale_pattern' => true,
7676
'match_implicit_locale' => true,
7777
),

0 commit comments

Comments
 (0)