From 99ab24ec23fdda36fe214e1b19f8dcd8282ffb44 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Mon, 18 Oct 2021 13:17:08 +0100 Subject: [PATCH] [CORE][Controller] Allow routes without text/html response Improve GET getters --- components/Avatar/Avatar.php | 3 ++- src/Core/Controller.php | 43 ++++++++++++++++++++++-------------- src/Entity/Actor.php | 6 ++--- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/components/Avatar/Avatar.php b/components/Avatar/Avatar.php index dae757a16c..cf8bf54a18 100644 --- a/components/Avatar/Avatar.php +++ b/components/Avatar/Avatar.php @@ -24,6 +24,7 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\GSFile; use App\Core\Modules\Component; +use App\Core\Router\RouteLoader; use App\Core\Router\Router; use App\Util\Common; use Component\Avatar\Controller as C; @@ -36,7 +37,7 @@ class Avatar extends Component { } - public function onAddRoute($r): bool + public function onAddRoute(RouteLoader $r): bool { $r->connect('avatar_actor', '/actor/{actor_id<\d+>}/avatar/{size?full}', [Controller\Avatar::class, 'avatar_view']); $r->connect('avatar_default', '/avatar/default/{size?full}', [Controller\Avatar::class, 'default_avatar_view']); diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 63c116f121..809dcb0413 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -1,5 +1,7 @@ + * @author Diogo Peralta Cordeiro <@diogo.site> * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ @@ -48,10 +51,11 @@ use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Validator\Exception\ValidatorException; /** - * @method int int(string $param) - * @method bool bool(string $param) + * @method int int(string $param) + * @method bool bool(string $param) * @method string string(string $param) - * @method mixed handle(Request $request, mixed ...$extra) + * @method string params(string $param) + * @method mixed handle(Request $request, mixed ...$extra) */ abstract class Controller extends AbstractController implements EventSubscriberInterface { @@ -72,7 +76,7 @@ abstract class Controller extends AbstractController implements EventSubscriberI { $this->request = $request; $class = static::class; - $method = 'on' . ucfirst(strtolower($request->getMethod())); + $method = 'on' . ucfirst(mb_strtolower($request->getMethod())); $attributes = array_diff_key($request->attributes->get('_route_params'), array_flip(['_format', '_fragment', '_locale', 'template', 'accept', 'is_system_path'])); if (method_exists($class, $method)) { return $this->{$method}($request, ...$attributes); @@ -108,7 +112,7 @@ abstract class Controller extends AbstractController implements EventSubscriberI { $request = $event->getRequest(); $response = $event->getControllerResult(); - if (!is_array($response)) { + if (!\is_array($response)) { // This means it's not one of our custom format responses, nothing to do // @codeCoverageIgnoreStart return $event; @@ -117,7 +121,7 @@ abstract class Controller extends AbstractController implements EventSubscriberI $this->vars = array_merge_recursive($this->vars, $response); - $template = $this->vars['_template']; + $template = \array_key_exists('_template', $this->vars) ? $this->vars['_template'] : null; unset($this->vars['_template'], $response['_template']); // Respond in the most preferred acceptable content type @@ -132,12 +136,17 @@ abstract class Controller extends AbstractController implements EventSubscriberI 'response' => &$potential_response, ]) !== Event::stop) { switch ($format) { - case 'html': - $event->setResponse($this->render($template, $this->vars)); - break; case 'json': $event->setResponse(new JsonResponse($response)); break; + case 'html': + if ($template !== null) { + $event->setResponse($this->render($template, $this->vars)); + break; + } else { + // no break, goto default + } + // no break default: throw new ClientException(_m('Unsupported format: {format}', ['format' => $format]), 406); // 406 Not Acceptable } @@ -192,22 +201,22 @@ abstract class Controller extends AbstractController implements EventSubscriberI /** * Get and convert GET parameters. Can be called with `int`, `bool`, `string`, etc * - * @throws ValidatorException * @throws Exception + * @throws ValidatorException * - * @return null|mixed the value or null if no paramter exists + * @return null|array|bool|int|string the value or null if no parameter exists */ - public function __call(string $method, array $args) + public function __call(string $method, array $args): array|bool|int|string|null { - $name = $args[0]; - $value = $this->request->query->get($name); switch ($method) { case 'int': - return (int) $value; + return $this->request->query->getInt($args[0]); case 'bool': - return (bool) $value; + return $this->request->query->getBoolean($args[0]); case 'string': - return (string) $value; + return $this->request->query->get($args[0]); + case 'params': + return $this->request->query->all(); default: // @codeCoverageIgnoreStart Log::critical($m = "Method '{$method}' on class App\\Core\\Controller not found (__call)"); diff --git a/src/Entity/Actor.php b/src/Entity/Actor.php index f53895ab18..a2e1e195cd 100644 --- a/src/Entity/Actor.php +++ b/src/Entity/Actor.php @@ -307,12 +307,12 @@ class Actor extends Entity public function getUri(): string { - return Router::url('actor_id', ['actor_id' => $this->getId()]); + return Router::url('actor_view_id', ['id' => $this->getId()]); } - public function getUrl(): string + public function getUrl(int $type = Router::ABSOLUTE_PATH): string { - return Router::url('actor_nickname', ['actor_nickname' => $this->getNickname()]); + return Router::url('actor_view_nickname', ['nickname' => $this->getNickname()], $type); } public static function schemaDef(): array