@@ -146,4 +146,56 @@ public static function patchBeforeMake(BuilderBase $builder): void
146146 }
147147 }
148148 }
149+
150+ /**
151+ * @throws FileSystemException
152+ */
153+ public static function patchHardcodedINI (array $ ini = []): bool
154+ {
155+ $ cli_c = SOURCE_PATH . '/php-src/sapi/cli/php_cli.c ' ;
156+ $ cli_c_bak = SOURCE_PATH . '/php-src/sapi/cli/php_cli.c.bak ' ;
157+ $ micro_c = SOURCE_PATH . '/php-src/sapi/micro/php_micro.c ' ;
158+ $ micro_c_bak = SOURCE_PATH . '/php-src/sapi/micro/php_micro.c.bak ' ;
159+
160+ // Try to reverse backup file
161+ $ find_pattern = 'const char HARDCODED_INI[] = ' ;
162+ $ patch_str = '' ;
163+ foreach ($ ini as $ key => $ value ) {
164+ $ patch_str .= "\"{$ key }= {$ value }\\n \"\n" ;
165+ }
166+ $ patch_str = "const char HARDCODED_INI[] = \n{$ patch_str }" ;
167+
168+ // Detect backup, if we have backup, it means we need to reverse first
169+ if (file_exists ($ cli_c_bak ) || file_exists ($ micro_c_bak )) {
170+ self ::unpatchHardcodedINI ();
171+ }
172+
173+ // Backup it
174+ $ result = file_put_contents ($ cli_c_bak , file_get_contents ($ cli_c ));
175+ $ result = $ result && file_put_contents ($ micro_c_bak , file_get_contents ($ micro_c ));
176+ if ($ result === false ) {
177+ return false ;
178+ }
179+
180+ // Patch it
181+ FileSystem::replaceFile ($ cli_c , REPLACE_FILE_STR , $ find_pattern , $ patch_str );
182+ FileSystem::replaceFile ($ micro_c , REPLACE_FILE_STR , $ find_pattern , $ patch_str );
183+ return true ;
184+ }
185+
186+ public static function unpatchHardcodedINI (): bool
187+ {
188+ $ cli_c = SOURCE_PATH . '/php-src/sapi/cli/php_cli.c ' ;
189+ $ cli_c_bak = SOURCE_PATH . '/php-src/sapi/cli/php_cli.c.bak ' ;
190+ $ micro_c = SOURCE_PATH . '/php-src/sapi/micro/php_micro.c ' ;
191+ $ micro_c_bak = SOURCE_PATH . '/php-src/sapi/micro/php_micro.c.bak ' ;
192+ if (!file_exists ($ cli_c_bak ) && !file_exists ($ micro_c_bak )) {
193+ return false ;
194+ }
195+ $ result = file_put_contents ($ cli_c , file_get_contents ($ cli_c_bak ));
196+ $ result = $ result && file_put_contents ($ micro_c , file_get_contents ($ micro_c_bak ));
197+ @unlink ($ cli_c_bak );
198+ @unlink ($ micro_c_bak );
199+ return $ result ;
200+ }
149201}
0 commit comments