From 19a0a9a4e7e9e511f5f3d99c697e57e7bb0260e6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julius=20H=C3=A4rtl?= <jus@bitgrid.net>
Date: Mon, 29 Jan 2018 13:09:32 +0100
Subject: [PATCH] Move getAppInfo and getAppVersion to IAppManager
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Julius Härtl <jus@bitgrid.net>
---
 lib/private/App/AppManager.php | 50 ++++++++++++++++++++++++++++------
 lib/private/legacy/app.php     | 50 +++++-----------------------------
 lib/public/App.php             |  4 ++-
 lib/public/App/IAppManager.php | 20 ++++++++++++++
 4 files changed, 72 insertions(+), 52 deletions(-)

diff --git a/lib/private/App/AppManager.php b/lib/private/App/AppManager.php
index f2b396b29d0..0e921ba1b7f 100644
--- a/lib/private/App/AppManager.php
+++ b/lib/private/App/AppManager.php
@@ -80,6 +80,12 @@ class AppManager implements IAppManager {
 	/** @var string[] */
 	private $alwaysEnabled;
 
+	/** @var array */
+	private $appInfos = [];
+
+	/** @var array */
+	private $appVersions = [];
+
 	/**
 	 * @param IUserSession $userSession
 	 * @param AppConfig $appConfig
@@ -341,17 +347,45 @@ class AppManager implements IAppManager {
 	 *
 	 * @param string $appId app id
 	 *
+	 * @param bool $path
+	 * @param null $lang
 	 * @return array app info
-	 *
-	 * @internal
 	 */
-	public function getAppInfo($appId) {
-		$appInfo = \OC_App::getAppInfo($appId);
-		if (!isset($appInfo['version'])) {
-			// read version from separate file
-			$appInfo['version'] = \OC_App::getAppVersion($appId);
+	public function getAppInfo(string $appId, bool $path = false, $lang = null) {
+		if ($path) {
+			$file = $appId;
+		} else {
+			if ($lang === null && isset($this->appInfos[$appId])) {
+				return $this->appInfos[$appId];
+			}
+			try {
+				$appPath = $this->getAppPath($appId);
+			} catch (AppPathNotFoundException $e) {
+				return null;
+			}
+			$file = $appPath . '/appinfo/info.xml';
+		}
+
+		$parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
+		$data = $parser->parse($file);
+
+		if (is_array($data)) {
+			$data = \OC_App::parseAppInfo($data, $lang);
+		}
+
+		if ($lang === null) {
+			$this->appInfos[$appId] = $data;
+		}
+
+		return $data;
+	}
+
+	public function getAppVersion(string $appId, bool $useCache = true) {
+		if(!$useCache || !isset($this->appVersions[$appId])) {
+			$appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
+			$this->appVersions[$appId] = ($appInfo !== null) ? $appInfo['version'] : '0';
 		}
-		return $appInfo;
+		return $this->appVersions[$appId];
 	}
 
 	/**
diff --git a/lib/private/legacy/app.php b/lib/private/legacy/app.php
index f8f49d22115..6364bff36ef 100644
--- a/lib/private/legacy/app.php
+++ b/lib/private/legacy/app.php
@@ -50,7 +50,6 @@
  *
  */
 use OC\App\DependencyAnalyzer;
-use OC\App\InfoParser;
 use OC\App\Platform;
 use OC\DB\MigrationService;
 use OC\Installer;
@@ -63,10 +62,8 @@ use OCP\App\ManagerEvent;
  * upgrading and removing apps.
  */
 class OC_App {
-	static private $appVersion = [];
 	static private $adminForms = array();
 	static private $personalForms = array();
-	static private $appInfo = array();
 	static private $appTypes = array();
 	static private $loadedApps = array();
 	static private $altLogin = array();
@@ -590,15 +587,10 @@ class OC_App {
 	 * @param string $appId
 	 * @param bool $useCache
 	 * @return string
+	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion()
 	 */
 	public static function getAppVersion($appId, $useCache = true) {
-		if($useCache && isset(self::$appVersion[$appId])) {
-			return self::$appVersion[$appId];
-		}
-
-		$file = self::getAppPath($appId);
-		self::$appVersion[$appId] = ($file !== false) ? self::getAppVersionByPath($file) : '0';
-		return self::$appVersion[$appId];
+		return \OC::$server->getAppManager()->getAppVersion($appId, $useCache);
 	}
 
 	/**
@@ -609,7 +601,7 @@ class OC_App {
 	 */
 	public static function getAppVersionByPath($path) {
 		$infoFile = $path . '/appinfo/info.xml';
-		$appData = self::getAppInfo($infoFile, true);
+		$appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
 		return isset($appData['version']) ? $appData['version'] : '';
 	}
 
@@ -622,39 +614,10 @@ class OC_App {
 	 * @param string $lang
 	 * @return array|null
 	 * @note all data is read from info.xml, not just pre-defined fields
+	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo()
 	 */
 	public static function getAppInfo($appId, $path = false, $lang = null) {
-		if ($path) {
-			$file = $appId;
-		} else {
-			if ($lang === null && isset(self::$appInfo[$appId])) {
-				return self::$appInfo[$appId];
-			}
-			$appPath = self::getAppPath($appId);
-			if($appPath === false) {
-				return null;
-			}
-			$file = $appPath . '/appinfo/info.xml';
-		}
-
-		$parser = new InfoParser(\OC::$server->getMemCacheFactory()->createLocal('core.appinfo'));
-		$data = $parser->parse($file);
-
-		if (is_array($data)) {
-			$data = OC_App::parseAppInfo($data, $lang);
-		}
-		if(isset($data['ocsid'])) {
-			$storedId = \OC::$server->getConfig()->getAppValue($appId, 'ocsid');
-			if($storedId !== '' && $storedId !== $data['ocsid']) {
-				$data['ocsid'] = $storedId;
-			}
-		}
-
-		if ($lang === null) {
-			self::$appInfo[$appId] = $data;
-		}
-
-		return $data;
+		return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang);
 	}
 
 	/**
@@ -1068,7 +1031,8 @@ class OC_App {
 
 		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
 		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
-		unset(self::$appVersion[$appId]);
+		// update appversion in app manager
+		\OC::$server->getAppManager()->getAppVersion($appId, false);
 
 		// run upgrade code
 		if (file_exists($appPath . '/appinfo/update.php')) {
diff --git a/lib/public/App.php b/lib/public/App.php
index 3afd36f2c96..d44f719b7ea 100644
--- a/lib/public/App.php
+++ b/lib/public/App.php
@@ -114,6 +114,7 @@ class App {
 	 * @param string $app id of the app or the path of the info.xml file
 	 * @param boolean $path (optional)
 	 * @return array|null
+	 * @deprecated 14.0.0 ise \OC::$server->getAppManager()->getAppInfo($appId)
 	 * @since 4.0.0
 	*/
 	public static function getAppInfo( $app, $path=false ) {
@@ -148,8 +149,9 @@ class App {
 	 * @param string $app
 	 * @return string
 	 * @since 4.0.0
+	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion($appId)
 	 */
 	public static function getAppVersion( $app ) {
-		return \OC_App::getAppVersion( $app );
+		return \OC::$server->getAppManager()->getAppVersion($app);
 	}
 }
diff --git a/lib/public/App/IAppManager.php b/lib/public/App/IAppManager.php
index f602a6de81e..0c087b61515 100644
--- a/lib/public/App/IAppManager.php
+++ b/lib/public/App/IAppManager.php
@@ -36,6 +36,26 @@ use OCP\IUser;
  * @since 8.0.0
  */
 interface IAppManager {
+
+	/**
+	 * Returns the app information from "appinfo/info.xml".
+	 *
+	 * @param string $appId
+	 * @return mixed
+	 * @since 14.0.0
+	 */
+	public function getAppInfo(string $appId, bool $path = false, $lang = null);
+
+	/**
+	 * Returns the app information from "appinfo/info.xml".
+	 *
+	 * @param string $appId
+	 * @param bool $useCache
+	 * @return mixed
+	 * @since 14.0.0
+	 */
+	public function getAppVersion(string $appId, bool $useCache = true);
+
 	/**
 	 * Check if an app is enabled for user
 	 *
-- 
GitLab