[CONTROLLER] Add Controller base class, which handles rendering templates if requested HTML or json, accordingly

This commit is contained in:
Hugo Sales 2020-07-06 20:51:08 +00:00 committed by Hugo Sales
parent 6f9c70398b
commit 43e56c08f7
5 changed files with 95 additions and 26 deletions

View File

@ -30,21 +30,19 @@
namespace App\Controller;
// use App\Core\Event;
// use App\Util\Common;
use App\Core\Controller;
use App\Core\DB\DB;
use App\Core\DB\DefaultSettings;
use App\Core\Form;
use function App\Core\I18n\_m;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
class AdminConfigController extends AbstractController
class AdminConfigController extends Controller
{
public function __invoke(Request $request)
public function handle(Request $request)
{
$defaults = DefaultSettings::$defaults;
$options = [];
@ -78,12 +76,13 @@ class AdminConfigController extends AbstractController
'default' => $default,
]);
} else {
// Display error
// TODO Display error
}
}
return $this->render('config/admin.html.twig', [
'form' => $form->createView(),
]);
return [
'_template' => 'config/admin.html.twig',
'form' => $form->createView(),
];
}
}

View File

@ -30,18 +30,23 @@
namespace App\Controller;
use App\Core\Controller;
use App\Core\Event;
use App\Util\Common;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class NetworkPublic extends AbstractController
class NetworkPublic extends Controller
{
public function __invoke()
public function onPost()
{
return ['_template' => 'network/public.html.twig'];
}
public function handle()
{
Event::handle('Test', ['foobar']);
Common::config('url', 'shortener');
return $this->render('network/public.html.twig', []);
return ['_template' => 'network/public.html.twig'];
}
}

View File

@ -31,31 +31,31 @@
namespace App\Controller;
// use App\Core\Event;
// use App\Util\Common;
use App\Core\Controller;
use App\Core\Form;
use function App\Core\I18n\_m;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
class UserAdminPanel extends AbstractController
class UserAdminPanel extends Controller
{
public function __invoke(Request $request)
public function handle(Request $request)
{
$prof = Form::create([
[_m('Nickname'), TextType::class],
[_m('FullName'), TextType::class],
[_m('Homepage'), TextType::class],
[_m('Bio'), TextType::class],
[_m('Bio'), TextType::class],
[_m('Location'), TextType::class],
['save', SubmitType::class, ['label' => _m('Save')]], ]);
['save', SubmitType::class, ['label' => _m('Save')]],
]);
$prof->handleRequest($request);
return $this->render('settings/profile.html.twig', [
'prof' => $prof->createView(),
]);
return [
'_template' => 'settings/profile.html.twig',
'prof' => $prof->createView(),
];
}
}

67
src/Core/Controller.php Normal file
View File

@ -0,0 +1,67 @@
<?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;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class Controller extends AbstractController
{
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));
} else {
$vars = array_merge_recursive($vars, $class::handle($request, $vars));
}
Event::handle('EndTwigPopulateVars', [&$vars]);
$template = $vars['_template'];
unset($vars['_template'], $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);
case 'json':
return new JsonResponse($vars);
default:
throw new BadRequestHttpException('Unsupported format', null, 406);
}
}
}

View File

@ -118,9 +118,7 @@ abstract class Event
*/
public static function handle(string $name, array $args = [], string $ns = 'GNUsocial/'): bool
{
return !(self::$dispatcher->dispatch(
new GenericEvent($ns . $name, $args),
$name)->isPropagationStopped());
return !(self::$dispatcher->dispatch(new GenericEvent($ns . $name, $args), $name)->isPropagationStopped());
}
/**