123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- declare(strict_types = 1);
- namespace Plugin\Oomox;
- use App\Core\Cache;
- use App\Core\DB\DB;
- use App\Core\Event;
- use App\Core\Modules\Plugin;
- use App\Core\Router\RouteLoader;
- use App\Core\Router\Router;
- use App\Entity\LocalUser;
- use App\Util\Common;
- use App\Util\Exception\NotFoundException;
- use App\Util\Exception\RedirectException;
- use App\Util\Exception\ServerException;
- use Plugin\Oomox\Controller as C;
- use Symfony\Component\HttpFoundation\Request;
- class Oomox extends Plugin
- {
-
- public function onAddRoute(RouteLoader $r): bool
- {
- $r->connect('oomox_settings', 'settings/oomox', [Controller\Oomox::class, 'oomoxSettings']);
- $r->connect('oomox_css', 'plugins/oomox/colours', [Controller\Oomox::class, 'oomoxCSS']);
- return Event::next;
- }
-
- public function onPopulateSettingsTabs(Request $request, string $section, array &$tabs): bool
- {
- if ($section === 'colours') {
- $tabs[] = [
- 'title' => 'Light theme colours',
- 'desc' => 'Change the theme colours.',
- 'id' => 'settings-light-theme-colours-details',
- 'controller' => C\Oomox::oomoxSettingsLight($request),
- ];
- $tabs[] = [
- 'title' => 'Dark theme colours',
- 'desc' => 'Change the theme colours.',
- 'id' => 'settings-dark-theme-colours-details',
- 'controller' => C\Oomox::oomoxSettingsDark($request),
- ];
- }
- return Event::next;
- }
-
- public static function cacheKey(LocalUser $user): string
- {
- return "oomox-css-{$user->getId()}";
- }
-
- public static function getEntity(LocalUser $user): ?Entity\Oomox
- {
- try {
- return Cache::get(self::cacheKey($user), static fn () => DB::findOneBy('oomox', ['actor_id' => $user->getId()]));
- } catch (NotFoundException $e) {
- return null;
- }
- }
-
- public function onEndShowStyles(array &$styles, string $route): bool
- {
- $user = Common::user();
- if ($user && Cache::get(self::cacheKey($user), static fn () => self::getEntity($user))) {
- $styles[] = Router::url('oomox_css');
- }
- return Event::next;
- }
- }
|