Skip to content

Commit 468e84b

Browse files
ddevsrkenjis
andauthored
feat: additional opcache setting in check php.ini (#9032)
* 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]>
1 parent d2c86f2 commit 468e84b

File tree

4 files changed

+133
-7
lines changed

4 files changed

+133
-7
lines changed

system/Commands/Utilities/PhpIniCheck.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Commands\Utilities;
1515

1616
use CodeIgniter\CLI\BaseCommand;
17+
use CodeIgniter\CLI\CLI;
1718
use CodeIgniter\Security\CheckPhpIni;
1819

1920
/**
@@ -56,6 +57,7 @@ final class PhpIniCheck extends BaseCommand
5657
* @var array<string, string>
5758
*/
5859
protected $arguments = [
60+
'opcache' => 'Check detail opcache values in production environment.',
5961
];
6062

6163
/**
@@ -70,7 +72,24 @@ final class PhpIniCheck extends BaseCommand
7072
*/
7173
public function run(array $params)
7274
{
73-
CheckPhpIni::run();
75+
if (isset($params[0]) && ! in_array($params[0], array_keys($this->arguments), true)) {
76+
CLI::error('You must specify a correct argument.');
77+
CLI::write(' Usage: ' . $this->usage);
78+
CLI::write(' Example: phpini:check opcache');
79+
CLI::write('Arguments:');
80+
81+
$length = max(array_map(strlen(...), array_keys($this->arguments)));
82+
83+
foreach ($this->arguments as $argument => $description) {
84+
CLI::write(CLI::color($this->setPad($argument, $length, 2, 2), 'green') . $description);
85+
}
86+
87+
return EXIT_ERROR;
88+
}
89+
90+
$argument = $params[0] ?? null;
91+
92+
CheckPhpIni::run(argument: $argument);
7493

7594
return EXIT_SUCCESS;
7695
}

system/Security/CheckPhpIni.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class CheckPhpIni
2929
*
3030
* @return string|null HTML string or void in CLI
3131
*/
32-
public static function run(bool $isCli = true)
32+
public static function run(bool $isCli = true, ?string $argument = null)
3333
{
34-
$output = static::checkIni();
34+
$output = static::checkIni($argument);
3535

3636
$thead = ['Directive', 'Global', 'Current', 'Recommended', 'Remark'];
3737
$tbody = [];
@@ -115,8 +115,9 @@ private static function outputForWeb(array $output, array $thead, array $tbody):
115115
* @internal Used for testing purposes only.
116116
* @testTag
117117
*/
118-
public static function checkIni(): array
118+
public static function checkIni(?string $argument = null): array
119119
{
120+
// Default items
120121
$items = [
121122
'error_reporting' => ['recommended' => '5111'],
122123
'display_errors' => ['recommended' => '0'],
@@ -134,12 +135,31 @@ public static function checkIni(): array
134135
'date.timezone' => ['recommended' => 'UTC'],
135136
'mbstring.language' => ['recommended' => 'neutral'],
136137
'opcache.enable' => ['recommended' => '1'],
137-
'opcache.enable_cli' => [],
138-
'opcache.jit' => [],
139-
'opcache.jit_buffer_size' => [],
138+
'opcache.enable_cli' => ['recommended' => '1'],
139+
'opcache.jit' => ['recommended' => 'tracing'],
140+
'opcache.jit_buffer_size' => ['recommended' => '128', 'remark' => 'Adjust with your free space of memory'],
140141
'zend.assertions' => ['recommended' => '-1'],
141142
];
142143

144+
if ($argument === 'opcache') {
145+
$items = [
146+
'opcache.enable' => ['recommended' => '1'],
147+
'opcache.enable_cli' => ['recommended' => '0', 'remark' => 'Enable when you using CLI'],
148+
'opcache.jit' => ['recommended' => 'tracing', 'remark' => 'Disable when you used third-party extensions'],
149+
'opcache.jit_buffer_size' => ['recommended' => '128', 'remark' => 'Adjust with your free space of memory'],
150+
'opcache.memory_consumption' => ['recommended' => '128', 'remark' => 'Adjust with your free space of memory'],
151+
'opcache.interned_strings_buffer' => ['recommended' => '16'],
152+
'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)'],
153+
'opcache.max_wasted_percentage' => ['recommended' => '10'],
154+
'opcache.validate_timestamps' => ['recommended' => '0', 'remark' => 'When you disabled, opcache hold your code into shared memory. Restart webserver needed'],
155+
'opcache.revalidate_freq' => [],
156+
'opcache.file_cache' => ['remark' => 'Location file caching, It should improve performance when SHM memory is full'],
157+
'opcache.file_cache_only' => ['remark' => 'Opcode caching in shared memory, Disabled when you using Windows'],
158+
'opcache.file_cache_fallback' => ['remark' => 'Set enable when you using Windows'],
159+
'opcache.save_comments' => ['recommended' => '0', 'remark' => 'Enable when you using package require docblock annotation'],
160+
];
161+
}
162+
143163
$output = [];
144164
$ini = ini_get_all();
145165

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands\Utilities;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\StreamFilterTrait;
18+
use PHPUnit\Framework\Attributes\Group;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[Group('Others')]
24+
final class PhpIniCheckTest extends CIUnitTestCase
25+
{
26+
use StreamFilterTrait;
27+
28+
protected function setUp(): void
29+
{
30+
$this->resetServices();
31+
parent::setUp();
32+
}
33+
34+
protected function tearDown(): void
35+
{
36+
$this->resetServices();
37+
parent::tearDown();
38+
}
39+
40+
protected function getBuffer(): string
41+
{
42+
return $this->getStreamFilterBuffer();
43+
}
44+
45+
public function testCommandCheckNoArg(): void
46+
{
47+
command('phpini:check');
48+
49+
$result = $this->getBuffer();
50+
51+
$this->assertStringContainsString('Directive', $result);
52+
$this->assertStringContainsString('Global', $result);
53+
$this->assertStringContainsString('Current', $result);
54+
$this->assertStringContainsString('Recommended', $result);
55+
$this->assertStringContainsString('Remark', $result);
56+
}
57+
58+
public function testCommandCheckOpcache(): void
59+
{
60+
command('phpini:check opcache');
61+
62+
$this->assertStringContainsString('opcache.save_comments', $this->getBuffer());
63+
}
64+
65+
public function testCommandCheckNoExistsArg(): void
66+
{
67+
command('phpini:check noexists');
68+
69+
$this->assertStringContainsString(
70+
'You must specify a correct argument.',
71+
$this->getBuffer()
72+
);
73+
}
74+
}

tests/system/Security/CheckPhpIniTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ public function testCheckIni(): void
3737
$this->assertSame($expected, $output['display_errors']);
3838
}
3939

40+
public function testCheckIniOpcache(): void
41+
{
42+
$output = CheckPhpIni::checkIni('opcache');
43+
44+
$expected = [
45+
'global' => '1',
46+
'current' => '1',
47+
'recommended' => '0',
48+
'remark' => 'Enable when you using package require docblock annotation',
49+
];
50+
$this->assertSame($expected, $output['opcache.save_comments']);
51+
}
52+
4053
public function testRunCli(): void
4154
{
4255
// Set MockInputOutput to CLI.

0 commit comments

Comments
 (0)