Switch to PSR based host detection.
This commit is contained in:
parent
2280546c05
commit
826f66a6fb
3 changed files with 62 additions and 46 deletions
46
src/App.php
46
src/App.php
|
|
@ -35,8 +35,10 @@ use Monolog\Logger;
|
|||
use Monolog\Processor\PsrLogMessageProcessor;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim;
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Factory\ServerRequestCreatorFactory;
|
||||
use Symfony\Bridge\Twig\Extension as SymfonyTwigExtensions;
|
||||
use Symfony\Component\Filesystem\Exception\IOException;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
|
@ -76,7 +78,7 @@ class App
|
|||
AppFactory::setContainer($container);
|
||||
|
||||
// If we're not on the CLI and Sessions ARE enabled...
|
||||
if ('cli' != php_sapi_name() && $this->isSessionsEnabled) {
|
||||
if ('cli' !== php_sapi_name() && $this->isSessionsEnabled) {
|
||||
// Call SessionService out of the container to force initialise it
|
||||
$container->get(SessionService::class);
|
||||
}
|
||||
|
|
@ -396,7 +398,22 @@ class App
|
|||
;
|
||||
}
|
||||
|
||||
public function loadAllRoutes()
|
||||
public function runHttp(): void
|
||||
{
|
||||
$serverRequestCreator = ServerRequestCreatorFactory::create();
|
||||
$request = $serverRequestCreator->createServerRequestFromGlobals();
|
||||
|
||||
$this->loadAllRoutes($request);
|
||||
|
||||
$this->debugBar['time']->startMeasure('runHTTP', 'HTTP runtime');
|
||||
$this->app->run($request);
|
||||
|
||||
if ($this->debugBar['time']->hasStartedMeasure('runHTTP')) {
|
||||
$this->debugBar['time']->stopMeasure('runHTTP');
|
||||
}
|
||||
}
|
||||
|
||||
protected function loadAllRoutes(ServerRequestInterface $request): self
|
||||
{
|
||||
$this->debugBar['time']->startMeasure('interrogateControllers', 'Time to interrogate controllers for routes');
|
||||
$this->interrogateControllers();
|
||||
|
|
@ -404,22 +421,11 @@ class App
|
|||
|
||||
$this->logger->debug(sprintf('Bootstrap complete in %sms', number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2)));
|
||||
|
||||
$this->router->populateRoutes($this->getApp());
|
||||
$this->router->populateRoutes($this->getApp(), $request);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function runHttp(): void
|
||||
{
|
||||
$this->loadAllRoutes();
|
||||
$this->debugBar['time']->startMeasure('runHTTP', 'HTTP runtime');
|
||||
$this->app->run();
|
||||
|
||||
if ($this->debugBar['time']->hasStartedMeasure('runHTTP')) {
|
||||
$this->debugBar['time']->stopMeasure('runHTTP');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
|
|
@ -483,7 +489,7 @@ class App
|
|||
return;
|
||||
}
|
||||
foreach (new \DirectoryIterator($stringPath) as $translationFile) {
|
||||
if ('yaml' == $translationFile->getExtension()) {
|
||||
if ('yaml' === $translationFile->getExtension()) {
|
||||
$languageName = substr($translationFile->getBasename(), 0, -5);
|
||||
$this->addSupportedLanguage($languageName);
|
||||
}
|
||||
|
|
@ -498,13 +504,13 @@ class App
|
|||
$this->interrogateControllersComplete = true;
|
||||
|
||||
if ($this->environmentService->has('ROUTE_CACHE')
|
||||
&& 'on' == strtolower($this->environmentService->get('ROUTE_CACHE'))
|
||||
&& 'on' === strtolower($this->environmentService->get('ROUTE_CACHE'))
|
||||
&& $this->router->loadCache()
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$appClass = new \ReflectionClass(get_called_class());
|
||||
$appClass = new \ReflectionClass(static::class);
|
||||
$this->router->loadRoutesFromAnnotations(
|
||||
[
|
||||
APP_ROOT.'/src/Controllers',
|
||||
|
|
@ -512,10 +518,8 @@ class App
|
|||
$appClass->getNamespaceName()
|
||||
);
|
||||
|
||||
$this->router
|
||||
->weighRoutes()
|
||||
->cache()
|
||||
;
|
||||
// Cache routes before weighing.
|
||||
$this->router->cache();
|
||||
|
||||
$this->logger->debug(sprintf(
|
||||
'ROUTE_CACHE miss. Perhaps enable ROUTE_CACHE envvar.'
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace Benzine\Router;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim\App;
|
||||
|
||||
class Route
|
||||
|
|
@ -401,10 +402,14 @@ class Route
|
|||
return count($this->validDomains) > 0;
|
||||
}
|
||||
|
||||
public function isInContainedInValidDomains(): bool
|
||||
public function isInContainedInValidDomains(string $host = null): bool
|
||||
{
|
||||
if (null === $host) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->validDomains as $validDomain) {
|
||||
if (isset($_SERVER['HTTP_HOST']) && fnmatch($validDomain, $_SERVER['HTTP_HOST'])) {
|
||||
if (fnmatch($validDomain, $host)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use Cache\Adapter\Chain\CachePoolChain;
|
|||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||
use Monolog\Logger;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim\App;
|
||||
|
||||
class Router
|
||||
|
|
@ -91,34 +92,18 @@ class Router
|
|||
}
|
||||
}
|
||||
|
||||
public function weighRoutes(): Router
|
||||
{
|
||||
$allocatedRoutes = [];
|
||||
if (is_array($this->routes) && count($this->routes) > 0) {
|
||||
uasort($this->routes, function (Route $a, Route $b) {
|
||||
return $a->getWeight() > $b->getWeight();
|
||||
});
|
||||
|
||||
foreach ($this->routes as $index => $route) {
|
||||
if (($route->isInContainedInValidDomains() || !$route->hasValidDomains())
|
||||
&& !isset($allocatedRoutes[$route->getHttpMethod().$route->getRouterPattern()])) {
|
||||
$allocatedRoutes[$route->getHttpMethod().$route->getRouterPattern()] = true;
|
||||
} else {
|
||||
unset($this->routes[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function populateRoutes(App $app)
|
||||
public function populateRoutes(App $app, ServerRequestInterface $request = null): App
|
||||
{
|
||||
if ($this->routesArePopulated) {
|
||||
return $app;
|
||||
}
|
||||
|
||||
$this->weighRoutes();
|
||||
$host = (null !== $request)
|
||||
? $request->getUri()->getHost()
|
||||
: null;
|
||||
|
||||
$this->weighRoutes($host);
|
||||
|
||||
if (count($this->routes) > 0) {
|
||||
foreach ($this->getRoutes() as $route) {
|
||||
$app = $route->populateRoute($app);
|
||||
|
|
@ -130,6 +115,27 @@ class Router
|
|||
return $app;
|
||||
}
|
||||
|
||||
protected function weighRoutes(string $host = null): self
|
||||
{
|
||||
$allocatedRoutes = [];
|
||||
if (is_array($this->routes) && count($this->routes) > 0) {
|
||||
uasort($this->routes, function (Route $a, Route $b) {
|
||||
return $a->getWeight() > $b->getWeight();
|
||||
});
|
||||
|
||||
foreach ($this->routes as $index => $route) {
|
||||
$routeKey = $route->getHttpMethod().$route->getRouterPattern();
|
||||
if (!isset($allocatedRoutes[$routeKey]) && ($route->isInContainedInValidDomains($host) || !$route->hasValidDomains())) {
|
||||
$allocatedRoutes[$routeKey] = true;
|
||||
} else {
|
||||
unset($this->routes[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addRoute(Route $route)
|
||||
{
|
||||
$this->routes[$route->getUniqueIdentifier()] = $route;
|
||||
|
|
@ -152,6 +158,7 @@ class Router
|
|||
if (!$cacheItem || null === $cacheItem->get()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->routes = $cacheItem->get();
|
||||
$this->logger->debug(sprintf('Loaded routes from Cache in %sms', number_format((microtime(true) - $time) * 1000, 2)));
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue