Merge pull request #17509 from nextcloud/fix/application-singleton

Fix Application instances created multiple times
This commit is contained in:
John Molakvoæ 2019-11-04 18:18:32 +01:00 committed by GitHub
commit 13960b69da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 96 additions and 75 deletions

View File

@ -29,7 +29,8 @@ use Symfony\Component\EventDispatcher\GenericEvent;
\OC_App::loadApps(['dav']); \OC_App::loadApps(['dav']);
$app = new Application(); /** @var Application $app */
$app = \OC::$server->query(Application::class);
$app->registerHooks(); $app->registerHooks();
\OC::$server->registerService('CardDAVSyncService', function() use ($app) { \OC::$server->registerService('CardDAVSyncService', function() use ($app) {

View File

@ -28,7 +28,8 @@ namespace OCA\Encryption\AppInfo;
$encryptionSystemReady = \OC::$server->getEncryptionManager()->isReady(); $encryptionSystemReady = \OC::$server->getEncryptionManager()->isReady();
$app = new Application(); /** @var Application $app */
$app = \OC::$server->query(Application::class);
if ($encryptionSystemReady) { if ($encryptionSystemReady) {
$app->registerEncryptionModule(); $app->registerEncryptionModule();
$app->registerHooks(); $app->registerHooks();

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* *
@ -21,41 +22,37 @@
* *
*/ */
return [
namespace OCA\Encryption\AppInfo; 'routes' => [
[
(new Application())->registerRoutes($this, array('routes' => array( 'name' => 'Recovery#adminRecovery',
'url' => '/ajax/adminRecovery',
[ 'verb' => 'POST'
'name' => 'Recovery#adminRecovery', ],
'url' => '/ajax/adminRecovery', [
'verb' => 'POST' 'name' => 'Settings#updatePrivateKeyPassword',
], 'url' => '/ajax/updatePrivateKeyPassword',
[ 'verb' => 'POST'
'name' => 'Settings#updatePrivateKeyPassword', ],
'url' => '/ajax/updatePrivateKeyPassword', [
'verb' => 'POST' 'name' => 'Settings#setEncryptHomeStorage',
], 'url' => '/ajax/setEncryptHomeStorage',
[ 'verb' => 'POST'
'name' => 'Settings#setEncryptHomeStorage', ],
'url' => '/ajax/setEncryptHomeStorage', [
'verb' => 'POST' 'name' => 'Recovery#changeRecoveryPassword',
], 'url' => '/ajax/changeRecoveryPassword',
[ 'verb' => 'POST'
'name' => 'Recovery#changeRecoveryPassword', ],
'url' => '/ajax/changeRecoveryPassword', [
'verb' => 'POST' 'name' => 'Recovery#userSetRecovery',
], 'url' => '/ajax/userSetRecovery',
[ 'verb' => 'POST'
'name' => 'Recovery#userSetRecovery', ],
'url' => '/ajax/userSetRecovery', [
'verb' => 'POST' 'name' => 'Status#getStatus',
], 'url' => '/ajax/getStatus',
[ 'verb' => 'GET'
'name' => 'Status#getStatus', ],
'url' => '/ajax/getStatus',
'verb' => 'GET'
] ]
];
)));

View File

@ -22,5 +22,6 @@
namespace OCA\Federation\AppInfo; namespace OCA\Federation\AppInfo;
$app = new Application(); /** @var Application $app */
$app = \OC::$server->query(Application::class);
$app->registerHooks(); $app->registerHooks();

View File

@ -29,7 +29,8 @@ declare(strict_types=1);
*/ */
namespace OCA\Files\AppInfo; namespace OCA\Files\AppInfo;
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$application->registerRoutes( $application->registerRoutes(
$this, $this,
[ [

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2016, ownCloud, Inc. * @copyright Copyright (c) 2016, ownCloud, Inc.
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
@ -22,10 +23,7 @@
* *
*/ */
namespace OCA\Files_Trashbin\AppInfo; return [
$application = new Application();
$application->registerRoutes($this, [
'routes' => [ 'routes' => [
[ [
'name' => 'Preview#getPreview', 'name' => 'Preview#getPreview',
@ -33,4 +31,4 @@ $application->registerRoutes($this, [
'verb' => 'GET', 'verb' => 'GET',
], ],
], ],
]); ];

View File

@ -46,12 +46,7 @@ class Application extends App {
/* /*
* Register expiration * Register expiration
*/ */
$container->registerService('Expiration', function($c) { $container->registerAlias('Expiration', Expiration::class);
return new Expiration(
$c->query('ServerContainer')->getConfig(),
$c->query(ITimeFactory::class)
);
});
/* /*
* Register $principalBackend for the DAV collection * Register $principalBackend for the DAV collection

View File

@ -62,7 +62,8 @@ class ExpireTrash extends \OC\BackgroundJob\TimedJob {
} }
protected function fixDIForJobs() { protected function fixDIForJobs() {
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$this->userManager = \OC::$server->getUserManager(); $this->userManager = \OC::$server->getUserManager();
$this->expiration = $application->getContainer()->query('Expiration'); $this->expiration = $application->getContainer()->query('Expiration');
} }

View File

@ -49,7 +49,11 @@ class Expiration {
public function __construct(IConfig $config,ITimeFactory $timeFactory){ public function __construct(IConfig $config,ITimeFactory $timeFactory){
$this->timeFactory = $timeFactory; $this->timeFactory = $timeFactory;
$this->retentionObligation = $config->getSystemValue('trashbin_retention_obligation', 'auto'); $this->setRetentionObligation($config->getSystemValue('trashbin_retention_obligation', 'auto'));
}
public function setRetentionObligation(string $obligation) {
$this->retentionObligation = $obligation;
if ($this->retentionObligation !== 'disabled') { if ($this->retentionObligation !== 'disabled') {
$this->parseRetentionObligation(); $this->parseRetentionObligation();

View File

@ -747,7 +747,8 @@ class Trashbin {
*/ */
private static function scheduleExpire($user) { private static function scheduleExpire($user) {
// let the admin disable auto expire // let the admin disable auto expire
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$expiration = $application->getContainer()->query('Expiration'); $expiration = $application->getContainer()->query('Expiration');
if ($expiration->isEnabled()) { if ($expiration->isEnabled()) {
\OC::$server->getCommandBus()->push(new Expire($user)); \OC::$server->getCommandBus()->push(new Expire($user));
@ -764,7 +765,8 @@ class Trashbin {
* @return int size of deleted files * @return int size of deleted files
*/ */
protected static function deleteFiles($files, $user, $availableSpace) { protected static function deleteFiles($files, $user, $availableSpace) {
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$expiration = $application->getContainer()->query('Expiration'); $expiration = $application->getContainer()->query('Expiration');
$size = 0; $size = 0;
@ -791,8 +793,8 @@ class Trashbin {
* @return integer[] size of deleted files and number of deleted files * @return integer[] size of deleted files and number of deleted files
*/ */
public static function deleteExpiredFiles($files, $user) { public static function deleteExpiredFiles($files, $user) {
$application = new Application(); /** @var Expiration $expiration */
$expiration = $application->getContainer()->query('Expiration'); $expiration = \OC::$server->query(Expiration::class);
$size = 0; $size = 0;
$count = 0; $count = 0;
foreach ($files as $file) { foreach ($files as $file) {

View File

@ -67,7 +67,7 @@ class TrashbinTest extends \Test\TestCase {
// clear share hooks // clear share hooks
\OC_Hook::clear('OCP\\Share'); \OC_Hook::clear('OCP\\Share');
\OC::registerShareHooks(); \OC::registerShareHooks();
$application = new \OCA\Files_Sharing\AppInfo\Application(); $application = \OC::$server->query(\OCA\Files_Sharing\AppInfo\Application::class);
$application->registerMountProviders(); $application->registerMountProviders();
//disable encryption //disable encryption
@ -76,7 +76,9 @@ class TrashbinTest extends \Test\TestCase {
$config = \OC::$server->getConfig(); $config = \OC::$server->getConfig();
//configure trashbin //configure trashbin
self::$rememberRetentionObligation = $config->getSystemValue('trashbin_retention_obligation', \OCA\Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION); self::$rememberRetentionObligation = $config->getSystemValue('trashbin_retention_obligation', \OCA\Files_Trashbin\Expiration::DEFAULT_RETENTION_OBLIGATION);
$config->setSystemValue('trashbin_retention_obligation', 'auto, 2'); /** @var \OCA\Files_Trashbin\Expiration $expiration */
$expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class);
$expiration->setRetentionObligation('auto, 2');
// register hooks // register hooks
\OCA\Files_Trashbin\Trashbin::registerHooks(); \OCA\Files_Trashbin\Trashbin::registerHooks();
@ -94,7 +96,9 @@ class TrashbinTest extends \Test\TestCase {
$user->delete(); $user->delete();
} }
\OC::$server->getConfig()->setSystemValue('trashbin_retention_obligation', self::$rememberRetentionObligation); /** @var \OCA\Files_Trashbin\Expiration $expiration */
$expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class);
$expiration->setRetentionObligation(self::$rememberRetentionObligation);
\OC_Hook::clear(); \OC_Hook::clear();
@ -152,7 +156,9 @@ class TrashbinTest extends \Test\TestCase {
*/ */
public function testExpireOldFiles() { public function testExpireOldFiles() {
$currentTime = time(); /** @var \OCP\AppFramework\Utility\ITimeFactory $time */
$time = \OC::$server->query(\OCP\AppFramework\Utility\ITimeFactory::class);
$currentTime = $time->getTime();
$expireAt = $currentTime - 2 * 24 * 60 * 60; $expireAt = $currentTime - 2 * 24 * 60 * 60;
$expiredDate = $currentTime - 3 * 24 * 60 * 60; $expiredDate = $currentTime - 3 * 24 * 60 * 60;
@ -684,9 +690,9 @@ class TrashbinForTesting extends \OCA\Files_Trashbin\Trashbin {
* @param OCP\Files\FileInfo[] $files * @param OCP\Files\FileInfo[] $files
* @param integer $limit * @param integer $limit
*/ */
public function dummyDeleteExpiredFiles($files, $limit) { public function dummyDeleteExpiredFiles($files) {
// dummy value for $retention_obligation because it is not needed here // dummy value for $retention_obligation because it is not needed here
return parent::deleteExpiredFiles($files, TrashbinTest::TEST_TRASHBIN_USER1, $limit, 0); return parent::deleteExpiredFiles($files, TrashbinTest::TEST_TRASHBIN_USER1);
} }
/** /**

View File

@ -26,7 +26,8 @@
namespace OCA\Files_Versions\AppInfo; namespace OCA\Files_Versions\AppInfo;
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$application->registerRoutes($this, [ $application->registerRoutes($this, [
'routes' => [ 'routes' => [
[ [

View File

@ -850,8 +850,8 @@ class Storage {
* @return Expiration * @return Expiration
*/ */
protected static function getExpiration(){ protected static function getExpiration(){
if (is_null(self::$application)) { if (self::$application === null) {
self::$application = new Application(); self::$application = \OC::$server->query(Application::class);
} }
return self::$application->getContainer()->query(Expiration::class); return self::$application->getContainer()->query(Expiration::class);
} }

View File

@ -25,9 +25,19 @@ declare(strict_types=1);
namespace OCA\Provisioning_API; namespace OCA\Provisioning_API;
use OCA\FederatedFileSharing\AppInfo\Application; use OCA\FederatedFileSharing\AppInfo\Application;
use OCP\IServerContainer;
class FederatedFileSharingFactory { class FederatedFileSharingFactory {
public function get(): Application {
return new Application(); /** @var IServerContainer */
private $serverContainer;
public function __construct(IServerContainer $serverContainer) {
$this->serverContainer = $serverContainer;
} }
public function get(): Application {
return $this->serverContainer->query(Application::class);
}
} }

View File

@ -38,7 +38,8 @@ namespace OCA\Settings;
use OCA\Settings\AppInfo\Application; use OCA\Settings\AppInfo\Application;
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$this->useCollection('root'); $this->useCollection('root');
$application->registerRoutes($this, [ $application->registerRoutes($this, [
'resources' => [ 'resources' => [

View File

@ -90,7 +90,7 @@ class PersonalInfo implements ISettings {
$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing'); $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
$lookupServerUploadEnabled = false; $lookupServerUploadEnabled = false;
if($federatedFileSharingEnabled) { if($federatedFileSharingEnabled) {
$federatedFileSharing = new Application(); $federatedFileSharing = \OC::$server->query(Application::class);
$shareProvider = $federatedFileSharing->getFederatedShareProvider(); $shareProvider = $federatedFileSharing->getFederatedShareProvider();
$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled(); $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
} }

View File

@ -34,7 +34,8 @@
use OC\Core\Application; use OC\Core\Application;
$application = new Application(); /** @var Application $application */
$application = \OC::$server->query(Application::class);
$application->registerRoutes($this, [ $application->registerRoutes($this, [
'routes' => [ 'routes' => [
['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'],

View File

@ -727,7 +727,7 @@ class OC {
// Make sure that the application class is not loaded before the database is setup // Make sure that the application class is not loaded before the database is setup
if ($systemConfig->getValue("installed", false)) { if ($systemConfig->getValue("installed", false)) {
OC_App::loadApp('settings'); OC_App::loadApp('settings');
$settings = new \OCA\Settings\AppInfo\Application(); $settings = \OC::$server->query(\OCA\Settings\AppInfo\Application::class);
$settings->register(); $settings->register();
} }

View File

@ -371,7 +371,7 @@ class Router implements IRouter {
$applicationClassName = $appNameSpace . '\\AppInfo\\Application'; $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
if (class_exists($applicationClassName)) { if (class_exists($applicationClassName)) {
$application = new $applicationClassName(); $application = \OC::$server->query($applicationClassName);
} else { } else {
$application = new App($appName); $application = new App($appName);
} }

View File

@ -100,8 +100,9 @@ class ServerContainer extends SimpleContainer {
if (!isset($this->hasNoAppContainer[$namespace])) { if (!isset($this->hasNoAppContainer[$namespace])) {
$applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application'; $applicationClassName = 'OCA\\' . $sensitiveNamespace . '\\AppInfo\\Application';
if (class_exists($applicationClassName)) { if (class_exists($applicationClassName)) {
new $applicationClassName(); $app = new $applicationClassName();
if (isset($this->appContainers[$namespace])) { if (isset($this->appContainers[$namespace])) {
$this->appContainers[$namespace]->offsetSet($applicationClassName, $app);
return $this->appContainers[$namespace]; return $this->appContainers[$namespace];
} }
} }