2020-07-06 21:51:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for controllers
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Controller
|
|
|
|
*
|
|
|
|
* @author Hugo Sales <hugo@fc.up.pt>
|
|
|
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Core;
|
|
|
|
|
2020-09-10 21:38:40 +01:00
|
|
|
use App\Util\Common;
|
2020-09-06 22:38:37 +01:00
|
|
|
use App\Util\Exception\RedirectException;
|
2020-07-06 21:51:08 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2020-07-23 18:55:06 +01:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2020-07-06 21:51:08 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2020-09-06 22:38:37 +01:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2020-07-06 21:51:08 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2020-07-23 18:55:06 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
2020-09-06 22:38:37 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
|
2020-07-23 18:55:06 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent;
|
|
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
2020-07-06 21:51:08 +01:00
|
|
|
|
2020-07-23 18:55:06 +01:00
|
|
|
class Controller extends AbstractController implements EventSubscriberInterface
|
2020-07-06 21:51:08 +01:00
|
|
|
{
|
2020-08-15 14:49:38 +01:00
|
|
|
private array $vars = [];
|
2020-07-23 18:55:06 +01:00
|
|
|
|
2020-07-06 21:51:08 +01:00
|
|
|
public function __invoke(Request $request)
|
|
|
|
{
|
|
|
|
$class = get_called_class();
|
|
|
|
$method = 'on' . ucfirst(strtolower($request->getMethod()));
|
|
|
|
if (method_exists($class, $method)) {
|
2020-07-23 18:55:06 +01:00
|
|
|
return $class::$method($request, $this->vars);
|
2020-07-06 21:51:08 +01:00
|
|
|
} else {
|
2020-07-23 18:55:06 +01:00
|
|
|
return $class::handle($request, $this->vars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onKernelController(ControllerEvent $event)
|
|
|
|
{
|
|
|
|
$controller = $event->getController();
|
|
|
|
$request = $event->getRequest();
|
|
|
|
|
2020-09-10 21:38:40 +01:00
|
|
|
$this->vars = ['controler' => $controller, 'request' => $request, 'have_user' => Common::user() !== null];
|
2020-10-19 19:22:59 +01:00
|
|
|
Event::handle('StartTwigPopulateVars', [&$this->vars]);
|
2020-07-23 18:55:06 +01:00
|
|
|
|
2020-08-15 14:49:38 +01:00
|
|
|
$event->stopPropagation();
|
2020-07-23 18:55:06 +01:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onKernelView(ViewEvent $event)
|
|
|
|
{
|
|
|
|
$request = $event->getRequest();
|
|
|
|
$response = $event->getControllerResult();
|
|
|
|
if (!is_array($response)) {
|
|
|
|
return $event;
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|
2020-07-23 18:55:06 +01:00
|
|
|
|
|
|
|
$this->vars = array_merge_recursive($this->vars, $response);
|
2020-10-19 19:22:59 +01:00
|
|
|
Event::handle('EndTwigPopulateVars', [&$this->vars]);
|
2020-07-23 18:55:06 +01:00
|
|
|
|
|
|
|
$template = $this->vars['_template'];
|
|
|
|
unset($this->vars['_template'], $this->vars['request']);
|
2020-07-06 21:51:08 +01:00
|
|
|
|
|
|
|
// Respond in the the most preffered acceptable content type
|
|
|
|
$format = $request->getFormat($request->getAcceptableContentTypes()[0]);
|
|
|
|
switch ($format) {
|
|
|
|
case 'html':
|
2020-07-23 18:55:06 +01:00
|
|
|
$event->setResponse($this->render($template, $this->vars));
|
|
|
|
break;
|
2020-07-06 21:51:08 +01:00
|
|
|
case 'json':
|
2020-07-23 18:55:06 +01:00
|
|
|
$event->setResponse(new JsonResponse($this->vars));
|
|
|
|
// no break
|
2020-07-06 21:51:08 +01:00
|
|
|
default:
|
|
|
|
throw new BadRequestHttpException('Unsupported format', null, 406);
|
|
|
|
}
|
2020-07-23 18:55:06 +01:00
|
|
|
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2020-09-06 22:38:37 +01:00
|
|
|
public function onKernelException(ExceptionEvent $event)
|
|
|
|
{
|
2020-09-10 21:38:40 +01:00
|
|
|
$except = $event->getThrowable();
|
|
|
|
do {
|
|
|
|
if ($except instanceof RedirectException) {
|
|
|
|
if (($redir = $except->redirect_response) != null) {
|
|
|
|
$event->setResponse($redir);
|
|
|
|
} else {
|
|
|
|
$event->setResponse(new RedirectResponse($event->getRequest()->getPathInfo()));
|
|
|
|
}
|
2020-09-06 22:38:37 +01:00
|
|
|
}
|
2020-09-10 21:38:40 +01:00
|
|
|
} while ($except != null && ($except = $except->getPrevious()) != null);
|
2020-09-06 22:38:37 +01:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2020-07-23 18:55:06 +01:00
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
KernelEvents::CONTROLLER => 'onKernelController',
|
2020-09-06 22:38:37 +01:00
|
|
|
KernelEvents::EXCEPTION => 'onKernelException',
|
2020-07-23 18:55:06 +01:00
|
|
|
KernelEvents::VIEW => 'onKernelView',
|
|
|
|
];
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|
|
|
|
}
|