/**
  * Create a RoutePathCompilationException from a route
  * and an optional previous exception
  *
  * @param Route $route The route that failed to compile
  * @param Exception $previous The previous exception
  * @return RoutePathCompilationException
  */
 public static function createFromRoute(Route $route, Exception $previous = null)
 {
     $error = null !== $previous ? $previous->getMessage() : null;
     $code = null !== $previous ? $previous->getCode() : null;
     $message = sprintf(static::MESSAGE_FORMAT, $route->getPath());
     $message .= ' ' . sprintf(static::FAILURE_MESSAGE_TITLE_FORMAT, $error);
     $exception = new static($message, $code, $previous);
     $exception->setRoute($route);
     return $exception;
 }
Example #2
0
 /**
  * On lance le dispatch de la requete avec les routes inséré
  * @param Response $response
  * @return bool
  */
 public function perform(\Router\Response $response)
 {
     // ajout de la route global si existe
     $aApplyRoute = $this->routes['ANY'];
     if (isset($this->routes[$this->method])) {
         $aApplyRoute = array_merge($this->routes[$this->method], $aApplyRoute);
     }
     if (!empty($aApplyRoute)) {
         require_once __DIR__ . "/Route.php";
         foreach ($aApplyRoute as $route) {
             // récuperation des params de la route et creation de l'objet pour test
             list($pattern_uri, $callback, $filters) = $route;
             $oRoute = new \Router\Route($pattern_uri, array_merge($this->filters, $filters));
             if (false === ($params = $oRoute->match($this->request->getRequestUri()))) {
                 continue;
             }
             // insert des params dans le request
             foreach ($params as $key => $value) {
                 $this->request->setParam($key, $value);
             }
             // appel du callback si c'est possible
             if (is_callable($callback) === true) {
                 ob_start();
                 call_user_func_array($callback, array($this->request, $response));
                 $response->appendBody(ob_get_contents());
                 ob_end_clean();
             }
             return true;
         }
         unset($oRoute);
     }
     // renvoi false si aucune regle n'est passé
     return false;
 }
Example #3
0
 public function testGetPathFor()
 {
     // Test data
     $test_path = '/test';
     $test_name = 'Test Route Thing';
     $route = new Route($this->getTestCallable());
     $route->setPath($test_path);
     $route->setName($test_name);
     $this->klein_app->getRoutes()->addRoute($route);
     // Make sure it fails if not prepared
     try {
         $this->klein_app->getPathFor($test_name);
     } catch (Exception $e) {
         $this->assertTrue($e instanceof OutOfBoundsException);
     }
     $this->klein_app->getRoutes()->prepareNamed();
     $returned_path = $this->klein_app->getPathFor($test_name);
     $this->assertNotEmpty($returned_path);
     $this->assertSame($test_path, $returned_path);
 }
Example #4
0
 /**
  * Handle a route's callback
  *
  * This handles common exceptions and their output
  * to keep the "dispatch()" method DRY
  *
  * @param Route $route
  * @param RouteCollection $matched
  * @param int $methods_matched
  * @return void
  */
 protected function handleRouteCallback(Route $route, RouteCollection $matched, $methods_matched)
 {
     // Handle the callback
     $returned = call_user_func($route->getCallback(), $this->request, $this->response, $this->app, $this, $matched, $methods_matched);
     if ($returned instanceof AbstractResponse) {
         $this->response = $returned;
     } else {
         // Otherwise, attempt to append the returned data
         try {
             $this->response->append($returned);
         } catch (LockedResponseException $e) {
             // Do nothing, since this is an automated behavior
         }
     }
 }
Example #5
0
 /**
  * @expectedException InvalidArgumentException
  */
 public function testMethodSetWithIncorrectType()
 {
     $route = new Route($this->getTestCallable());
     // Test setting with the WRONG type
     $route->setMethod(100);
 }
Example #6
0
<?php

require __DIR__ . "/../Psr4Autoloader.php";
$autoloader = new Psr4Autoloader();
$autoloader->register();
$autoloader->addNamespace("Container", __DIR__ . "/../lib/Container");
$autoloader->addNamespace("Router", __DIR__ . "/../lib/Router");
$autoloader->addNamespace("Actions", __DIR__ . "/../src/Actions");
$autoloader->loadClass('Container\\Container');
use Container\Container;
use Router\Router;
use Router\RouteCollection;
use Router\Route;
use Actions\AbstractAction;
use Router\RouteNotFoundException;
$container = new Container();
$routes = new RouteCollection();
$routes->add('home', new Route('/', array('_action' => 'Actions\\HomeAction')));
$router = new Router($routes);
try {
    $route = $router->resolve($_SERVER['REQUEST_URI']);
} catch (RouteNotFoundException $e) {
    $route = new Route('error404', array('_action' => 'Actions\\Error404Action'));
}
$actionClass = $route->getDefault('_action');
/** @var AbstractAction $action */
$action = new $actionClass($container);
echo $action->render();