1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Command; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\BrowscapUpdater; |
7
|
|
|
use BrowscapPHP\Helper\IniLoaderInterface; |
8
|
|
|
use BrowscapPHP\Helper\LoggerHelper; |
9
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Psr\SimpleCache\CacheInterface; |
12
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Command to fetch a browscap ini file from the remote host and store the content in a local file |
21
|
|
|
*/ |
22
|
|
|
class FetchCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ?CacheInterface |
26
|
|
|
*/ |
27
|
|
|
private $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $defaultIniFile; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $defaultCacheFolder; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
private $logger; |
43
|
|
|
|
44
|
1 |
|
public function __construct( |
45
|
|
|
string $defaultCacheFolder, |
46
|
|
|
string $defaultIniFile, |
47
|
|
|
?CacheInterface $cache = null, |
48
|
|
|
?LoggerInterface $logger = null |
49
|
|
|
) { |
50
|
1 |
|
$this->defaultCacheFolder = $defaultCacheFolder; |
51
|
1 |
|
$this->defaultIniFile = $defaultIniFile; |
52
|
1 |
|
$this->cache = $cache; |
53
|
1 |
|
$this->logger = $logger; |
54
|
|
|
|
55
|
1 |
|
parent::__construct(); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
protected function configure() : void |
59
|
|
|
{ |
60
|
|
|
$this |
61
|
1 |
|
->setName('browscap:fetch') |
62
|
1 |
|
->setDescription('Fetches an updated INI file for Browscap.') |
63
|
1 |
|
->addArgument( |
64
|
1 |
|
'file', |
65
|
1 |
|
InputArgument::OPTIONAL, |
66
|
1 |
|
'browscap.ini file', |
67
|
1 |
|
$this->defaultIniFile |
68
|
|
|
) |
69
|
1 |
|
->addOption( |
70
|
1 |
|
'remote-file', |
71
|
1 |
|
'r', |
72
|
1 |
|
InputOption::VALUE_OPTIONAL, |
73
|
1 |
|
'browscap.ini file to download from remote location (possible values are: ' . IniLoaderInterface::PHP_INI_LITE |
74
|
1 |
|
. ', ' . IniLoaderInterface::PHP_INI . ', ' . IniLoaderInterface::PHP_INI_FULL . ')', |
75
|
1 |
|
IniLoaderInterface::PHP_INI |
76
|
|
|
) |
77
|
1 |
|
->addOption( |
78
|
1 |
|
'debug', |
79
|
1 |
|
'd', |
80
|
1 |
|
InputOption::VALUE_NONE, |
81
|
1 |
|
'Should the debug mode entered?' |
82
|
|
|
) |
83
|
1 |
|
->addOption( |
84
|
1 |
|
'cache', |
85
|
1 |
|
'c', |
86
|
1 |
|
InputOption::VALUE_OPTIONAL, |
87
|
1 |
|
'Where the cache files are located', |
88
|
1 |
|
$this->defaultCacheFolder |
89
|
|
|
); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
93
|
|
|
{ |
94
|
|
|
$logger = $this->getLogger($input); |
95
|
|
|
|
96
|
|
|
$file = $input->getArgument('file'); |
97
|
|
|
if (! $file) { |
98
|
|
|
$file = $this->defaultIniFile; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$logger->info('started fetching remote file'); |
102
|
|
|
|
103
|
|
|
$browscap = new BrowscapUpdater($this->getCache($input), $logger); |
104
|
|
|
$browscap->fetch($file, $input->getOption('remote-file')); |
105
|
|
|
|
106
|
|
|
$logger->info('finished fetching remote file'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function getCache(InputInterface $input) : CacheInterface |
110
|
|
|
{ |
111
|
|
|
if (null === $this->cache) { |
112
|
|
|
$fileCache = new FilesystemCache($input->getOption('cache')); |
113
|
|
|
$this->cache = new SimpleCacheAdapter($fileCache); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->cache; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function getLogger(InputInterface $input) : LoggerInterface |
120
|
|
|
{ |
121
|
|
|
if (null === $this->logger) { |
122
|
|
|
$this->logger = LoggerHelper::createDefaultLogger($input->getOption('debug')); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->logger; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|