1414use function in_array ;
1515use function ini_get ;
1616use function preg_replace ;
17+ use function strlen ;
18+ use function strtolower ;
1719
1820use const PHP_VERSION_ID ;
1921
2224 */
2325class PsalmRestarter extends XdebugHandler
2426{
27+ private const REQUIRED_OPCACHE_SETTINGS = [
28+ 'enable_cli ' => true ,
29+ 'jit ' => 1205 ,
30+ 'jit_buffer_size ' => 512 * 1024 * 1024 ,
31+ 'optimization_level ' => '0x7FFEBFFF ' ,
32+ 'preload ' => '' ,
33+ ];
34+
2535 private bool $ required = false ;
2636
2737 /**
@@ -53,28 +63,54 @@ protected function requiresRestart($default): bool
5363 static fn (string $ extension ): bool => extension_loaded ($ extension )
5464 );
5565
56- if (PHP_VERSION_ID >= 8_00_00 && (extension_loaded ('opcache ' ) || extension_loaded ('Zend OPcache ' ))) {
66+ $ opcache_loaded = extension_loaded ('opcache ' ) || extension_loaded ('Zend OPcache ' );
67+
68+ if (PHP_VERSION_ID >= 8_00_00 && $ opcache_loaded ) {
5769 // restart to enable JIT if it's not configured in the optimal way
58- if (!in_array (ini_get ('opcache.enable_cli ' ), ['1 ' , 'true ' , true , 1 ])) {
59- return true ;
60- }
70+ $ opcache_settings = [
71+ 'enable_cli ' => in_array (ini_get ('opcache.enable_cli ' ), ['1 ' , 'true ' , true , 1 ]),
72+ 'jit ' => (int ) ini_get ('opcache.jit ' ),
73+ 'optimization_level ' => (string ) ini_get ('opcache.optimization_level ' ),
74+ 'preload ' => (string ) ini_get ('opcache.preload ' ),
75+ 'jit_buffer_size ' => self ::toBytes (ini_get ('opcache.jit_buffer_size ' )),
76+ ];
6177
62- if (((int ) ini_get ('opcache.jit ' )) !== 1205 ) {
63- return true ;
78+ foreach (self ::REQUIRED_OPCACHE_SETTINGS as $ ini_name => $ required_value ) {
79+ if ($ opcache_settings [$ ini_name ] !== $ required_value ) {
80+ return true ;
81+ }
6482 }
83+ }
6584
66- if (((int ) ini_get ('opcache.jit ' )) === 0 ) {
67- return true ;
68- }
85+ return $ default || $ this ->required ;
86+ }
6987
70- if (ini_get ('opcache.optimization_level ' ) !== '0x7FFEBFFF ' ) {
71- return true ;
72- }
88+ private static function toBytes (string $ value ): int
89+ {
90+ $ unit = strtolower ($ value [strlen ($ value ) - 1 ]);
91+
92+ if (in_array ($ unit , ['g ' , 'm ' , 'k ' ], true )) {
93+ $ value = (int ) $ value ;
94+ } else {
95+ $ unit = '' ;
96+ $ value = (int ) $ value ;
7397 }
7498
75- return $ default || $ this ->required ;
99+ switch ($ unit ) {
100+ case 'g ' :
101+ $ value *= 1024 ;
102+ // no break
103+ case 'm ' :
104+ $ value *= 1024 ;
105+ // no break
106+ case 'k ' :
107+ $ value *= 1024 ;
108+ }
109+
110+ return $ value ;
76111 }
77112
113+
78114 /**
79115 * No type hint to allow xdebug-handler v1 and v2 usage
80116 *
@@ -93,17 +129,19 @@ protected function restart($command): void
93129 }
94130
95131 $ additional_options = [];
132+ $ opcache_loaded = extension_loaded ('opcache ' ) || extension_loaded ('Zend OPcache ' );
96133
97134 // executed in the parent process (before restart)
98135 // if it wasn't loaded then we apparently don't have opcache installed and there's no point trying
99136 // to tweak it
100137 // If we're running on 7.4 there's no JIT available
101- if (PHP_VERSION_ID >= 8_00_00 && ( extension_loaded ( ' opcache ' ) || extension_loaded ( ' Zend OPcache ' )) ) {
138+ if (PHP_VERSION_ID >= 8_00_00 && $ opcache_loaded ) {
102139 $ additional_options = [
103140 '-dopcache.enable_cli=true ' ,
104141 '-dopcache.jit_buffer_size=512M ' ,
105142 '-dopcache.jit=1205 ' ,
106143 '-dopcache.optimization_level=0x7FFEBFFF ' ,
144+ '-dopcache.preload= ' ,
107145 ];
108146 }
109147
0 commit comments