Skip to content

Commit

Permalink
feat: additional opcache setting in check php.ini (#9032)
Browse files Browse the repository at this point in the history
* feat: additional opcache setting in check PHP.ini

* fix: PHPUnit fails

* fix: remark memory_consumption

* fix: adjust remark

* fix: add argument for opcache only

* tests: add argument for opcache only

* fix: typo

* fix: errors on GA

* Update system/Commands/Utilities/PhpIniCheck.php

Co-authored-by: kenjis <[email protected]>

* Update system/Security/CheckPhpIni.php

Co-authored-by: kenjis <[email protected]>

* fix: typo

* refactor: remark certain provisions

* tests: fix remark save_comments

* fix: typo

---------

Co-authored-by: kenjis <[email protected]>
  • Loading branch information
ddevsr and kenjis authored Jan 11, 2025
1 parent d2c86f2 commit 468e84b
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
21 changes: 20 additions & 1 deletion system/Commands/Utilities/PhpIniCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\Commands\Utilities;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Security\CheckPhpIni;

/**
Expand Down Expand Up @@ -56,6 +57,7 @@ final class PhpIniCheck extends BaseCommand
* @var array<string, string>
*/
protected $arguments = [
'opcache' => 'Check detail opcache values in production environment.',
];

/**
Expand All @@ -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;
}
Expand Down
32 changes: 26 additions & 6 deletions system/Security/CheckPhpIni.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ class CheckPhpIni
*
* @return string|null 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 = [];
Expand Down Expand Up @@ -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'],
Expand All @@ -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();

Expand Down
74 changes: 74 additions & 0 deletions tests/system/Commands/Utilities/PhpIniCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* 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()
);
}
}
13 changes: 13 additions & 0 deletions tests/system/Security/CheckPhpIniTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 468e84b

Please sign in to comment.