I've created a PHP script which take an array of filenames as a comma-separated parameter and concatenates them, serving them as one, single JavaScript file.
I've also built a caching mechanism into it - each time, it checks if any of the individual JS files have been updated and, if one or more of them has, it updates the cache and serves the new output. Otherwise, it simply loads the relevant cached file.
<?php
$scripts_to_concatenate = $_GET['scripts'];
$scripts_array = explode(',', $scripts_to_concatenate);
$cached_script = '/public_html/assets/scripts/cache/' . str_replace('.js', '', str_replace(',', '_', $scripts_to_concatenate)) . '.js.cache';
$cache_expired = false;
if (file_exists($cached_script)) {
foreach ($scripts_array as $script) {
if (filemtime($script) > filemtime($cached_script)) {
$cache_expired = true;
}
}
} else {
$cache_expired = true;
}
if ($cache_expired) {
foreach ($scripts_array as $script) {
$script = '/public_html/assets/scripts/' . $script;
if (file_exists($script)) {
$output = $output . @file_get_contents($script);
}
}
file_put_contents($cached_script, $output);
} else {
$output = @file_get_contents($cached_script);
}
echo $output;
Is anyone able to suggest any ways for me to improve this code, to make it more efficient or more fool-proof?
UPDATE: I've incorporated janos's recommendations into my code (original, unoptimised code above). Does anyone have any other suggestions?