forked from symfony/requirements-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,22 +17,25 @@ | |
* | ||
* @author Tobias Schultze <http://tobion.de> | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Ghislain Flandin <[email protected]> | ||
*/ | ||
class SymfonyRequirements extends RequirementCollection | ||
{ | ||
const REQUIRED_PHP_VERSION = '7.2.8'; | ||
const REQUIRED_MYSQL_VERSION = '5.7.0'; | ||
const REQUIRED_MARIADB_VERSION = '10.2.7'; | ||
|
||
public function __construct($rootDir) | ||
{ | ||
/* mandatory requirements follow */ | ||
|
||
$appEnv = getenv('APP_ENV') ?? 'dev'; | ||
|
||
$installedPhpVersion = phpversion(); | ||
$installedMySQLVersion = $this->getMySQLVersion(); | ||
|
||
$rootDir = $this->getComposerRootDir($rootDir); | ||
$options = $this->readComposer($rootDir); | ||
$appEnv = getenv('APP_ENV') ?? 'dev'; | ||
|
||
|
||
$this->addRequirement( | ||
version_compare($installedPhpVersion, self::REQUIRED_PHP_VERSION, '>='), | ||
|
@@ -43,6 +46,20 @@ public function __construct($rootDir) | |
sprintf('Install PHP %s or newer (installed version is %s)', self::REQUIRED_PHP_VERSION, $installedPhpVersion) | ||
); | ||
|
||
$this->addRequirement( | ||
$this->connectDatabase(), | ||
sprintf('Application must be able to connect to MySQL database.'), | ||
sprintf('Using .env identifiers, application must be able to connect to database') | ||
); | ||
$this->addRequirement( | ||
$this->validateMySQLVersion(), | ||
sprintf('MySQL version must be at least %s (%s installed)', self::REQUIRED_MYSQL_VERSION, $installedMySQLVersion), | ||
sprintf('You are running MySQL version "<strong>%s</strong>", but application needs at least MySQL "<strong>%s</strong>" to run, if using . | ||
Before using Symfony, upgrade your MySQL installation, preferably to the latest version.', | ||
$installedMySQLVersion, self::REQUIRED_MYSQL_VERSION), | ||
sprintf('Install MySQL %s or newer (installed version is %s)', self::REQUIRED_MYSQL_VERSION, $installedMySQLVersion) | ||
); | ||
|
||
$this->addRequirement( | ||
is_dir($rootDir.'/vendor/composer'), | ||
'Vendor libraries must be installed', | ||
|
@@ -493,4 +510,46 @@ private function readComposer($rootDir) | |
|
||
return $options; | ||
} | ||
|
||
private function connectDatabase() | ||
{ | ||
$identifiers = $this->splitDatabaseIdentifiers(getenv('DATABASE_URL')); | ||
$link = mysqli_connect($identifiers['host'], $identifiers['user'], $identifiers['password'], $identifiers['database'], $identifiers['port']); | ||
return mysqli_connect_errno() ? false : true; | ||
} | ||
|
||
private function splitDatabaseIdentifiers($str) { | ||
preg_match("/mysql:\/\/(.*):(.*)@(.*):(.*)\/(.*)/", $str, $matches); | ||
$result = []; | ||
$result['user'] = $matches[1]; | ||
$result['password'] = $matches[2]; | ||
$result['host'] = $matches[3]; | ||
$result['port'] = $matches[4]; | ||
$result['database'] = $matches[5]; | ||
return $result; | ||
} | ||
|
||
private function getMySQLVersion() { | ||
$identifiers = $this->splitDatabaseIdentifiers(getenv('DATABASE_URL')); | ||
$link = mysqli_connect($identifiers['host'], $identifiers['user'], $identifiers['password'], $identifiers['database'], $identifiers['port']); | ||
|
||
if ($result = $link->query("SELECT VERSION() AS 'version'")) { | ||
while ($row = $result->fetch_assoc()) { | ||
return $row['version']; | ||
} | ||
} | ||
return $link->get_server_info(); | ||
} | ||
|
||
private function validateMySQLVersion() { | ||
$version = $this->getMySQLVersion(); | ||
|
||
if (strpos(strtolower($version), 'mariadb') !== false) { | ||
return strpos($version, self::REQUIRED_MARIADB_VERSION) !== false || version_compare($version, self::REQUIRED_MARIADB_VERSION, '>='); | ||
} else { | ||
return version_compare($version, self::REQUIRED_MYSQL_VERSION, '>='); | ||
} | ||
|
||
return true; | ||
} | ||
} |