diff --git a/src/library/FOSSBilling/Doctrine/EntityManagerFactory.php b/src/library/FOSSBilling/Doctrine/EntityManagerFactory.php index cee77ee12..d9a569ef3 100644 --- a/src/library/FOSSBilling/Doctrine/EntityManagerFactory.php +++ b/src/library/FOSSBilling/Doctrine/EntityManagerFactory.php @@ -16,6 +16,7 @@ use Doctrine\ORM\Mapping\UnderscoreNamingStrategy; use Doctrine\ORM\ORMSetup; use Doctrine\ORM\Proxy\ProxyFactory; use FOSSBilling\Environment; +use SplFileInfo; use Symfony\Component\Filesystem\Path; use Symfony\Component\Finder\Finder; @@ -25,11 +26,15 @@ class EntityManagerFactory { $finder = new Finder(); $finder->directories()->in(PATH_MODS . '/*/Entity')->depth('== 0'); - $moduleEntityPaths = iterator_to_array($finder); + $moduleEntityPaths = array_map( + static fn (SplFileInfo $directory): string => $directory->getPathname(), + iterator_to_array($finder) + ); - $config = ORMSetup::createAttributeMetadataConfiguration( + $config = ORMSetup::createAttributeMetadataConfig( paths: $moduleEntityPaths, - isDevMode: Environment::isDevelopment() + isDevMode: Environment::isDevelopment(), + cacheNamespaceSeed: self::getCacheNamespaceSeed($moduleEntityPaths) ); $config->setNamingStrategy(new UnderscoreNamingStrategy(CASE_LOWER)); // Consistency with already existing RedBean tables @@ -52,4 +57,27 @@ class EntityManagerFactory return new EntityManager($connection, $config); } + + /** + * Build a cache namespace seed that changes when local entity definitions change. + * This prevents stale production metadata caches from surviving reinstalls/upgrades. + * + * @param list $entityDirectories + */ + private static function getCacheNamespaceSeed(array $entityDirectories): string + { + if ($entityDirectories === []) { + return PATH_ROOT; + } + + $finder = new Finder(); + $finder->files()->in($entityDirectories)->name('*.php')->sortByName(); + + $seed = [PATH_ROOT]; + foreach ($finder as $file) { + $seed[] = sprintf('%s:%d:%d', $file->getPathname(), $file->getMTime(), $file->getSize()); + } + + return implode('|', $seed); + } } diff --git a/tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php b/tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php new file mode 100644 index 000000000..24d00e8ad --- /dev/null +++ b/tests-legacy/library/FOSSBilling/FOSSBilling_Doctrine_EntityManagerFactoryTest.php @@ -0,0 +1,43 @@ +mkdir($directory); + $filesystem->dumpFile($file, "invokePrivateStaticMethod('getCacheNamespaceSeed', [[$directory]]); + + sleep(1); + $filesystem->dumpFile($file, "invokePrivateStaticMethod('getCacheNamespaceSeed', [[$directory]]); + + $this->assertNotSame($seedBefore, $seedAfter); + } finally { + $filesystem->remove($directory); + } + } + + private function invokePrivateStaticMethod(string $method, array $args = []): mixed + { + $reflection = new ReflectionClass(EntityManagerFactory::class); + $methodReflection = $reflection->getMethod($method); + + return $methodReflection->invokeArgs(null, $args); + } +}