Skip to content

Commit 21db0f2

Browse files
committed
[Routing] Add stateless route attribute
1 parent 7995fed commit 21db0f2

20 files changed

+67
-8
lines changed

src/Symfony/Component/Routing/Annotation/Route.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public function __construct(array $data)
7272
unset($data['utf8']);
7373
}
7474

75+
if (isset($data['stateless'])) {
76+
$data['defaults']['_stateless'] = filter_var($data['stateless'], FILTER_VALIDATE_BOOLEAN) ?: false;
77+
unset($data['stateless']);
78+
}
79+
7580
foreach ($data as $key => $value) {
7681
$method = 'set'.str_replace('_', '', $key);
7782
if (!method_exists($this, $method)) {

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CHANGELOG
1010
* added argument `$priority` to `RouteCollection::add()`
1111
* deprecated the `RouteCompiler::REGEX_DELIMITER` constant
1212
* added `ExpressionLanguageProvider` to expose extra functions to route conditions
13+
* added support for a `stateless` keyword for configuring route stateless in PHP, YAML and XML configurations.
1314

1415
5.0.0
1516
-----

src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,16 @@ final public function format(string $format): self
160160

161161
return $this;
162162
}
163+
164+
/**
165+
* Adds the "_stateless" entry to defaults.
166+
*
167+
* @return $this
168+
*/
169+
final public function stateless(bool $stateless): self
170+
{
171+
$this->route->addDefaults(['_stateless' => $stateless]);
172+
173+
return $this;
174+
}
163175
}

src/Symfony/Component/Routing/Loader/XmlFileLoader.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ private function parseConfigs(\DOMElement $node, string $path): array
300300
if ($node->hasAttribute('utf8')) {
301301
$options['utf8'] = XmlUtils::phpize($node->getAttribute('utf8'));
302302
}
303+
if ($stateless = $node->getAttribute('stateless')) {
304+
if (isset($defaults['_stateless'])) {
305+
$name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName);
306+
307+
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" attribute and the defaults key "_stateless" for %s.', $path, $name));
308+
}
309+
310+
$defaults['_stateless'] = XmlUtils::phpize($stateless);
311+
}
303312

304313
return [$defaults, $requirements, $options, $condition, $paths, $prefixes];
305314
}

src/Symfony/Component/Routing/Loader/YamlFileLoader.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class YamlFileLoader extends FileLoader
3333
use PrefixTrait;
3434

3535
private static $availableKeys = [
36-
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8', 'exclude',
36+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8', 'exclude', 'stateless',
3737
];
3838
private $yamlParser;
3939

@@ -134,6 +134,9 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
134134
if (isset($config['utf8'])) {
135135
$options['utf8'] = $config['utf8'];
136136
}
137+
if (isset($config['stateless'])) {
138+
$defaults['_stateless'] = $config['stateless'];
139+
}
137140

138141
$route = $this->createLocalizedRoute($collection, $name, $config['path']);
139142
$route->addDefaults($defaults);
@@ -179,6 +182,9 @@ protected function parseImport(RouteCollection $collection, array $config, strin
179182
if (isset($config['utf8'])) {
180183
$options['utf8'] = $config['utf8'];
181184
}
185+
if (isset($config['stateless'])) {
186+
$defaults['_stateless'] = $config['stateless'];
187+
}
182188

183189
$this->setCurrentDir(\dirname($path));
184190

@@ -245,5 +251,8 @@ protected function validate($config, string $name, string $path)
245251
if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
246252
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
247253
}
254+
if (isset($config['stateless']) && isset($config['defaults']['_stateless'])) {
255+
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" key and the defaults key "_stateless" for "%s".', $path, $name));
256+
}
248257
}
249258
}

src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<xsd:attribute name="locale" type="xsd:string" />
5656
<xsd:attribute name="format" type="xsd:string" />
5757
<xsd:attribute name="utf8" type="xsd:boolean" />
58+
<xsd:attribute name="stateless" type="xsd:boolean" />
5859
</xsd:complexType>
5960

6061
<xsd:complexType name="import">
@@ -76,6 +77,7 @@
7677
<xsd:attribute name="format" type="xsd:string" />
7778
<xsd:attribute name="trailing-slash-on-root" type="xsd:boolean" />
7879
<xsd:attribute name="utf8" type="xsd:boolean" />
80+
<xsd:attribute name="stateless" type="xsd:boolean" />
7981
</xsd:complexType>
8082

8183
<xsd:complexType name="default" mixed="true">

src/Symfony/Component/Routing/Tests/Fixtures/defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
$routes->add('defaults', '/defaults')
77
->locale('en')
88
->format('html')
9+
->stateless(true)
910
;
1011
};

src/Symfony/Component/Routing/Tests/Fixtures/defaults.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
xsi:schemaLocation="http://symfony.com/schema/routing
55
https://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<route id="defaults" path="/defaults" locale="en" format="html" />
7+
<route id="defaults" path="/defaults" locale="en" format="html" stateless="true" />
88
</routes>

src/Symfony/Component/Routing/Tests/Fixtures/defaults.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ defaults:
22
path: /defaults
33
locale: en
44
format: html
5+
stateless: true

src/Symfony/Component/Routing/Tests/Fixtures/importer-with-defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
->prefix('/defaults')
88
->locale('g_locale')
99
->format('g_format')
10+
->stateless(true)
1011
;
1112
};

0 commit comments

Comments
 (0)