misuzu/src/MisuzuContext.php

236 lines
12 KiB
PHP

<?php
namespace Misuzu;
use Index\Dependencies;
use Index\Config\Config;
use Index\Config\Db\DbConfig;
use Index\Http\{HttpRequest,HttpResponseBuilder};
use Index\Http\Routing\{Router,RouteHandler,RouteHandlerCommon};
use Index\Http\Routing\Filters\PrefixFilter;
use Index\Performance\Timings;
use Index\Snowflake\{BinarySnowflake,RandomSnowflake,SnowflakeGenerator};
use Index\Urls\{ArrayUrlRegistry,UrlRegistry,UrlSource};
class MisuzuContext implements RouteHandler {
use RouteHandlerCommon;
public private(set) Dependencies $deps;
public private(set) Config $config;
public private(set) Router $router;
public private(set) UrlRegistry $urls;
public private(set) SnowflakeGenerator $snowflake;
public private(set) BinarySnowflake $sfBinary;
public private(set) RandomSnowflake $sfRandom;
public private(set) AssetInfo $assetInfo;
public private(set) SiteInfo $siteInfo;
public private(set) Auth\AuthInfo $authInfo;
public private(set) Perms\PermissionsData $perms;
public private(set) WebFinger\WebFingerRegistry $webfinger;
public private(set) Changelog\ChangelogData $changelog;
public private(set) Counters\CountersData $counters;
public private(set) News\NewsData $news;
public private(set) CsrfContext $csrfCtx;
public private(set) DatabaseContext $dbCtx;
public private(set) Apps\AppsContext $appsCtx;
public private(set) Auth\AuthContext $authCtx;
public private(set) Colours\ColoursContext $coloursCtx;
public private(set) Comments\CommentsContext $commentsCtx;
public private(set) Emoticons\EmotesContext $emotesCtx;
public private(set) Forum\ForumContext $forumCtx;
public private(set) Kaomoji\KaomojiContext $kaomojiCtx;
public private(set) Logs\LogsContext $logsCtx;
public private(set) Messages\MessagesContext $messagesCtx;
public private(set) OAuth2\OAuth2Context $oauth2Ctx;
public private(set) Profile\ProfileContext $profileCtx;
public private(set) Redirects\RedirectsContext $redirectsCtx;
public private(set) Routing\RoutingContext $routingCtx;
public private(set) Storage\StorageContext $storageCtx;
public private(set) Templating\TemplatingContext $tplCtx;
public private(set) Users\UsersContext $usersCtx;
public function __construct(
public private(set) Timings $timings,
#[\SensitiveParameter] string $dsn,
string $domainRoles,
string $storageLocalPath,
string $storageRemotePath,
string $templateCachePath,
) {
$this->deps = new Dependencies;
$this->deps->register($this);
$this->deps->register($this->deps);
$this->deps->register($timings);
$this->deps->register($this->dbCtx = new DatabaseContext($dsn));
$this->deps->register($this->dbCtx->conn);
$this->deps->register($this->config = $this->deps->constructLazy(
DbConfig::class,
tableName: 'msz_config',
));
$this->deps->register($this->snowflake = $this->deps->constructLazy(SnowflakeGenerator::class));
$this->deps->register($this->sfBinary = $this->deps->constructLazy(BinarySnowflake::class));
$this->deps->register($this->sfRandom = $this->deps->constructLazy(RandomSnowflake::class));
$this->deps->register($this->perms = $this->deps->constructLazy(Perms\PermissionsData::class));
$this->deps->register($this->authInfo = $this->deps->constructLazy(Auth\AuthInfo::class));
$this->deps->register($this->siteInfo = $this->deps->constructLazy(
SiteInfo::class,
config: $this->config->scopeTo('site'),
));
$this->deps->register($this->assetInfo = $this->deps->constructLazy(
AssetInfo::class,
pathOrAssets: AssetInfo::CURRENT_PATH,
));
$this->deps->register($this->webfinger = $this->deps->constructLazy(WebFinger\WebFingerRegistry::class));
$this->deps->register($this->appsCtx = $this->deps->constructLazy(Apps\AppsContext::class));
$this->deps->register($this->authCtx = $this->deps->constructLazy(
Auth\AuthContext::class,
config: $this->config->scopeTo('auth'),
));
$this->deps->register($this->coloursCtx = $this->deps->constructLazy(Colours\ColoursContext::class));
$this->deps->register($this->commentsCtx = $this->deps->constructLazy(Comments\CommentsContext::class));
$this->deps->register($this->csrfCtx = $this->deps->constructLazy(
CsrfContext::class,
config: $this->config->scopeTo('csrf'),
));
$this->deps->register($this->emotesCtx = $this->deps->constructLazy(Emoticons\EmotesContext::class));
$this->deps->register($this->forumCtx = $this->deps->constructLazy(
Forum\ForumContext::class,
config: $this->config->scopeTo('forum'),
));
$this->deps->register($this->kaomojiCtx = $this->deps->constructLazy(Kaomoji\KaomojiContext::class));
$this->deps->register($this->logsCtx = $this->deps->constructLazy(Logs\LogsContext::class));
$this->deps->register($this->messagesCtx = $this->deps->constructLazy(
Messages\MessagesContext::class,
config: $this->config->scopeTo('messages'),
));
$this->deps->register($this->oauth2Ctx = $this->deps->constructLazy(
OAuth2\OAuth2Context::class,
config: $this->config->scopeTo('oauth2'),
));
$this->deps->register($this->profileCtx = $this->deps->constructLazy(Profile\ProfileContext::class));
$this->deps->register($this->tplCtx = $this->deps->constructLazy(
Templating\TemplatingContext::class,
cachePath: $templateCachePath,
));
$this->deps->register($this->usersCtx = $this->deps->constructLazy(
Users\UsersContext::class,
config: $this->config->scopeTo('users'),
));
$this->deps->register($this->redirectsCtx = $this->deps->constructLazy(
Redirects\RedirectsContext::class,
config: $this->config->scopeTo('redirects'),
));
$this->deps->register($this->changelog = $this->deps->constructLazy(Changelog\ChangelogData::class));
$this->deps->register($this->counters = $this->deps->constructLazy(Counters\CountersData::class));
$this->deps->register($this->news = $this->deps->constructLazy(News\NewsData::class));
$this->deps->register($this->routingCtx = $this->deps->construct(
Routing\RoutingContext::class,
domainRoles: $domainRoles,
));
$this->deps->register($this->storageCtx = $this->deps->construct(
Storage\StorageContext::class,
localPath: $storageLocalPath,
remotePath: $storageRemotePath,
));
Mailer::init($this->config->scopeTo('mail'));
Template::init($this);
}
#[PrefixFilter('/')]
public function filterPoweredBy(HttpResponseBuilder $response): void {
$response->setPoweredBy(__NAMESPACE__);
}
private ?bool $hasManageAccess = null;
public function hasManageAccess(): bool {
$this->hasManageAccess ??= $this->authInfo->loggedIn
&& !$this->usersCtx->hasActiveBan($this->authInfo->userInfo)
&& $this->authInfo->getPerms('global')->check(Perm::G_IS_JANITOR);
return $this->hasManageAccess;
}
public function registerRequestRoutes(HttpRequest $request): void {
$roles = $this->routingCtx->domainRoles->getRoles($request->getHeaderLine('Host'));
$handlers = $this->deps->all(RouteHandler::class);
foreach($handlers as $handler)
if($handler instanceof Routing\RouteHandler)
$handler->registerRoleRoutes($this->routingCtx->router, $roles);
else
$handler->registerRoutes($this->routingCtx->router);
$sources = $this->deps->all(UrlSource::class);
foreach($sources as $source)
$source->registerUrls($this->routingCtx->urls);
// needs better registration process
// hijack registerRoutes for WebFingerRoutes to automate this
$this->webfinger->register($this->deps->constructLazy(
Users\UsersWebFingerResolver::class
));
$this->routingCtx->register($this->deps->constructLazy(Auth\AuthProcessors::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(WebFinger\WebFingerRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Colours\ColoursApiRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Emoticons\EmotesApiRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Kaomoji\KaomojiApiRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Users\UsersApiRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Home\HomeRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Users\Assets\AssetsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Info\InfoRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(News\NewsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Comments\CommentsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(
Messages\MessagesRoutes::class,
config: $this->config->scopeTo('messages')
), $roles);
$this->routingCtx->register($this->deps->constructLazy(Storage\Tasks\TasksRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Storage\Uploads\UploadsLegacyRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(OAuth2\OAuth2ApiRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(OAuth2\OAuth2WebRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(WebFinger\WebFingerRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Forum\ForumCategoriesRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Forum\ForumTopicsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Forum\ForumPostsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Changelog\ChangelogRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(
SharpChat\SharpChatRoutes::class,
config: $this->config->scopeTo('sockChat'),
), $roles);
$this->routingCtx->register($this->deps->constructLazy(
Satori\SatoriRoutes::class,
config: $this->config->scopeTo('satori')
), $roles);
$this->routingCtx->register($this->deps->constructLazy(LegacyRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Redirects\LandingRedirectsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Redirects\AliasRedirectsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Redirects\IncrementalRedirectsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Redirects\SocialRedirectsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Redirects\NamedRedirectsRoutes::class), $roles);
$this->routingCtx->register($this->deps->constructLazy(Storage\Uploads\UploadsViewRoutes::class), $roles);
}
}