From 7190a5f4dfb803cb980237afd4b696b1ef7094aa Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 6 Sep 2023 11:59:44 +0000 Subject: [PATCH] Syntactic sugar for mass route registration. --- composer.lock | 14 ++++++------- src/Changelog/ChangelogRoutes.php | 6 ++++-- src/Home/HomeRoutes.php | 6 ++++-- src/Info/InfoRoutes.php | 5 +++-- src/MisuzuContext.php | 34 +++++++++++++++---------------- src/News/NewsRoutes.php | 6 ++++-- src/Satori/SatoriRoutes.php | 6 ++++-- src/SharpChat/SharpChatRoutes.php | 6 ++++-- src/Users/Assets/AssetsRoutes.php | 7 +++++-- 9 files changed, 52 insertions(+), 38 deletions(-) diff --git a/composer.lock b/composer.lock index 53c50c3..b89b54b 100644 --- a/composer.lock +++ b/composer.lock @@ -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": [], diff --git a/src/Changelog/ChangelogRoutes.php b/src/Changelog/ChangelogRoutes.php index aa29dc9..320f3be 100644 --- a/src/Changelog/ChangelogRoutes.php +++ b/src/Changelog/ChangelogRoutes.php @@ -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(...)); diff --git a/src/Home/HomeRoutes.php b/src/Home/HomeRoutes.php index 16b9fd6..c0f41f3 100644 --- a/src/Home/HomeRoutes.php +++ b/src/Home/HomeRoutes.php @@ -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) diff --git a/src/Info/InfoRoutes.php b/src/Info/InfoRoutes.php index f4b06b8..fe52cab 100644 --- a/src/Info/InfoRoutes.php +++ b/src/Info/InfoRoutes.php @@ -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(...)); diff --git a/src/MisuzuContext.php b/src/MisuzuContext.php index ad822f0..a1292e6 100644 --- a/src/MisuzuContext.php +++ b/src/MisuzuContext.php @@ -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 diff --git a/src/News/NewsRoutes.php b/src/News/NewsRoutes.php index 07dcb56..f4293b3 100644 --- a/src/News/NewsRoutes.php +++ b/src/News/NewsRoutes.php @@ -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(...)); diff --git a/src/Satori/SatoriRoutes.php b/src/Satori/SatoriRoutes.php index 58b9d8e..7069ce3 100644 --- a/src/Satori/SatoriRoutes.php +++ b/src/Satori/SatoriRoutes.php @@ -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) { diff --git a/src/SharpChat/SharpChatRoutes.php b/src/SharpChat/SharpChatRoutes.php index 8b640bb..3bc5ef9 100644 --- a/src/SharpChat/SharpChatRoutes.php +++ b/src/SharpChat/SharpChatRoutes.php @@ -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) { diff --git a/src/Users/Assets/AssetsRoutes.php b/src/Users/Assets/AssetsRoutes.php index fa47b60..272e1b6 100644 --- a/src/Users/Assets/AssetsRoutes.php +++ b/src/Users/Assets/AssetsRoutes.php @@ -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(...));