Skip to content

Commit

Permalink
Fix routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbaggett committed May 25, 2024
1 parent d1e18f9 commit 3f54c01
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 68 deletions.
24 changes: 1 addition & 23 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @Annotation
* @Target("METHOD")
*/
class Route
#[\Attribute] class Route
{
public array $methods = ['GET'];

Expand Down
21 changes: 10 additions & 11 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
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;
Expand Down Expand Up @@ -376,6 +378,10 @@ public function setupContainer(): Container
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,
Expand Down Expand Up @@ -500,12 +506,9 @@ public static function Log($message, Level $level = Level::Debug): void

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');
Expand Down Expand Up @@ -618,18 +621,15 @@ protected function interrogateControllers(): void
}

$appClass = new \ReflectionClass(static::class);

$this->router->loadRoutesFromAnnotations(
[
APP_ROOT . '/src/Controllers',
APP_ROOT . '/src/Actions',
],
$appClass->getNamespaceName()
);


$this->router->cache();

// $this->logger->info('ROUTE_CACHE miss.');
}

public function getContainer(): ContainerInterface
Expand All @@ -641,11 +641,10 @@ public function getContainer(): ContainerInterface
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();
Expand Down
51 changes: 18 additions & 33 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\Common\Annotations\AnnotationRegistry;
use Monolog\Logger;
use Psr\Http\Message\ServerRequestInterface;
use PushToLive\Kernel;
use SebastianBergmann\Timer\Timer;
use Slim\App;

Expand Down Expand Up @@ -39,25 +40,11 @@ public function loadRoutesFromAnnotations(
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 = [
Expand All @@ -66,6 +53,7 @@ public function loadRoutesFromAnnotations(
'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.', [
Expand All @@ -92,25 +80,22 @@ public function loadRoutesFromAnnotations(
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);
}
}

Expand Down

0 comments on commit 3f54c01

Please sign in to comment.