From c3e34039e9dce1814931de29009ca8f14f3f3c1a Mon Sep 17 00:00:00 2001
From: Bart Jaskulski <bjaskulski@protonmail.com>
Date: Thu, 3 Oct 2024 12:54:29 +0200
Subject: [PATCH] fix: compile container with version-aware name

Thanks to this, whenever client updates the plugin, cached container is
recompiled. Should we miss that, we might end up in a situation, when
definitions from v1 are used in plugin's v2 and lead to fatal error on
any constructor changes in classes included in container.

Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com>
---
 src/Kernel.php | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/src/Kernel.php b/src/Kernel.php
index c1b4559..0f6cbeb 100644
--- a/src/Kernel.php
+++ b/src/Kernel.php
@@ -90,16 +90,12 @@ final class Kernel {
 	}
 
 	private function get_container_name( Plugin $plugin ): string {
-		return str_replace( '-', '_', $plugin->get_slug() ) . '_container';
+		return preg_replace( '/[^\w_]/', '_', implode("_", [ $plugin->get_slug(), $plugin->get_version(), 'container' ]) );
 	}
 
 	private function initialize_container( Plugin $plugin ): Container {
 		$original_builder = new DiBuilder();
 
-		// If there's a cache file, use it as we are in production environment.
-		// Otherwise, build the container from scratch and use live version, without compilation.
-		//
-		// On failure, restart container compilation, without cache.
 		if ( $this->config->get( 'debug', false ) === false ) {
 			$original_builder->enableCompilation(
 				$this->get_cache_path(),
-- 
GitLab