Skip to content

Commit 3eef9cd

Browse files
committed
convert settings to options
1 parent d81035a commit 3eef9cd

File tree

11 files changed

+70
-96
lines changed

11 files changed

+70
-96
lines changed

Doctrine/Phpcr/LocaleListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected function updateLocale(Route $doc, $id, $force = false)
162162
$doc->setRequirement('_locale', $locale);
163163
}
164164
} elseif ($this->addLocalePattern) {
165-
$doc->setAddLocalePattern(true);
165+
$doc->setOption('add_locale_pattern', true);
166166
}
167167
}
168168
}

Doctrine/Phpcr/RedirectRoute.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212

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

15-
use Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface;
15+
use Doctrine\Common\Collections\Collection;
1616
use Symfony\Cmf\Bundle\RoutingBundle\Model\RedirectRoute as RedirectRouteModel;
1717
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
18+
use Symfony\Cmf\Bundle\CoreBundle\Model\ChildInterface;
1819

1920
/**
2021
* {@inheritDoc}
@@ -45,13 +46,6 @@ class RedirectRoute extends RedirectRouteModel implements PrefixInterface, Child
4546
*/
4647
protected $children;
4748

48-
/**
49-
* if to add "/" to the pattern
50-
*
51-
* @var Boolean
52-
*/
53-
protected $addTrailingSlash;
54-
5549
/**
5650
* The part of the PHPCR path that does not belong to the url
5751
*
@@ -62,18 +56,17 @@ class RedirectRoute extends RedirectRouteModel implements PrefixInterface, Child
6256
protected $idPrefix;
6357

6458
/**
65-
* Overwrite to be able to create route without pattern
59+
* Overwrite to be able to create route without pattern.
60+
*
61+
* Additional options:
6662
*
67-
* @param bool $addFormatPattern if to add ".{_format}" to the route pattern
68-
* also implicitly sets a default/require on "_format" to "html"
69-
* @param bool $addTrailingSlash whether to add a trailing slash to the route, defaults to not add one
63+
* * add_trailing_slash: When set, a trailing slash is appended to the route
7064
*/
71-
public function __construct($addFormatPattern = false, $addTrailingSlash = false)
65+
public function __construct(array $options = array())
7266
{
73-
parent::__construct($addFormatPattern);
67+
parent::__construct($options);
7468

7569
$this->children = array();
76-
$this->addTrailingSlash = $addTrailingSlash;
7770
}
7871

7972
/**
@@ -225,7 +218,7 @@ public function generateStaticPrefix($id, $idPrefix)
225218
public function getPath()
226219
{
227220
$pattern = parent::getPath();
228-
if ($this->addTrailingSlash && '/' !== $pattern[strlen($pattern)-1]) {
221+
if ($this->getOption('add_trailing_slash') && '/' !== $pattern[strlen($pattern)-1]) {
229222
$pattern .= '/';
230223
};
231224

Doctrine/Phpcr/Route.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ class Route extends RouteModel implements PrefixInterface, ChildInterface
4949
*/
5050
protected $children;
5151

52-
/**
53-
* if to add "/" to the pattern
54-
*
55-
* @var Boolean
56-
*/
57-
protected $addTrailingSlash;
58-
5952
/**
6053
* The part of the PHPCR path that does not belong to the url
6154
*
@@ -66,28 +59,34 @@ class Route extends RouteModel implements PrefixInterface, ChildInterface
6659
protected $idPrefix;
6760

6861
/**
69-
* PHPCR id can not end on '/', so we need an additional option.
62+
* PHPCR id can not end on '/', so we need an additional option for a
63+
* trailing slash.
7064
*
71-
* Additional supported settings are:
65+
* Additional supported option is:
7266
*
73-
* * addTrailingSlash: When set, a trailing slash is appended to the route
67+
* * add_trailing_slash: When set, a trailing slash is appended to the route
7468
*/
75-
public function __construct(array $settings = array())
69+
public function __construct(array $options= array())
7670
{
77-
parent::__construct($settings);
71+
parent::__construct($options);
7872

7973
$this->children = new ArrayCollection();
80-
$this->addTrailingSlash = empty($settings['addTrailingSlash']);
8174
}
8275

76+
/**
77+
* @deprecated use getOption('add_trailing_slash') instead
78+
*/
8379
public function getAddTrailingSlash()
8480
{
85-
return $this->addTrailingSlash;
81+
return $this->getOption('add_trailing_slash');
8682
}
8783

84+
/**
85+
* @deprecated use setOption('add_trailing_slash', $add) instead
86+
*/
8887
public function setAddTrailingSlash($addTrailingSlash)
8988
{
90-
$this->addTrailingSlash = $addTrailingSlash;
89+
$this->setOption('add_trailing_slash', $addTrailingSlash);
9190
}
9291

9392
/**
@@ -221,10 +220,6 @@ public function getStaticPrefix()
221220
$path = $this->getId();
222221
$prefix = $this->getPrefix();
223222

224-
if ($this->addLocalePattern) {
225-
$path = $prefix . '/{_locale}' . substr($path, strlen($prefix));
226-
}
227-
228223
return $this->generateStaticPrefix($path, $prefix);
229224
}
230225

@@ -256,11 +251,13 @@ public function generateStaticPrefix($id, $idPrefix)
256251

257252
/**
258253
* {@inheritDoc}
254+
*
255+
* Handle the trailing slash option.
259256
*/
260257
public function getPath()
261258
{
262259
$pattern = parent::getPath();
263-
if ($this->addTrailingSlash && '/' !== $pattern[strlen($pattern)-1]) {
260+
if ($this->getOption('add_trailing_slash') && '/' !== $pattern[strlen($pattern)-1]) {
264261
$pattern .= '/';
265262
};
266263

Model/Route.php

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,6 @@ class Route extends SymfonyRoute implements RouteObjectInterface
5353
*/
5454
protected $variablePattern;
5555

56-
/**
57-
* Whether to append ".{_format}" to the pattern.
58-
*
59-
* @var Boolean
60-
*/
61-
protected $addFormatPattern;
62-
63-
/**
64-
* Whether to prepend "{_locale}" to the pattern.
65-
* @var boolean
66-
*/
67-
protected $addLocalePattern;
68-
6956
/**
7057
* Whether this route was changed since being last compiled.
7158
*
@@ -78,46 +65,40 @@ class Route extends SymfonyRoute implements RouteObjectInterface
7865
/**
7966
* Overwrite to be able to create route without pattern
8067
*
81-
* Supported settings are:
68+
* Additional supported options are:
8269
*
83-
* * addFormatPattern: When set, ".{_format}" is appended to the route pattern.
84-
* Also implicitly sets a default/require on "_format" to "html".
85-
* * addLocalePattern: When set, "/{_locale}" is prepended to the route pattern.
70+
* * add_format_pattern: When set, ".{_format}" is appended to the route pattern.
71+
* Also implicitly sets a default/require on "_format" to "html".
72+
* * add_locale_pattern: When set, "/{_locale}" is prepended to the route pattern.
8673
*
87-
* @param array $settings
74+
* @param array $options
8875
*/
89-
public function __construct(array $settings = array())
76+
public function __construct(array $options = array())
9077
{
9178
$this->setDefaults(array());
9279
$this->setRequirements(array());
93-
$this->setOptions(array());
80+
$this->setOptions($options);
9481

95-
$this->addFormatPattern = !empty($settings['addFormatPattern']);
96-
if ($this->addFormatPattern) {
82+
if ($this->getOption('add_format_pattern')) {
9783
$this->setDefault('_format', 'html');
9884
$this->setRequirement('_format', 'html');
9985
}
100-
$this->addLocalePattern = !empty($settings['addLocalePattern']);
10186
}
10287

88+
/**
89+
* @deprecated use getOption('add_format_pattern') instead
90+
*/
10391
public function getAddFormatPattern()
10492
{
105-
return $this->addFormatPattern;
93+
return $this->getOption('add_format_pattern');
10694
}
10795

96+
/**
97+
* @deprecated use setOption('add_format_pattern', $bool) instead
98+
*/
10899
public function setAddFormatPattern($addFormatPattern)
109100
{
110-
$this->addFormatPattern = $addFormatPattern;
111-
}
112-
113-
public function getAddLocalePattern()
114-
{
115-
return $this->addLocalePattern;
116-
}
117-
118-
public function setAddLocalePattern($addLocalePattern)
119-
{
120-
$this->addLocalePattern = $addLocalePattern;
101+
$this->setOption('add_format_pattern', $addFormatPattern);
121102
}
122103

123104
/**
@@ -235,8 +216,13 @@ public function getPattern()
235216
*/
236217
public function getPath()
237218
{
238-
$pattern = $this->getStaticPrefix() . $this->getVariablePattern();
239-
if ($this->addFormatPattern && !preg_match('/(.+)\.[a-z]+$/i', $pattern, $matches)) {
219+
$pattern = '';
220+
if ($this->getOption('add_locale_pattern')) {
221+
$pattern .= '/{_locale}';
222+
}
223+
$pattern .= $this->getStaticPrefix();
224+
$pattern .= $this->getVariablePattern();
225+
if ($this->getOption('add_format_pattern') && !preg_match('/(.+)\.[a-z]+$/i', $pattern, $matches)) {
240226
$pattern .= '.{_format}';
241227
};
242228

Resources/config/doctrine-model/Route.orm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
<mapped-superclass name="Symfony\Cmf\Bundle\RoutingBundle\Model\Route">
66
<field name="variablePattern" type="string" nullable="true"/>
7-
<field name="addFormatPattern" type="boolean"/>
87
<field name="staticPrefix" type="string" nullable="true"/>
98

109
<indexes>

Resources/config/doctrine-model/Route.phpcr.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<mapped-superclass name="Symfony\Cmf\Bundle\RoutingBundle\Model\Route" referenceable="true">
1010
<field name="variablePattern" type="string" nullable="true"/>
11-
<field name="addFormatPattern" type="boolean"/>
1211
<reference-one name="content" property="routeContent"/>
1312
</mapped-superclass>
1413

Resources/config/doctrine-phpcr/RedirectRoute.phpcr.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<id name="id">
1010
<generator strategy="PARENT"/>
1111
</id>
12-
<field name="addTrailingSlash" type="boolean"/>
1312
<nodename name="name"/>
1413
<parent-document name="parent"/>
1514
<children name="children"/>

Resources/config/doctrine-phpcr/Route.phpcr.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<id name="id">
1111
<generator strategy="PARENT"/>
1212
</id>
13-
<field name="addTrailingSlash" type="boolean"/>
1413
<nodename name="name"/>
1514
<parent-document name="parent"/>
1615
<children name="children"/>

Tests/Functional/Doctrine/Phpcr/RouteTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setUp()
2929

3030
public function testPersist()
3131
{
32-
$route = new Route;
32+
$route = new Route();
3333
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
3434

3535
$route->setContent($root); // this happens to be a referenceable node
@@ -68,7 +68,7 @@ public function testPersist()
6868

6969
public function testPersistEmptyOptions()
7070
{
71-
$route = new Route;
71+
$route = new Route();
7272
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
7373

7474
$route->setPosition($root, 'empty');
@@ -129,13 +129,13 @@ public function testInvalidIdPrefix()
129129
*/
130130
public function testPrefixNonpersisted()
131131
{
132-
$route = new Route;
132+
$route = new Route();
133133
$route->getPattern();
134134
}
135135

136136
public function testDefaultFormat()
137137
{
138-
$route = new Route(true);
138+
$route = new Route(array('add_format_pattern' => true));
139139

140140
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
141141

Tests/Functional/Routing/DynamicRouterTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,37 @@ public function setUp()
5151
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
5252

5353
// do not set a content here, or we need a valid request and so on...
54-
$route = new Route;
54+
$route = new Route();
5555
$route->setPosition($root, 'testroute');
5656
$route->setVariablePattern('/{slug}/{id}');
5757
$route->setDefault('id', '0');
5858
$route->setRequirement('id', '[0-9]+');
5959
$route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
60-
// TODO: what are the options used for? we should test them too if it makes sense
60+
61+
//TODO options
62+
6163
$this->getDm()->persist($route);
6264

63-
$childroute = new Route;
65+
$childroute = new Route();
6466
$childroute->setPosition($route, 'child');
6567
$childroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
6668
$this->getDm()->persist($childroute);
6769

68-
$formatroute = new Route(true);
70+
$formatroute = new Route(array('add_format_pattern' => true));
6971
$formatroute->setPosition($root, 'format');
7072
$formatroute->setVariablePattern('/{id}');
7173
$formatroute->setRequirement('_format', 'html|json');
7274
$formatroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
7375
$this->getDm()->persist($formatroute);
7476

75-
$format2jsonroute = new Route(true);
77+
$format2jsonroute = new Route(array('add_format_pattern' => true));
7678
$format2jsonroute->setPosition($root, 'format2.json');
7779
$format2jsonroute->setDefault('_format', 'json');
7880
$format2jsonroute->setRequirement('_format', 'json');
7981
$format2jsonroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testJsonController');
8082
$this->getDm()->persist($format2jsonroute);
8183

82-
$format2route = new Route(true);
84+
$format2route = new Route(array('add_format_pattern' => true));
8385
$format2route->setPosition($root, 'format2');
8486
$format2route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
8587
$this->getDm()->persist($format2route);
@@ -137,7 +139,7 @@ public function testNotAllowed()
137139
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
138140

139141
// do not set a content here, or we need a valid request and so on...
140-
$route = new Route;
142+
$route = new Route();
141143
$route->setPosition($root, 'notallowed');
142144
$route->setRequirement('_method', 'GET');
143145
$route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
@@ -213,15 +215,15 @@ public function testNoMatchingFormat()
213215

214216
public function testMatchLocale()
215217
{
216-
$route = new Route;
218+
$route = new Route();
217219
$route->setPosition($this->getDm()->find(null, self::ROUTE_ROOT), 'de');
218220
$route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
219221
$this->getDm()->persist($route);
220-
$childroute = new Route;
222+
$childroute = new Route();
221223
$childroute->setPosition($route, 'testroute');
222224
$childroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
223225
$this->getDm()->persist($childroute);
224-
$nolocale = new Route;
226+
$nolocale = new Route();
225227
$nolocale->setPosition($this->getDm()->find(null, self::ROUTE_ROOT), 'es');
226228
$nolocale->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
227229
$this->getDm()->persist($nolocale);
@@ -317,7 +319,7 @@ public function testEnhanceTemplateByClass()
317319

318320
// put a route for this content
319321
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
320-
$route = new Route;
322+
$route = new Route();
321323
$route->setContent($document);
322324
$route->setPosition($root, 'templatebyclass');
323325
$this->getDm()->persist($route);

0 commit comments

Comments
 (0)