Automated cleaning. Please remember to make clean before committing.
This commit is contained in:
parent
d61bbd5668
commit
236a8189c9
5 changed files with 84 additions and 83 deletions
|
|
@ -1,31 +1,32 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Benzine\Annotations;
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Required;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*
|
||||
* @Target("METHOD")
|
||||
*/
|
||||
class Route
|
||||
{
|
||||
/** @var array */
|
||||
public array $methods = ['GET'];
|
||||
|
||||
/**
|
||||
* @Required
|
||||
* @var string
|
||||
*/
|
||||
public string $path;
|
||||
|
||||
/** @var string */
|
||||
public string $access = \Benzine\Router\Route::ACCESS_PUBLIC;
|
||||
|
||||
/** @var int */
|
||||
public int $weight = 100;
|
||||
|
||||
/** @var array */
|
||||
public array $domains = [];
|
||||
}
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Benzine\Annotations;
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Required;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
*
|
||||
* @Target("METHOD")
|
||||
*/
|
||||
class Route
|
||||
{
|
||||
/** @var array */
|
||||
public array $methods = ['GET'];
|
||||
|
||||
/**
|
||||
* @Required
|
||||
*/
|
||||
public string $path;
|
||||
|
||||
/** @var string */
|
||||
public string $access = \Benzine\Router\Route::ACCESS_PUBLIC;
|
||||
|
||||
/** @var int */
|
||||
public int $weight = 100;
|
||||
|
||||
/** @var array */
|
||||
public array $domains = [];
|
||||
}
|
||||
|
|
|
|||
28
src/App.php
28
src/App.php
|
|
@ -5,7 +5,6 @@ namespace Benzine;
|
|||
use Benzine\ORM\Connection\Databases;
|
||||
use Benzine\ORM\Laminator;
|
||||
use Benzine\Redis\Redis;
|
||||
use Benzine\Router\Route;
|
||||
use Benzine\Router\Router;
|
||||
use Benzine\Services\ConfigurationService;
|
||||
use Benzine\Services\EnvironmentService;
|
||||
|
|
@ -34,7 +33,6 @@ use Monolog\Handler\ErrorLogHandler;
|
|||
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;
|
||||
|
|
@ -416,19 +414,6 @@ class App
|
|||
}
|
||||
}
|
||||
|
||||
protected function loadAllRoutes(ServerRequestInterface $request): self
|
||||
{
|
||||
$this->debugBar['time']->startMeasure('interrogateControllers', 'Time to interrogate controllers for routes');
|
||||
$this->interrogateControllers();
|
||||
$this->debugBar['time']->stopMeasure('interrogateControllers');
|
||||
|
||||
$this->logger->debug(sprintf('Bootstrap complete in %sms', number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2)));
|
||||
|
||||
$this->router->populateRoutes($this->getApp(), $request);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
|
|
@ -485,6 +470,19 @@ class App
|
|||
return $this->logger;
|
||||
}
|
||||
|
||||
protected function loadAllRoutes(ServerRequestInterface $request): self
|
||||
{
|
||||
$this->debugBar['time']->startMeasure('interrogateControllers', 'Time to interrogate controllers for routes');
|
||||
$this->interrogateControllers();
|
||||
$this->debugBar['time']->stopMeasure('interrogateControllers');
|
||||
|
||||
$this->logger->debug(sprintf('Bootstrap complete in %sms', number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2)));
|
||||
|
||||
$this->router->populateRoutes($this->getApp(), $request);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function interrogateTranslations(): void
|
||||
{
|
||||
$stringPath = APP_ROOT.'/src/Strings';
|
||||
|
|
|
|||
|
|
@ -113,18 +113,19 @@ abstract class AbstractController
|
|||
;
|
||||
}
|
||||
|
||||
protected function returnFile(Filesystem $filesystem, string $filename) : Response{
|
||||
protected function returnFile(Filesystem $filesystem, string $filename): Response
|
||||
{
|
||||
$response = new Response();
|
||||
if(!$filesystem->has($filename)){
|
||||
if (!$filesystem->has($filename)) {
|
||||
return $this->pageNotFound();
|
||||
}
|
||||
|
||||
//\Kint::dump($filesystem->getMimetype($assetName));exit;
|
||||
|
||||
$response->getBody()
|
||||
->write($filesystem->read($filename));
|
||||
->write($filesystem->read($filename))
|
||||
;
|
||||
|
||||
return $response->withHeader("Content-type", $filesystem->getMimetype($filename));
|
||||
return $response->withHeader('Content-type', $filesystem->getMimetype($filename));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
namespace Benzine\Router;
|
||||
|
||||
use Monolog\Logger;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Slim\App;
|
||||
|
||||
class Route
|
||||
|
|
|
|||
|
|
@ -45,8 +45,8 @@ class Router
|
|||
foreach ($phpFiles as $controllerFile) {
|
||||
$fileClassName = ltrim(str_replace([$controllerPath, '/', '.php'], ['', '\\', ''], $controllerFile[0]), '\\');
|
||||
$expectedClasses = [
|
||||
$baseNamespace . '\\Controllers\\' . $fileClassName,
|
||||
'Benzine\\Controllers\\' . $fileClassName,
|
||||
$baseNamespace.'\\Controllers\\'.$fileClassName,
|
||||
'Benzine\\Controllers\\'.$fileClassName,
|
||||
];
|
||||
|
||||
foreach ($expectedClasses as $expectedClass) {
|
||||
|
|
@ -69,14 +69,15 @@ class Router
|
|||
continue;
|
||||
}
|
||||
|
||||
foreach($routeAnnotation->methods as $httpMethod) {
|
||||
foreach ($routeAnnotation->methods as $httpMethod) {
|
||||
$newRoute = new Route($this->logger);
|
||||
|
||||
$newRoute
|
||||
->setHttpMethod($httpMethod)
|
||||
->setRouterPattern('/' . ltrim($routeAnnotation->path, '/'))
|
||||
->setCallback($method->class . ':' . $method->name)
|
||||
->setWeight($routeAnnotation->weight);
|
||||
->setRouterPattern('/'.ltrim($routeAnnotation->path, '/'))
|
||||
->setCallback($method->class.':'.$method->name)
|
||||
->setWeight($routeAnnotation->weight)
|
||||
;
|
||||
|
||||
foreach ($routeAnnotation->domains as $domain) {
|
||||
$newRoute->addValidDomain($domain);
|
||||
|
|
@ -115,32 +116,6 @@ 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) {
|
||||
$a1 = $a->getWeight();
|
||||
$b1 = $b->getWeight();
|
||||
if ($a1 === $b1) {
|
||||
return 0;
|
||||
}
|
||||
return ($a1 > $b1) ? +1 : -1;
|
||||
});
|
||||
|
||||
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;
|
||||
|
|
@ -181,4 +156,31 @@ class Router
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
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) {
|
||||
$a1 = $a->getWeight();
|
||||
$b1 = $b->getWeight();
|
||||
if ($a1 === $b1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ($a1 > $b1) ? +1 : -1;
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue