mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-01 04:40:45 +09:00
Add cache namespace seed for entity metadata (#3559)
This commit is contained in:
1 parent
b2be307766
commit
3bd8e853b5
2 files changed
+74
-3
No files matched your search
@@ -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<string> $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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use FOSSBilling\Doctrine\EntityManagerFactory;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Filesystem\Path;
|
||||
|
||||
#[Group('Core')]
|
||||
final class FOSSBilling_Doctrine_EntityManagerFactoryTest extends BBTestCase
|
||||
{
|
||||
public function testCacheNamespaceSeedChangesWhenEntityDefinitionChanges(): void
|
||||
{
|
||||
$filesystem = new Filesystem();
|
||||
$directory = Path::join(sys_get_temp_dir(), 'fb-doctrine-seed-' . bin2hex(random_bytes(8)));
|
||||
$file = Path::join($directory, 'Currency.php');
|
||||
|
||||
try {
|
||||
$filesystem->mkdir($directory);
|
||||
$filesystem->dumpFile($file, "<?php\nfinal class Currency {}\n");
|
||||
|
||||
$seedBefore = $this->invokePrivateStaticMethod('getCacheNamespaceSeed', [[$directory]]);
|
||||
|
||||
sleep(1);
|
||||
$filesystem->dumpFile($file, "<?php\nfinal class Currency { public string \$code; }\n");
|
||||
|
||||
$seedAfter = $this->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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user