Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For easier command line using, I've added a new command with symfony/console #106

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions bin/minify
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env php
<?php

// check if script run in cli environment
if ('cli' !== php_sapi_name()) {
fwrite(STDERR, $argv[1].' must be run in the command line'.PHP_EOL);
exit(1);
}

// command line utility to minify CSS
if (file_exists(__DIR__.'/../../../autoload.php')) {
// if composer install
require_once __DIR__.'/../../../autoload.php';
} else {
require __DIR__.'/../vendor/autoload.php';
}

$minify = new \MatthiasMullie\Minify\Command\MinifyCommand();

array_splice($argv, 1, 0, $minify->getName());

$argInput = new \Symfony\Component\Console\Input\ArgvInput($argv);

$app = new \Symfony\Component\Console\Application();
$app->add($minify);
$app->run($argInput);
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,6 +30,7 @@
},
"bin": [
"bin/minifycss",
"bin/minifyjs"
"bin/minifyjs",
"bin/minify"
]
}
118 changes: 118 additions & 0 deletions src/Command/MinifyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace MatthiasMullie\Minify\Command;

use MatthiasMullie\Minify\CSS;
use MatthiasMullie\Minify\JS;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MinifyCommand extends Command
{
protected function configure()
{
$this->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>Error: type of input files is not all the same!</error>');

return 1;
}
}

$type = $autoDetectedType;
}

if (empty($type)) {
$output->writeln('<error>Error: cannot find the type of input file!</error>');

return 1;
}

switch (strtolower($type)) {
case 'css':
$minifier = new CSS();
break;
case 'js':
$minifier = new JS();
break;
default:
$output->writeln("<error>Error: Unsupported type: $type</error>");

return 3;
}

foreach ($from as $fromFile) {
if (!file_exists($fromFile)) {
$output->writeln("<error>Error: File '{$fromFile}' not found!</error>");

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, '.'), '.');
}
}