Skip to content

Instantly share code, notes, and snippets.

@Abhinav1217
Last active Aug 29, 2015
Embed
What would you like to do?
A simple php script to Minify PHP files in PHP project using PHP shell command in terminal..
<?php
/**
* @author Abhinav Kulshreshtha
* @version 1.0
* @copyright 2015-End_of_Time Abhinav Kulshreshtha
* @license DO WHAT THE F**K YOU WANT TO PUBLIC LICENSE < http://www.wtfpl.net/ >
*
* @todo Looks Like I missed managing symlinks.
*/
$source = realpath('./Main/');
$target = realpath('.').'/target';
// Remove target directory if already exists. Makes testing easy
if (is_dir($target)) {
shell_exec('rm -rf '.$target);
}
// Sleep for few seconds to ensure Shell command works.
usleep(10000);
// Create Target Directory.
mkdir($target, 0755);
foreach (
// A lot of silly object encapsulation, just because...
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item) {
if ($item->isDir()) {
mkdir($target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
// some debugging codes
// var_dump($item->getPathName());
// var_dump($item->getExtension());
// var_dump($target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
$sourceFile = $item->getPathName();
$targetFile = $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName();
// checking for php extensions
if(strcasecmp($item->getExtension(), 'php')===0){
// Php files will be minified with the shell command. It is extreamly slow but works safe.
echo "Minifyinf php file using shell".PHP_EOL.'Source - $sourceFile'.PHP_EOL.'Target - $targetFile';
shell_exec("php -w ".$sourceFile.' > '. $targetFile);
} else {
// all other non php files will be directly copied .
echo "Copying a ". $item->getExtension() ." file".PHP_EOL.'Source - $sourceFile'.PHP_EOL.'Target - $targetFile';
copy($item, $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
echo PHP_EOL.'-------------------------'.PHP_EOL; // good looking messages on terminal.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment