Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
Disallow using placeholders beginning with _, they are reserved for i…
Browse files Browse the repository at this point in the history
…nternal use
  • Loading branch information
krzysztof-magosa committed Oct 28, 2014
1 parent 764e18c commit a21093b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/KM/Saffron/RouteCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use KM\Saffron\Route;
use KM\Saffron\RouteCompiled;
use KM\Saffron\Exception\InvalidArgument;

class RouteCompiler
{
Expand Down Expand Up @@ -100,12 +101,26 @@ protected function getDomainRegex(Route $route)
return '#^'.$regex.'$#s';
}

protected function validateRoute(Route $route)
{
if (false !== strpos($route->getUri(), '{_') || false !== strpos($route->getDomain(), '{_')) {
throw new InvalidArgument(
sprintf(
'Placeholders cannot begin with _. Route: %s.',
$route->getName()
)
);
}
}

/**
* @param Route $route
* @return RouteCompiled
*/
public function compile(Route $route)
{
$this->validateRoute($route);

$compiled = new RouteCompiled(
$this->getPrefix($route),
$this->getPrefix($route) != $route->getUri() ? $this->getUriRegex($route) : null,
Expand Down
42 changes: 42 additions & 0 deletions tests/RouteCompilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Copyright 2014 Krzysztof Magosa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use KM\Saffron\Route;

class RouteCompilerTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException \KM\Saffron\Exception\InvalidArgument
* @expectedExceptionMessage Placeholders cannot begin with _. Route: route.
*/
public function testInvalidPlaceholderInUri()
{
$route = new Route('route');
$route->setUri('/{_name}');
$route->getCompiled();
}


/**
* @expectedException \KM\Saffron\Exception\InvalidArgument
* @expectedExceptionMessage Placeholders cannot begin with _. Route: route.
*/
public function testInvalidPlaceholderInDomain()
{
$route = new Route('route');
$route->setDomain('{_name}.example.com');
$route->getCompiled();
}
}

0 comments on commit a21093b

Please sign in to comment.