From 2b072ef4e6e37ba9deb37baefb0ee5e44ffd914d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrgen=20Haas?= <juergen.haas@lakedrops.com>
Date: Sun, 24 Apr 2022 11:48:53 +0200
Subject: [PATCH] Cleanup configuration by removing non-existent imports Fix a
 syntax issue

---
 src/Handler.php | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/Handler.php b/src/Handler.php
index fdba9b2..9788b25 100644
--- a/src/Handler.php
+++ b/src/Handler.php
@@ -56,8 +56,28 @@ class Handler extends BaseHandler {
         }
       }
     }
+
+    // Cleanup configuration: remove non-existant includes.
+    foreach ($ahoy['commands'] as $command => $commands) {
+      if (isset($commands['imports']) && is_array($commands['imports'])) {
+        $deleteKeys = [];
+        foreach ($commands['imports'] as $key => $import) {
+          if (!file_exists($import)) {
+            $deleteKeys[] = $key;
+          }
+        }
+        arsort($deleteKeys);
+        foreach ($deleteKeys as $deleteKey) {
+          unset($ahoy['commands'][$command]['imports'][$deleteKey]);
+        }
+        if (empty($ahoy['commands'][$command]['imports'])) {
+          unset($ahoy['commands'][$command]);
+        }
+      }
+    }
+
     $file = $rootDir . '/.ahoy.yml';
-    file_put_contents($file, $rendered = Yaml::dump($ahoy, 9, 2));
+    file_put_contents($file, Yaml::dump($ahoy, 9, 2));
     $this->gitIgnore('.ahoy.yml');
   }
 
-- 
GitLab