Skip to content

add basepath support #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 17, 2015
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sudo: false
language: php
php:
- 5.3
Expand Down
4 changes: 2 additions & 2 deletions src/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class Regex
* @return bool
*
*/
public function match(Route $route, $path)
public function match(Route $route, $path, $basepath = null)
{
$this->route = $route;
$this->regex = $this->route->path;
$this->regex = $basepath . $this->route->path;
$this->setRegexOptionalParams();
$this->setRegexParams();
$this->setRegexWildcard();
Expand Down
12 changes: 6 additions & 6 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ public function __isset($key)
* @return bool
*
*/
public function isMatch($path, array $server)
public function isMatch($path, array $server, $basepath = null)
{
$this->debug = array();
$this->params = array();
$this->score = 0;
$this->failed = null;
if ($this->isFullMatch($path, $server)) {
if ($this->isFullMatch($path, $server, $basepath)) {
$this->setParams();
return true;
}
Expand All @@ -266,11 +266,11 @@ public function isMatch($path, array $server)
* @return bool
*
*/
protected function isFullMatch($path, array $server)
protected function isFullMatch($path, array $server, $basepath = null)
{
return $this->isRoutableMatch()
&& $this->isSecureMatch($server)
&& $this->isRegexMatch($path)
&& $this->isRegexMatch($path, $basepath)
&& $this->isMethodMatch($server)
&& $this->isAcceptMatch($server)
&& $this->isServerMatch($server)
Expand Down Expand Up @@ -394,10 +394,10 @@ protected function serverIsSecure($server)
* @return bool True on a match, false if not.
*
*/
protected function isRegexMatch($path)
protected function isRegexMatch($path, $basepath = null)
{
$regex = clone $this->regex;
$match = $regex->match($this, $path);
$match = $regex->match($this, $path, $basepath);
if (! $match) {
return $this->fail(self::FAILED_REGEX);
}
Expand Down
16 changes: 11 additions & 5 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Router
*/
protected $failed_route = null;

protected $basepath;

/**
*
* Constructor.
Expand All @@ -81,10 +83,14 @@ class Router
* @param Generator $generator A URL path generator.
*
*/
public function __construct(RouteCollection $routes, Generator $generator)
{
public function __construct(
RouteCollection $routes,
Generator $generator,
$basepath = null
) {
$this->routes = $routes;
$this->generator = $generator;
$this->basepath = rtrim($basepath, '/');
}

/**
Expand Down Expand Up @@ -124,7 +130,7 @@ public function match($path, array $server = array())

$this->debug[] = $route;

$match = $route->isMatch($path, $server);
$match = $route->isMatch($path, $server, $this->basepath);
if ($match) {
$this->matched_route = $route;
return $route;
Expand Down Expand Up @@ -187,7 +193,7 @@ public function getMatchedRoute()
public function generate($name, $data = array())
{
$route = $this->getRouteForGenerate($name);
return $this->generator->generate($route, $data);
return $this->basepath . $this->generator->generate($route, $data);
}

/**
Expand All @@ -208,7 +214,7 @@ public function generate($name, $data = array())
public function generateRaw($name, $data = array())
{
$route = $this->getRouteForGenerate($name);
return $this->generator->generateRaw($route, $data);
return $this->basepath . $this->generator->generateRaw($route, $data);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/RouterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class RouterFactory
* @return Router
*
*/
public function newInstance()
public function newInstance($basepath = null)
{
return new Router(
new RouteCollection(new RouteFactory),
new Generator
new Generator,
$basepath
);
}
}
43 changes: 40 additions & 3 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class RouterTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
parent::setUp();
$this->factory = new RouterFactory;
$this->router = $this->newRouter();
}

protected function newRouter()
protected function newRouter($basepath = null)
{
return $this->factory->newInstance();
$factory = new RouterFactory;
return $factory->newInstance($basepath);
}

protected function assertIsRoute($actual)
Expand Down Expand Up @@ -498,4 +498,41 @@ public function testAddWithAction()
$actual = $this->router->match('/foo/bar');
$this->assertSame('DirectAction', $actual->values['action']);
}

public function testWithBasepathIndex()
{
$this->router = $this->newRouter('/path/to/sub/index.php');
$this->router->add('foo.bar', '/foo/bar', 'DirectAction');

// should fail without basepath
$this->assertFalse($this->router->match('/foo/bar'));

// should pass with basepath
$actual = $this->router->match('/path/to/sub/index.php/foo/bar');
$this->assertSame('DirectAction', $actual->values['action']);

// should get the basepath in place
$expect = '/path/to/sub/index.php/foo/bar';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to have one more test with no index.php . So is /path/to/sub/foo/bar

$actual = $this->router->generate('foo.bar');
$this->assertSame($expect, $actual);
}


public function testWithBasepathDir()
{
$this->router = $this->newRouter('/path/to/sub');
$this->router->add('foo.bar', '/foo/bar', 'DirectAction');

// should fail without basepath
$this->assertFalse($this->router->match('/foo/bar'));

// should pass with basepath
$actual = $this->router->match('/path/to/sub/foo/bar');
$this->assertSame('DirectAction', $actual->values['action']);

// should get the basepath in place
$expect = '/path/to/sub/foo/bar';
$actual = $this->router->generate('foo.bar');
$this->assertSame($expect, $actual);
}
}