DatabaseCheckPlatformMysql::class, DoctrinePDOMySqlDriver::class => DatabaseCheckPlatformMysql::class, DoctrinePDOPgSqlDriver::class => DatabaseCheckPlatformPostgreSql::class, DoctrinePDOSqliteDriver::class => DatabaseCheckPlatformSqlite::class, ]; /** * @var string[] */ private static $driverMap = [ 'pdo_mysql' => DoctrinePDOMySqlDriver::class, 'pdo_sqlite' => DoctrinePDOSqliteDriver::class, 'pdo_pgsql' => DoctrinePDOPgSqlDriver::class, 'pdo_oci' => DoctrinePDOOCIDriver::class, 'oci8' => DoctrineOCI8Driver::class, 'ibm_db2' => DB2Driver::class, 'mysqli' => DoctrineMysqliDriver::class, ]; /** * List of database driver to check * * @var string[] */ private $databaseDriverCheckMap = [ DoctrineMysqliDriver::class => DatabaseCheckDriverMysqli::class, DoctrinePDOMySqlDriver::class => DatabaseCheckDriverPdoMysql::class, DoctrinePDOPgSqlDriver::class => DatabaseCheckDriverPDOPgSql::class, DoctrinePDOSqliteDriver::class => DatabaseCheckDriverPDOSqlite::class, ]; public function __construct( private ?ConnectionPool $connectionPool = null, ) { $this->messageQueue = new FlashMessageQueue('install-database-check'); } /** * Get status of each database platform identified to be installed on the system */ public function getStatus(): FlashMessageQueue { $installedDrivers = $this->identifyInstalledDatabaseDriver(); // check requirements of database platform for installed driver foreach ($installedDrivers as $driver) { try { $this->checkDatabasePlatformRequirements($driver); } catch (Exception $exception) { $this->messageQueue->enqueue( new FlashMessage( '', $exception->getMessage(), ContextualFeedbackSeverity::INFO ) ); } } // check requirements of database driver for installed driver foreach ($installedDrivers as $driver) { try { $this->checkDatabaseDriverRequirements($driver); } catch (Exception $exception) { $this->messageQueue->enqueue( new FlashMessage( '', $exception->getMessage(), ContextualFeedbackSeverity::INFO ) ); } } return $this->messageQueue; } public function checkDatabaseDriverRequirements(string $databaseDriver): FlashMessageQueue { if (!empty($this->databaseDriverCheckMap[$databaseDriver])) { /** @var CheckInterface $databaseDriverCheck */ $databaseDriverCheck = new $this->databaseDriverCheckMap[$databaseDriver](); foreach ($databaseDriverCheck->getStatus() as $message) { $this->messageQueue->addMessage($message); } return $this->messageQueue; } throw new Exception( sprintf( 'There are no database driver checks available for the given database driver: %s', $databaseDriver ), 1572521099 ); } /** * Get the status of a specific database platform * * @throws Exception */ public function checkDatabasePlatformRequirements(string $databaseDriver): FlashMessageQueue { static $checkedPlatform = []; $databasePlatformClass = self::$databaseDriverToPlatformMapping[$databaseDriver]; // execute platform checks only once if (in_array($databasePlatformClass, $checkedPlatform, true)) { return $this->messageQueue; } if ($this->connectionPool === null) { // Skip checks as we can not check as long as connections are not established yet. return $this->messageQueue; } if (!empty(self::$databaseDriverToPlatformMapping[$databaseDriver])) { $platformMessageQueue = (new $databasePlatformClass($this->connectionPool))->getStatus(); foreach ($platformMessageQueue as $message) { $this->messageQueue->enqueue($message); } $checkedPlatform[] = $databasePlatformClass; return $this->messageQueue; } throw new Exception( sprintf( 'There are no database platform checks available for the given database driver: %s', $databaseDriver ), 1573753070 ); } public function identifyInstalledDatabaseDriver(): array { $installedDrivers = []; if (static::isMysqli()) { $installedDrivers[] = DoctrineMysqliDriver::class; } if (static::isPdoMysql()) { $installedDrivers[] = DoctrinePDOMySqlDriver::class; } if (static::isPdoPgsql()) { $installedDrivers[] = DoctrinePDOPgSqlDriver::class; } if (static::isPdoSqlite()) { $installedDrivers[] = DoctrinePDOSqliteDriver::class; } return $installedDrivers; } /** * @throws Exception * @todo This method seems to be unused. Check if it can be removed or if it needs to be deprecated. */ public static function retrieveDatabasePlatformByDriverName(string $databaseDriverName): string { $databaseDriverClassName = static::retrieveDatabaseDriverClassByDriverName($databaseDriverName); if (!empty(self::$databaseDriverToPlatformMapping[$databaseDriverClassName])) { return self::$databaseDriverToPlatformMapping[$databaseDriverClassName]; } throw new Exception( sprintf('There is no database platform available for the given driver: %s', $databaseDriverName), 1573753057 ); } /** * @throws Exception */ public static function retrieveDatabaseDriverClassByDriverName(string $driverName): string { if (!empty(self::$driverMap[$driverName])) { return self::$driverMap[$driverName]; } throw new Exception( sprintf('There is no database driver available for the given driver name: %s', $driverName), 1573740447 ); } public function getMessageQueue(): FlashMessageQueue { return $this->messageQueue; } public static function isMysqli(): bool { return extension_loaded('mysqli'); } public static function isPdoMysql(): bool { return extension_loaded('pdo_mysql'); } public static function isPdoPgsql(): bool { return extension_loaded('pdo_pgsql'); } public static function isPdoSqlite(): bool { return extension_loaded('pdo_sqlite'); } }