[CONTROLLER][ROUTES] Refactor the base Controller to not reinvent the wheel too much and rely on Symfony's events

This commit is contained in:
Hugo Sales 2020-07-23 17:55:06 +00:00 committed by Hugo Sales
parent a1c90f2e15
commit cac00dd6d4
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 60 additions and 19 deletions

View File

@ -44,7 +44,7 @@ use Symfony\Component\HttpFoundation\Request;
class AdminPanel extends Controller
{
public function handle(Request $request)
public function site(Request $request)
{
$defaults = DefaultSettings::$defaults;
$options = [];

View File

@ -3,12 +3,11 @@
namespace App\Controller;
use App\Core\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class Security extends Controller
{
public function login(AuthenticationUtils $authenticationUtils): Response
public function login(AuthenticationUtils $authenticationUtils)
{
if ($this->getUser()) {
return $this->redirectToRoute('main_all');

View File

@ -33,35 +33,74 @@
namespace App\Core;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class Controller extends AbstractController
class Controller extends AbstractController implements EventSubscriberInterface
{
private array $vars;
public function __invoke(Request $request)
{
$class = get_called_class();
$method = 'on' . ucfirst(strtolower($request->getMethod()));
$vars = ['request' => $request];
Event::handle('StartTwigPopulateVars', [&$vars]);
if (method_exists($class, $method)) {
$vars = array_merge_recursive($vars, $class::$method($request, $vars));
return $class::$method($request, $this->vars);
} else {
$vars = array_merge_recursive($vars, $class::handle($request, $vars));
return $class::handle($request, $this->vars);
}
Event::handle('EndTwigPopulateVars', [&$vars]);
$template = $vars['_template'];
unset($vars['_template'], $vars['request']);
}
public function onKernelController(ControllerEvent $event)
{
$controller = $event->getController();
$request = $event->getRequest();
$this->vars = ['controler' => $controller, 'request' => $request];
Event::handle('StartTwigPopulateVars', [&$this->vars]);
return $event;
}
public function onKernelView(ViewEvent $event)
{
$request = $event->getRequest();
$response = $event->getControllerResult();
if (!is_array($response)) {
return $event;
}
$this->vars = array_merge_recursive($this->vars, $response);
Event::handle('EndTwigPopulateVars', [&$this->vars]);
$template = $this->vars['_template'];
unset($this->vars['_template'], $this->vars['request']);
// Respond in the the most preffered acceptable content type
$format = $request->getFormat($request->getAcceptableContentTypes()[0]);
switch ($format) {
case 'html':
return $this->render($template, $vars);
$event->setResponse($this->render($template, $this->vars));
break;
case 'json':
return new JsonResponse($vars);
$event->setResponse(new JsonResponse($this->vars));
// no break
default:
throw new BadRequestHttpException('Unsupported format', null, 406);
}
return $event;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::VIEW => 'onKernelView',
];
}
}

View File

@ -35,27 +35,30 @@ namespace App\Routes;
use App\Controller as C;
use App\Core\Router\RouteLoader;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
abstract class Main
{
public static function load(RouteLoader $r): void
{
$r->connect('main_all', '/main/all', C\NetworkPublic::class);
$r->connect('admin_panel', '/panel/admin', C\AdminPanel::class);
$r->connect('login', '/login', [C\Security::class, 'login']);
$r->connect('logout', '/logout', [C\Security::class, 'logout']);
$r->connect('main_all', '/main/all', C\NetworkPublic::class);
$r->connect('panel', '/panel', [C\AdminPanel::class, 'site']);
$r->connect('panel_site', '/panel/site', [C\AdminPanel::class, 'site']);
// FAQ static pages
foreach (['faq', 'contact', 'tags', 'groups', 'openid'] as $s) {
$r->connect('doc_' . $s, 'doc/' . $s, TemplateController::class, [], ['defaults' => ['template' => 'faq/' . $s . '.html.twig']]);
$r->connect('doc_' . $s, '/doc/' . $s, TemplateController::class, [], ['defaults' => ['template' => 'faq/' . $s . '.html.twig']]);
}
// Settings pages
$r->connect('settings', '/settings' . $s, [C\UserPanel::class, 'profile']);
$r->connect('settings', '/settings', RedirectController::class, [], ['defaults' => ['route' => 'settings_profile']]);
foreach (['profile', 'avatar', 'misc', 'account'] as $s) {
$r->connect('settings_' . $s, '/settings' . $s, [C\UserPanel::class, $s]);
$r->connect('settings_' . $s, '/settings/' . $s, [C\UserPanel::class, $s]);
}
}
}