From 9e5945cbd4615f317f81095f29f55c9a4d05ade3 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 29 Jan 2025 23:56:56 +0000 Subject: [PATCH] Initial implementation of the dependency injector thingy. --- src/MisuzuContext.php | 188 ++++++++++--------------------- templates/manage/users/role.twig | 2 +- templates/manage/users/user.twig | 2 +- 3 files changed, 62 insertions(+), 130 deletions(-) diff --git a/src/MisuzuContext.php b/src/MisuzuContext.php index 722faf9f..6788356a 100644 --- a/src/MisuzuContext.php +++ b/src/MisuzuContext.php @@ -1,6 +1,7 @@ perms = new PermissionsData($dbConn); - $this->authInfo = new AuthInfo($this->perms); - $this->siteInfo = new SiteInfo($config->scopeTo('site')); + $this->deps = new Dependencies; + $this->deps->register($this->deps); + $this->deps->register($this->dbConn); + $this->deps->register($this->config); + $this->deps->register($this->env); - $this->authCtx = new AuthContext($dbConn, $config->scopeTo('auth')); - $this->forumCtx = new ForumContext($dbConn); - $this->messagesCtx = new MessagesContext($dbConn); - $this->usersCtx = new UsersContext($dbConn); - $this->redirectsCtx = new RedirectsContext($dbConn, $config->scopeTo('redirects')); + $this->deps->register($this->perms = new PermissionsData($dbConn)); + $this->deps->register($this->authInfo = $this->deps->constructLazy(AuthInfo::class)); + $this->deps->register($this->siteInfo = $this->deps->constructLazy(SiteInfo::class, config: $config->scopeTo('site'))); - $this->auditLog = new AuditLogData($dbConn); - $this->changelog = new ChangelogData($dbConn); - $this->comments = new CommentsData($dbConn); - $this->counters = new CountersData($dbConn); - $this->emotes = new EmotesData($dbConn); - $this->news = new NewsData($dbConn); - $this->profileFields = new ProfileFieldsData($dbConn); + $this->deps->register($this->authCtx = $this->deps->constructLazy(AuthContext::class, config: $config->scopeTo('auth'))); + $this->deps->register($this->forumCtx = $this->deps->constructLazy(ForumContext::class)); + $this->deps->register($this->messagesCtx = $this->deps->constructLazy(MessagesContext::class)); + $this->deps->register($this->usersCtx = $this->deps->constructLazy(UsersContext::class)); + $this->deps->register($this->redirectsCtx = $this->deps->constructLazy(RedirectsContext::class, config: $config->scopeTo('redirects'))); + + $this->deps->register($this->auditLog = $this->deps->constructLazy(AuditLogData::class)); + $this->deps->register($this->changelog = $this->deps->constructLazy(ChangelogData::class)); + $this->deps->register($this->comments = $this->deps->constructLazy(CommentsData::class)); + $this->deps->register($this->counters = $this->deps->constructLazy(CountersData::class)); + $this->deps->register($this->emotes = $this->deps->constructLazy(EmotesData::class)); + $this->deps->register($this->news = $this->deps->constructLazy(NewsData::class)); + $this->deps->register($this->profileFields = $this->deps->constructLazy(ProfileFieldsData::class)); } public function getDbQueryCount(): int { @@ -156,7 +165,7 @@ class MisuzuContext { $purposes = $this->env->getArray($prefix); $routingCtx = new BackedRoutingContext; - $this->urls = $routingCtx->urls; + $this->deps->register($this->urls = $routingCtx->urls); if(in_array('main', $purposes)) { $scopedInfo = $hostInfo->scopeTo('main'); @@ -165,126 +174,49 @@ class MisuzuContext { $scopedInfo->getString('path') ); - $scopedCtx->register(new \Misuzu\Home\HomeRoutes( - $this->config, - $this->dbConn, - $this->siteInfo, - $this->authInfo, - $this->changelog, - $this->comments, - $this->counters, - $this->news, - $this->usersCtx + $scopedCtx->register($this->deps->constructLazy(Home\HomeRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Users\Assets\AssetsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Info\InfoRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(News\NewsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy( + Messages\MessagesRoutes::class, + config: $this->config->scopeTo('messages') )); - - $scopedCtx->register(new \Misuzu\Users\Assets\AssetsRoutes( - $this->authInfo, - $this->urls, - $this->usersCtx + $scopedCtx->register($this->deps->constructLazy(Forum\ForumCategoriesRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Forum\ForumTopicsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Forum\ForumPostsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Changelog\ChangelogRoutes::class)); + $scopedCtx->register($this->deps->constructLazy( + SharpChat\SharpChatRoutes::class, + config: $this->config->scopeTo('sockChat'), + impersonateConfig: $this->config->scopeTo('impersonate') )); - - $scopedCtx->register(new \Misuzu\Info\InfoRoutes); - - $scopedCtx->register(new \Misuzu\News\NewsRoutes( - $this->siteInfo, - $this->authInfo, - $this->urls, - $this->news, - $this->usersCtx, - $this->comments + $scopedCtx->register($this->deps->constructLazy( + Satori\SatoriRoutes::class, + config: $this->config->scopeTo('satori') )); - - $scopedCtx->register(new \Misuzu\Messages\MessagesRoutes( - $this->config->scopeTo('messages'), - $this->urls, - $this->authInfo, - $this->messagesCtx, - $this->usersCtx, - $this->perms - )); - - $scopedCtx->register(new \Misuzu\Forum\ForumCategoriesRoutes( - $this->forumCtx, - $this->usersCtx, - $this->authInfo, - )); - $scopedCtx->register(new \Misuzu\Forum\ForumTopicsRoutes( - $this->forumCtx, - $this->usersCtx, - $this->auditLog, - $this->authInfo, - )); - $scopedCtx->register(new \Misuzu\Forum\ForumPostsRoutes( - $this->urls, - $this->forumCtx, - $this->usersCtx, - $this->auditLog, - $this->authInfo, - )); - - $scopedCtx->register(new \Misuzu\Changelog\ChangelogRoutes( - $this->siteInfo, - $this->urls, - $this->changelog, - $this->usersCtx, - $this->authInfo, - $this->comments - )); - - $scopedCtx->register(new \Misuzu\SharpChat\SharpChatRoutes( - $this->config->scopeTo('sockChat'), - $this->config->scopeTo('impersonate'), - $this->urls, - $this->usersCtx, - $this->authCtx, - $this->emotes, - $this->perms, - $this->authInfo, - $this->counters - )); - - $scopedCtx->register(new \Misuzu\Satori\SatoriRoutes( - $this->config->scopeTo('satori'), - $this->usersCtx, - $this->forumCtx, - $this->profileFields - )); - - $routingCtx->register(new LegacyRoutes($this->urls)); + $scopedCtx->register($this->deps->constructLazy(LegacyRoutes::class)); $rpcServer = new HttpRpcServer; $scopedCtx->register($rpcServer->createRouteHandler( new HmacVerificationProvider(fn() => $this->config->getString('aleister.secret')) )); - - $rpcServer->register(new Auth\AuthRpcHandler( - $this->config->scopeTo('impersonate'), - $this->usersCtx, - $this->authCtx + $rpcServer->register($this->deps->constructLazy( + Auth\AuthRpcHandler::class, + impersonateConfig: $this->config->scopeTo('impersonate') )); + $rpcServer->register($this->deps->constructLazy(Emoticons\EmotesRpcHandler::class)); + $rpcServer->register($this->deps->constructLazy(Users\UsersRpcHandler::class)); - $rpcServer->register(new Emoticons\EmotesRpcHandler( - $this->emotes - )); - - $rpcServer->register(new Users\UsersRpcHandler( - $this->siteInfo, - $this->urls, - $this->usersCtx - )); - - // This RPC server will eventually despawn when Hanyuu fully owns auth $hanyuuRpcServer = new HttpRpcServer; $scopedCtx->scopeTo('', '/_hanyuu')->register($hanyuuRpcServer->createRouteHandler( new HmacVerificationProvider(fn() => $this->config->getString('hanyuu.secret')) )); - $hanyuuRpcServer->register(new Hanyuu\HanyuuRpcHandler( - fn() => $this->config->getString('hanyuu.endpoint'), - $this->config->scopeTo('impersonate'), - $this->urls, - $this->usersCtx, - $this->authCtx + $hanyuuRpcServer->register($this->deps->constructLazy( + Hanyuu\HanyuuRpcHandler::class, + getBaseUrl: fn() => $this->config->getString('hanyuu.endpoint'), + impersonateConfig: $this->config->scopeTo('impersonate') )); } @@ -295,14 +227,14 @@ class MisuzuContext { $scopedInfo->getString('path') ); - $scopedCtx->register(new Redirects\LandingRedirectsRoutes); - $scopedCtx->register(new Redirects\AliasRedirectsRoutes($this->redirectsCtx)); - $scopedCtx->register(new Redirects\IncrementalRedirectsRoutes($this->redirectsCtx)); - $scopedCtx->register(new Redirects\SocialRedirectsRoutes( - $this->redirectsCtx, - $this->getWebAssetInfo(...) + $scopedCtx->register($this->deps->constructLazy(Redirects\LandingRedirectsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Redirects\AliasRedirectsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy(Redirects\IncrementalRedirectsRoutes::class)); + $scopedCtx->register($this->deps->constructLazy( + Redirects\SocialRedirectsRoutes::class, + getWebAssetInfo: $this->getWebAssetInfo(...) )); - $scopedCtx->register(new Redirects\NamedRedirectsRoutes($this->redirectsCtx)); + $scopedCtx->register($this->deps->constructLazy(Redirects\NamedRedirectsRoutes::class)); } return $routingCtx; diff --git a/templates/manage/users/role.twig b/templates/manage/users/role.twig index c2a7c8dd..5b336573 100644 --- a/templates/manage/users/role.twig +++ b/templates/manage/users/role.twig @@ -59,7 +59,7 @@ diff --git a/templates/manage/users/user.twig b/templates/manage/users/user.twig index d718e472..cffc5503 100644 --- a/templates/manage/users/user.twig +++ b/templates/manage/users/user.twig @@ -99,7 +99,7 @@