Exemplo n.º 1
0
 /**
  * Initialize the route dispatcher and defined routes.
  */
 protected function initRoutes()
 {
     $this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $r) {
         $r->addRoute('GET', '/', 'IndexController@index');
         $r->addRoute('GET', '/{id:\\d+}', 'IndexController@withId');
     });
 }
Exemplo n.º 2
0
 /**
  * Router constructor.
  */
 public function __construct()
 {
     $dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $collector) {
         $collector->addRoute(['GET'], '/logout', [new \App\Controllers\AdminController(), 'logout']);
         $collector->addRoute(['GET', 'POST'], '/[{table}[/{action}[/{id}]]]', [new \App\Controllers\AdminController(), 'main']);
     });
     $factory = new Abimo\Factory();
     $request = $factory->request();
     $method = $request->method();
     $uri = $request->uri();
     $route = $dispatcher->dispatch($method, $uri);
     switch ($route[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             $response = $factory->response();
             $response->header('404', true, 404)->send();
             throw new \ErrorException("Route {$method} {$uri} not found.");
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $response = $factory->response();
             $response->header('405', true, 405)->send();
             throw new \ErrorException("Method {$method} not allowed.");
             break;
         case FastRoute\Dispatcher::FOUND:
             $handler = $route[1];
             $arguments = $route[2];
             call_user_func_array($handler, $arguments);
     }
 }
Exemplo n.º 3
0
 /**
  * Create a new server application instance.
  *
  * @param string $path The path to the application files.
  */
 public function __construct(string $path)
 {
     $this->path = $path;
     // Register some routes.
     $this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/', actions\IndexAction::class);
         $r->addRoute('GET', '/blog', actions\BlogAction::class);
         $r->addRoute('GET', '/category/{category}', actions\CategoryAction::class);
         $r->addRoute('GET', '/portfolio', actions\PortfolioAction::class);
         $r->addRoute('GET', '/{year:\\d{4}}/{month:\\d{2}}/{day:\\d{2}}/{name}', actions\ArticleAction::class);
         $r->addRoute('GET', '/{asset:(?:content|assets)/[^?]+}[?{query}]', actions\AssetAction::class);
         // Complicated feed routes :/
         $r->addRoute('GET', '/sitemap.xml', actions\SitemapAction::class);
         $r->addRoute('GET', '/feed[/{category:[a-z]+}]', actions\AtomFeedAction::class);
         $r->addRoute('GET', '/feed.atom', actions\AtomFeedAction::class);
         $r->addRoute('GET', '/feed/{category:[a-z]+}.atom', actions\AtomFeedAction::class);
         $r->addRoute('GET', '/feed.rss', actions\RssFeedAction::class);
         $r->addRoute('GET', '/feed/{category:[a-z]+}.rss', actions\RssFeedAction::class);
     });
     // Create the server object.
     $this->server = new Server(new RequestHandler($this));
     // Create some core services.
     $this->renderer = new Renderer($path . '/templates');
     $this->assetManager = new AssetManager($path . '/static');
     $this->articleStore = new ArticleStore($path . '/articles');
 }
Exemplo n.º 4
0
 public function setUp()
 {
     $router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/', ['TestApp\\Controller\\Index', 'index']);
     });
     $this->app = new App($router);
 }
Exemplo n.º 5
0
 public function setUp()
 {
     chdir(__DIR__ . '/app/');
     $this->router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/load', ['TestApp\\Controller\\Index', 'loadedParams'], ['name' => 'load']);
     });
 }
 /**
  * Invocation
  *
  * Register routes container and request attributes
  *
  * @param ServerRequestInterface $request Request
  * @param ResponseInterface $response Response
  * @param TornadoHttp $next Next Middleware - TornadoHttp container
  * @return ResponseInterface
  * @throws HttpMethodNotAllowedException
  * @throws HttpNotFoundException
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, TornadoHttp $next)
 {
     /** @var \FastRoute\Dispatcher\GroupCountBased $dispatcher */
     $dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $routeCollector) {
         foreach ($this->routes as $key => $route) {
             $routeCollector->addRoute($route['methods'], $route['path'], $key);
         }
     });
     $method = $request->getMethod();
     $uri = rawurldecode(parse_url($request->getUri(), \PHP_URL_PATH));
     $route = $dispatcher->dispatch($method, $uri);
     $handler = null;
     $vars = null;
     switch ($route[0]) {
         case Dispatcher::NOT_FOUND:
             throw new HttpNotFoundException('Inexistent route for the url ' . $request->getUri());
         case Dispatcher::METHOD_NOT_ALLOWED:
             throw new HttpMethodNotAllowedException('Method not allowed');
         case Dispatcher::FOUND:
             $handler = $route[1];
             $vars = $route[2];
             break;
     }
     $request = $request->withAttribute(Router::REGISTER_KEY, $handler);
     foreach ($vars as $name => $value) {
         $request = $request->withAttribute($name, $value);
     }
     return $next($request, $response);
 }
Exemplo n.º 7
0
 public function setUp()
 {
     $config = Loader::load();
     $config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/', ['TestApp\\Controller\\IndexController', 'index']);
     });
     $this->app = new App(Container\PHPDiFactory::buildContainer($config));
 }
Exemplo n.º 8
0
 public function __construct()
 {
     $this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/', 'IndexController');
         $r->addRoute('POST', '/', 'IndexController');
     });
     $this->request = Request::createFromGlobals();
 }
Exemplo n.º 9
0
 /**
  * Register necessary routes with route provider
  */
 protected function registerRoutes()
 {
     $this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $routeCollector) {
         $this->each(function (RouteRequest $routeRequest) use($routeCollector) {
             $routeCollector->addRoute($routeRequest->method, $routeRequest->route, $routeRequest->callable);
         });
     });
 }
 /**
  * Instantiates the router using the routes in the container.
  */
 private function createDispatcher()
 {
     $this->dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $collector) {
         foreach ($this->container->get('routes') as $route) {
             $collector->addRoute($route['method'], $route['pattern'], $route['service']);
         }
     });
 }
Exemplo n.º 11
0
 public function setUp()
 {
     $this->router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $manager) {
         $manager->addRoute('GET', '/', ['TestApp\\Controller\\IndexController', 'index'], ['name' => 'index']);
         $manager->addRoute('GET', '/fail', ['TestApp\\Controller\\IndexController', 'none'], ['name' => 'fail']);
         $manager->addRoute('GET', '/dummy', ['TestApp\\Controller\\IndexController', 'dummy'], ['name' => 'dummy']);
     });
 }
Exemplo n.º 12
0
 /**
  * @return Dispatcher
  */
 protected function dispatcher()
 {
     return FastRoute\simpleDispatcher(function (RouteCollector $collector) {
         foreach ($this->directory as $request => $action) {
             list($method, $path) = explode(' ', $request, 2);
             $collector->addRoute($method, $this->directory->prefix($path), $action);
         }
     });
 }
Exemplo n.º 13
0
 public function setUp()
 {
     chdir(__DIR__ . '/app/');
     $config = Loader::load();
     $config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/load', ['TestApp\\Controller\\IndexController', 'loadedParams'], ['name' => 'load']);
     });
     $this->container = Container\PHPDiFactory::buildContainer($config);
 }
Exemplo n.º 14
0
 public function setUp()
 {
     $config = Loader::load();
     $config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/', [get_class($this), 'index']);
     });
     $config['dispatcher'] = \Di\object('PennyTest\\Utils\\FastSymfonyDispatcher')->constructor(\Di\get('router'), \Di\get('di'));
     $this->app = new App(Container\PHPDiFactory::buildContainer($config));
 }
Exemplo n.º 15
0
 public function setUp()
 {
     $this->router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $router) {
         $router->addRoute('GET', '/', ['TestApp\\Controller\\Index', 'diTest']);
     });
     $builder = new ContainerBuilder();
     $builder->useAnnotations(true);
     $dic = $builder->build();
     $this->container = $dic;
 }
Exemplo n.º 16
0
 public function compile()
 {
     if (!$this->dispatcher) {
         $this->dispatcher = BaseFastRoute\simpleDispatcher(function (BaseFastRoute\RouteCollector $r) {
             foreach ($this->routes as $route) {
                 $r->addRoute($route[0], $route[1], $route[2]);
             }
         });
     }
 }
Exemplo n.º 17
0
 /**
  * @return Dispatcher
  */
 protected function dispatcher()
 {
     return FastRoute\simpleDispatcher(function (RouteCollector $collector) {
         foreach ($this->directory as $request => $action) {
             // 'GET /foo' becomes ['GET', '/foo']
             list($method, $path) = explode(' ', $request, 2);
             $collector->addRoute($method, $path, $action);
         }
     });
 }
 /**
  * @return Dispatcher
  */
 protected function getRouteDispatcher()
 {
     $dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $routes = Routes::$routeRegister;
         foreach ($routes as $route) {
             $r->addRoute($route->getRequestMethod(), $route->getPathPattern(), $route);
         }
     });
     return $dispatcher;
 }
Exemplo n.º 19
0
 /**
  * Add routes definition to collector and dispatch it
  * 
  * @param   array   $routes
  * @param   array   $routerOptions
  */
 public function __construct(array $routes, array $routerOptions = [])
 {
     $this->routes = $routes;
     $this->routerOptions = array_merge($this->routerOptions, $routerOptions);
     if (!$this->routerOptions['cacheDisabled']) {
         $this->dispatcher = cachedDispatcher($this->prepareRoutes(), $this->routerOptions);
     } else {
         $this->dispatcher = simpleDispatcher($this->prepareRoutes(), $this->routerOptions);
     }
 }
Exemplo n.º 20
0
 /**
  * Initialize a Router.
  *
  * Routes require a 'path' key and may have optional 'methods' and 'defaults' keys.
  *
  * @param array $routeData Array of routes
  */
 public function __construct(array $routeData)
 {
     $this->dispatcher = simpleDispatcher(function ($r) use($routeData) {
         foreach ($routeData as $data) {
             if (is_array($data)) {
                 $data = Route::fromArray($data);
             }
             $r->addRoute($data->getMethods(), $data->getPath(), $data);
         }
     });
 }
Exemplo n.º 21
0
 public function __construct(UserMapper $userMapper, Acl $acl, Directory $directory)
 {
     $this->userMapper = $userMapper;
     $this->acl = $acl;
     $this->directory = $directory;
     $this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $collector) {
         foreach ($this->directory as $request => $action) {
             list($method, $path) = explode(' ', $request, 2);
             $collector->addRoute($method, $path, $action);
         }
     });
 }
Exemplo n.º 22
0
 /**
  * Defines blog and page routes.
  *
  * @throws RuntimeException
  */
 protected function setRoutes()
 {
     if (empty($this->config['routes'])) {
         throw new RuntimeException('No routes defined in configuration file.');
     }
     $this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $collector) {
         foreach ($this->config['routes'] as $routeName => $route) {
             if (empty($route['method']) || empty($route['route']) || empty($route['action'])) {
                 throw new RuntimeException('Invalid route in configuration.');
             }
             $collector->addRoute($route['method'], $route['route'], $route['action']);
         }
     });
 }
Exemplo n.º 23
0
 /**
  * This method is not part of the public API. Implementations should use
  * Router::fromArray().
  * 
  * @param RouteInterface[] A collection of routes
  */
 public function __construct(array $routes)
 {
     $this->routes = array();
     $this->dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $collector) use($routes) {
         foreach ($routes as $route) {
             if (!$route instanceof RouteInterface) {
                 throw new \InvalidArgumentException('Routes array must contain only RouteInterface objects');
             }
             $key = spl_object_hash($route);
             $this->routes[$key] = $route;
             $collector->addRoute($route->getMethods(), $route->getPattern(), $key);
         }
     });
 }
Exemplo n.º 24
0
 public function route($url)
 {
     $routes = $this->_container->get('routes');
     \Debug::addRoutes($routes);
     $dispatcher = null;
     $callable = function (FastRoute\RouteCollector $r) use($routes) {
         foreach ($routes as $key => $value) {
             $methodsall = $value->getMethods();
             if (count($methodsall) == 0) {
                 $methodsall = array('GET', 'POST');
             }
             $chaine = $key . "::" . $value->getDefaults()['controller'] . "::" . $value->getDefaults()['action'];
             $r->addRoute($methodsall, $value->getPath(), $chaine);
         }
     };
     if (DEVELOPMENT_ENVIRONMENT) {
         $dispatcher = FastRoute\simpleDispatcher($callable);
     } else {
         $pathCache = CACHEPATH . DS . "route" . DS . "route.cache";
         $arrCache = array('cacheFile' => $pathCache, 'cacheDisabled' => false);
         $dispatcher = FastRoute\cachedDispatcher($callable, $arrCache);
     }
     $method = $this->_request->getMethod();
     if (false !== ($pos = strpos($url, '?'))) {
         $url = substr($url, 0, $pos);
     }
     $uri = rawurldecode($url);
     $routeInfo = $dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case FastRoute\Dispatcher::NOT_FOUND:
             $this->_route = "";
             break;
         case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
             $allowedMethods = $routeInfo[1];
             $this->_route = "";
             throw new \GL\Core\Exception\MethodNotAllowedException();
             break;
         case FastRoute\Dispatcher::FOUND:
             $handler = $routeInfo[1];
             $vars = $routeInfo[2];
             $params = explode("::", $handler);
             $this->_args = $vars;
             $this->_route = $params[0];
             $this->_controller = $params[1];
             $this->_method = $params[2];
             break;
     }
     return $this->_route != "";
 }
Exemplo n.º 25
0
 /**
  * Route-ok betöltése. A megfelelő tömb struktúra az osztály leírásában található.
  *
  * @param array $routes
  */
 public function setRoutes(array $routes)
 {
     $this->routes = $routes;
     $this->dispatcher = simpleDispatcher(function (RouteCollector $r) use($routes) {
         foreach ($routes as $route) {
             // Ha kötelező, akkor csak locale-os url-t készítünk.
             if (!isset($route["locale"]) || $route["locale"] == LanguageRouter::LOCALE_REQUIRED) {
                 $r->addRoute($route["method"], "/{locale:[a-z]+}" . $route["url"], $route["handler"]);
             } elseif ($route["locale"] == LanguageRouter::LOCALE_OPTIONAL) {
                 $r->addRoute($route["method"], $route["url"], $route["handler"]);
                 $r->addRoute($route["method"], "/{locale:[a-z]+}" . $route["url"], $route["handler"]);
             } elseif ($route["locale"] == LanguageRouter::LOCALE_NOT_ALLOWED) {
                 $r->addRoute($route["method"], $route["url"], $route["handler"]);
             }
         }
     });
 }
Exemplo n.º 26
0
 public function detectUri()
 {
     $returnObj = new \stdClass();
     $returnObj->fileArbo = '';
     $returnObj->nameCtr = '';
     $returnObj->nameMethode = '';
     $httpMethod = $_SERVER['REQUEST_METHOD'];
     $uriParse = parse_url($_SERVER['REQUEST_URI']);
     $request = rawurldecode($uriParse['path']);
     $routes = (require path . 'configs/bfw-fastroute/routes.php');
     $dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $router) use($routes) {
         foreach ($routes as $slug => $infos) {
             $slug = trim($slug);
             $method = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'];
             if (isset($infos['httpMethod'])) {
                 $method = $infos['httpMethod'];
             }
             $handler = $infos;
             unset($handler['httpMethod']);
             $router->addRoute($method, $slug, $handler);
         }
     });
     if ($request === '') {
         $request = '/';
     }
     $routeInfo = $dispatcher->dispatch($httpMethod, $request);
     $routeStatus = $routeInfo[0];
     $routeError = 0;
     if ($routeStatus === FastRoute\Dispatcher::METHOD_NOT_ALLOWED) {
         $routeError = 405;
     }
     if ($routeStatus !== FastRoute\Dispatcher::FOUND) {
         $routeError = 404;
     }
     if ($routeError !== 0) {
         ErrorView($routeError, true);
         //exit doing in ErrorView
     }
     $fastRouteCallback = $this->config->routeCallback;
     if (isset($fastRouteCallback) && is_callable($fastRouteCallback)) {
         $fastRouteCallback($returnObj, $routeInfo[1]);
     }
     $this->getArgs = $routeInfo[2];
     return $returnObj;
 }
Exemplo n.º 27
0
 public function setUp()
 {
     $router = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
         $r->addRoute('GET', '/', ['TestApp\\Controller\\Index', 'index']);
         $r->addRoute('GET', '/{id:\\d+}', ['TestApp\\Controller\\Index', 'getSingle']);
         $r->addRoute('GET', '/fail', ['TestApp\\Controller\\Index', 'failed']);
         $r->addRoute('GET', '/dummy', ['TestApp\\Controller\\Index', 'dummy']);
     });
     $this->app = new App($router);
     $this->app->getContainer()->get('event_manager')->attach('ERROR_DISPATCH', function ($e) {
         if ($e->getException() instanceof RouteNotFound) {
             $response = $e->getResponse()->withStatus(404);
             $e->setResponse($response);
         }
         if (405 == $e->getException() instanceof MethodNotAllowed) {
             $response = $e->getResponse()->withStatus(405);
             $e->setResponse($response);
         }
     });
 }
Exemplo n.º 28
0
 /**
  * Get routes from dispatcher.
  *
  * @return mixed
  */
 public function getDispatcher()
 {
     $dispatcher = FastRoute\simpleDispatcher(function (RouteCollector $route) {
         // Added all routes
         foreach ($this->routes as $param) {
             // Check method is defined
             if (count($param[2]) == 2) {
                 $route->addRoute($param[0], $param[1], $param[2]);
             } elseif (isset($param[2][0])) {
                 // Create object reflection
                 $obj = new ReflectionClass($param[2][0]);
                 // Check object implements restful interface
                 if ($obj->implementsInterface(RestfulControllerInterface::class)) {
                     // Get restful methods
                     $this->addRestfulMethods($route, $param);
                 }
             }
         }
     });
     return $dispatcher;
 }
Exemplo n.º 29
0
 public function __construct(Injector $injector, Session $session, array $routes)
 {
     $this->injector = $injector;
     $this->session = $session;
     $this->dispatcher = simpleDispatcher(function (RouteCollector $r) use($routes) {
         foreach ($routes as $i => $route) {
             if (!isset($route['method'], $route['pattern'])) {
                 throw new InvalidRouteException('Invalid route at index ' . $i . ': method and pattern required');
             }
             if (isset($route['target_builder'])) {
                 $r->addRoute($route['method'], $route['pattern'], $route['target_builder']);
             } else {
                 if (isset($route['target_class'], $route['target_method'])) {
                     $r->addRoute($route['method'], $route['pattern'], ['class' => $route['target_class'], 'method' => $route['target_method']]);
                 } else {
                     throw new InvalidRouteException('Invalid route at index ' . $i . ': no valid target');
                 }
             }
         }
     });
 }
Exemplo n.º 30
0
 /**
  * @param $method
  * @param $uri
  * @return array
  * @throws HttpNotFoundException
  */
 public function findRoute($method, $uri)
 {
     $routes = $this->getRoutes();
     //Hozzá kell még adni a nyelvi url-t, ha van.
     $translator = Application::Translator();
     if ($translator != null) {
         $langRoute = $translator->getCookieSetUrl();
         if ($langRoute != null) {
             $routes["langSetRoute"] = ["method" => $langRoute["method"], "url" => $langRoute["url"], "handler" => $langRoute["handler"]];
         }
     }
     $this->dispatcher = simpleDispatcher(function (RouteCollector $r) use($routes) {
         foreach ($routes as $route) {
             $r->addRoute($route["method"], $route["url"], $route["handler"]);
         }
     });
     // Strip query string (?foo=bar) and decode URI
     if (false !== ($pos = strpos($uri, '?'))) {
         $uri = substr($uri, 0, $pos);
     }
     $uri = rawurldecode($uri);
     $routeInfo = $this->dispatcher->dispatch($method, $uri);
     switch ($routeInfo[0]) {
         case RouterInterface::NOT_FOUND:
             throw new HttpNotFoundException();
         case RouterInterface::METHOD_NOT_ALLOWED:
             throw new HttpNotFoundException();
         case RouterInterface::FOUND:
             foreach ($routes as $route) {
                 if ($route["handler"] == $routeInfo[1]) {
                     $this->actualRoute = $route;
                     break;
                 }
             }
             return ["handler" => $routeInfo[1], "params" => $routeInfo[2]];
         default:
             return [];
     }
 }