Skip to content

Commit e2383c6

Browse files
committed
Update utility loader generator to prevent function redeclaration errors
1 parent ed1e3db commit e2383c6

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

src/generate-utility-list.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,32 @@
2424
$php_content = "<?php\n";
2525
$php_content .= "/**\n * GENERATED UTILITY LOADER FILE.\n * DO NOT EDIT THIS FILE MANUALLY.\n * Run 'php generate-utility-list.php' to regenerate this list.\n */\n\n";
2626

27-
// Start the function definition
28-
$php_content .= "function load_core_utilities()\n{\n";
27+
// Wrap function declaration to prevent duplicate function errors
28+
$php_content .= "if (!function_exists('load_core_utilities')) {\n";
29+
$php_content .= " function load_core_utilities()\n";
30+
$php_content .= " {\n";
2931

3032
// Start the array list
31-
$php_content .= " \$utility_files = [\n";
33+
$php_content .= " \$utility_files = [\n";
3234

33-
// Add each file to the array list, ensuring critical files are first for safety
34-
// You must manually define the critical ordering (e.g., cookies-baked.php) here.
35-
// For demonstration, we'll list them alphabetically and trust dependencies are fine,
36-
// but you should manually move critical files (like 'cookies-baked.php') to the top of the array if needed.
35+
// Add each file to the array list
3736
foreach ($utility_files as $file) {
3837
$php_content .= " '" . $file . "',\n";
3938
}
4039

4140
$php_content .= " ];\n\n";
4241

43-
$php_content .= " \$utility_dir = __DIR__ . DIRECTORY_SEPARATOR . 'utility' . DIRECTORY_SEPARATOR;\n";
44-
$php_content .= " foreach (\$utility_files as \$file) {\n";
45-
$php_content .= " require_once \$utility_dir . \$file;\n";
42+
$php_content .= " \$utility_dir = __DIR__ . DIRECTORY_SEPARATOR . 'utility' . DIRECTORY_SEPARATOR;\n";
43+
$php_content .= " foreach (\$utility_files as \$file) {\n";
44+
$php_content .= " require_once \$utility_dir . \$file;\n";
45+
$php_content .= " }\n";
4646
$php_content .= " }\n";
4747
$php_content .= "}\n\n";
48-
$php_content .= "load_core_utilities();\n";
48+
49+
// Add guard to prevent calling before function is defined
50+
$php_content .= "if (!function_exists('load_core_utilities')) {\n";
51+
$php_content .= " load_core_utilities();\n";
52+
$php_content .= "}\n";
4953

5054
// Write the content to the target file
5155
if (file_put_contents($output_file, $php_content) !== false) {

src/lib/utility-loader.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* GENERATED UTILITY LOADER FILE.
54
* DO NOT EDIT THIS FILE MANUALLY.
@@ -158,6 +157,7 @@ function load_core_utilities()
158157
'protected-post.php',
159158
'purify-dirty-html.php',
160159
'random-generator.php',
160+
'rate-limiter.php',
161161
'read-datetime.php',
162162
'regenerate-session.php',
163163
'relative-url.php',
@@ -221,10 +221,10 @@ function load_core_utilities()
221221
'write-nginx-config.php',
222222
];
223223

224-
$utility_dir = __DIR__ . DIRECTORY_SEPARATOR . 'utility' . DIRECTORY_SEPARATOR;
225-
foreach ($utility_files as $file) {
226-
require_once $utility_dir . $file;
227-
}
224+
$utility_dir = __DIR__ . DIRECTORY_SEPARATOR . 'utility' . DIRECTORY_SEPARATOR;
225+
foreach ($utility_files as $file) {
226+
require_once $utility_dir . $file;
227+
}
228228
}
229229
}
230230

0 commit comments

Comments
 (0)