Syntactic sugar for mass route registration.

This commit is contained in:
flash 2023-09-06 11:59:44 +00:00
parent 5c67d49225
commit 7190a5f4df
9 changed files with 52 additions and 38 deletions

14
composer.lock generated
View file

@ -348,7 +348,7 @@
"source": {
"type": "git",
"url": "https://git.flash.moe/flash/index.git",
"reference": "1172115e699acf44580ffcdcf86c9e3987d2f969"
"reference": "2b8b31289d2f4b27c1bc8355348e529cb2177ebc"
},
"require": {
"ext-mbstring": "*",
@ -386,7 +386,7 @@
],
"description": "Composer package for the common library for my projects.",
"homepage": "https://railgun.sh/index",
"time": "2023-08-28T13:58:51+00:00"
"time": "2023-09-06T11:49:54+00:00"
},
{
"name": "flashwave/sasae",
@ -1721,16 +1721,16 @@
"packages-dev": [
{
"name": "phpstan/phpstan",
"version": "1.10.32",
"version": "1.10.33",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "c47e47d3ab03137c0e121e77c4d2cb58672f6d44"
"reference": "03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/c47e47d3ab03137c0e121e77c4d2cb58672f6d44",
"reference": "c47e47d3ab03137c0e121e77c4d2cb58672f6d44",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1",
"reference": "03b1cf9f814ba0863c4e9affea49a4d1ed9a2ed1",
"shasum": ""
},
"require": {
@ -1779,7 +1779,7 @@
"type": "tidelift"
}
],
"time": "2023-08-24T21:54:50+00:00"
"time": "2023-09-04T12:20:53+00:00"
}
],
"aliases": [],

View file

@ -4,6 +4,7 @@ namespace Misuzu\Changelog;
use ErrorException;
use RuntimeException;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Pagination;
use Misuzu\Template;
use Misuzu\Auth\AuthInfo;
@ -16,7 +17,7 @@ use Misuzu\Feeds\AtomFeedSerializer;
use Misuzu\Feeds\RssFeedSerializer;
use Misuzu\Users\Users;
final class ChangelogRoutes {
final class ChangelogRoutes implements IRouteHandler {
private IConfig $config;
private Changelog $changelog;
private Users $users;
@ -27,7 +28,6 @@ final class ChangelogRoutes {
private array $userColours = [];
public function __construct(
IRouter $router,
IConfig $config,
Changelog $changelog,
Users $users,
@ -39,7 +39,9 @@ final class ChangelogRoutes {
$this->users = $users;
$this->authInfo = $authInfo;
$this->comments = $comments;
}
public function registerRoutes(IRouter $router): void {
$router->get('/changelog', $this->getIndex(...));
$router->get('/changelog.rss', $this->getFeedRSS(...));
$router->get('/changelog.atom', $this->getFeedAtom(...));

View file

@ -6,6 +6,7 @@ use Index\DateTime;
use Index\Data\DbTools;
use Index\Data\IDbConnection;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Pagination;
use Misuzu\Template;
use Misuzu\Auth\AuthInfo;
@ -16,7 +17,7 @@ use Misuzu\Counters\Counters;
use Misuzu\News\News;
use Misuzu\Users\Users;
class HomeRoutes {
class HomeRoutes implements IRouteHandler {
private IConfig $config;
private IDbConnection $dbConn;
private AuthInfo $authInfo;
@ -27,7 +28,6 @@ class HomeRoutes {
private Users $users;
public function __construct(
IRouter $router,
IConfig $config,
IDbConnection $dbConn,
AuthInfo $authInfo,
@ -45,7 +45,9 @@ class HomeRoutes {
$this->counters = $counters;
$this->news = $news;
$this->users = $users;
}
public function registerRoutes(IRouter $router): void {
$router->get('/', $this->getIndex(...));
if(MSZ_DEBUG)

View file

@ -2,10 +2,11 @@
namespace Misuzu\Info;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Template;
use Misuzu\Parsers\Parser;
class InfoRoutes {
class InfoRoutes implements IRouteHandler {
private const DOCS_PATH = MSZ_ROOT . '/docs';
private const PROJECT_PATHS = [
'misuzu' => MSZ_ROOT,
@ -16,7 +17,7 @@ class InfoRoutes {
'index' => 'Index Project » %s',
];
public function __construct(IRouter $router) {
public function registerRoutes(IRouter $router): void {
$router->get('/info', $this->getIndex(...));
$router->get('/info/:name', $this->getDocsPage(...));
$router->get('/info/:project/:name', $this->getProjectPage(...));

View file

@ -320,37 +320,37 @@ class MisuzuContext {
}
private function registerHttpRoutes(): void {
new HomeRoutes(
$this->router, $this->config, $this->dbConn, $this->authInfo,
$this->router->register(new HomeRoutes(
$this->config, $this->dbConn, $this->authInfo,
$this->changelog, $this->comments, $this->counters, $this->news,
$this->users
);
));
new AssetsRoutes($this->router, $this->authInfo, $this->bans, $this->users);
$this->router->register(new AssetsRoutes($this->authInfo, $this->bans, $this->users));
new InfoRoutes($this->router);
$this->router->register(new InfoRoutes);
new NewsRoutes(
$this->router, $this->config, $this->authInfo,
$this->router->register(new NewsRoutes(
$this->config, $this->authInfo,
$this->news, $this->users, $this->comments
);
));
new ChangelogRoutes(
$this->router, $this->config, $this->changelog,
$this->router->register(new ChangelogRoutes(
$this->config, $this->changelog,
$this->users, $this->authInfo, $this->comments
);
));
new SharpChatRoutes(
$this->router, $this->config->scopeTo('sockChat'),
$this->router->register(new SharpChatRoutes(
$this->config->scopeTo('sockChat'),
$this->bans, $this->emotes, $this->users,
$this->sessions, $this->perms, $this->authInfo,
$this->createAuthTokenPacker(...)
);
));
new SatoriRoutes(
$this->router->register(new SatoriRoutes(
$this->dbConn, $this->config->scopeTo('satori'),
$this->router, $this->users, $this->profileFields
);
$this->users, $this->profileFields
));
// below is still only otherwise available as stinky php files

View file

@ -6,6 +6,7 @@ use Index\DateTime;
use Index\Data\DbTools;
use Index\Data\IDbConnection;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Pagination;
use Misuzu\Template;
use Misuzu\Auth\AuthInfo;
@ -22,7 +23,7 @@ use Misuzu\News\NewsCategoryInfo;
use Misuzu\Users\Users;
use Misuzu\Parsers\Parser;
class NewsRoutes {
class NewsRoutes implements IRouteHandler {
private IConfig $config;
private AuthInfo $authInfo;
private News $news;
@ -30,7 +31,6 @@ class NewsRoutes {
private Comments $comments;
public function __construct(
IRouter $router,
IConfig $config,
AuthInfo $authInfo,
News $news,
@ -42,7 +42,9 @@ class NewsRoutes {
$this->news = $news;
$this->users = $users;
$this->comments = $comments;
}
public function registerRoutes(IRouter $router): void {
$router->get('/news', $this->getIndex(...));
$router->get('/news.rss', $this->getFeedRss(...));
$router->get('/news.atom', $this->getFeedAtom(...));

View file

@ -6,12 +6,13 @@ use Index\Data\DbTools;
use Index\Data\IDbConnection;
use Index\Http\HttpFx;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Pagination;
use Misuzu\Config\IConfig;
use Misuzu\Profile\ProfileFields;
use Misuzu\Users\Users;
final class SatoriRoutes {
final class SatoriRoutes implements IRouteHandler {
private IDbConnection $dbConn;
private IConfig $config;
private Users $users;
@ -20,7 +21,6 @@ final class SatoriRoutes {
public function __construct(
IDbConnection $dbConn,
IConfig $config,
IRouter $router,
Users $users,
ProfileFields $profileFields
) {
@ -28,7 +28,9 @@ final class SatoriRoutes {
$this->config = $config;
$this->users = $users;
$this->profileFields = $profileFields;
}
public function registerRoutes(IRouter $router): void {
// Simplify default error pages
if($router instanceof HttpFx)
$router->use('/_satori', function() use($router) {

View file

@ -5,6 +5,7 @@ use Closure;
use RuntimeException;
use Index\Colour\Colour;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Index\Http\HttpFx;
use Misuzu\Auth\AuthInfo;
use Misuzu\Auth\Sessions;
@ -14,7 +15,7 @@ use Misuzu\Perms\Permissions;
use Misuzu\Users\Bans;
use Misuzu\Users\Users;
final class SharpChatRoutes {
final class SharpChatRoutes implements IRouteHandler {
private IConfig $config;
private Bans $bans;
private Emotes $emotes;
@ -26,7 +27,6 @@ final class SharpChatRoutes {
private string $hashKey;
public function __construct(
IRouter $router,
IConfig $config,
Bans $bans,
Emotes $emotes,
@ -45,7 +45,9 @@ final class SharpChatRoutes {
$this->authInfo = $authInfo;
$this->createAuthTokenPacker = $createAuthTokenPacker;
$this->hashKey = $this->config->getString('hashKey', 'woomy');
}
public function registerRoutes(IRouter $router): void {
// Simplify default error pages
if($router instanceof HttpFx)
$router->use('/_sockchat', function() use($router) {

View file

@ -4,22 +4,25 @@ namespace Misuzu\Users\Assets;
use InvalidArgumentException;
use RuntimeException;
use Index\Routing\IRouter;
use Index\Routing\IRouteHandler;
use Misuzu\Perm;
use Misuzu\Auth\AuthInfo;
use Misuzu\Users\Bans;
use Misuzu\Users\Users;
use Misuzu\Users\UserInfo;
class AssetsRoutes {
class AssetsRoutes implements IRouteHandler {
private AuthInfo $authInfo;
private Bans $bans;
private Users $users;
public function __construct(IRouter $router, AuthInfo $authInfo, Bans $bans, Users $users) {
public function __construct(AuthInfo $authInfo, Bans $bans, Users $users) {
$this->authInfo = $authInfo;
$this->bans = $bans;
$this->users = $users;
}
public function registerRoutes(IRouter $router): void {
$router->get('/assets/avatar', $this->getAvatar(...));
$router->get('/assets/avatar/:filename', $this->getAvatar(...));
$router->get('/assets/profile-background', $this->getProfileBackground(...));