Skip to content

Instantly share code, notes, and snippets.

@pinscript
Created November 3, 2011 12:23
Show Gist options
  • Select an option

  • Save pinscript/1336371 to your computer and use it in GitHub Desktop.

Select an option

Save pinscript/1336371 to your computer and use it in GitHub Desktop.
<?php
class Smash {
/**
* Combine all files with a certain extension in a directory into a single file
*/
public function smash_directory($directory, $extension, $output) {
$files = glob($directory . '*.' . $extension);
$smashed = '';
foreach($files as $file) {
$content = sprintf($this->get_comment($extension), $file);
$content .= $this->read_file($file);
$smashed .= $content; // Add file to combined output
}
$this->write_file($output, $smashed);
}
private function read_file($path) {
return file_get_contents($path);
}
private function write_file($path, $content) {
file_put_contents($path, $content);
}
private function get_comment($filetype) {
switch($filetype) {
case 'css':
case 'js':
return '/* %s */';
case 'php':
return '<?php /* */ ?>';
}
trigger_error('Unknown filetype: ' . $filetype);
}
}
$smash = new Smash;
$smash->smash_directory('functions', 'css', 'smashed.css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment