2022-08-04 22:44:14 +02:00
< ? php
2023-06-11 18:59:07 +02:00
declare ( strict_types = 1 );
2022-11-29 21:21:26 +01:00
/*
* This file is part of Part - DB ( https :// github . com / Part - DB / Part - DB - symfony ) .
*
* Copyright ( C ) 2019 - 2022 Jan Böhmer ( https :// github . com / jbtronics )
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
2022-08-04 22:44:14 +02:00
namespace App\Command ;
2023-06-11 14:55:06 +02:00
use Symfony\Component\Console\Attribute\AsCommand ;
2022-08-04 22:44:14 +02:00
use Symfony\Component\Console\Command\Command ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Console\Style\SymfonyStyle ;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface ;
2023-06-11 14:55:06 +02:00
#[AsCommand('partdb:check-requirements', 'Checks if the requirements Part-DB needs or recommends are fulfilled.')]
2022-08-04 22:44:14 +02:00
class CheckRequirementsCommand extends Command
{
2023-06-11 14:15:46 +02:00
public function __construct ( protected ContainerBagInterface $params )
2022-08-04 22:44:14 +02:00
{
parent :: __construct ();
}
protected function configure () : void
{
2023-05-28 01:21:05 +02:00
$this -> addOption ( 'only_issues' , 'i' , InputOption :: VALUE_NONE , 'Only show issues, not success messages.' )
2022-08-04 22:44:14 +02:00
;
}
protected function execute ( InputInterface $input , OutputInterface $output ) : int
{
$io = new SymfonyStyle ( $input , $output );
$only_issues = ( bool ) $input -> getOption ( 'only_issues' );
$io -> title ( 'Checking PHP configuration...' );
$this -> checkPHP ( $io , $only_issues );
$io -> title ( 'Checking PHP extensions...' );
$this -> checkPHPExtensions ( $io , $only_issues );
$io -> title ( 'Checking Part-DB configuration' );
$this -> checkPartDBConfig ( $io , $only_issues );
return self :: SUCCESS ;
}
protected function checkPHP ( SymfonyStyle $io , $only_issues = false ) : void
{
//Check PHP versions
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'Checking PHP version...' );
}
2023-05-27 20:07:03 +02:00
//We recommend PHP 8.2, but 8.1 is the minimum
if ( PHP_VERSION_ID < 80200 ) {
2022-08-04 22:44:14 +02:00
$io -> warning ( 'You are using PHP ' . PHP_VERSION . '. This will work, but a newer version is recommended.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'PHP version is sufficient.' );
2022-08-04 22:44:14 +02:00
}
//Check if opcache is enabled
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'Checking Opcache...' );
}
2022-08-04 22:44:14 +02:00
$opcache_enabled = ini_get ( 'opcache.enable' ) === '1' ;
if ( ! $opcache_enabled ) {
$io -> warning ( 'Opcache is not enabled. This will work, but performance will be better with opcache enabled. Set opcache.enable=1 in your php.ini to enable it' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'Opcache is enabled.' );
2022-08-04 22:44:14 +02:00
}
//Check if opcache is configured correctly
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'Checking Opcache configuration...' );
}
2022-08-04 22:44:14 +02:00
if ( $opcache_enabled && ( ini_get ( 'opcache.memory_consumption' ) < 256 || ini_get ( 'opcache.max_accelerated_files' ) < 20000 )) {
$io -> warning ( 'Opcache configuration can be improved. See https://symfony.com/doc/current/performance.html for more info.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'Opcache configuration is already performance optimized.' );
2022-08-04 22:44:14 +02:00
}
}
protected function checkPartDBConfig ( SymfonyStyle $io , $only_issues = false ) : void
{
//Check if APP_ENV is set to prod
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'Checking debug mode...' );
}
if ( $this -> params -> get ( 'kernel.debug' )) {
2022-08-04 22:44:14 +02:00
$io -> warning ( 'You have activated debug mode, this is will leak informations in a production environment.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'Debug mode disabled.' );
2022-08-04 22:44:14 +02:00
}
}
protected function checkPHPExtensions ( SymfonyStyle $io , $only_issues = false ) : void
{
//Get all installed PHP extensions
$extensions = get_loaded_extensions ();
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'Your PHP installation has ' . count ( $extensions ) . ' extensions installed: ' . implode ( ', ' , $extensions ));
}
2022-08-04 22:44:14 +02:00
$db_drivers_count = 0 ;
if ( ! in_array ( 'pdo_mysql' , $extensions )) {
$io -> error ( 'pdo_mysql is not installed. You will not be able to use MySQL databases.' );
} else {
2023-06-11 14:15:46 +02:00
if ( ! $only_issues ) {
$io -> success ( 'PHP extension pdo_mysql is installed.' );
}
2022-08-04 22:44:14 +02:00
$db_drivers_count ++ ;
}
if ( ! in_array ( 'pdo_sqlite' , $extensions )) {
$io -> error ( 'pdo_sqlite is not installed. You will not be able to use SQLite. databases' );
} else {
2023-06-11 14:15:46 +02:00
if ( ! $only_issues ) {
$io -> success ( 'PHP extension pdo_sqlite is installed.' );
}
2022-08-04 22:44:14 +02:00
$db_drivers_count ++ ;
}
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'You have ' . $db_drivers_count . ' database drivers installed.' );
}
2022-08-04 22:44:14 +02:00
if ( $db_drivers_count === 0 ) {
$io -> error ( 'You have no database drivers installed. You have to install at least one database driver!' );
}
2023-06-11 14:15:46 +02:00
if ( ! in_array ( 'curl' , $extensions )) {
2022-08-04 22:44:14 +02:00
$io -> warning ( 'curl extension is not installed. Install curl extension for better performance' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'PHP extension curl is installed.' );
2022-08-04 22:44:14 +02:00
}
$gd_installed = in_array ( 'gd' , $extensions );
2023-06-11 14:15:46 +02:00
if ( ! $gd_installed ) {
2022-08-04 22:44:14 +02:00
$io -> error ( 'GD is not installed. GD is required for image processing.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'PHP extension GD is installed.' );
2022-08-04 22:44:14 +02:00
}
//Check if GD has jpeg support
2023-06-11 14:15:46 +02:00
if ( $io -> isVerbose ()) {
$io -> comment ( 'Checking if GD has jpeg support...' );
}
2022-08-04 22:44:14 +02:00
if ( $gd_installed ) {
$gd_info = gd_info ();
2023-06-11 14:15:46 +02:00
if ( $gd_info [ 'JPEG Support' ] === false ) {
2022-08-04 22:44:14 +02:00
$io -> warning ( 'Your GD does not have jpeg support. You will not be able to generate thumbnails of jpeg images.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'GD has jpeg support.' );
2022-08-04 22:44:14 +02:00
}
2023-06-11 14:15:46 +02:00
if ( $gd_info [ 'PNG Support' ] === false ) {
2022-08-04 22:44:14 +02:00
$io -> warning ( 'Your GD does not have png support. You will not be able to generate thumbnails of png images.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'GD has png support.' );
2022-08-04 22:44:14 +02:00
}
2023-06-11 14:15:46 +02:00
if ( $gd_info [ 'WebP Support' ] === false ) {
2022-08-04 22:44:14 +02:00
$io -> warning ( 'Your GD does not have WebP support. You will not be able to generate thumbnails of WebP images.' );
2023-06-11 14:15:46 +02:00
} elseif ( ! $only_issues ) {
$io -> success ( 'GD has WebP support.' );
2022-08-04 22:44:14 +02:00
}
}
}
2023-06-11 18:59:07 +02:00
}