From e1fc9623c54d6ce978c85a483b997c761996e194 Mon Sep 17 00:00:00 2001 From: RJ Garcia Date: Tue, 3 May 2016 07:32:09 -0700 Subject: [PATCH] [Routing] Added trim flag to Route::setPath Added a trim flag to Route::setPath in order to allow paths to be set as is without any trimming to sanitize the input Signed-off-by: RJ Garcia --- src/Symfony/Component/Routing/Route.php | 5 +++-- src/Symfony/Component/Routing/Tests/RouteTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Routing/Route.php b/src/Symfony/Component/Routing/Route.php index ad006961ee299..bfa51079df0ed 100644 --- a/src/Symfony/Component/Routing/Route.php +++ b/src/Symfony/Component/Routing/Route.php @@ -148,14 +148,15 @@ public function getPath() * This method implements a fluent interface. * * @param string $pattern The path pattern + * @param bool $trim whether or not to trim and format the $pattern. * * @return Route The current Route instance */ - public function setPath($pattern) + public function setPath($pattern, $trim = true) { // A pattern must start with a slash and must not have multiple slashes at the beginning because the // generated path for this route would be confused with a network path, e.g. '//domain.com/path'. - $this->path = '/'.ltrim(trim($pattern), '/'); + $this->path = $trim ? '/'.ltrim(trim($pattern), '/') : $pattern; $this->compiled = null; return $this; diff --git a/src/Symfony/Component/Routing/Tests/RouteTest.php b/src/Symfony/Component/Routing/Tests/RouteTest.php index dc8e4fa2e39c7..2d29063bea5e8 100644 --- a/src/Symfony/Component/Routing/Tests/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteTest.php @@ -236,4 +236,17 @@ public function testSerializedRepresentationKeepsWorking() $this->assertEquals($route, $unserialized); $this->assertNotSame($route, $unserialized); } + + /** + * Tests that the setPath trim flag can be changed to allow non-trimming + * of routes. + */ + public function testSetPathTrimFlag() + { + $route = new Route('/'); + $path = ' path '; + $route->setPath($path, false); + + $this->assertEquals($path, $route->getPath()); + } }