Example #1
0
 public static function notAuthorizedRoutesFunction(\FastRoute\RouteCollector $r)
 {
     $r->addRoute('GET', Route::oauthStart(), 'GithubExample\\Controller\\Controller::oauthStart');
     $r->addRoute('GET', Route::oauthConfirm(), 'GithubExample\\Controller\\Controller::oauthConfirm');
     $r->addRoute('GET', Route::oauthReturn(), 'GithubExample\\Controller\\Controller::oauthReturn');
     self::commonRoutesFunction($r);
 }
Example #2
0
 public static function routesFunction(\FastRoute\RouteCollector $r)
 {
     $r->addRoute('GET', '/', ['TierJigSkeleton\\Controller\\Basic', 'index']);
     $r->addRoute('GET', '/notepad', ['TierJigSkeleton\\Controller\\Basic', 'notepad']);
     $r->addRoute('GET', '/login', ['TierJigSkeleton\\Controller\\Basic', 'login']);
     $r->addRoute('POST', '/login', ['TierJigSkeleton\\Controller\\Basic', 'loginProcess']);
     $r->addRoute('POST', '/testBadMethod', ['TierJigSkeleton\\Controller\\Index', 'testBadMethod']);
 }
Example #3
0
 /**
  * @inheritDoc
  */
 public function parse(array $routes) : array
 {
     /** @var \Venta\Contracts\Routing\Route $route */
     foreach ($routes as $route) {
         $this->collector->addRoute($route->getMethods(), $route->getPath(), $route);
     }
     return $this->collector->getData();
 }
Example #4
0
 /**
  * Add a route to the collection.
  *
  * Uses the HTTP methods associated (creating sane defaults for an empty
  * list or Route::HTTP_METHOD_ANY) and the path, and uses the path as
  * the name (to allow later lookup of the middleware).
  *
  * @param Route $route
  */
 public function addRoute(Route $route)
 {
     $methods = $route->getAllowedMethods();
     if ($methods === Route::HTTP_METHOD_ANY) {
         $methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE'];
     }
     if (empty($methods)) {
         $methods = ['GET', 'HEAD', 'OPTIONS'];
     }
     $this->router->addRoute($methods, $route->getPath(), $route->getPath());
     $this->routes[$route->getName()] = $route;
 }
 public function __invoke(RouteCollector $router)
 {
     $routes = $this->collection->all();
     foreach ($routes as $name => $route) {
         $router->addRoute($route->getMethods(), $route->getPath(), $name);
     }
 }
Example #6
0
 /**
  * @param string|string[] $methods
  * @param string          $path
  * @param callable        $handler
  * @return Route
  */
 public function route($methods, $path, $handler)
 {
     $route = $this->routeMap->add($path, $handler);
     $this->collector->addRoute($methods, $path, $route);
     $this->middleware[] = $route;
     return $route;
 }
Example #7
0
 protected function buildRules()
 {
     $this->routeCollector = new RouteCollector($this->getParser(), new GroupCountBased());
     foreach ($this->rules as $rule) {
         $this->routeCollector->addRoute($rule->getMethod(), $rule->getPattern(), $rule->getRoute());
     }
 }
Example #8
0
 function it_parses_routes(RouteCollector $collector, Route $route)
 {
     $route->getMethods()->willReturn(['GET']);
     $route->getPath()->willReturn('/url');
     $collector->addRoute(['GET'], '/url', $route)->shouldBeCalled();
     $collector->getData()->willReturn(['parse result']);
     $this->parse([$route])->shouldBe(['parse result']);
 }
Example #9
0
 /**
  * Added restful routes.
  *
  * @param RouteCollector $route
  * @param array $param
  */
 private function addRestfulMethods($route, $param)
 {
     $uri = $param[1];
     $class = $param[2][0];
     $methods = ['index' => ['GET', $uri], 'read' => ['GET', $uri . '/{id}'], 'create' => ['POST', $uri], 'update' => ['PUT', $uri . '/{id}'], 'delete' => ['DELETE', $uri . '/{id}']];
     foreach ($methods as $key => $method) {
         $route->addRoute($method[0], $method[1], [$class, $key]);
     }
 }
Example #10
0
 public function init()
 {
     $collector = new RouteCollector(new StdRouteParser(), new GroupCountBasedDataGenerator());
     foreach ($this->routes as $route) {
         foreach ($route->definition($this->container) as list($method, $route_name, $handler_name)) {
             $collector->addRoute($method, $route_name, $handler_name);
         }
     }
     $this->dispatcher = new GroupCountBasedDispatcher($collector->getData());
 }
 /**
  * @param \ReflectionMethod $method
  * @param string $controllerPath
  * @throws RouterConfigurationException
  */
 private function configureControllerMethod(\ReflectionMethod $method, $controllerPath)
 {
     /** @var HttpVerb $httpVerbAnnotation */
     $httpVerbAnnotation = null;
     $annotations = Annotations::ofMethod($method);
     foreach ($annotations as $annotation) {
         if (in_array(get_class($annotation), $this->httpVerbsAnnotations)) {
             $httpVerbAnnotation = $annotation;
             break;
         }
     }
     if ($httpVerbAnnotation === null) {
         return;
     }
     $methodPath = $httpVerbAnnotation->path;
     $fullPath = $this->basePath . $controllerPath . $methodPath;
     $methodFullName = $method->getDeclaringClass()->getName() . '::' . $method->getName();
     $httpVerb = $this->httpVerbsByAnnotations[get_class($httpVerbAnnotation)];
     $this->routeCollector->addRoute($httpVerb, $fullPath, $methodFullName);
 }
Example #12
0
 /**
  * @param array $methods
  * @param string $path
  * @param string $className
  * @param string $methodName
  * @param array|string $middlewares
  * @return \Wandu\Router\Route
  */
 public function createRoute(array $methods, $path, $className, $methodName = 'index', array $middlewares = [])
 {
     $path = '/' . trim($this->prefix . $path, '/');
     if (!is_array($middlewares)) {
         $middlewares = [$middlewares];
     }
     $middlewares = array_merge($this->middlewares, $middlewares);
     $handler = implode(',', $methods) . $path;
     $this->collector->addRoute($methods, $path, $handler);
     return $this->routes[$handler] = new Route($className, $methodName, $middlewares);
 }
Example #13
0
 private function addRoutes(FastRoute\RouteCollector $route)
 {
     foreach ($this->routes as $definedRoute) {
         $definedRoute[3] = isset($definedRoute[3]) ? $definedRoute[3] : $this->defaultReturnType;
         $route->addRoute(strtoupper($definedRoute[0]), $definedRoute[1], function ($args) use($definedRoute) {
             $returnType = $definedRoute[3];
             $controller = $definedRoute[2];
             list($definedRoute, $action) = explode("/", $controller);
             return ['definedRoute' => $definedRoute, 'action' => $action, 'returnType' => $returnType, 'args' => $args];
         });
     }
 }
/**
 * Add route to route collector
 *
 * @param string                   $swaggerJson     Swagger json file path (can be an URL)
 * @param RouteCollector           $routeCollector  FastRoute route collector
 * @param OperationParserInterface $operationParser Swagger operation parser.
 * @param array                    $options         Options (@see config/swaggerConfig.dist.php)
 */
function addRoutes($swaggerJson, RouteCollector $routeCollector, OperationParserInterface $operationParser, $options = [])
{
    if (!isset($options['routeFile']) || !file_exists($options['routeFile'])) {
        $routes = scan($swaggerJson, $operationParser);
    } else {
        $routes = (require $options['routeFile']);
    }
    if (isset($options['routeFile']) && isset($options['cacheEnabled']) && $options['cacheEnabled']) {
        cacheRoutes($routes, $options['routeFile']);
    }
    foreach ($routes as $route) {
        $routeCollector->addRoute($route['method'], $route['uri'], $route['action']);
    }
}
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function addRoute($methods, $path, $controller, $name = null, array $options = [])
 {
     if ($this->isCaching()) {
         return null;
     }
     $methods = (array) $methods;
     $path = $this->makePath($path);
     $route = $this->createRoute($methods, $path, $controller, $name, $options);
     $this->routes->attach($route);
     if ($name) {
         $this->addNamedRoute($name, $route);
     }
     $this->routeCollector->addRoute($route->getMethods(), $path, $route);
     return $route;
 }
Example #16
0
 /**
  * Add a route to the collection
  *
  * @param  string          $method
  * @param  string          $route
  * @param  string|\Closure $handler
  * @param  integer         $strategy
  * @return \Orno\Route\RouteCollection
  */
 public function addRoute($method, $route, $handler, $strategy = self::REQUEST_RESPONSE_STRATEGY)
 {
     // are we running a single strategy for the collection?
     $strategy = isset($this->strategy) ? $this->strategy : $strategy;
     // if the handler is an anonymous function, we need to store it for later use
     // by the dispatcher, otherwise we just throw the handler string at FastRoute
     if ($handler instanceof \Closure) {
         $callback = $handler;
         $handler = uniqid('orno::route::', true);
         $this->routes[$handler]['callback'] = $callback;
     }
     $this->routes[$handler]['strategy'] = $strategy;
     $route = $this->parseRouteString($route);
     parent::addRoute($method, $route, $handler);
     return $this;
 }
Example #17
0
 /**
  * Add a route to the collection
  *
  * @param  string|string[]                          $method
  * @param  string                                   $route
  * @param  string|\Closure                          $handler
  * @param  \League\Route\Strategy\StrategyInterface $strategy
  * @return \League\Route\RouteCollection
  */
 public function addRoute($method, $route, $handler, Strategy\StrategyInterface $strategy = null)
 {
     // are we running a single strategy for the collection?
     $strategy = !is_null($this->strategy) ? $this->strategy : $strategy;
     // if the handler is an anonymous function, we need to store it for later use
     // by the dispatcher, otherwise we just throw the handler string at FastRoute
     if (!is_string($handler) && is_callable($handler)) {
         $callback = $handler;
         $handler = uniqid('league::route::', true);
         $this->routes[$handler]['callback'] = $callback;
     } elseif (is_object($handler)) {
         throw new \RuntimeException('Object controllers must be callable.');
     }
     $this->routes[$handler]['strategy'] = is_null($strategy) ? new Strategy\RequestResponseStrategy() : $strategy;
     $route = $this->parseRouteString($route);
     parent::addRoute($method, $route, $handler);
     return $this;
 }
Example #18
0
 public function addRoute($httpMethod, $route, $handler)
 {
     if ($httpMethod == 'REST') {
         // Make REST methods available
         // TODO: find a better way to do this witout duplicates with an appended slash character
         parent::addRoute('GET', $route[0], array($handler, 'index'));
         parent::addRoute('GET', $route[0] . '/', array($handler, 'index'));
         parent::addRoute('POST', $route[0] . '', array($handler, 'create'));
         parent::addRoute('POST', $route[0] . '/', array($handler, 'create'));
         parent::addRoute('GET', $route[0] . '/' . $route[1], array($handler, 'show'));
         parent::addRoute('GET', $route[0] . '/' . $route[1] . '/', array($handler, 'show'));
         parent::addRoute('PUT', $route[0] . '/' . $route[1], array($handler, 'edit'));
         parent::addRoute('PUT', $route[0] . '/' . $route[1] . '/', array($handler, 'edit'));
         parent::addRoute('PATCH', $route[0] . '/' . $route[1], array($handler, 'edit'));
         parent::addRoute('PATCH', $route[0] . '/' . $route[1] . '/', array($handler, 'edit'));
         parent::addRoute('DELETE', $route[0] . '/' . $route[1], array($handler, 'delete'));
         parent::addRoute('DELETE', $route[0] . '/' . $route[1] . '/', array($handler, 'delete'));
         return;
     }
     parent::addRoute($httpMethod, $route, $handler);
 }
 /**
  * Add trailing slash route or add route without trailing slash so both work
  * @param string $httpMethod 
  * @param string $route 
  * @param RouteHandler $handler 
  */
 public function addRoute($httpMethod, $route, $handler)
 {
     parent::addRoute($httpMethod, $route, $handler);
     // Route ended with slash , redirect non slash to slash
     if (substr($route, -1) === '/') {
         parent::addRoute($httpMethod, substr($route, 0, -1), new RouteHandler(function () {
             $url = tiga_url($this->request->getPathInfo() . "/");
             $response = \Tiga\Framework\Response\ResponseFactory::redirect($url);
             $response->sendHeaders();
             die;
         }));
     } else {
         $route .= "/";
         parent::addRoute($httpMethod, $route, new RouteHandler(function () {
             $url = rtrim(tiga_url($this->request->getPathInfo()), '/');
             $response = \Tiga\Framework\Response\ResponseFactory::redirect($url);
             $response->sendHeaders();
             die;
         }));
     }
 }
Example #20
0
 protected function getCollector()
 {
     $collector = new RouteCollector(new Std(), new GroupCountBased2());
     $this->routesIndex = [];
     foreach ($this->routes->getGroups() as $group) {
         foreach ($group->getRoutes() as $route) {
             $uri = $route->getRoute();
             $key = implode('_', (array) $route->getHttpMethod()) . '_' . $uri;
             if (!isset($this->routesIndex[$key])) {
                 $collector->addRoute($route->getHttpMethod(), $uri, $key);
             }
             $this->routesIndex[$key][] = $route;
         }
     }
     foreach ($this->routes->getRoutes() as $route) {
         $uri = $route->getRoute();
         $key = implode('_', (array) $route->getHttpMethod()) . '_' . $uri;
         if (!isset($this->routesIndex[$key])) {
             $collector->addRoute($route->getHttpMethod(), $uri, $key);
         }
         $this->routesIndex[$key][] = $route;
     }
     return $collector;
 }
Example #21
0
 /**
  * @param string|string[] $methods
  * @param string $path
  * @param callable $handler
  */
 public function route($methods, $path, $handler)
 {
     $this->collector->addRoute($methods, $path, $handler);
 }
Example #22
0
<?php

use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std as RouteParser;
use FastRoute\DataGenerator\GroupCountBased as RouteGenerator;
use FastRoute\Dispatcher\GroupCountBased as Dispatcher;
$r = new RouteCollector(new RouteParser(), new RouteGenerator());
$r->addRoute('GET', '/', ['ACME\\Controller\\Index', 'index']);
$r->addRoute('GET', '/about[/{name}]', ['ACME\\Controller\\Index', 'about']);
return new Dispatcher($r->getData());
Example #23
0
 public function it_should_able_to_run(ContainerInterface $container, RouteCollector $collector, ServerInterface $server, EventDispatcherInterface $event)
 {
     /**
      * predictions
      */
     $container->offsetSet('Middlewares', [])->shouldBeCalled();
     $container->offsetSet('ServiceProviders', [])->shouldBeCalled();
     $container->offsetSet('Routes', [])->shouldBeCalled();
     $collector->addRoute('GET', '/', 'test')->shouldBeCalled();
     $server->run()->shouldBeCalled();
     /**
      * mocks
      */
     $container->offsetGet('Routes')->willReturn([['method' => 'GET', 'route' => '/', 'handler' => 'test']]);
     $container->offsetGet('Server')->willReturn($server);
     $container->offsetGet('RouteCollector')->willReturn($collector);
     $container->offsetGet('EventDispatcher')->willReturn($event);
     /**
      * test method
      */
     $this->run();
 }
Example #24
0
 /**
  * Collect routes from map.
  *
  * @param RouteCollector $collector
  */
 public function map(RouteCollector $collector)
 {
     foreach ($this->getRoutes() as $row) {
         $collector->addRoute($row[0], $row[1], $row[2]);
     }
 }
Example #25
0
function routesFunction(\FastRoute\RouteCollector $r)
{
    $categories = '{category:Imagick|ImagickDraw|ImagickPixel|ImagickPixelIterator|ImagickKernel|Tutorial}';
    //Category indices
    $r->addRoute('GET', "/{$categories}", ['ImagickDemo\\Controller\\Page', 'renderCategoryIndex']);
    //Category + example
    $r->addRoute('GET', "/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Page', 'renderExamplePage']);
    //Images
    $r->addRoute('GET', "/imageStatus/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Image', 'getImageJobStatus']);
    //Images
    $r->addRoute('GET', "/image/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Image', 'getImageResponse']);
    //Original image
    $r->addRoute('GET', "/imageOriginal/{$categories}/{example:[a-zA-Z]+}", ['ImagickDemo\\Controller\\Image', 'getOriginalImage']);
    //Custom images
    $r->addRoute('GET', "/customImage/{$categories}/{example:[a-zA-Z]*}", ['ImagickDemo\\Controller\\Image', 'getCustomImageResponse']);
    $r->addRoute('GET', '/info', ['ImagickDemo\\Controller\\ServerInfo', 'createResponse']);
    $r->addRoute('GET', '/queueinfo', ['ImagickDemo\\Controller\\QueueInfo', 'createResponse']);
    $r->addRoute('GET', '/queuedelete', ['ImagickDemo\\Controller\\QueueInfo', 'deleteQueue']);
    $r->addRoute('GET', '/opinfo', ['ImagickDemo\\Controller\\ServerInfo', 'renderOPCacheInfo']);
    $r->addRoute('GET', '/settingsCheck', ['ImagickDemo\\Controller\\ServerInfo', 'serverSettings']);
    $r->addRoute('GET', '/', ['ImagickDemo\\Controller\\Page', 'renderTitlePage']);
    $r->addRoute('GET', "/css/{cssInclude}", ['ScriptServer\\Controller\\ScriptServer', 'getPackedCSS']);
    $r->addRoute('GET', '/js/{jsInclude}', ['ScriptServer\\Controller\\ScriptServer', 'getPackedJavascript']);
}
Example #26
0
function routesFunction(\FastRoute\RouteCollector $r)
{
    $r->addRoute('GET', '/introduction', ['TierTest\\Controller\\BasicController', 'introduction']);
}
 public function addParser(string $method, string $path, string $httpRequestParser) : ApplicationServiceProvider
 {
     $this->routeCollector->addRoute($method, $this->routeRoot . $path, $httpRequestParser);
     return $this;
 }