Merge pull request #9461 from creative-commoners/pulls/4/remove-db-config-glob

Cache results of _configure_database.php glob
This commit is contained in:
Loz Calver 2020-04-20 08:45:48 +01:00 committed by GitHub
commit e08bf1cdd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 4 deletions

View File

@ -33,3 +33,7 @@ SilverStripe\Core\Injector\Injector:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: 'ThemeResourceLoader'
Psr\SimpleCache\CacheInterface.DatabaseAdapterRegistry:
factory: SilverStripe\Core\Cache\CacheFactory
constructor:
namespace: 'DatabaseAdapterRegistry'

View File

@ -3,7 +3,10 @@
namespace SilverStripe\Dev\Install;
use InvalidArgumentException;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Core\Flushable;
/**
* This class keeps track of the available database adapters
@ -12,7 +15,7 @@ use SilverStripe\Dev\Deprecation;
*
* @author Tom Rix
*/
class DatabaseAdapterRegistry
class DatabaseAdapterRegistry implements Flushable
{
/**
@ -144,14 +147,48 @@ class DatabaseAdapterRegistry
} else {
$databaseConfig = $config;
}
// Search through all composer packages in vendor, updating $databaseConfig
foreach (glob(BASE_PATH . '/vendor/*/*/_configure_database.php') as $configFile) {
include_once $configFile;
foreach (static::getConfigureDatabasePaths() as $configureDatabasePath) {
include_once $configureDatabasePath;
}
// Update modified variable
$config = $databaseConfig;
}
/**
* Including _configure_database.php is a legacy method of configuring a database
* It's still used by https://github.com/silverstripe/silverstripe-sqlite3
*/
protected static function getConfigureDatabasePaths(): array
{
// autoconfigure() will get called before flush() on ?flush, so manually flush just to ensure no weirdness
if (isset($_GET['flush'])) {
static::flush();
}
$cache = static::getCache();
$key = __FUNCTION__;
if ($cache->has($key)) {
return (array) $cache->get($key);
} else {
try {
$paths = glob(BASE_PATH . '/vendor/*/*/_configure_database.php');
} catch (Exception $e) {
$paths = [];
}
$cache->set($key, $paths);
return $paths;
}
}
public static function getCache(): CacheInterface
{
return Injector::inst()->get(CacheInterface::class . '.DatabaseAdapterRegistry');
}
public static function flush()
{
static::getCache()->clear();
}
/**
* Return all registered adapters
*