From 922feaedbdae60833b6c696eb21ada653a7405a4 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Tue, 9 Apr 2024 10:33:28 +0000
Subject: [PATCH] add command to reset compiled di container, fixes #3979

Closes #3979

Merge request studip/studip!2829
---
 cli/Commands/DI/Reset.php   | 30 ++++++++++++++++++++++++++++++
 cli/studip                  |  1 +
 lib/classes/DIContainer.php | 15 ++++++++++++++-
 3 files changed, 45 insertions(+), 1 deletion(-)
 create mode 100644 cli/Commands/DI/Reset.php

diff --git a/cli/Commands/DI/Reset.php b/cli/Commands/DI/Reset.php
new file mode 100644
index 00000000000..646bfb6eaa2
--- /dev/null
+++ b/cli/Commands/DI/Reset.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Studip\Cli\Commands\DI;
+
+use Studip\DIContainer;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class Reset extends Command
+{
+    protected static $defaultName = 'di:reset';
+
+    protected function configure(): void
+    {
+        $this->setDescription('Resets the compiled DI container');
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $file = DIContainer::getCompilationPath() . '/' . DIContainer::getCompilationClass() . '.php';
+
+        if (file_exists($file) && !unlink($file)) {
+            $output->writeln('<error>Could not removed compiled file.</error>');
+            return Command::FAILURE;
+        }
+
+        return Command::SUCCESS;
+    }
+}
diff --git a/cli/studip b/cli/studip
index a46a00debd5..971ad8e6368 100755
--- a/cli/studip
+++ b/cli/studip
@@ -33,6 +33,7 @@ $commands = [
     Commands\Cronjobs\CronjobWorker::class,
     Commands\DB\Dump::class,
     Commands\DB\MoveMatrikelnummer::class,
+    Commands\DI\Reset::class,
     Commands\Files\Dump::class,
     Commands\Fix\Biest7789::class,
     Commands\Fix\Biest7866::class,
diff --git a/lib/classes/DIContainer.php b/lib/classes/DIContainer.php
index 818d1910267..307ed4ad718 100644
--- a/lib/classes/DIContainer.php
+++ b/lib/classes/DIContainer.php
@@ -47,7 +47,10 @@ class DIContainer
     {
         $builder = new ContainerBuilder();
         if (\Studip\ENV == 'production') {
-            $builder->enableCompilation($GLOBALS['TMP_PATH']);
+            $builder->enableCompilation(
+                self::getCompilationPath(),
+                self::getCompilationClass()
+            );
         }
         $builder->ignorePhpDocErrors(true);
         $builder->addDefinitions('lib/bootstrap-definitions.php');
@@ -60,4 +63,14 @@ class DIContainer
 
         return $builder;
     }
+
+    public static function getCompilationPath(): string
+    {
+        return $GLOBALS['TMP_PATH'];
+    }
+
+    public static function getCompilationClass(): string
+    {
+        return 'CompiledContainer';
+    }
 }
-- 
GitLab