1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MatthiasMullie\Minify\Command; |
4
|
|
|
|
5
|
|
|
use MatthiasMullie\Minify\CSS; |
6
|
|
|
use MatthiasMullie\Minify\JS; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class MinifyCommand. |
16
|
|
|
*/ |
17
|
|
|
class MinifyCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
* |
22
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
23
|
|
|
*/ |
24
|
|
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
$this->setName('minify') |
27
|
|
|
->setDescription('Minify js or css') |
28
|
|
|
->addArgument( |
29
|
|
|
'from', |
30
|
|
|
InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
31
|
|
|
'Input files to minify' |
32
|
|
|
) |
33
|
|
|
->addOption( |
34
|
|
|
'type', |
35
|
|
|
't', |
36
|
|
|
InputOption::VALUE_OPTIONAL, |
37
|
|
|
'Type of file to minify, js or css (Default is auto detected according to the extension name.)' |
38
|
|
|
) |
39
|
|
|
->addOption( |
40
|
|
|
'preserveComments', |
41
|
|
|
'c', |
42
|
|
|
InputOption::VALUE_NONE, |
43
|
|
|
'Save preserved comments in minified files' |
44
|
|
|
) |
45
|
|
|
->addOption( |
46
|
|
|
'output', |
47
|
|
|
'o', |
48
|
|
|
InputOption::VALUE_OPTIONAL, |
49
|
|
|
'The output file (Default is STDOUT)' |
50
|
|
|
) |
51
|
|
|
->addOption( |
52
|
|
|
'append', |
53
|
|
|
'a', |
54
|
|
|
InputOption::VALUE_NONE, |
55
|
|
|
'Append to the file (Default is overwrite)' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
63
|
|
|
* @throws \Symfony\Component\Console\Exception\RuntimeException |
64
|
|
|
*/ |
65
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
66
|
|
|
{ |
67
|
|
|
$from = $input->getArgument('from'); |
68
|
|
|
$type = $input->getOption('type'); |
69
|
|
|
$preserveComments = $input->getOption('preserveComments'); |
70
|
|
|
$outputFile = $input->getOption('output'); |
71
|
|
|
$appendFile = $input->getOption('append'); |
72
|
|
|
if ($outputFile === null && $appendFile) { |
73
|
|
|
throw new RuntimeException('Append file used without output file specification!'); |
74
|
|
|
} |
75
|
|
|
if (!is_array($from)) { |
76
|
|
|
$from = (array) $from; |
77
|
|
|
} |
78
|
|
|
if (empty($type)) { |
79
|
|
|
$autoDetectedType = self::getFileExt($from[0]); |
80
|
|
|
foreach ($from as $fromFile) { |
81
|
|
|
$fileExt = self::getFileExt($fromFile); |
82
|
|
|
if (strcasecmp($fileExt, $autoDetectedType) !== 0) { |
83
|
|
|
throw new RuntimeException('Type of input files is not all the same!'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
$type = $autoDetectedType; |
87
|
|
|
} |
88
|
|
|
if (empty($type)) { |
89
|
|
|
throw new RuntimeException('Error: cannot find the type of input file!'); |
90
|
|
|
} |
91
|
|
|
switch (strtolower($type)) { |
92
|
|
|
case 'css': |
93
|
|
|
$minifier = new CSS(); |
94
|
|
|
break; |
95
|
|
|
case 'js': |
96
|
|
|
$minifier = new JS(); |
97
|
|
|
break; |
98
|
|
|
default: |
99
|
|
|
throw new RuntimeException("Unsupported type: {$type}"); |
100
|
|
|
} |
101
|
|
|
foreach ($from as $fromFile) { |
102
|
|
|
if (!file_exists($fromFile)) { |
103
|
|
|
throw new RuntimeException("File '{$fromFile}' not found!"); |
104
|
|
|
} |
105
|
|
|
$minifier->add($fromFile); |
106
|
|
|
} |
107
|
|
|
if ($preserveComments) { |
108
|
|
|
$minifier->setLeavePreservedComments(true); |
109
|
|
|
} |
110
|
|
|
$result = $minifier->minify(); |
111
|
|
|
if ($outputFile === null) { |
112
|
|
|
$output->writeln($result, OutputInterface::OUTPUT_RAW); |
113
|
|
|
} elseif ($appendFile) { |
114
|
|
|
file_put_contents($outputFile, $result, FILE_APPEND); |
115
|
|
|
} else { |
116
|
|
|
file_put_contents($outputFile, $result); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $fileName |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public static function getFileExt($fileName) |
128
|
|
|
{ |
129
|
|
|
return ltrim(strrchr($fileName, '.'), '.'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|