Completed
Pull Request — master (#105)
by
unknown
02:01
created

MinifyCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 16
c 2
b 1
f 1
lcom 0
cbo 5
dl 0
loc 106
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 0 28 1
C execute() 0 69 14
A getFileExt() 0 4 1
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