]> source.dussan.org Git - nextcloud-server.git/commitdiff
Sort app scripts by dependencies (Fixes: #30278)
authorJonas Meurer <jonas@freesources.org>
Wed, 15 Dec 2021 12:41:36 +0000 (13:41 +0100)
committerJonas Meurer <jonas@freesources.org>
Mon, 20 Dec 2021 10:40:10 +0000 (11:40 +0100)
Signed-off-by: Jonas Meurer <jonas@freesources.org>
lib/public/Util.php

index d0b23ddd3e057d935b051c7bb33be4335b46b147..c22ee95563c1ca28052e8367f38d688fffc0407c 100644 (file)
@@ -78,6 +78,9 @@ class Util {
        /** @var array */
        private static $scripts = [];
 
+       /** @var array */
+       private static $scriptDeps = [];
+
        /**
         * get the current installed version of Nextcloud
         * @return array
@@ -216,13 +219,15 @@ class Util {
                //      ]
                // ]
                if (!empty($afterAppId)) {
-                       // init afterAppId app array if it doesn't exists
+                       // init afterAppId app array if it doesn't exist
                        if (!array_key_exists($afterAppId, self::$scripts)) {
                                self::$scripts[$afterAppId] = ['first' => [], 'last' => []];
                        }
                        self::$scripts[$afterAppId]['last'][] = $path;
+                       // store dependency
+                       self::$scriptDeps[$application] = $afterAppId;
                } else {
-                       // init app array if it doesn't exists
+                       // init app array if it doesn't exist
                        if (!array_key_exists($application, self::$scripts)) {
                                self::$scripts[$application] = ['first' => [], 'last' => []];
                        }
@@ -237,12 +242,31 @@ class Util {
         */
        public static function getScripts(): array {
                // merging first and last data set
-               $mapFunc = function (array $scriptsArray): array {
+               $mapFunc = static function (array $scriptsArray): array {
                        return array_merge(...array_values($scriptsArray));
                };
                $appScripts = array_map($mapFunc, self::$scripts);
+
+               // Sort by dependency if any
+               $sortedScripts = [];
+               foreach ($appScripts as $app => $scripts) {
+                       if (array_key_exists($app, self::$scriptDeps)) {
+                               $dep = self::$scriptDeps[$app];
+                               if (!array_key_exists($dep, $sortedScripts)) {
+                                       $sortedScripts[$dep] = [];
+                               }
+                               array_push($sortedScripts[$dep], ...$scripts);
+                       } else {
+                               if (!array_key_exists($app, $sortedScripts)) {
+                                       $sortedScripts[$app] = [];
+                               }
+                               array_unshift($sortedScripts[$app], ...$scripts);
+                       }
+               }
+
                // sort core first
-               $scripts = array_merge(isset($appScripts['core']) ? $appScripts['core'] : [], ...array_values($appScripts));
+               $scripts = array_merge(isset($sortedScripts['core']) ? $sortedScripts['core'] : [], ...array_values($sortedScripts));
+
                // remove duplicates
                return array_unique($scripts);
        }