|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Marco Muths |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace Squeeze; |
|
27
|
|
|
|
|
28
|
|
|
use Squeeze\MessageInterface as Message; |
|
29
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
|
30
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
31
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
32
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
33
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
34
|
|
|
use Symfony\Component\Finder\Finder; |
|
35
|
|
|
|
|
36
|
|
|
class Command extends BaseCommand |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var Finder */ |
|
39
|
|
|
private $finder; |
|
40
|
|
|
|
|
41
|
|
|
/** @var Filter */ |
|
42
|
|
|
private $filter; |
|
43
|
|
|
|
|
44
|
|
|
/** @var Writer */ |
|
45
|
|
|
private $writer; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Finder $finder |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setFinder(Finder $finder) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->finder = $finder; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Filter $filter |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setFilter(Filter $filter) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->filter = $filter; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Writer $writer |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setWriter(Writer $writer) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->writer = $writer; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
protected function configure() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->addArgument('target', InputArgument::REQUIRED, Message::ARGUMENT_TARGET); |
|
75
|
|
|
$this->addOption('source', 's', InputOption::VALUE_OPTIONAL, Message::OPTION_SOURCE, '.'); |
|
76
|
|
|
$this->addOption('exclude', 'e', InputOption::VALUE_OPTIONAL, Message::OPTION_EXCLUDE); |
|
77
|
|
|
$this->addOption('notname', 'f', InputOption::VALUE_OPTIONAL, Message::OPTION_NOTNAME); |
|
78
|
|
|
$this->addOption('nocomments', 'c', InputOption::VALUE_NONE, Message::OPTION_NOCOMMENTS); |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
83
|
|
|
{ |
|
84
|
|
|
$output->writeln($this->getDescription() . PHP_EOL); |
|
85
|
|
|
|
|
86
|
|
|
$target = $input->getArgument('target'); |
|
87
|
|
|
$sources = $this->createArrayBy($input->getOption('source')); |
|
88
|
|
|
$excludes = $this->createArrayBy($input->getOption('exclude')); |
|
89
|
|
|
$noComments = (bool) $input->getOption('nocomments'); |
|
90
|
|
|
|
|
91
|
|
|
$this->writer->setTarget($target); |
|
92
|
|
|
$this->finder->in($sources)->exclude($excludes); |
|
93
|
|
|
|
|
94
|
|
|
if ($notName = $input->getOption('notname')) { |
|
95
|
|
|
$this->finder->notName($notName); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$output->writeln(sprintf(Message::PROGRESS_FILTER, $this->finder->count())); |
|
99
|
|
|
$classMap = $this->filter->extractClassMapFrom($this->finder->getIterator()); |
|
100
|
|
|
|
|
101
|
|
|
$output->writeln(sprintf(Message::PROGRESS_WRITE, $target)); |
|
102
|
|
|
$this->writer->minify($classMap, $noComments); |
|
103
|
|
|
|
|
104
|
|
|
$output->writeln(PHP_EOL . Message::PROGRESS_DONE . PHP_EOL); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $string |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
private function createArrayBy($string) |
|
112
|
|
|
{ |
|
113
|
|
|
return array_filter(array_map('trim', explode(',', $string))); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|