Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2da68ba

Browse filesBrowse files
committed
[Routing] Add stateless route attribute
1 parent 7995fed commit 2da68ba
Copy full SHA for 2da68ba

20 files changed

+67
-10
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Annotation/Route.php
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/CHANGELOG.md
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php
+12Lines 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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/XmlFileLoader.php
+9-1Lines 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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/YamlFileLoader.php
+10-2Lines 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
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd
+2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/defaults.php
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/defaults.xml
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/defaults.yml
+1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/importer-with-defaults.php
+1Lines 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
};

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/importer-with-defaults.xml
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
<import resource="imported-with-defaults.xml" prefix="/defaults"
88
locale="g_locale"
9-
format="g_format" />
9+
format="g_format"
10+
stateless="true" />
1011
</routes>

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/importer-with-defaults.yml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ defaults:
33
prefix: /defaults
44
locale: g_locale
55
format: g_format
6+
stateless: true

‎src/Symfony/Component/Routing/Tests/Fixtures/php_dsl.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/php_dsl.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
->condition('abc')
1010
->options(['utf8' => true])
1111
->add('buz', 'zub')
12-
->controller('foo:act');
12+
->controller('foo:act')
13+
->stateless(true);
1314

1415
$routes->import('php_dsl_sub.php')
1516
->prefix('/sub')

‎src/Symfony/Component/Routing/Tests/Fixtures/php_object_dsl.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/php_object_dsl.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public function __invoke(RoutingConfigurator $routes)
1111
->condition('abc')
1212
->options(['utf8' => true])
1313
->add('buz', 'zub')
14-
->controller('foo:act');
14+
->controller('foo:act')
15+
->stateless(true);
1516

1617
$routes->import('php_dsl_sub.php')
1718
->prefix('/sub')

‎src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$collection = new RouteCollection();
77
$collection->add('blog_show', new Route(
88
'/blog/{slug}',
9-
['_controller' => 'MyBlogBundle:Blog:show'],
9+
['_controller' => 'MyBlogBundle:Blog:show', '_stateless' => true],
1010
['locale' => '\w+'],
1111
['compiler_class' => 'RouteCompiler'],
1212
'{locale}.example.com',

‎src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
<route id="blog_show" path="/blog/{slug}" host="{locale}.example.com" methods="GET|POST put,OpTiOnS" schemes="hTTps">
88
<default key="_controller">MyBundle:Blog:show</default>
9+
<default key="_stateless">
10+
<bool>true</bool>
11+
</default>
912
<requirement key="locale">\w+</requirement>
1013
<option key="compiler_class">RouteCompiler</option>
1114
<condition>context.getMethod() == "GET"</condition>

‎src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
blog_show:
22
path: /blog/{slug}
3-
defaults: { _controller: "MyBundle:Blog:show" }
3+
defaults: { _controller: "MyBundle:Blog:show", _stateless: true }
44
host: "{locale}.example.com"
55
requirements: { 'locale': '\w+' }
66
methods: ['GET','POST','put','OpTiOnS']

‎src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testLoadWithRoute()
4343
foreach ($routes as $route) {
4444
$this->assertSame('/blog/{slug}', $route->getPath());
4545
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
46+
$this->assertTrue($route->getDefault('_stateless'));
4647
$this->assertSame('{locale}.example.com', $route->getHost());
4748
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
4849
$this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
@@ -109,9 +110,11 @@ public function testLoadingImportedRoutesWithDefaults()
109110
$expectedRoutes->add('one', $localeRoute = new Route('/defaults/one'));
110111
$localeRoute->setDefault('_locale', 'g_locale');
111112
$localeRoute->setDefault('_format', 'g_format');
113+
$localeRoute->setDefault('_stateless', true);
112114
$expectedRoutes->add('two', $formatRoute = new Route('/defaults/two'));
113115
$formatRoute->setDefault('_locale', 'g_locale');
114116
$formatRoute->setDefault('_format', 'g_format');
117+
$formatRoute->setDefault('_stateless', true);
115118
$formatRoute->setDefault('specific', 'imported');
116119

117120
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/imported-with-defaults.php'));
@@ -172,7 +175,7 @@ public function testRoutingConfigurator()
172175
->setCondition('abc')
173176
);
174177
$expectedCollection->add('buz', (new Route('/zub'))
175-
->setDefaults(['_controller' => 'foo:act'])
178+
->setDefaults(['_controller' => 'foo:act', '_stateless' => true])
176179
);
177180
$expectedCollection->add('c_root', (new Route('/sub/pub/'))
178181
->setRequirements(['id' => '\d+'])

‎src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function testLoadWithRoute()
4747
$this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
4848
$this->assertEquals(['https'], $route->getSchemes());
4949
$this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
50+
$this->assertTrue($route->getDefault('_stateless'));
5051
}
5152

5253
public function testLoadWithNamespacePrefix()
@@ -98,6 +99,7 @@ public function testLoadingRouteWithDefaults()
9899
$this->assertSame('/defaults', $defaultsRoute->getPath());
99100
$this->assertSame('en', $defaultsRoute->getDefault('_locale'));
100101
$this->assertSame('html', $defaultsRoute->getDefault('_format'));
102+
$this->assertTrue($defaultsRoute->getDefault('_stateless'));
101103
}
102104

103105
public function testLoadingImportedRoutesWithDefaults()
@@ -111,9 +113,11 @@ public function testLoadingImportedRoutesWithDefaults()
111113
$expectedRoutes->add('one', $localeRoute = new Route('/defaults/one'));
112114
$localeRoute->setDefault('_locale', 'g_locale');
113115
$localeRoute->setDefault('_format', 'g_format');
116+
$localeRoute->setDefault('_stateless', true);
114117
$expectedRoutes->add('two', $formatRoute = new Route('/defaults/two'));
115118
$formatRoute->setDefault('_locale', 'g_locale');
116119
$formatRoute->setDefault('_format', 'g_format');
120+
$formatRoute->setDefault('_stateless', true);
117121
$formatRoute->setDefault('specific', 'imported');
118122

119123
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/imported-with-defaults.xml'));

‎src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public function testLoadWithRoute()
9090
$this->assertEquals(['GET', 'POST', 'PUT', 'OPTIONS'], $route->getMethods());
9191
$this->assertEquals(['https'], $route->getSchemes());
9292
$this->assertEquals('context.getMethod() == "GET"', $route->getCondition());
93+
$this->assertTrue($route->getDefault('_stateless'));
9394
}
9495

9596
public function testLoadWithResource()
@@ -232,6 +233,7 @@ public function testLoadingRouteWithDefaults()
232233
$this->assertSame('/defaults', $defaultsRoute->getPath());
233234
$this->assertSame('en', $defaultsRoute->getDefault('_locale'));
234235
$this->assertSame('html', $defaultsRoute->getDefault('_format'));
236+
$this->assertTrue($defaultsRoute->getDefault('_stateless'));
235237
}
236238

237239
public function testLoadingImportedRoutesWithDefaults()
@@ -245,9 +247,11 @@ public function testLoadingImportedRoutesWithDefaults()
245247
$expectedRoutes->add('one', $localeRoute = new Route('/defaults/one'));
246248
$localeRoute->setDefault('_locale', 'g_locale');
247249
$localeRoute->setDefault('_format', 'g_format');
250+
$localeRoute->setDefault('_stateless', true);
248251
$expectedRoutes->add('two', $formatRoute = new Route('/defaults/two'));
249252
$formatRoute->setDefault('_locale', 'g_locale');
250253
$formatRoute->setDefault('_format', 'g_format');
254+
$formatRoute->setDefault('_stateless', true);
251255
$formatRoute->setDefault('specific', 'imported');
252256

253257
$expectedRoutes->addResource(new FileResource(__DIR__.'/../Fixtures/imported-with-defaults.yml'));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.