Skip to content

Commit 044f770

Browse files
committed
[Routing] Add stateless route attribute
1 parent eee5911 commit 044f770

20 files changed

+67
-10
lines changed

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)) {

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
-----

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 = true): self
170+
{
171+
$this->route->addDefaults(['_stateless' => $stateless]);
172+
173+
return $this;
174+
}
163175
}

Loader/XmlFileLoader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait;
1818
use Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait;
1919
use Symfony\Component\Routing\RouteCollection;
20-
use Symfony\Component\Routing\RouteCompiler;
2120

2221
/**
2322
* XmlFileLoader loads XML routing files.
@@ -300,6 +299,15 @@ private function parseConfigs(\DOMElement $node, string $path): array
300299
if ($node->hasAttribute('utf8')) {
301300
$options['utf8'] = XmlUtils::phpize($node->getAttribute('utf8'));
302301
}
302+
if ($stateless = $node->getAttribute('stateless')) {
303+
if (isset($defaults['_stateless'])) {
304+
$name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName);
305+
306+
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" attribute and the defaults key "_stateless" for %s.', $path, $name));
307+
}
308+
309+
$defaults['_stateless'] = XmlUtils::phpize($stateless);
310+
}
303311

304312
return [$defaults, $requirements, $options, $condition, $paths, $prefixes];
305313
}

Loader/YamlFileLoader.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait;
1717
use Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait;
1818
use Symfony\Component\Routing\RouteCollection;
19-
use Symfony\Component\Routing\RouteCompiler;
2019
use Symfony\Component\Yaml\Exception\ParseException;
2120
use Symfony\Component\Yaml\Parser as YamlParser;
2221
use Symfony\Component\Yaml\Yaml;
@@ -33,7 +32,7 @@ class YamlFileLoader extends FileLoader
3332
use PrefixTrait;
3433

3534
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',
35+
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8', 'exclude', 'stateless',
3736
];
3837
private $yamlParser;
3938

@@ -134,6 +133,9 @@ protected function parseRoute(RouteCollection $collection, string $name, array $
134133
if (isset($config['utf8'])) {
135134
$options['utf8'] = $config['utf8'];
136135
}
136+
if (isset($config['stateless'])) {
137+
$defaults['_stateless'] = $config['stateless'];
138+
}
137139

138140
$route = $this->createLocalizedRoute($collection, $name, $config['path']);
139141
$route->addDefaults($defaults);
@@ -179,6 +181,9 @@ protected function parseImport(RouteCollection $collection, array $config, strin
179181
if (isset($config['utf8'])) {
180182
$options['utf8'] = $config['utf8'];
181183
}
184+
if (isset($config['stateless'])) {
185+
$defaults['_stateless'] = $config['stateless'];
186+
}
182187

183188
$this->setCurrentDir(\dirname($path));
184189

@@ -245,5 +250,8 @@ protected function validate($config, string $name, string $path)
245250
if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
246251
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
247252
}
253+
if (isset($config['stateless']) && isset($config['defaults']['_stateless'])) {
254+
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" key and the defaults key "_stateless" for "%s".', $path, $name));
255+
}
248256
}
249257
}

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">

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
};

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>

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

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)