diff --git a/system/Commands/Utilities/PhpIniCheck.php b/system/Commands/Utilities/PhpIniCheck.php index f65dcd855fa4..eb2192434133 100644 --- a/system/Commands/Utilities/PhpIniCheck.php +++ b/system/Commands/Utilities/PhpIniCheck.php @@ -14,6 +14,7 @@ namespace CodeIgniter\Commands\Utilities; use CodeIgniter\CLI\BaseCommand; +use CodeIgniter\CLI\CLI; use CodeIgniter\Security\CheckPhpIni; /** @@ -56,6 +57,7 @@ final class PhpIniCheck extends BaseCommand * @var array */ protected $arguments = [ + 'opcache' => 'Check detail opcache values in production environment.', ]; /** @@ -70,7 +72,24 @@ final class PhpIniCheck extends BaseCommand */ public function run(array $params) { - CheckPhpIni::run(); + if (isset($params[0]) && ! in_array($params[0], array_keys($this->arguments), true)) { + CLI::error('You must specify a correct argument.'); + CLI::write(' Usage: ' . $this->usage); + CLI::write(' Example: phpini:check opcache'); + CLI::write('Arguments:'); + + $length = max(array_map(strlen(...), array_keys($this->arguments))); + + foreach ($this->arguments as $argument => $description) { + CLI::write(CLI::color($this->setPad($argument, $length, 2, 2), 'green') . $description); + } + + return EXIT_ERROR; + } + + $argument = $params[0] ?? null; + + CheckPhpIni::run(argument: $argument); return EXIT_SUCCESS; } diff --git a/system/Security/CheckPhpIni.php b/system/Security/CheckPhpIni.php index 59cd587360d5..d32fadc271c8 100644 --- a/system/Security/CheckPhpIni.php +++ b/system/Security/CheckPhpIni.php @@ -29,9 +29,9 @@ class CheckPhpIni * * @return string|void HTML string or void in CLI */ - public static function run(bool $isCli = true) + public static function run(bool $isCli = true, ?string $argument = null) { - $output = static::checkIni(); + $output = static::checkIni($argument); $thead = ['Directive', 'Global', 'Current', 'Recommended', 'Remark']; $tbody = []; @@ -115,8 +115,9 @@ private static function outputForWeb(array $output, array $thead, array $tbody): * @internal Used for testing purposes only. * @testTag */ - public static function checkIni(): array + public static function checkIni(?string $argument = null): array { + // Default items $items = [ 'error_reporting' => ['recommended' => '5111'], 'display_errors' => ['recommended' => '0'], @@ -134,12 +135,31 @@ public static function checkIni(): array 'date.timezone' => ['recommended' => 'UTC'], 'mbstring.language' => ['recommended' => 'neutral'], 'opcache.enable' => ['recommended' => '1'], - 'opcache.enable_cli' => [], - 'opcache.jit' => [], - 'opcache.jit_buffer_size' => [], + 'opcache.enable_cli' => ['recommended' => '1'], + 'opcache.jit' => ['recommended' => 'tracing'], + 'opcache.jit_buffer_size' => ['recommended' => '128', 'remark' => 'Adjust with your free space of memory'], 'zend.assertions' => ['recommended' => '-1'], ]; + if ($argument === 'opcache') { + $items = [ + 'opcache.enable' => ['recommended' => '1'], + 'opcache.enable_cli' => ['recommended' => '0', 'remark' => 'Enable when you using CLI'], + 'opcache.jit' => ['recommended' => 'tracing', 'remark' => 'Disable when you used third-party extensions'], + 'opcache.jit_buffer_size' => ['recommended' => '128', 'remark' => 'Adjust with your free space of memory'], + 'opcache.memory_consumption' => ['recommended' => '128', 'remark' => 'Adjust with your free space of memory'], + 'opcache.interned_strings_buffer' => ['recommended' => '16'], + 'opcache.max_accelerated_files' => ['remark' => 'Adjust based on the number of PHP files in your project (e.g.: find your_project/ -iname \'*.php\'|wc -l)'], + 'opcache.max_wasted_percentage' => ['recommended' => '10'], + 'opcache.validate_timestamps' => ['recommended' => '0', 'remark' => 'When you disabled, opcache hold your code into shared memory. Restart webserver needed'], + 'opcache.revalidate_freq' => [], + 'opcache.file_cache' => ['remark' => 'Location file caching, It should improve performance when SHM memory is full'], + 'opcache.file_cache_only' => ['remark' => 'Opcode caching in shared memory, Disabled when you using Windows'], + 'opcache.file_cache_fallback' => ['remark' => 'Set enable when you using Windows'], + 'opcache.save_comments' => ['recommended' => '0', 'remark' => 'Enable when you using package require docblock annotation'], + ]; + } + $output = []; $ini = ini_get_all(); diff --git a/tests/system/Commands/Utilities/PhpIniCheckTest.php b/tests/system/Commands/Utilities/PhpIniCheckTest.php new file mode 100644 index 000000000000..9565fd1cf3e9 --- /dev/null +++ b/tests/system/Commands/Utilities/PhpIniCheckTest.php @@ -0,0 +1,74 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities; + +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\StreamFilterTrait; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class PhpIniCheckTest extends CIUnitTestCase +{ + use StreamFilterTrait; + + protected function setUp(): void + { + $this->resetServices(); + parent::setUp(); + } + + protected function tearDown(): void + { + $this->resetServices(); + parent::tearDown(); + } + + protected function getBuffer(): string + { + return $this->getStreamFilterBuffer(); + } + + public function testCommandCheckNoArg(): void + { + command('phpini:check'); + + $result = $this->getBuffer(); + + $this->assertStringContainsString('Directive', $result); + $this->assertStringContainsString('Global', $result); + $this->assertStringContainsString('Current', $result); + $this->assertStringContainsString('Recommended', $result); + $this->assertStringContainsString('Remark', $result); + } + + public function testCommandCheckOpcache(): void + { + command('phpini:check opcache'); + + $this->assertStringContainsString('opcache.save_comments', $this->getBuffer()); + } + + public function testCommandCheckNoExistsArg(): void + { + command('phpini:check noexists'); + + $this->assertStringContainsString( + 'You must specify a correct argument.', + $this->getBuffer() + ); + } +} diff --git a/tests/system/Security/CheckPhpIniTest.php b/tests/system/Security/CheckPhpIniTest.php index 8636337f2893..e100079dfa79 100644 --- a/tests/system/Security/CheckPhpIniTest.php +++ b/tests/system/Security/CheckPhpIniTest.php @@ -37,6 +37,19 @@ public function testCheckIni(): void $this->assertSame($expected, $output['display_errors']); } + public function testCheckIniOpcache(): void + { + $output = CheckPhpIni::checkIni('opcache'); + + $expected = [ + 'global' => '1', + 'current' => '1', + 'recommended' => '0', + 'remark' => 'Enable when you using package require docblock annotation', + ]; + $this->assertSame($expected, $output['opcache.save_comments']); + } + public function testRunCli(): void { // Set MockInputOutput to CLI.