D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
local
/
psa
/
admin
/
plib
/
modules
/
rest-api
/
vendor
/
slim
/
slim
/
Slim
/
Routing
/
Filename :
Dispatcher.php
back
Copy
<?php declare (strict_types=1); namespace PleskRestApi\Slim\Routing; use PleskRestApi\FastRoute\DataGenerator\GroupCountBased; use PleskRestApi\FastRoute\RouteCollector as FastRouteCollector; use PleskRestApi\FastRoute\RouteParser\Std; use PleskRestApi\Slim\Interfaces\DispatcherInterface; use PleskRestApi\Slim\Interfaces\RouteCollectorInterface; class Dispatcher implements DispatcherInterface { private RouteCollectorInterface $routeCollector; private ?FastRouteDispatcher $dispatcher = null; public function __construct(RouteCollectorInterface $routeCollector) { $this->routeCollector = $routeCollector; } protected function createDispatcher() : FastRouteDispatcher { if ($this->dispatcher) { return $this->dispatcher; } $routeDefinitionCallback = function (FastRouteCollector $r) : void { $basePath = $this->routeCollector->getBasePath(); foreach ($this->routeCollector->getRoutes() as $route) { $r->addRoute($route->getMethods(), $basePath . $route->getPattern(), $route->getIdentifier()); } }; $cacheFile = $this->routeCollector->getCacheFile(); if ($cacheFile) { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = \PleskRestApi\FastRoute\cachedDispatcher($routeDefinitionCallback, ['dataGenerator' => GroupCountBased::class, 'dispatcher' => FastRouteDispatcher::class, 'routeParser' => new Std(), 'cacheFile' => $cacheFile]); } else { /** @var FastRouteDispatcher $dispatcher */ $dispatcher = \PleskRestApi\FastRoute\simpleDispatcher($routeDefinitionCallback, ['dataGenerator' => GroupCountBased::class, 'dispatcher' => FastRouteDispatcher::class, 'routeParser' => new Std()]); } $this->dispatcher = $dispatcher; return $this->dispatcher; } /** * {@inheritdoc} */ public function dispatch(string $method, string $uri) : RoutingResults { $dispatcher = $this->createDispatcher(); $results = $dispatcher->dispatch($method, $uri); return new RoutingResults($this, $method, $uri, $results[0], $results[1], $results[2]); } /** * {@inheritdoc} */ public function getAllowedMethods(string $uri) : array { $dispatcher = $this->createDispatcher(); return $dispatcher->getAllowedMethods($uri); } }