-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommandUI.php
141 lines (130 loc) · 5.39 KB
/
CommandUI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* slince template collector library
* @author Tao <[email protected]>
*/
namespace Slince\Collector;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Slince\Event\Event;
class CommandUI extends BaseCommand
{
/**
* @var Collector
*/
protected $collector;
/**
* @var ProgressBar
*/
protected $progressBar;
/**
* 默认的采集器配置文件名
* var string
*/
const CONFIG_FILE = 'collector.json';
function initialize(InputInterface $input, OutputInterface $output)
{
$this->collector = new Collector('./', '');
}
function configure()
{
$this->setName('capture')
->addArgument('url', InputArgument::OPTIONAL, 'Entrance url,collector will collect from this link')
->addOption('savepath', null, InputOption::VALUE_OPTIONAL, 'Template save path', './')
->addOption('whitelist',null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Whitelist url,colllector will collect')
->addOption('blacklist', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Blacklist url,colllector will not collect')
->addOption('hosts', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The host allowed collect')
->addOption('patterns', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The capture url regex patterns')
->addOption('onlyPatterns', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The host allowed collect');
}
function execute(InputInterface $input, OutputInterface $output)
{
$parameters = $this->extractArgumentsAndOptions($input);
//如果当前目录下有配置文件自动加载配置文件
if (is_file($configFile = getcwd() . DIRECTORY_SEPARATOR . self::CONFIG_FILE)) {
$data = json_decode(file_get_contents($configFile), true);
$parameters = array_merge($parameters, $data);
}
call_user_func_array([$this, 'initializeCollector'], $parameters);
$this->collector->getDispatcher()->bind(Collector::EVENT_CAPTURE_URL_REPOSITORY, function(Event $event) use ($output){
$repository = $event->getArgument('repository');
$output->writeln(PHP_EOL);
$output->writeln($repository->getUrl()->getUrlString());
$progressBar = new ProgressBar($output, 100);
$progressBar->start();
$repository->getUrl()->setParameter('progressBar', $progressBar);
});
$this->collector->getDispatcher()->bind(Collector::EVENT_CAPTURED_URL_REPOSITORY, function(Event $event) use ($output){
$repository = $event->getArgument('repository');
$progressBar = $repository->getUrl()->getParameter('progressBar');
$progressBar->advance(50);
$progressBar->finish();
});
$this->collector->run();
}
/**
* 提取参数
* @param InputInterface $input
* @return array
*/
protected function extractArgumentsAndOptions(InputInterface $input)
{
$entranceUrl = $input->getArgument('url');
$savePath = $input->getOption('savepath');
$whitelistUrls = $input->getOption('whitelist');
$blacklistUrls = $input->getOption('blacklist');
$allowCaptureHosts = $input->getOption('hosts');
$urlPatterns = $input->getOption('patterns');
$onlyCaptureUrlPatterns = $input->getOption('onlyPatterns');
return [
'url' => $entranceUrl,
'savepath' => $savePath,
'whitelist' => $whitelistUrls,
'blacklist' => $blacklistUrls,
'hosts' => $allowCaptureHosts,
'patterns' => $urlPatterns,
'onlyPatterns' => $onlyCaptureUrlPatterns
];
}
/**
* 初始化采集器
* @param $entranceUrl
* @param $savePath
* @param array $allowCaptureHosts
* @param $whitelistUrls
* @param $blacklistUrls
* @param $urlPatterns
* @param $onlyCaptureUrlPatterns
*/
protected function initializeCollector($entranceUrl, $savePath, array $allowCaptureHosts, array $whitelistUrls, array $blacklistUrls, array $urlPatterns, $onlyCaptureUrlPatterns)
{
$this->collector->setRawEntranceUrl($entranceUrl);
$this->collector->setSavePath($savePath);
if (!empty($allowCaptureHosts)) {
$this->collector->setAllowedCaptureHosts($allowCaptureHosts);
}
if (!empty($whitelistUrls)) {
$this->collector->setWhitelistUrls($whitelistUrls);
}
if (!empty($blacklistUrls)) {
$this->collector->setBlacklistUrls($blacklistUrls);
}
if (!empty($urlPatterns)) {
$this->collector->setUrlPatterns($urlPatterns);
}
$this->collector->setOnlyCaptureUrlPatterns($onlyCaptureUrlPatterns);
}
static function main()
{
$application = new Application();
$command = new static();
$application->add($command);
$application->setDefaultCommand($command->getName());
$application->run();
}
}