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
|
|
|
|
13
|
|
|
class MinifyCommand extends Command |
14
|
|
|
{ |
15
|
|
|
protected function configure() |
16
|
|
|
{ |
17
|
|
|
$this->setName('minify') |
18
|
|
|
->setDescription('Minify js or css') |
19
|
|
|
->addArgument( |
20
|
|
|
'from', |
21
|
|
|
InputArgument::REQUIRED | InputArgument::IS_ARRAY, |
22
|
|
|
'From which files you wanna to minify' |
23
|
|
|
) |
24
|
|
|
->addOption( |
25
|
|
|
'type', |
26
|
|
|
't', |
27
|
|
|
InputOption::VALUE_OPTIONAL, |
28
|
|
|
'Which type of file you wanna minify? js or css (Default is auto detected according to the extension name.)' |
29
|
|
|
) |
30
|
|
|
->addOption( |
31
|
|
|
'output', |
32
|
|
|
'o', |
33
|
|
|
InputOption::VALUE_OPTIONAL, |
34
|
|
|
'The output file (Default is STDOUT)' |
35
|
|
|
) |
36
|
|
|
->addOption( |
37
|
|
|
'append', |
38
|
|
|
'a', |
39
|
|
|
InputOption::VALUE_OPTIONAL, |
40
|
|
|
'Append to the file (Default is overwrite)' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
45
|
|
|
{ |
46
|
|
|
$from = $input->getArgument('from'); |
47
|
|
|
$type = $input->getOption('type'); |
48
|
|
|
$outputFile = $input->getOption('output'); |
49
|
|
|
$appendFile = $input->getOption('append'); |
50
|
|
|
|
51
|
|
|
if (!is_array($from)) { |
52
|
|
|
$from = (array) $from; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (empty($type)) { |
56
|
|
|
$autoDetectedType = self::getFileExt($from[0]); |
57
|
|
|
foreach ($from as $fromFile) { |
58
|
|
|
$fileExt = self::getFileExt($fromFile); |
59
|
|
|
if (strcasecmp($fileExt, $autoDetectedType) !== 0) { |
60
|
|
|
$output->writeln('<error>Error: type of input files is not all the same!</error>'); |
61
|
|
|
|
62
|
|
|
return 1; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$type = $autoDetectedType; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (empty($type)) { |
70
|
|
|
$output->writeln('<error>Error: cannot find the type of input file!</error>'); |
71
|
|
|
|
72
|
|
|
return 1; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
switch (strtolower($type)) { |
76
|
|
|
case 'css': |
77
|
|
|
$minifier = new CSS(); |
78
|
|
|
break; |
79
|
|
|
case 'js': |
80
|
|
|
$minifier = new JS(); |
81
|
|
|
break; |
82
|
|
|
default: |
83
|
|
|
$output->writeln("<error>Error: Unsupported type: $type</error>"); |
84
|
|
|
|
85
|
|
|
return 3; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($from as $fromFile) { |
89
|
|
|
if (!file_exists($fromFile)) { |
90
|
|
|
$output->writeln("<error>Error: File '{$fromFile}' not found!</error>"); |
91
|
|
|
|
92
|
|
|
return 2; |
93
|
|
|
} |
94
|
|
|
$minifier->add($fromFile); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$result = $minifier->minify(); |
98
|
|
|
|
99
|
|
|
if (empty($outputFile) && empty($appendFile)) { |
100
|
|
|
$output->writeln($result, OutputInterface::OUTPUT_RAW); |
101
|
|
|
} else { |
102
|
|
|
if (!empty($outputFile)) { |
103
|
|
|
file_put_contents($outputFile, $result); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!empty($appendFile)) { |
107
|
|
|
file_put_contents($appendFile, $result, FILE_APPEND); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function getFileExt($fileName) |
115
|
|
|
{ |
116
|
|
|
return ltrim(strrchr($fileName, '.'), '.'); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|