convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
- stopOnFailure="true">
+ stopOnFailure="false">
<testsuites>
<testsuite name="PHP Router">
<directory>tests</directory>
<?php
-/**
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * This software consists of voluntary contributions made by many
- * individuals and is licensed under the MIT license.
- */
namespace PHPRouter;
return null;
}
- // todo use reflection
- $instance = new $class();
-
if (empty($action[1]) || trim($action[1]) === '') {
$method = "__invoke";
}
- if ( !method_exists($instance, $method)) {
+ try {
+ $classRexl = new \ReflectionClass($class);
+ } catch (\ReflectionException $ex) {
+ return null;
+ }
+
+ try {
+ $classRexl->getMethod($method);
+ } catch (\ReflectionException $ex) {
return null;
}
public function dispatch()
{
$action = explode('::', $this->config['_controller']);
- $instance = new $action[0];
+ $instance = new $action[0]();
if ($this->parametersByName) {
$this->parameters = array($this->parameters);
<?php
-/**
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
- * CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * This software consists of voluntary contributions made by many
- * individuals and is licensed under the MIT license.
- */
namespace PHPRouter;
foreach ($this->routes->all() as $routes) {
// compare server request method with route's allowed http methods
- if ( !in_array($requestMethod, (array)$routes->getMethods(), true)) {
+ if (!in_array($requestMethod, (array)$routes->getMethods(), true)) {
continue;
}
$route = rtrim($routes->getRegex(), '/');
$pattern = '@^' . preg_quote($this->basePath) . $route . '/?$@i';
- if ( !preg_match($pattern, $requestUrl, $matches)) {
+ if (!preg_match($pattern, $requestUrl, $matches)) {
continue;
}
);
}
+ public function getRoute($requestUrl, $requestMethod = RequestMethodInterface::METHOD_GET)
+ {
+ /** @var Route $route */
+ list($route, $params) = $this->findRoute($requestUrl, $requestMethod);
+
+ if ($route !== null) {
+ $route->setParameters($params);
+
+ return $route;
+ } else {
+ return null;
+ }
+ }
+
/**
* Match given request _url and request method and see if a route
* has been defined for it If so, return route's target If called
* @param string $requestUrl
* @param string $requestMethod
*
- * @return bool|Route
+ * @return null|Route
*/
public function match($requestUrl, $requestMethod = RequestMethodInterface::METHOD_GET)
{
if ($route !== null) {
$route->setParameters($params);
+
return $route->dispatch();
} else {
- return;
+ return null;
}
}
public function generate($routeName, array $params = array())
{
// Check if route exists
- if ( !isset($this->namedRoutes[$routeName])) {
+ if (!isset($this->namedRoutes[$routeName])) {
throw new Exception("No route with the name $routeName has been found.");
}
*/
public function testMatch($router, $path, $expected)
{
- $result = $router->match($path);
+ $buffer = $router->match($path);
- $this->assertEquals($expected, $result);
+ if($expected === true) {
+ $this->assertNotNull($buffer);
+ } else {
+ $this->assertNull($buffer);
+ }
}
public function testMatchWrongMethod()
{
$router = $this->getRouter();
- $this->assertFalse($router->match('/users', 'POST'));
+ $buffer = $router->match('/users', 'POST');
+
+ $this->assertNull($buffer);
}
public function testBasePathConfigIsSettedProperly()
foreach ($this->serverProvider() as $server) {
$_SERVER = $server;
- $this->assertTrue((bool)$router->matchCurrentRequest());
+ $buffer = $router->matchCurrentRequest();
+
+ $this->assertNotNull($buffer);
}
}
$router = new Router($collection);
$this->assertEquals(
- array(array('page_id' => 'MySuperPage')),
- $router->match('/page/MySuperPage')->getParameters()
+ array('page_id' => 'MySuperPage'),
+ $router->getRoute('/page/MySuperPage')->getParameters()
);
}
$router = new Router($collection);
$this->assertEquals(
- array(array('filename' => 'someJsFile')),
- $router->match('/js/someJsFile.js')->getParameters()
+ array('filename' => 'someJsFile'),
+ $router->getRoute('/js/someJsFile.js')->getParameters()
);
$this->assertEquals(
- array(array('filename' => 'someJsFile.min')),
- $router->match('/js/someJsFile.min.js')->getParameters()
+ array('filename' => 'someJsFile.min'),
+ $router->getRoute('/js/someJsFile.min.js')->getParameters()
);
$this->assertEquals(
- array(array('filename' => 'someJsFile.min.js')),
- $router->match('/js/someJsFile.min.js.js')->getParameters()
+ array('filename' => 'someJsFile.min.js'),
+ $router->getRoute('/js/someJsFile.min.js.js')->getParameters()
);
}