Skip to content

Commit 52b88a0

Browse files
Merge pull request #736 from nextcloud/backport/722/stable22
[stable22] Allow specify a config prefix for another database connection
2 parents 837be37 + 96cab25 commit 52b88a0

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

lib/AppInfo/Application.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,34 @@
2323

2424
namespace OCA\Activity\AppInfo;
2525

26+
use OC\DB\ConnectionAdapter;
2627
use OC\Files\View;
28+
use OC\SystemConfig;
2729
use OCA\Activity\Capabilities;
2830
use OCA\Activity\Consumer;
31+
use OCA\Activity\Data;
2932
use OCA\Activity\FilesHooksStatic;
3033
use OCA\Activity\Hooks;
3134
use OCA\Activity\Listener\LoadSidebarScripts;
35+
use OCA\Activity\MailQueueHandler;
3236
use OCA\Activity\NotificationGenerator;
3337
use OCA\Files\Event\LoadSidebar;
38+
use OCP\Activity\IManager;
3439
use OCP\AppFramework\App;
3540
use OCP\AppFramework\Bootstrap\IBootContext;
3641
use OCP\AppFramework\Bootstrap\IBootstrap;
3742
use OCP\AppFramework\Bootstrap\IRegistrationContext;
43+
use OCP\IConfig;
44+
use OCP\IDateTimeFormatter;
45+
use OCP\IDBConnection;
46+
use OCP\ILogger;
47+
use OCP\IURLGenerator;
48+
use OCP\IUserManager;
49+
use OCP\L10N\IFactory;
50+
use OCP\Mail\IMailer;
51+
use OCP\RichObjectStrings\IValidator;
3852
use OCP\Util;
53+
use Psr\Container\ContainerInterface;
3954

4055
class Application extends App implements IBootstrap {
4156
public const APP_ID = 'activity';
@@ -44,7 +59,68 @@ public function __construct() {
4459
parent::__construct(self::APP_ID);
4560
}
4661

62+
/**
63+
* @psalm-suppress UndefinedClass
64+
*/
4765
public function register(IRegistrationContext $context): void {
66+
$context->registerService('ActivityDBConnection', function (ContainerInterface $c) {
67+
$systemConfig = $c->get(SystemConfig::class);
68+
$factory = new \OC\DB\ConnectionFactory($systemConfig);
69+
$type = $systemConfig->getValue('dbtype', 'sqlite');
70+
if (!$factory->isValidType($type)) {
71+
/** @psalm-suppress InvalidThrow */
72+
throw new \OC\DatabaseException('Invalid database type');
73+
}
74+
$connectionParams = $factory->createConnectionParams('activity_');
75+
$connection = $factory->getConnection($type, $connectionParams);
76+
/** @psalm-suppress MissingDependency */
77+
$connection->getConfiguration()->setSQLLogger($c->get(\OCP\Diagnostics\IQueryLogger::class));
78+
return $connection;
79+
});
80+
81+
/**
82+
* @psalm-suppress UndefinedClass
83+
*/
84+
$context->registerService('ActivityConnectionAdapter', function (ContainerInterface $c) {
85+
$systemConfig = $c->get(SystemConfig::class);
86+
$configPrefix = 'activity_';
87+
88+
if ($systemConfig->getValue($configPrefix . 'dbuser', null) === null &&
89+
$systemConfig->getValue($configPrefix . 'dbpassword', null) === null &&
90+
$systemConfig->getValue($configPrefix . 'dbname', null) === null &&
91+
$systemConfig->getValue($configPrefix . 'dbhost', null) === null &&
92+
$systemConfig->getValue($configPrefix . 'dbport', null) === null &&
93+
$systemConfig->getValue($configPrefix . 'dbdriveroptions', null) === null) {
94+
return $c->get(IDBConnection::class);
95+
}
96+
97+
return new ConnectionAdapter(
98+
$c->get('ActivityDBConnection')
99+
);
100+
});
101+
102+
$context->registerService(Data::class, function (ContainerInterface $c) {
103+
return new Data(
104+
$c->get(IManager::class),
105+
$c->get('ActivityConnectionAdapter')
106+
);
107+
});
108+
109+
$context->registerService(MailQueueHandler::class, function (ContainerInterface $c) {
110+
return new MailQueueHandler(
111+
$c->get(IDateTimeFormatter::class),
112+
$c->get('ActivityConnectionAdapter'),
113+
$c->get(IMailer::class),
114+
$c->get(IURLGenerator::class),
115+
$c->get(IUserManager::class),
116+
$c->get(IFactory::class),
117+
$c->get(IManager::class),
118+
$c->get(IValidator::class),
119+
$c->get(IConfig::class),
120+
$c->get(ILogger::class)
121+
);
122+
});
123+
48124
// Allow automatic DI for the View, until we migrated to Nodes API
49125
$context->registerService(View::class, function () {
50126
return new View('');

0 commit comments

Comments
 (0)