2020-03-22 16:55:23 +00:00
< ? php
2020-08-31 20:40:31 +02:00
class ERequirementNotMet extends Exception
{
}
2020-03-22 16:55:23 +00:00
2023-12-16 15:08:59 +01:00
const REQUIRED_PHP_EXTENSIONS = [ 'fileinfo' , 'pdo_sqlite' , 'gd' , 'ctype' , 'intl' , 'zlib' , 'mbstring' ,
2021-08-04 17:25:24 +02:00
// These are core extensions, so normally can't be missing, but seems to be the case, however, on FreeBSD
2023-12-16 15:08:59 +01:00
'filter' , 'iconv' , 'tokenizer' , 'json'
2021-08-04 17:25:24 +02:00
];
2023-12-16 15:08:59 +01:00
const REQUIRED_PHP_VERSION = '8.2.0' ;
2022-12-25 20:53:49 +01:00
const REQUIRED_SQLITE_VERSION = '3.34.0' ;
2020-03-22 16:55:23 +00:00
2020-03-22 18:02:19 +01:00
class PrerequisiteChecker
{
2020-08-29 16:41:27 +02:00
public function checkRequirements ()
{
2022-12-25 20:49:11 +01:00
self :: checkForPhpVersion ();
2020-08-29 16:41:27 +02:00
self :: checkForConfigFile ();
self :: checkForConfigDistFile ();
self :: checkForComposer ();
self :: checkForPhpExtensions ();
self :: checkForSqliteVersion ();
}
2020-03-22 16:55:23 +00:00
2020-08-31 20:40:31 +02:00
private function checkForComposer ()
2020-08-29 16:41:27 +02:00
{
2023-06-03 17:36:01 +02:00
if ( ! file_exists ( __DIR__ . '/../packages/autoload.php' ))
2020-08-29 16:41:27 +02:00
{
2023-06-03 17:36:01 +02:00
throw new ERequirementNotMet ( '/packages/autoload.php not found. Have you run Composer?' );
2020-08-29 16:41:27 +02:00
}
}
2020-03-22 16:55:23 +00:00
2020-08-29 16:41:27 +02:00
private function checkForConfigDistFile ()
{
if ( ! file_exists ( __DIR__ . '/../config-dist.php' ))
{
throw new ERequirementNotMet ( 'config-dist.php not found. Please do not remove this file.' );
}
}
2020-04-29 19:54:05 +02:00
2020-08-31 20:40:31 +02:00
private function checkForConfigFile ()
2020-08-29 16:41:27 +02:00
{
2020-08-31 20:40:31 +02:00
if ( ! file_exists ( GROCY_DATAPATH . '/config.php' ))
2020-08-29 16:41:27 +02:00
{
2020-08-31 20:40:31 +02:00
throw new ERequirementNotMet ( 'config.php in data directory (' . GROCY_DATAPATH . ') not found. Have you copied config-dist.php to the data directory and renamed it to config.php?' );
2020-08-29 16:41:27 +02:00
}
}
2020-04-29 19:54:05 +02:00
2020-08-29 16:41:27 +02:00
private function checkForPhpExtensions ()
{
$loadedExtensions = get_loaded_extensions ();
foreach ( REQUIRED_PHP_EXTENSIONS as $extension )
{
if ( ! in_array ( $extension , $loadedExtensions ))
{
throw new ERequirementNotMet ( " PHP module ' { $extension } ' not installed, but required. " );
}
}
2020-08-31 20:40:31 +02:00
}
2020-08-29 16:41:27 +02:00
private function checkForSqliteVersion ()
{
$sqliteVersion = self :: getSqlVersionAsString ();
if ( version_compare ( $sqliteVersion , REQUIRED_SQLITE_VERSION , '<' ))
{
throw new ERequirementNotMet ( 'SQLite ' . REQUIRED_SQLITE_VERSION . ' is required, however you are running ' . $sqliteVersion );
}
}
2020-04-29 19:54:05 +02:00
2022-12-25 20:49:11 +01:00
private function checkForPhpVersion ()
{
$phpVersion = phpversion ();
if ( version_compare ( $phpVersion , REQUIRED_PHP_VERSION , '<' ))
{
throw new ERequirementNotMet ( 'PHP ' . REQUIRED_PHP_VERSION . ' is required, however you are running ' . $phpVersion );
}
}
2020-08-31 20:40:31 +02:00
private function getSqlVersionAsString ()
{
$dbh = new PDO ( 'sqlite::memory:' );
return $dbh -> query ( 'select sqlite_version()' ) -> fetch ()[ 0 ];
}
2020-03-22 16:55:23 +00:00
}