diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | lib/private/InitialStateService.php | 74 | ||||
-rw-r--r-- | lib/private/Server.php | 3 | ||||
-rw-r--r-- | lib/private/TemplateLayout.php | 4 | ||||
-rw-r--r-- | lib/public/IInitialStateService.php | 56 |
6 files changed, 141 insertions, 0 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 28f0cb0ea0c..c75e8f95fe9 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -284,6 +284,7 @@ return array( 'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php', 'OCP\\IImage' => $baseDir . '/lib/public/IImage.php', + 'OCP\\IInitialStateService' => $baseDir . '/lib/public/IInitialStateService.php', 'OCP\\IL10N' => $baseDir . '/lib/public/IL10N.php', 'OCP\\ILogger' => $baseDir . '/lib/public/ILogger.php', 'OCP\\IMemcache' => $baseDir . '/lib/public/IMemcache.php', @@ -854,6 +855,7 @@ return array( 'OC\\Http\\Client\\ClientService' => $baseDir . '/lib/private/Http/Client/ClientService.php', 'OC\\Http\\Client\\Response' => $baseDir . '/lib/private/Http/Client/Response.php', 'OC\\Http\\CookieHelper' => $baseDir . '/lib/private/Http/CookieHelper.php', + 'OC\\InitialStateService' => $baseDir . '/lib/private/InitialStateService.php', 'OC\\Installer' => $baseDir . '/lib/private/Installer.php', 'OC\\IntegrityCheck\\Checker' => $baseDir . '/lib/private/IntegrityCheck/Checker.php', 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException' => $baseDir . '/lib/private/IntegrityCheck/Exceptions/InvalidSignatureException.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 840fd3acc34..4e1c5f67c36 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -314,6 +314,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php', 'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php', 'OCP\\IImage' => __DIR__ . '/../../..' . '/lib/public/IImage.php', + 'OCP\\IInitialStateService' => __DIR__ . '/../../..' . '/lib/public/IInitialStateService.php', 'OCP\\IL10N' => __DIR__ . '/../../..' . '/lib/public/IL10N.php', 'OCP\\ILogger' => __DIR__ . '/../../..' . '/lib/public/ILogger.php', 'OCP\\IMemcache' => __DIR__ . '/../../..' . '/lib/public/IMemcache.php', @@ -884,6 +885,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Http\\Client\\ClientService' => __DIR__ . '/../../..' . '/lib/private/Http/Client/ClientService.php', 'OC\\Http\\Client\\Response' => __DIR__ . '/../../..' . '/lib/private/Http/Client/Response.php', 'OC\\Http\\CookieHelper' => __DIR__ . '/../../..' . '/lib/private/Http/CookieHelper.php', + 'OC\\InitialStateService' => __DIR__ . '/../../..' . '/lib/private/InitialStateService.php', 'OC\\Installer' => __DIR__ . '/../../..' . '/lib/private/Installer.php', 'OC\\IntegrityCheck\\Checker' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Checker.php', 'OC\\IntegrityCheck\\Exceptions\\InvalidSignatureException' => __DIR__ . '/../../..' . '/lib/private/IntegrityCheck/Exceptions/InvalidSignatureException.php', diff --git a/lib/private/InitialStateService.php b/lib/private/InitialStateService.php new file mode 100644 index 00000000000..df7f94fbda1 --- /dev/null +++ b/lib/private/InitialStateService.php @@ -0,0 +1,74 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OC; + +use OCP\IInitialStateService; +use OCP\ILogger; + +class InitialStateService implements IInitialStateService { + + /** @var ILogger */ + private $logger; + + /** @var array */ + private $states = []; + + /** @var array */ + private $lazyStates = []; + + public function __construct(ILogger $logger) { + $this->logger = $logger; + } + + public function provideInitialState(string $appName, $data) { + // Scalars and JsonSerializable are fine + if (is_scalar($data) || $data instanceof \JsonSerializable || is_array($data)) { + $this->states[$appName] = json_encode($data); + return; + } + + $this->logger->warning('Invalid data provided to provideInitialState by ' . $appName); + } + + public function provideLazyInitialState(string $appName, \Closure $closure) { + $this->lazyStates[$appName] = $closure; + } + + public function getInitialStates(): array { + $states = $this->states; + foreach ($this->lazyStates as $app => $lazyState) { + $state = $lazyState(); + + if (!($lazyState instanceof \JsonSerializable)) { + $this->logger->warning($app . ' provided an invalid lazy state'); + } + + $states[$app] = json_encode($state); + } + + return $states; + } + +} diff --git a/lib/private/Server.php b/lib/private/Server.php index a20676e5271..86d304af5ef 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -144,6 +144,7 @@ use OCP\GlobalScale\IConfig; use OCP\Group\ISubAdmin; use OCP\ICacheFactory; use OCP\IDBConnection; +use OCP\IInitialStateService; use OCP\IL10N; use OCP\IServerContainer; use OCP\ITempManager; @@ -1204,6 +1205,8 @@ class Server extends ServerContainer implements IServerContainer { $this->registerAlias(ISubAdmin::class, SubAdmin::class); + $this->registerAlias(IInitialStateService::class, InitialStateService::class); + $this->connectDispatcher(); } diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index 284f14c5db4..e4a2724de67 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -220,6 +220,10 @@ class TemplateLayout extends \OC_Template { } } + + /** @var InitialStateService $initialState */ + $initialState = \OC::$server->query(InitialStateService::class); + $this->assign('initialStates', $initialState->getInitialStates()); } /** diff --git a/lib/public/IInitialStateService.php b/lib/public/IInitialStateService.php new file mode 100644 index 00000000000..ff6144e0d45 --- /dev/null +++ b/lib/public/IInitialStateService.php @@ -0,0 +1,56 @@ +<?php +declare(strict_types=1); +/** + * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> + * + * @author Roeland Jago Douma <roeland@famdouma.nl> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP; + +/** + * @since 16.0.0 + */ +interface IInitialStateService { + /** + * Allows an app to provide its initial state to the template system. + * Use this if you know your initial state sill be used for example if + * you are in the render function of you controller. + * + * @since 16.0.0 + * + * @param string $appName + * @param bool|int|float|string|array|\JsonSerializable $data + */ + public function provideInitialState(string $appName, $data); + + /** + * Allows an app to provide its initial state via a lazy method. + * This will call the closure when the template is being generated. + * Use this if your app is injected into pages. Since then the render method + * is not called explicitly. But we do not want to load the state on webdav + * requests for example. + * + * @since 16.0.0 + * + * @param string $appName + * @param \Closure $closure Has to return an object that implements JsonSerializable + */ + public function provideLazyInitialState(string $appName, \Closure $closure); +} |