2020-05-07 22:10:30 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
*
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2020-05-07 22:10:30 +03:00
|
|
|
*
|
|
|
|
* @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
|
2020-08-24 15:54:25 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2020-05-07 22:10:30 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\AppFramework\Bootstrap;
|
|
|
|
|
|
|
|
use Closure;
|
2020-06-17 16:17:59 +03:00
|
|
|
use OC\Support\CrashReport\Registry;
|
2020-05-07 22:10:30 +03:00
|
|
|
use OCP\AppFramework\App;
|
|
|
|
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
2020-06-23 16:23:28 +03:00
|
|
|
use OCP\Dashboard\IManager;
|
2020-05-07 22:10:30 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
class RegistrationContext {
|
|
|
|
|
|
|
|
/** @var array[] */
|
|
|
|
private $capabilities = [];
|
|
|
|
|
2020-06-17 16:17:59 +03:00
|
|
|
/** @var array[] */
|
|
|
|
private $crashReporters = [];
|
|
|
|
|
2020-06-23 16:23:28 +03:00
|
|
|
/** @var array[] */
|
|
|
|
private $dashboardPanels = [];
|
|
|
|
|
2020-05-07 22:10:30 +03:00
|
|
|
/** @var array[] */
|
|
|
|
private $services = [];
|
|
|
|
|
|
|
|
/** @var array[] */
|
|
|
|
private $aliases = [];
|
|
|
|
|
|
|
|
/** @var array[] */
|
|
|
|
private $parameters = [];
|
|
|
|
|
|
|
|
/** @var array[] */
|
|
|
|
private $eventListeners = [];
|
|
|
|
|
|
|
|
/** @var array[] */
|
|
|
|
private $middlewares = [];
|
|
|
|
|
2020-05-11 11:35:54 +03:00
|
|
|
/** @var array[] */
|
|
|
|
private $searchProviders = [];
|
|
|
|
|
2020-07-15 18:06:27 +03:00
|
|
|
/** @var array[] */
|
|
|
|
private $alternativeLogins = [];
|
|
|
|
|
2020-10-03 16:47:38 +03:00
|
|
|
/** @var array[] */
|
|
|
|
private $initialStates = [];
|
|
|
|
|
2020-05-07 22:10:30 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
public function __construct(ILogger $logger) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function for(string $appId): IRegistrationContext {
|
|
|
|
return new class($appId, $this) implements IRegistrationContext {
|
|
|
|
/** @var string */
|
|
|
|
private $appId;
|
|
|
|
|
|
|
|
/** @var RegistrationContext */
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
public function __construct(string $appId, RegistrationContext $context) {
|
|
|
|
$this->appId = $appId;
|
|
|
|
$this->context = $context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerCapability(string $capability): void {
|
|
|
|
$this->context->registerCapability(
|
|
|
|
$this->appId,
|
|
|
|
$capability
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:17:59 +03:00
|
|
|
public function registerCrashReporter(string $reporterClass): void {
|
|
|
|
$this->context->registerCrashReporter(
|
|
|
|
$this->appId,
|
|
|
|
$reporterClass
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-04 16:20:05 +03:00
|
|
|
public function registerDashboardWidget(string $widgetClass): void {
|
2020-06-23 16:23:28 +03:00
|
|
|
$this->context->registerDashboardPanel(
|
|
|
|
$this->appId,
|
2020-08-04 16:20:05 +03:00
|
|
|
$widgetClass
|
2020-06-23 16:23:28 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:10:30 +03:00
|
|
|
public function registerService(string $name, callable $factory, bool $shared = true): void {
|
|
|
|
$this->context->registerService(
|
|
|
|
$this->appId,
|
|
|
|
$name,
|
|
|
|
$factory,
|
|
|
|
$shared
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerServiceAlias(string $alias, string $target): void {
|
|
|
|
$this->context->registerServiceAlias(
|
|
|
|
$this->appId,
|
|
|
|
$alias,
|
|
|
|
$target
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerParameter(string $name, $value): void {
|
|
|
|
$this->context->registerParameter(
|
|
|
|
$this->appId,
|
|
|
|
$name,
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerEventListener(string $event, string $listener, int $priority = 0): void {
|
|
|
|
$this->context->registerEventListener(
|
|
|
|
$this->appId,
|
|
|
|
$event,
|
|
|
|
$listener,
|
|
|
|
$priority
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerMiddleware(string $class): void {
|
|
|
|
$this->context->registerMiddleware(
|
|
|
|
$this->appId,
|
|
|
|
$class
|
|
|
|
);
|
|
|
|
}
|
2020-05-11 11:35:54 +03:00
|
|
|
|
|
|
|
public function registerSearchProvider(string $class): void {
|
|
|
|
$this->context->registerSearchProvider(
|
|
|
|
$this->appId,
|
|
|
|
$class
|
|
|
|
);
|
|
|
|
}
|
2020-07-15 18:06:27 +03:00
|
|
|
|
|
|
|
public function registerAlternativeLogin(string $class): void {
|
|
|
|
$this->context->registerAlternativeLogin(
|
|
|
|
$this->appId,
|
|
|
|
$class
|
|
|
|
);
|
|
|
|
}
|
2020-10-03 16:47:38 +03:00
|
|
|
|
|
|
|
public function registerInitialStateProvider(string $class): void {
|
|
|
|
$this->context->registerInitialState(
|
|
|
|
$this->appId,
|
|
|
|
$class
|
|
|
|
);
|
|
|
|
}
|
2020-05-07 22:10:30 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerCapability(string $appId, string $capability): void {
|
|
|
|
$this->capabilities[] = [
|
|
|
|
'appId' => $appId,
|
|
|
|
'capability' => $capability
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:17:59 +03:00
|
|
|
public function registerCrashReporter(string $appId, string $reporterClass): void {
|
|
|
|
$this->crashReporters[] = [
|
|
|
|
'appId' => $appId,
|
|
|
|
'class' => $reporterClass,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:23:28 +03:00
|
|
|
public function registerDashboardPanel(string $appId, string $panelClass): void {
|
|
|
|
$this->dashboardPanels[] = [
|
|
|
|
'appId' => $appId,
|
|
|
|
'class' => $panelClass
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:10:30 +03:00
|
|
|
public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void {
|
|
|
|
$this->services[] = [
|
|
|
|
"appId" => $appId,
|
|
|
|
"name" => $name,
|
|
|
|
"factory" => $factory,
|
2020-10-12 15:19:41 +03:00
|
|
|
"shared" => $shared,
|
2020-05-07 22:10:30 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerServiceAlias(string $appId, string $alias, string $target): void {
|
|
|
|
$this->aliases[] = [
|
|
|
|
"appId" => $appId,
|
|
|
|
"alias" => $alias,
|
|
|
|
"target" => $target,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerParameter(string $appId, string $name, $value): void {
|
|
|
|
$this->parameters[] = [
|
|
|
|
"appId" => $appId,
|
|
|
|
"name" => $name,
|
|
|
|
"value" => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void {
|
|
|
|
$this->eventListeners[] = [
|
|
|
|
"appId" => $appId,
|
|
|
|
"event" => $event,
|
|
|
|
"listener" => $listener,
|
|
|
|
"priority" => $priority,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerMiddleware(string $appId, string $class): void {
|
|
|
|
$this->middlewares[] = [
|
|
|
|
"appId" => $appId,
|
|
|
|
"class" => $class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:35:54 +03:00
|
|
|
public function registerSearchProvider(string $appId, string $class) {
|
|
|
|
$this->searchProviders[] = [
|
|
|
|
'appId' => $appId,
|
|
|
|
'class' => $class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-07-15 18:06:27 +03:00
|
|
|
public function registerAlternativeLogin(string $appId, string $class): void {
|
|
|
|
$this->alternativeLogins[] = [
|
|
|
|
'appId' => $appId,
|
|
|
|
'class' => $class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-03 16:47:38 +03:00
|
|
|
public function registerInitialState(string $appId, string $class): void {
|
|
|
|
$this->initialStates[] = [
|
|
|
|
'appId' => $appId,
|
|
|
|
'class' => $class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:10:30 +03:00
|
|
|
/**
|
|
|
|
* @param App[] $apps
|
|
|
|
*/
|
|
|
|
public function delegateCapabilityRegistrations(array $apps): void {
|
2020-11-24 10:28:19 +03:00
|
|
|
while (($registration = array_shift($this->capabilities)) !== null) {
|
2020-05-07 22:10:30 +03:00
|
|
|
try {
|
|
|
|
$apps[$registration['appId']]
|
|
|
|
->getContainer()
|
|
|
|
->registerCapability($registration['capability']);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $registration['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during capability registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:17:59 +03:00
|
|
|
/**
|
|
|
|
* @param App[] $apps
|
|
|
|
*/
|
|
|
|
public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void {
|
2020-11-24 10:28:19 +03:00
|
|
|
while (($registration = array_shift($this->crashReporters)) !== null) {
|
2020-06-17 16:17:59 +03:00
|
|
|
try {
|
|
|
|
$registry->registerLazy($registration['class']);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $registration['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:23:28 +03:00
|
|
|
/**
|
|
|
|
* @param App[] $apps
|
|
|
|
*/
|
|
|
|
public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void {
|
2020-11-24 10:28:19 +03:00
|
|
|
while (($panel = array_shift($this->dashboardPanels)) !== null) {
|
2020-06-23 16:23:28 +03:00
|
|
|
try {
|
2020-08-04 16:20:05 +03:00
|
|
|
$dashboardManager->lazyRegisterWidget($panel['class']);
|
2020-06-23 16:23:28 +03:00
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $panel['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during dashboard registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 22:10:30 +03:00
|
|
|
public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void {
|
2020-11-24 10:28:19 +03:00
|
|
|
while (($registration = array_shift($this->eventListeners)) !== null) {
|
2020-05-07 22:10:30 +03:00
|
|
|
try {
|
|
|
|
if (isset($registration['priority'])) {
|
|
|
|
$eventDispatcher->addServiceListener(
|
|
|
|
$registration['event'],
|
|
|
|
$registration['listener'],
|
|
|
|
$registration['priority']
|
|
|
|
);
|
|
|
|
} else {
|
2020-06-18 16:00:19 +03:00
|
|
|
$eventDispatcher->addServiceListener(
|
2020-05-07 22:10:30 +03:00
|
|
|
$registration['event'],
|
|
|
|
$registration['listener']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $registration['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during event listener registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App[] $apps
|
|
|
|
*/
|
|
|
|
public function delegateContainerRegistrations(array $apps): void {
|
2020-11-24 10:28:19 +03:00
|
|
|
while (($registration = array_shift($this->services)) !== null) {
|
2020-05-07 22:10:30 +03:00
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* Register the service and convert the callable into a \Closure if necessary
|
|
|
|
*/
|
|
|
|
$apps[$registration['appId']]
|
|
|
|
->getContainer()
|
|
|
|
->registerService(
|
|
|
|
$registration['name'],
|
|
|
|
Closure::fromCallable($registration['factory']),
|
|
|
|
$registration['shared'] ?? true
|
|
|
|
);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $registration['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during service registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->aliases as $registration) {
|
|
|
|
try {
|
|
|
|
$apps[$registration['appId']]
|
|
|
|
->getContainer()
|
|
|
|
->registerAlias(
|
|
|
|
$registration['alias'],
|
|
|
|
$registration['target']
|
|
|
|
);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $registration['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->parameters as $registration) {
|
|
|
|
try {
|
|
|
|
$apps[$registration['appId']]
|
|
|
|
->getContainer()
|
|
|
|
->registerParameter(
|
|
|
|
$registration['name'],
|
|
|
|
$registration['value']
|
|
|
|
);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $registration['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during service alias registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param App[] $apps
|
|
|
|
*/
|
|
|
|
public function delegateMiddlewareRegistrations(array $apps): void {
|
2020-11-24 10:28:19 +03:00
|
|
|
while (($middleware = array_shift($this->middlewares)) !== null) {
|
2020-05-07 22:10:30 +03:00
|
|
|
try {
|
|
|
|
$apps[$middleware['appId']]
|
|
|
|
->getContainer()
|
|
|
|
->registerMiddleWare($middleware['class']);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$appId = $middleware['appId'];
|
|
|
|
$this->logger->logException($e, [
|
|
|
|
'message' => "Error during capability registration of $appId: " . $e->getMessage(),
|
|
|
|
'level' => ILogger::ERROR,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 11:35:54 +03:00
|
|
|
|
|
|
|
/**
|
2020-07-14 11:21:39 +03:00
|
|
|
* @return array[]
|
2020-05-11 11:35:54 +03:00
|
|
|
*/
|
2020-07-14 11:21:39 +03:00
|
|
|
public function getSearchProviders(): array {
|
|
|
|
return $this->searchProviders;
|
2020-05-11 11:35:54 +03:00
|
|
|
}
|
2020-07-15 18:06:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function getAlternativeLogins(): array {
|
|
|
|
return $this->alternativeLogins;
|
|
|
|
}
|
2020-10-03 16:47:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @erturn array[]
|
|
|
|
*/
|
|
|
|
public function getInitialStates(): array {
|
|
|
|
return $this->initialStates;
|
|
|
|
}
|
2020-05-07 22:10:30 +03:00
|
|
|
}
|