Exemple #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);
 }
 public function __invoke(RouteCollector $router)
 {
     $routes = $this->collection->all();
     foreach ($routes as $name => $route) {
         $router->addRoute($route->getMethods(), $route->getPath(), $name);
     }
 }
Exemple #3
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']);
 }
Exemple #4
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();
 }
Exemple #5
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]);
     }
 }
Exemple #6
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());
 }
Exemple #7
0
 /**
  * Returns a listing of routes available.
  *
  * @return callable
  */
 public function getRoutes()
 {
     $routes = array_merge($this->routes, $this->collector->getData());
     return function (RouteCollector $collector) use($routes) {
         foreach ($routes as $route) {
             if (!empty($route)) {
                 $collector->addRoute($route[0], $route[1], $route[2]);
             }
         }
     };
 }
Exemple #8
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']);
    }
}
Exemple #10
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());
     }
 }
 /**
  * Create new router
  *
  * @param RouteParser   $parser
  * @param DataGenerator $generator
  */
 public function __construct(RouteParser $parser = null, DataGenerator $generator = null)
 {
     $parser = $parser ? $parser : new StdParser();
     $generator = $generator ? $generator : new GroupCountBasedGenerator();
     parent::__construct($parser, $generator);
     $this->routeParser = $parser;
 }
Exemple #12
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;
 }
 /**
  * Constructor.
  *
  * @param \Interop\Container\ContainerInterface $container
  * @param \FastRoute\RouteParser                $parser
  * @param \FastRoute\DataGenerator              $generator
  */
 public function __construct(ContainerInterface $container = null, RouteParser $parser = null, DataGenerator $generator = null)
 {
     $this->container = $container instanceof ContainerInterface ? $container : new Container();
     // build parent route collector
     $parser = $parser instanceof RouteParser ? $parser : new StdRouteParser();
     $generator = $generator instanceof DataGenerator ? $generator : new GroupCountBasedDataGenerator();
     parent::__construct($parser, $generator);
 }
Exemple #14
0
 protected function generateDispatchData()
 {
     $data = $this->routeCollector->getData();
     if ($this->cachePath !== null) {
         $filesys = new LockingFilesystem();
         $php = '<?php return ' . var_export($data, true) . ";\n";
         $filesys->write($this->cachePath, $php);
     }
     return $data;
 }
Exemple #15
0
 /**
  * @param  Request $request
  * @return RouteResult
  */
 public function match(Request $request)
 {
     $path = $request->getUri()->getPath();
     $method = $request->getMethod();
     $dispatcher = $this->getDispatcher($this->router->getData());
     $result = $dispatcher->dispatch($method, $path);
     if ($result[0] != Dispatcher::FOUND) {
         return $this->marshalFailedRoute($result);
     }
     return $this->marshalMatchedRoute($result, $method);
 }
Exemple #16
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);
 }
 /**
  * @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);
 }
 /**
  * Inject a Route instance into the underlying router.
  *
  * @param Route $route
  */
 private function injectRoute(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;
 }
Exemple #19
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']);
 }
Exemple #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;
 }
Exemple #21
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;
 }
Exemple #22
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;
 }
 /**
  * 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;
         }));
     }
 }
 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);
 }
Exemple #25
0
 /**
  * @param string|string[] $methods
  * @param string $path
  * @param callable $handler
  */
 public function route($methods, $path, $handler)
 {
     $this->collector->addRoute($methods, $path, $handler);
 }
 /** {@inheritdoc} */
 public function getHttpRequestParser() : HttpRequestParserInterface
 {
     return $this->router->setRouter(new GroupCountBasedDispatcher($this->routeCollector->getData()));
 }
Exemple #27
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]);
     }
 }
 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();
 }
Exemple #29
0
function routesFunction(\FastRoute\RouteCollector $r)
{
    $r->addRoute('GET', '/introduction', ['TierTest\\Controller\\BasicController', 'introduction']);
}
Exemple #30
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']);
}