Skip to content

Instantly share code, notes, and snippets.

@ereidland
Last active December 16, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ereidland/5377543 to your computer and use it in GitHub Desktop.
Save ereidland/5377543 to your computer and use it in GitHub Desktop.
Loads all JavaScript files in a directory, and echos back their contents. Needs some work, but it works for a quick hack during development when dealing with lots of javascript files.
<?php
//To echo everything alphabetically:
// includeJS("./");
//includeJS can take an entry set or a path.
//For getting an entry set:
// getJSEntries($path [, $baseentries = []])
//$baseentries can be specified so that all file entries within $baseentries will be ignored.
//If $path is a file, it adds that file to the existing entries (specified by $baseentries).
//If $path is a folder, it does the same as the above for each file. Note that folder searches are recursive.
//The return result is the $baseentries array, merged with the sorted results of getJSEntries.
//To echo the contents of the final product, do:
// includeJS($entries);
function getJSEntries($path, $baseentries = [])
{
$entries = [];
if (is_file($path))
{
if (!in_array($path, $baseentries))
{
$baseentries[] = $path;
return $baseentries;
}
}
else
{
if ($dir = opendir($path))
{
while (($entry = readdir($dir)) !== false)
{
$isDir = is_dir($path . $entry);
if ($isDir )
{
if (strpos($entry, ".") === false)
$entries = array_merge($entries, getJSEntries($path . $entry . "/"));
}
else
{
if (strpos($entry, ".js") !== false)
$entries[] = $path . $entry;
}
}
}
}
sort($entries);
return array_unique(array_merge($baseentries, $entries));
}
function includeJS($path)
{
if (is_array($path))
$entries = $path;
else
$entries = getJSEntries($path);
foreach ($entries as $entry)
echo "//$entry\n"
. file_get_contents($entry) . ";";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment