diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100644 index 7c48ca1..0000000 --- a/appinfo/app.php +++ /dev/null @@ -1,10 +0,0 @@ -isEnabled()) { - $app->registerAssets(); - $app->registerServices(); -} diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 0558e9a..f7f5e7e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -9,6 +9,9 @@ use OCA\SideMenu\Service\AppRepository; use OCA\SideMenu\Service\CategoryRepository; use OCA\SideMenu\Service\ConfigProxy; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\IUserSession; use OCP\Util; use Psr\Container\ContainerInterface; @@ -18,7 +21,7 @@ use Psr\Container\ContainerInterface; * * @author Simon Vieille */ -class Application extends App +class Application extends App implements IBootstrap { public const APP_ID = 'side_menu'; @@ -44,16 +47,9 @@ class Application extends App public function __construct(array $urlParams = []) { parent::__construct(self::APP_ID, $urlParams); - - $this->config = OC::$server->getConfig(); - $this->cspnm = OC::$server->getContentSecurityPolicyNonceManager(); - $this->user = OC::$server[IUserSession::class]->getUser(); } - /** - * Checks if this app is enabled. - */ - public function isEnabled(): bool + protected function isEnabled(): bool { $enabled = true; $isForced = (bool) $this->config->getAppValue(self::APP_ID, 'force', '0'); @@ -74,64 +70,65 @@ class Application extends App return $enabled; } - /** - * Registes services. - */ - public function registerServices() - { - $container = $this->getContainer(); - - $container->registerService('AppRepository', function (ContainerInterface $c) { - return new AppRepository(); - }); - - $container->registerService('CategoryRepository', function (ContainerInterface $c) { - return new CategoryRepository(); - }); - - $container->registerService('ConfigProxy', function (ContainerInterface $c) { - return new ConfigProxy(); - }); - } - - /** - * Registers assets. - */ - public function registerAssets() + protected function addAssets() { Util::addScript(self::APP_ID, 'sideMenu'); Util::addStyle(self::APP_ID, 'sideMenu'); - $stylesheet = OC::$server->getURLGenerator()->linkToRoute( - 'side_menu.Css.stylesheet', - [ - 'v' => $this->config->getAppValue(self::APP_ID, 'cache', '0'), - ] - ); - - $script = OC::$server->getURLGenerator()->linkToRoute( - 'side_menu.Js.script', - [ - 'v' => $this->config->getAppValue(self::APP_ID, 'cache', '0'), - ] - ); - - Util::addHeader( - 'link', - [ - 'href' => $stylesheet, - 'rel' => 'stylesheet', + $assets = [ + 'stylesheet' => [ + 'route' => 'side_menu.Css.stylesheet', + 'type' => 'link', + 'route_attr' => 'href', + 'attr' => [ + 'rel' => 'stylesheet', + ], ], - '' - ); - - Util::addHeader( - 'script', - [ - 'src' => $script, - 'nonce' => $this->cspnm->getNonce(), + 'script' => [ + 'route' => 'side_menu.Js.script', + 'type' => 'script', + 'route_attr' => 'src', + 'attr' => [ + 'nonce' => $this->cspnm->getNonce(), + ], ], - '' - ); + ]; + + $cache = $this->config->getAppValue(self::APP_ID, 'cache', '0'); + + foreach ($assets as $key => $value) { + $route = OC::$server->getURLGenerator()->linkToRoute($value['route'], ['v' => $cache]); + $value['attr'][$value['route_attr']] = $route; + + Util::addHeader($value['type'], $value['attr'], ''); + } + } + + public function register(IRegistrationContext $context): void + { + $context->registerService('AppRepository', function (ContainerInterface $c) { + return new AppRepository(); + }); + + $context->registerService('CategoryRepository', function (ContainerInterface $c) { + return new CategoryRepository(); + }); + + $context->registerService('ConfigProxy', function (ContainerInterface $c) { + return new ConfigProxy(); + }); + } + + public function boot(IBootContext $context): void + { + $this->config = OC::$server->getConfig(); + $this->cspnm = OC::$server->getContentSecurityPolicyNonceManager(); + $this->user = OC::$server[IUserSession::class]->getUser(); + + if (!$this->isEnabled()) { + return; + } + + $this->addAssets(); } }