Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static function config(array $config): array
* host?: string|array<string,string>,
* schemes?: string|list<string>,
* condition?: string,
* add_condition?: string,
* locale?: string,
* format?: string,
* utf8?: bool,
Expand All @@ -236,6 +237,7 @@ public static function config(array $config): array
* host?: string|array<string,string>,
* schemes?: string|list<string>,
* condition?: string,
* add_condition?: string,
* locale?: string,
* format?: string,
* utf8?: bool,
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
* Add a `firewall` option to route definitions, exposed as the `_firewall` route default
* Allow defining default query parameters with the `_query` route default
* Add `RequestContext::runWith()` to generate and match URLs for another host, scheme or base URL without leaking the change
* Add `Route::addCondition()`, `RouteCollection::addCondition()` and the `add_condition` import key to combine a condition with the existing ones

8.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* host?: string|array<string,string>,
* schemes?: string|list<string>,
* condition?: string,
* add_condition?: string,
* locale?: string,
* format?: string,
* utf8?: bool,
Expand All @@ -62,6 +63,7 @@
* host?: string|array<string,string>,
* schemes?: string|list<string>,
* condition?: string,
* add_condition?: string,
* locale?: string,
* format?: string,
* utf8?: bool,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ final public function condition(string $condition): static
return $this;
}

/**
* Adds a condition, combined with the existing one using "and".
*
* @return $this
*/
final public function addCondition(string $condition): static
{
$this->route->addCondition($condition);

return $this;
}

/**
* Sets the pattern for the host.
*
Expand Down
7 changes: 6 additions & 1 deletion src/Symfony/Component/Routing/Loader/ContentLoaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait ContentLoaderTrait
* Config keys accepted at the route or import level by {@see validate()}.
*/
private const AVAILABLE_KEYS = [
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8', 'exclude', 'stateless', 'firewall',
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'add_condition', 'controller', 'name_prefix', 'trailing_slash_on_root', 'locale', 'format', 'utf8', 'exclude', 'stateless', 'firewall',
];

/**
Expand Down Expand Up @@ -114,6 +114,7 @@ private function parseRoute(RouteCollection $collection, string $name, array $co
$routes->setSchemes($config['schemes'] ?? []);
$routes->setMethods($config['methods'] ?? []);
$routes->setCondition($config['condition'] ?? null);
$routes->addCondition($config['add_condition'] ?? '');

if (isset($config['host'])) {
$this->addHost($routes, $config['host']);
Expand All @@ -134,6 +135,7 @@ private function parseImport(RouteCollection $collection, array $config, string
$options = $config['options'] ?? [];
$host = $config['host'] ?? null;
$condition = $config['condition'] ?? null;
$addCondition = $config['add_condition'] ?? null;
$schemes = $config['schemes'] ?? null;
$methods = $config['methods'] ?? null;
$trailingSlashOnRoot = $config['trailing_slash_on_root'] ?? true;
Expand Down Expand Up @@ -177,6 +179,9 @@ private function parseImport(RouteCollection $collection, array $config, string
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $addCondition) {
$subCollection->addCondition($addCondition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
]
},
"condition": { "type": "string" },
"add_condition": { "type": "string" },
"locale": { "type": "string" },
"format": { "type": "string" },
"utf8": { "type": "boolean" },
Expand Down Expand Up @@ -128,6 +129,7 @@
]
},
"condition": { "type": "string" },
"add_condition": { "type": "string" },
"trailing_slash_on_root": { "type": "boolean" },
"methods": { "oneOf": [ { "type": "string" }, { "type": "array", "items": { "type": "string" } } ] },
"locale": { "type": "string" },
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,20 @@ public function setCondition(?string $condition): static
return $this;
}

/**
* Adds a condition, combined with the existing one using "and".
*
* @return $this
*/
public function addCondition(string $condition): static
{
if ('' === $condition) {
return $this;
}

return $this->setCondition('' === $this->condition ? $condition : \sprintf('(%s) and (%s)', $this->condition, $condition));
}

/**
* Compiles the route.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ public function setCondition(?string $condition): void
}
}

/**
* Adds a condition to all routes, combined with their existing one using "and".
*/
public function addCondition(string $condition): void
{
foreach ($this->routes as $route) {
$route->addCondition($condition);
}
}

/**
* Adds defaults to all routes.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Routing/Tests/Fixtures/add_condition.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
route_with_conditions:
path: /route
condition: 'context.getMethod() == "GET"'
add_condition: 'request.isSecure()'

import_with_add_condition:
resource: validpattern.yml
name_prefix: added_
add_condition: 'request.isSecure()'

import_with_both_conditions:
resource: validpattern.yml
name_prefix: replaced_
condition: 'context.getMethod() == "POST"'
add_condition: 'request.isSecure()'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->import('php_dsl_sub_add_condition.php')
->addCondition('request.isSecure()');

$routes->collection('c_')
->addCondition('request.isSecure()')
->add('foo', '/foo')
->addCondition('context.getMethod() == "GET"');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes) {
$routes->add('with_condition', '/with-condition')
->condition('context.getMethod() == "GET"');

$routes->add('without_condition', '/without-condition');
};
11 changes: 11 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ public function testRoutingConfigurator()
$this->assertEquals($expectedCollectionObject, $routeCollectionObject);
}

public function testRoutingConfiguratorAddCondition()
{
$locator = new FileLocator([__DIR__.'/../Fixtures']);
$loader = new PhpFileLoader($locator);
$routeCollection = $loader->load('php_dsl_add_condition.php');

$this->assertSame('(context.getMethod() == "GET") and (request.isSecure())', $routeCollection->get('with_condition')->getCondition());
$this->assertSame('request.isSecure()', $routeCollection->get('without_condition')->getCondition());
$this->assertSame('(request.isSecure()) and (context.getMethod() == "GET")', $routeCollection->get('c_foo')->getCondition());
}

public function testRoutingConfiguratorCanImportGlobPatterns()
{
$locator = new FileLocator([__DIR__.'/../Fixtures/glob']);
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public function testLoadWithResource()
}
}

public function testLoadWithAddCondition()
{
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
$routeCollection = $loader->load('add_condition.yml');

$this->assertSame('(context.getMethod() == "GET") and (request.isSecure())', $routeCollection->get('route_with_conditions')->getCondition());
$this->assertSame('(context.getMethod() == "GET") and (request.isSecure())', $routeCollection->get('added_blog_show')->getCondition());
$this->assertSame('request.isSecure()', $routeCollection->get('added_blog_show_inherited')->getCondition());
$this->assertSame('(context.getMethod() == "POST") and (request.isSecure())', $routeCollection->get('replaced_blog_show')->getCondition());
}

public function testLoadRouteWithControllerAttribute()
{
$loader = new YamlFileLoader(new FileLocator([__DIR__.'/../Fixtures/controller']));
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,20 @@ public function testSetCondition()
$this->assertEquals('context.getMethod() == "POST"', $routeb->getCondition());
}

public function testAddCondition()
{
$collection = new RouteCollection();
$routea = new Route('/a');
$routeb = new Route('/b', [], [], [], '', [], [], 'context.getMethod() == "GET"');
$collection->add('a', $routea);
$collection->add('b', $routeb);

$collection->addCondition('request.isSecure()');

$this->assertSame('request.isSecure()', $routea->getCondition());
$this->assertSame('(context.getMethod() == "GET") and (request.isSecure())', $routeb->getCondition());
}

public function testClone()
{
$collection = new RouteCollection();
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ public function testCondition()
$this->assertSame('context.getMethod() == "GET"', $route->getCondition());
}

public function testAddCondition()
{
$route = new Route('/');
$route->addCondition('request.isSecure()');
$this->assertSame('request.isSecure()', $route->getCondition());

$route->addCondition('context.getMethod() == "GET"');
$this->assertSame('(request.isSecure()) and (context.getMethod() == "GET")', $route->getCondition());

$route->addCondition('');
$this->assertSame('(request.isSecure()) and (context.getMethod() == "GET")', $route->getCondition());
}

public function testCompile()
{
$route = new Route('/{foo}');
Expand Down
Loading