From 0e2f00ec59424b2ef5708a928856e2c75abe6deb Mon Sep 17 00:00:00 2001
From: Morris Jobke <hey@morrisjobke.de>
Date: Fri, 24 Nov 2017 10:27:58 +0100
Subject: [PATCH] Get the Installer via DI

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
---
 core/Command/App/Install.php         |  8 +----
 core/Command/Maintenance/Install.php | 13 ++++++--
 core/Command/Upgrade.php             |  8 +++--
 core/ajax/update.php                 |  3 +-
 core/register_command.php            |  2 +-
 lib/base.php                         | 12 ++++++--
 lib/private/Server.php               | 10 +++++++
 lib/private/Setup.php                | 28 ++++++++++--------
 lib/private/Updater.php              | 17 +++++------
 lib/private/legacy/app.php           | 16 ++--------
 lib/private/legacy/util.php          | 11 +++++--
 settings/ajax/updateapp.php          |  8 +----
 tests/lib/InstallerTest.php          | 44 ++++++++++++++++------------
 tests/lib/SetupTest.php              |  6 +++-
 tests/lib/UpdaterTest.php            | 13 ++++++--
 15 files changed, 116 insertions(+), 83 deletions(-)

diff --git a/core/Command/App/Install.php b/core/Command/App/Install.php
index c8c4652d7ba..8f530975be9 100644
--- a/core/Command/App/Install.php
+++ b/core/Command/App/Install.php
@@ -51,13 +51,7 @@ class Install extends Command {
 		}
 
 		try {
-			$installer = new Installer(
-				\OC::$server->getAppFetcher(),
-				\OC::$server->getHTTPClientService(),
-				\OC::$server->getTempManager(),
-				\OC::$server->getLogger(),
-				\OC::$server->getConfig()
-			);
+			$installer = \OC::$server->query(Installer::class);
 			$installer->downloadApp($appId);
 			$result = $installer->installApp($appId);
 		} catch(\Exception $e) {
diff --git a/core/Command/Maintenance/Install.php b/core/Command/Maintenance/Install.php
index 755d3d386c1..be6a2da22c8 100644
--- a/core/Command/Maintenance/Install.php
+++ b/core/Command/Maintenance/Install.php
@@ -30,6 +30,7 @@
 namespace OC\Core\Command\Maintenance;
 
 use InvalidArgumentException;
+use OC\Installer;
 use OC\Setup;
 use OC\SystemConfig;
 use OCP\Defaults;
@@ -73,9 +74,15 @@ class Install extends Command {
 
 		// validate the environment
 		$server = \OC::$server;
-		$setupHelper = new Setup($this->config, $server->getIniWrapper(),
-			$server->getL10N('lib'), $server->query(Defaults::class), $server->getLogger(),
-			$server->getSecureRandom());
+		$setupHelper = new Setup(
+			$this->config,
+			$server->getIniWrapper(),
+			$server->getL10N('lib'),
+			$server->query(Defaults::class),
+			$server->getLogger(),
+			$server->getSecureRandom(),
+			\OC::$server->query(Installer::class)
+		);
 		$sysInfo = $setupHelper->getSystemInfo(true);
 		$errors = $sysInfo['errors'];
 		if (count($errors) > 0) {
diff --git a/core/Command/Upgrade.php b/core/Command/Upgrade.php
index 0877c19bc01..2a502dbe921 100644
--- a/core/Command/Upgrade.php
+++ b/core/Command/Upgrade.php
@@ -35,6 +35,7 @@
 namespace OC\Core\Command;
 
 use OC\Console\TimestampFormatter;
+use OC\Installer;
 use OC\Updater;
 use OCP\IConfig;
 use OCP\ILogger;
@@ -63,11 +64,13 @@ class Upgrade extends Command {
 	/**
 	 * @param IConfig $config
 	 * @param ILogger $logger
+	 * @param Installer $installer
 	 */
-	public function __construct(IConfig $config, ILogger $logger) {
+	public function __construct(IConfig $config, ILogger $logger, Installer $installer) {
 		parent::__construct();
 		$this->config = $config;
 		$this->logger = $logger;
+		$this->installer = $installer;
 	}
 
 	protected function configure() {
@@ -101,7 +104,8 @@ class Upgrade extends Command {
 			$updater = new Updater(
 					$this->config,
 					\OC::$server->getIntegrityCodeChecker(),
-					$this->logger
+					$this->logger,
+					$this->installer
 			);
 
 			if ($input->getOption('no-app-disable')) {
diff --git a/core/ajax/update.php b/core/ajax/update.php
index 71d60f5c432..2a29d1e536c 100644
--- a/core/ajax/update.php
+++ b/core/ajax/update.php
@@ -116,7 +116,8 @@ if (OC::checkUpgrade(false)) {
 	$updater = new \OC\Updater(
 			$config,
 			\OC::$server->getIntegrityCodeChecker(),
-			$logger
+			$logger,
+			\OC::$server->query(\OC\Installer::class)
 	);
 	$incompatibleApps = [];
 	$disabledThirdPartyApps = [];
diff --git a/core/register_command.php b/core/register_command.php
index 3f7fbf508e5..60e151a5f2c 100644
--- a/core/register_command.php
+++ b/core/register_command.php
@@ -137,7 +137,7 @@ if (\OC::$server->getConfig()->getSystemValue('installed', false)) {
 	$application->add(new OC\Core\Command\Maintenance\UpdateHtaccess());
 	$application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory()));
 
-	$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger()));
+	$application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->query(\OC\Installer::class)));
 	$application->add(new OC\Core\Command\Maintenance\Repair(
 		new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(),
 		\OC::$server->getEventDispatcher(), \OC::$server->getAppManager()));
diff --git a/lib/base.php b/lib/base.php
index dc09d0f533d..30f80695680 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -915,9 +915,15 @@ class OC {
 		// Check if Nextcloud is installed or in maintenance (update) mode
 		if (!$systemConfig->getValue('installed', false)) {
 			\OC::$server->getSession()->clear();
-			$setupHelper = new OC\Setup(\OC::$server->getSystemConfig(), \OC::$server->getIniWrapper(),
-				\OC::$server->getL10N('lib'), \OC::$server->query(\OCP\Defaults::class), \OC::$server->getLogger(),
-				\OC::$server->getSecureRandom());
+			$setupHelper = new OC\Setup(
+				\OC::$server->getSystemConfig(),
+				\OC::$server->getIniWrapper(),
+				\OC::$server->getL10N('lib'),
+				\OC::$server->query(\OCP\Defaults::class),
+				\OC::$server->getLogger(),
+				\OC::$server->getSecureRandom(),
+				\OC::$server->query(\OC\Installer::class)
+			);
 			$controller = new OC\Core\Controller\SetupController($setupHelper);
 			$controller->run($_POST);
 			exit();
diff --git a/lib/private/Server.php b/lib/private/Server.php
index faa0ce2f2ac..0c6338f6a4c 100644
--- a/lib/private/Server.php
+++ b/lib/private/Server.php
@@ -1099,6 +1099,16 @@ class Server extends ServerContainer implements IServerContainer {
 				$c->query(\OCP\Share\IManager::class)
 			);
 		});
+
+		$this->registerService(Installer::class, function(Server $c) {
+			return new Installer(
+				$c->getAppFetcher(),
+				$c->getHTTPClientService(),
+				$c->getTempManager(),
+				$c->getLogger(),
+				$c->getConfig()
+			);
+		});
 	}
 
 	/**
diff --git a/lib/private/Setup.php b/lib/private/Setup.php
index 8214db2d4ef..92246e8322e 100644
--- a/lib/private/Setup.php
+++ b/lib/private/Setup.php
@@ -65,6 +65,8 @@ class Setup {
 	protected $logger;
 	/** @var ISecureRandom */
 	protected $random;
+	/** @var Installer */
+	protected $installer;
 
 	/**
 	 * @param SystemConfig $config
@@ -73,13 +75,15 @@ class Setup {
 	 * @param Defaults $defaults
 	 * @param ILogger $logger
 	 * @param ISecureRandom $random
+	 * @param Installer $installer
 	 */
 	public function __construct(SystemConfig $config,
 						 IniGetWrapper $iniWrapper,
 						 IL10N $l10n,
 						 Defaults $defaults,
 						 ILogger $logger,
-						 ISecureRandom $random
+						 ISecureRandom $random,
+						 Installer $installer
 		) {
 		$this->config = $config;
 		$this->iniWrapper = $iniWrapper;
@@ -87,6 +91,7 @@ class Setup {
 		$this->defaults = $defaults;
 		$this->logger = $logger;
 		$this->random = $random;
+		$this->installer = $installer;
 	}
 
 	static protected $dbSetupClasses = [
@@ -371,18 +376,11 @@ class Setup {
 
 			// Install shipped apps and specified app bundles
 			Installer::installShippedApps();
-			$installer = new Installer(
-				\OC::$server->getAppFetcher(),
-				\OC::$server->getHTTPClientService(),
-				\OC::$server->getTempManager(),
-				\OC::$server->getLogger(),
-				\OC::$server->getConfig()
-			);
 			$bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib'));
 			$defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle();
 			foreach($defaultInstallationBundles as $bundle) {
 				try {
-					$installer->installAppBundle($bundle);
+					$this->installer->installAppBundle($bundle);
 				} catch (Exception $e) {}
 			}
 
@@ -444,9 +442,15 @@ class Setup {
 			$webRoot = !empty(\OC::$WEBROOT) ? \OC::$WEBROOT : '/';
 		}
 
-		$setupHelper = new \OC\Setup($config, \OC::$server->getIniWrapper(),
-			\OC::$server->getL10N('lib'), \OC::$server->query(Defaults::class), \OC::$server->getLogger(),
-			\OC::$server->getSecureRandom());
+		$setupHelper = new \OC\Setup(
+			$config,
+			\OC::$server->getIniWrapper(),
+			\OC::$server->getL10N('lib'),
+			\OC::$server->query(Defaults::class),
+			\OC::$server->getLogger(),
+			\OC::$server->getSecureRandom(),
+			\OC::$server->query(Installer::class)
+		);
 
 		$htaccessContent = file_get_contents($setupHelper->pathToHtaccess());
 		$content = "#### DO NOT CHANGE ANYTHING ABOVE THIS LINE ####\n";
diff --git a/lib/private/Updater.php b/lib/private/Updater.php
index 4f5bb45ae15..5c8d5a2cd4e 100644
--- a/lib/private/Updater.php
+++ b/lib/private/Updater.php
@@ -63,6 +63,9 @@ class Updater extends BasicEmitter {
 	/** @var Checker */
 	private $checker;
 
+	/** @var Installer */
+	private $installer;
+
 	/** @var bool */
 	private $skip3rdPartyAppsDisable;
 
@@ -78,13 +81,16 @@ class Updater extends BasicEmitter {
 	 * @param IConfig $config
 	 * @param Checker $checker
 	 * @param ILogger $log
+	 * @param Installer $installer
 	 */
 	public function __construct(IConfig $config,
 								Checker $checker,
-								ILogger $log = null) {
+								ILogger $log = null,
+								Installer $installer) {
 		$this->log = $log;
 		$this->config = $config;
 		$this->checker = $checker;
+		$this->installer = $installer;
 
 		// If at least PHP 7.0.0 is used we don't need to disable apps as we catch
 		// fatal errors and exceptions and disable the app just instead.
@@ -461,17 +467,10 @@ class Updater extends BasicEmitter {
 	private function upgradeAppStoreApps(array $disabledApps) {
 		foreach($disabledApps as $app) {
 			try {
-				$installer = new Installer(
-					\OC::$server->getAppFetcher(),
-					\OC::$server->getHTTPClientService(),
-					\OC::$server->getTempManager(),
-					$this->log,
-					\OC::$server->getConfig()
-				);
 				$this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
 				if (Installer::isUpdateAvailable($app, \OC::$server->getAppFetcher())) {
 					$this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]);
-					$installer->updateAppstoreApp($app);
+					$this->installer->updateAppstoreApp($app);
 				}
 				$this->emit('\OC\Updater', 'checkAppStoreApp', [$app]);
 			} catch (\Exception $ex) {
diff --git a/lib/private/legacy/app.php b/lib/private/legacy/app.php
index d2b0f96d593..1b9fc28873e 100644
--- a/lib/private/legacy/app.php
+++ b/lib/private/legacy/app.php
@@ -375,13 +375,7 @@ class OC_App {
 		self::$enabledAppsCache = []; // flush
 
 		// Check if app is already downloaded
-		$installer = new Installer(
-			\OC::$server->getAppFetcher(),
-			\OC::$server->getHTTPClientService(),
-			\OC::$server->getTempManager(),
-			\OC::$server->getLogger(),
-			\OC::$server->getConfig()
-		);
+		$installer = \OC::$server->query(Installer::class);
 		$isDownloaded = $installer->isDownloaded($appId);
 
 		if(!$isDownloaded) {
@@ -415,13 +409,7 @@ class OC_App {
 			return false;
 		}
 
-		$installer = new Installer(
-			\OC::$server->getAppFetcher(),
-			\OC::$server->getHTTPClientService(),
-			\OC::$server->getTempManager(),
-			\OC::$server->getLogger(),
-			\OC::$server->getConfig()
-		);
+		$installer = \OC::$server->query(Installer::class);
 		return $installer->removeApp($app);
 	}
 
diff --git a/lib/private/legacy/util.php b/lib/private/legacy/util.php
index 1e9090960c1..3ce11746672 100644
--- a/lib/private/legacy/util.php
+++ b/lib/private/legacy/util.php
@@ -708,8 +708,15 @@ class OC_Util {
 		}
 
 		$webServerRestart = false;
-		$setup = new \OC\Setup($config, \OC::$server->getIniWrapper(), \OC::$server->getL10N('lib'),
-			\OC::$server->query(\OCP\Defaults::class), \OC::$server->getLogger(), \OC::$server->getSecureRandom());
+		$setup = new \OC\Setup(
+			$config,
+			\OC::$server->getIniWrapper(),
+			\OC::$server->getL10N('lib'),
+			\OC::$server->query(\OCP\Defaults::class),
+			\OC::$server->getLogger(),
+			\OC::$server->getSecureRandom(),
+			\OC::$server->query(\OC\Installer::class)
+		);
 
 		$urlGenerator = \OC::$server->getURLGenerator();
 
diff --git a/settings/ajax/updateapp.php b/settings/ajax/updateapp.php
index 7b615cb56bb..3431c68dbd7 100644
--- a/settings/ajax/updateapp.php
+++ b/settings/ajax/updateapp.php
@@ -41,13 +41,7 @@ $appId = OC_App::cleanAppId($appId);
 $config = \OC::$server->getConfig();
 $config->setSystemValue('maintenance', true);
 try {
-	$installer = new \OC\Installer(
-		\OC::$server->getAppFetcher(),
-		\OC::$server->getHTTPClientService(),
-		\OC::$server->getTempManager(),
-		\OC::$server->getLogger(),
-		\OC::$server->getConfig()
-	);
+	$installer = \OC::$server->query(\OC\Installer::class);
 	$result = $installer->updateAppstoreApp($appId);
 	$config->setSystemValue('maintenance', false);
 } catch(Exception $ex) {
diff --git a/tests/lib/InstallerTest.php b/tests/lib/InstallerTest.php
index 107b9dcb41f..68f3676faa8 100644
--- a/tests/lib/InstallerTest.php
+++ b/tests/lib/InstallerTest.php
@@ -40,9 +40,6 @@ class InstallerTest extends TestCase {
 	/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
 	private $config;
 
-	/** @var Installer */
-	private $installer;
-
 	protected function setUp() {
 		parent::setUp();
 
@@ -51,13 +48,6 @@ class InstallerTest extends TestCase {
 		$this->tempManager = $this->createMock(ITempManager::class);
 		$this->logger = $this->createMock(ILogger::class);
 		$this->config = $this->createMock(IConfig::class);
-		$this->installer = new Installer(
-			$this->appFetcher,
-			$this->clientService,
-			$this->tempManager,
-			$this->logger,
-			$this->config
-		);
 
 		$config = \OC::$server->getConfig();
 		$this->appstore = $config->setSystemValue('appstoreenabled', true);
@@ -72,6 +62,16 @@ class InstallerTest extends TestCase {
 		$installer->removeApp(self::$appid);
 	}
 
+	protected function getInstaller() {
+		return new Installer(
+			$this->appFetcher,
+			$this->clientService,
+			$this->tempManager,
+			$this->logger,
+			$this->config
+		);
+	}
+
 	protected function tearDown() {
 		$installer = new Installer(
 			\OC::$server->getAppFetcher(),
@@ -197,7 +197,8 @@ gLgK8d8sKL60JMmKHN3boHrsThKBVA==
 			->willReturn($appArray);
 
 
-		$this->installer->downloadApp('news');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('news');
 	}
 
 	/**
@@ -239,7 +240,8 @@ YSu356M=
 			->method('get')
 			->willReturn($appArray);
 
-		$this->installer->downloadApp('news');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('news');
 	}
 
 	/**
@@ -281,7 +283,8 @@ cR92p/PYCFXkAKP3OO0RPlf6dXNKTw==
 			->method('get')
 			->willReturn($appArray);
 
-		$this->installer->downloadApp('news');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('news');
 	}
 
 	/**
@@ -348,7 +351,8 @@ cR92p/PYCFXkAKP3OO0RPlf6dXNKTw==
 			->method('newClient')
 			->willReturn($client);
 
-		$this->installer->downloadApp('passman');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('passman');
 	}
 
 	/**
@@ -431,7 +435,8 @@ YwDVP+QmNRzx72jtqAN/Kc3CvQ9nkgYhU65B95aX0xA=',
 			->method('newClient')
 			->willReturn($client);
 
-		$this->installer->downloadApp('testapp');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('testapp');
 	}
 
 	/**
@@ -513,7 +518,8 @@ YwDVP+QmNRzx72jtqAN/Kc3CvQ9nkgYhU65B95aX0xA=',
 			->method('newClient')
 			->willReturn($client);
 
-		$this->installer->downloadApp('testapp');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('testapp');
 	}
 
 	public function testDownloadAppSuccessful() {
@@ -591,7 +597,8 @@ MPLX6f5V9tCJtlH6ztmEcDROfvuVc0U3rEhqx2hphoyo+MZrPFpdcJL8KkIdMKbY
 			->method('newClient')
 			->willReturn($client);
 
-		$this->installer->downloadApp('testapp');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('testapp');
 
 		$this->assertTrue(file_exists(__DIR__ . '/../../apps/testapp/appinfo/info.xml'));
 		$this->assertEquals('0.9', \OC_App::getAppVersionByPath(__DIR__ . '/../../apps/testapp/'));
@@ -679,7 +686,8 @@ JXhrdaWDZ8fzpUjugrtC3qslsqL0dzgU37anS3HwrT8=',
 		$this->assertTrue(file_exists(__DIR__ . '/../../apps/testapp/appinfo/info.xml'));
 		$this->assertEquals('0.9', \OC_App::getAppVersionByPath(__DIR__ . '/../../apps/testapp/'));
 
-		$this->installer->downloadApp('testapp');
+		$installer = $this->getInstaller();
+		$installer->downloadApp('testapp');
 		$this->assertTrue(file_exists(__DIR__ . '/../../apps/testapp/appinfo/info.xml'));
 		$this->assertEquals('0.8', \OC_App::getAppVersionByPath(__DIR__ . '/../../apps/testapp/'));
 	}
diff --git a/tests/lib/SetupTest.php b/tests/lib/SetupTest.php
index 78c35a5b0bb..e6e9fb5c56c 100644
--- a/tests/lib/SetupTest.php
+++ b/tests/lib/SetupTest.php
@@ -9,6 +9,7 @@
 namespace Test;
 
 use bantu\IniGetWrapper\IniGetWrapper;
+use OC\Installer;
 use OC\SystemConfig;
 use OCP\Defaults;
 use OCP\IL10N;
@@ -31,6 +32,8 @@ class SetupTest extends \Test\TestCase {
 	protected $logger;
 	/** @var \OCP\Security\ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
 	protected $random;
+	/** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
+	protected $installer;
 
 	protected function setUp() {
 		parent::setUp();
@@ -41,9 +44,10 @@ class SetupTest extends \Test\TestCase {
 		$this->defaults = $this->createMock(Defaults::class);
 		$this->logger = $this->createMock(ILogger::class);
 		$this->random = $this->createMock(ISecureRandom::class);
+		$this->installer = $this->createMock(Installer::class);
 		$this->setupClass = $this->getMockBuilder('\OC\Setup')
 			->setMethods(['class_exists', 'is_callable', 'getAvailableDbDriversForPdo'])
-			->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10n, $this->defaults, $this->logger, $this->random])
+			->setConstructorArgs([$this->config, $this->iniWrapper, $this->l10n, $this->defaults, $this->logger, $this->random, $this->installer])
 			->getMock();
 	}
 
diff --git a/tests/lib/UpdaterTest.php b/tests/lib/UpdaterTest.php
index afa0635768d..a6a8224ac34 100644
--- a/tests/lib/UpdaterTest.php
+++ b/tests/lib/UpdaterTest.php
@@ -22,6 +22,7 @@
 
 namespace Test;
 
+use OC\Installer;
 use OC\Updater;
 use OCP\IConfig;
 use OCP\ILogger;
@@ -36,6 +37,8 @@ class UpdaterTest extends TestCase {
 	private $updater;
 	/** @var Checker | \PHPUnit_Framework_MockObject_MockObject */
 	private $checker;
+	/** @var Installer|\PHPUnit_Framework_MockObject_MockObject */
+	private $installer;
 
 	public function setUp() {
 		parent::setUp();
@@ -46,13 +49,17 @@ class UpdaterTest extends TestCase {
 			->disableOriginalConstructor()
 			->getMock();
 		$this->checker = $this->getMockBuilder(Checker::class)
-				->disableOriginalConstructor()
-				->getMock();
+			->disableOriginalConstructor()
+			->getMock();
+		$this->installer = $this->getMockBuilder(Installer::class)
+			->disableOriginalConstructor()
+			->getMock();
 
 		$this->updater = new Updater(
 			$this->config,
 			$this->checker,
-			$this->logger
+			$this->logger,
+			$this->installer
 		);
 	}
 
-- 
GitLab