Fix routing.
This commit is contained in:
parent
d1e18f9c0f
commit
3f54c010b6
4 changed files with 30 additions and 68 deletions
src
|
@ -24,31 +24,9 @@ abstract class Action
|
|||
public function __construct(LoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->response = new \Slim\Psr7\Response();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws HttpNotFoundException
|
||||
* @throws HttpBadRequestException
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
$this->args = $args;
|
||||
|
||||
try {
|
||||
return $this->action();
|
||||
} catch (DomainRecordNotFoundException $e) {
|
||||
throw new HttpNotFoundException($this->request, $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DomainRecordNotFoundException
|
||||
* @throws HttpBadRequestException
|
||||
*/
|
||||
abstract protected function action(): Response;
|
||||
|
||||
/**
|
||||
* @return array|object
|
||||
*/
|
||||
|
|
|
@ -10,7 +10,7 @@ use Doctrine\Common\Annotations\Annotation\Required;
|
|||
* @Annotation
|
||||
* @Target("METHOD")
|
||||
*/
|
||||
class Route
|
||||
#[\Attribute] class Route
|
||||
{
|
||||
public array $methods = ['GET'];
|
||||
|
||||
|
|
21
src/App.php
21
src/App.php
|
@ -46,6 +46,8 @@ use Monolog\Logger;
|
|||
use Monolog\Processor\PsrLogMessageProcessor;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use PushToLive\Kernel;
|
||||
use SebastianBergmann\Timer\Timer;
|
||||
use Slim;
|
||||
use Slim\Factory\AppFactory;
|
||||
|
@ -376,6 +378,10 @@ class App
|
|||
return $monolog;
|
||||
});
|
||||
|
||||
$container->set(LoggerInterface::class, function (Logger $logger) {
|
||||
return $logger;
|
||||
});
|
||||
|
||||
$container->set(Redis::class, function (ConfigurationService $configurationService, Logger $logger, EnvironmentService $environmentService) {
|
||||
return new Redis(
|
||||
$logger,
|
||||
|
@ -500,12 +506,9 @@ class App
|
|||
|
||||
public function runHttp(): void
|
||||
{
|
||||
$timer = new Timer();
|
||||
$timer->start();
|
||||
$serverRequestCreator = ServerRequestCreatorFactory::create();
|
||||
$request = $serverRequestCreator->createServerRequestFromGlobals();
|
||||
$duration = $timer->stop();
|
||||
\Kint::dump($duration->asSeconds());
|
||||
Kernel::Timing();
|
||||
$this->loadAllRoutes($request);
|
||||
|
||||
$this->debugBar['time']->startMeasure('runHTTP', 'HTTP runtime');
|
||||
|
@ -618,6 +621,7 @@ class App
|
|||
}
|
||||
|
||||
$appClass = new \ReflectionClass(static::class);
|
||||
|
||||
$this->router->loadRoutesFromAnnotations(
|
||||
[
|
||||
APP_ROOT . '/src/Controllers',
|
||||
|
@ -625,11 +629,7 @@ class App
|
|||
],
|
||||
$appClass->getNamespaceName()
|
||||
);
|
||||
|
||||
|
||||
$this->router->cache();
|
||||
|
||||
// $this->logger->info('ROUTE_CACHE miss.');
|
||||
}
|
||||
|
||||
public function getContainer(): ContainerInterface
|
||||
|
@ -641,11 +641,10 @@ class App
|
|||
static public function Timing(){
|
||||
$duration = self::$timer->stop();
|
||||
# Get caller
|
||||
$caller = debug_backtrace()[1];
|
||||
$caller = debug_backtrace()[0];
|
||||
if($duration->asSeconds() >= 1) {
|
||||
$timingMessage = sprintf("%f seconds: %s::%s (%s:%d)", $duration->asSeconds(), $caller['class'], $caller['function'], $caller['file'], $caller['line']);
|
||||
$timingMessage = sprintf("%f seconds: (%s:%d)", $duration->asSeconds(), $caller['file'], $caller['line']);
|
||||
\Kint::dump($timingMessage);
|
||||
self::DI(Logger::class)->debug($timingMessage);
|
||||
}
|
||||
|
||||
self::$timer->start();
|
||||
|
|
|
@ -10,6 +10,7 @@ use Doctrine\Common\Annotations\AnnotationReader;
|
|||
use Doctrine\Common\Annotations\AnnotationRegistry;
|
||||
use Monolog\Logger;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use PushToLive\Kernel;
|
||||
use SebastianBergmann\Timer\Timer;
|
||||
use Slim\App;
|
||||
|
||||
|
@ -39,25 +40,11 @@ class Router
|
|||
continue;
|
||||
}
|
||||
|
||||
$timer = new Timer();
|
||||
$timer->start();
|
||||
$dirIterator = new \RecursiveDirectoryIterator($controllerPath);
|
||||
$duration = $timer->stop();
|
||||
\Kint::dump($duration->asSeconds());
|
||||
|
||||
$timer = new Timer();
|
||||
$timer->start();
|
||||
$iteratorIterator = new \RecursiveIteratorIterator($dirIterator);
|
||||
$duration = $timer->stop();
|
||||
\Kint::dump($duration->asSeconds());
|
||||
|
||||
$timer = new Timer();
|
||||
$timer->start();
|
||||
$phpFiles = new \RegexIterator($iteratorIterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
|
||||
$duration = $timer->stop();
|
||||
\Kint::dump($duration->asSeconds());
|
||||
|
||||
\Kint::dump($phpFiles);exit;
|
||||
|
||||
foreach ($phpFiles as $controllerFile) {
|
||||
$fileClassName = ltrim(str_replace([$controllerPath, '/', '.php'], ['', '\\', ''], $controllerFile[0]), '\\');
|
||||
$expectedClasses = [
|
||||
|
@ -66,6 +53,7 @@ class Router
|
|||
'Benzine\\Controllers\\' . $fileClassName,
|
||||
];
|
||||
|
||||
|
||||
foreach ($expectedClasses as $expectedClass) {
|
||||
if (!class_exists($expectedClass)) {
|
||||
$this->logger->warning('While loading routes from annotations in {file}, expected class {expectedClass} does not exist.', [
|
||||
|
@ -92,25 +80,22 @@ class Router
|
|||
continue;
|
||||
}
|
||||
|
||||
$routeAnnotation = $reader->getMethodAnnotation($method, \Benzine\Annotations\Route::class);
|
||||
if (!$routeAnnotation instanceof \Benzine\Annotations\Route) {
|
||||
// This isn't a route annotation. Somehow.
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($routeAnnotation->methods as $httpMethod) {
|
||||
$newRoute = (new Route())
|
||||
->setHttpMethod($httpMethod)
|
||||
->setRouterPattern('/' . ltrim($routeAnnotation->path, '/'))
|
||||
->setCallback($expectedClass . ':' . $method->name)
|
||||
->setWeight($routeAnnotation->weight)
|
||||
;
|
||||
|
||||
foreach ($routeAnnotation->domains as $domain) {
|
||||
$newRoute->addValidDomain($domain);
|
||||
$routeAttibutes = $method->getAttributes(\Benzine\Annotations\Route::class);
|
||||
foreach($routeAttibutes as $routeAttibute){
|
||||
foreach($routeAttibute->getArguments()['methods'] as $httpMethod){
|
||||
$newRoute = (new Route())
|
||||
->setHttpMethod($httpMethod)
|
||||
->setRouterPattern('/' . ltrim($routeAttibute->getArguments()['path'], '/'))
|
||||
->setCallback($expectedClass . ':' . $method->name)
|
||||
->setWeight($routeAttibute->getArguments()['weight'] ?? 100)
|
||||
;
|
||||
if(isset($routeAttibute->getArguments()['domains']) && is_array($routeAttibute->getArguments()['domains'])){
|
||||
foreach ($routeAttibute->getArguments()['domains'] as $domain) {
|
||||
$newRoute->addValidDomain($domain);
|
||||
}
|
||||
}
|
||||
$this->addRoute($newRoute);
|
||||
}
|
||||
|
||||
$this->addRoute($newRoute);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue