diff --git a/bin/minify b/bin/minify new file mode 100644 index 0000000..af908fe --- /dev/null +++ b/bin/minify @@ -0,0 +1,26 @@ +#!/usr/bin/env php +getName()); + +$argInput = new \Symfony\Component\Console\Input\ArgvInput($argv); + +$app = new \Symfony\Component\Console\Application(); +$app->add($minify); +$app->run($argInput); diff --git a/composer.json b/composer.json index ae1bdf0..dbf8e56 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "require": { "php": ">=5.3.0", "ext-pcre": "*", - "matthiasmullie/path-converter": "~1.0" + "matthiasmullie/path-converter": "~1.0", + "symfony/console": ">=2.0" }, "require-dev": { "matthiasmullie/scrapbook": "~1.0", @@ -29,6 +30,7 @@ }, "bin": [ "bin/minifycss", - "bin/minifyjs" + "bin/minifyjs", + "bin/minify" ] } diff --git a/src/Command/MinifyCommand.php b/src/Command/MinifyCommand.php new file mode 100644 index 0000000..c89b625 --- /dev/null +++ b/src/Command/MinifyCommand.php @@ -0,0 +1,118 @@ +setName('minify') + ->setDescription('Minify js or css') + ->addArgument( + 'from', + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'From which files you wanna to minify' + ) + ->addOption( + 'type', + 't', + InputOption::VALUE_OPTIONAL, + 'Which type of file you wanna minify? js or css (Default is auto detected according to the extension name.)' + ) + ->addOption( + 'output', + 'o', + InputOption::VALUE_OPTIONAL, + 'The output file (Default is STDOUT)' + ) + ->addOption( + 'append', + 'a', + InputOption::VALUE_OPTIONAL, + 'Append to the file (Default is overwrite)' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $from = $input->getArgument('from'); + $type = $input->getOption('type'); + $outputFile = $input->getOption('output'); + $appendFile = $input->getOption('append'); + + if (!is_array($from)) { + $from = (array) $from; + } + + if (empty($type)) { + $autoDetectedType = self::getFileExt($from[0]); + foreach ($from as $fromFile) { + $fileExt = self::getFileExt($fromFile); + if (strcasecmp($fileExt, $autoDetectedType) !== 0) { + $output->writeln('Error: type of input files is not all the same!'); + + return 1; + } + } + + $type = $autoDetectedType; + } + + if (empty($type)) { + $output->writeln('Error: cannot find the type of input file!'); + + return 1; + } + + switch (strtolower($type)) { + case 'css': + $minifier = new CSS(); + break; + case 'js': + $minifier = new JS(); + break; + default: + $output->writeln("Error: Unsupported type: $type"); + + return 3; + } + + foreach ($from as $fromFile) { + if (!file_exists($fromFile)) { + $output->writeln("Error: File '{$fromFile}' not found!"); + + return 2; + } + $minifier->add($fromFile); + } + + $result = $minifier->minify(); + + if (empty($outputFile) && empty($appendFile)) { + $output->writeln($result, OutputInterface::OUTPUT_RAW); + } else { + if (!empty($outputFile)) { + file_put_contents($outputFile, $result); + } + + if (!empty($appendFile)) { + file_put_contents($appendFile, $result, FILE_APPEND); + } + } + + return 0; + } + + public static function getFileExt($fileName) + { + return ltrim(strrchr($fileName, '.'), '.'); + } +}