1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Command; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\BrowscapUpdater; |
7
|
|
|
use BrowscapPHP\Helper\LoggerHelper; |
8
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\SimpleCache\CacheInterface; |
11
|
|
|
use Roave\DoctrineSimpleCache\SimpleCacheAdapter; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Command to fetch a browscap ini file from the remote host, convert it into an array and store the content in a local |
19
|
|
|
* file |
20
|
|
|
*/ |
21
|
|
|
class CheckUpdateCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ?CacheInterface |
25
|
|
|
*/ |
26
|
|
|
private $cache; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $defaultCacheFolder; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var LoggerInterface |
35
|
|
|
*/ |
36
|
|
|
private $logger; |
37
|
|
|
|
38
|
1 |
|
public function __construct( |
39
|
|
|
string $defaultCacheFolder, |
40
|
|
|
?CacheInterface $cache = null, |
41
|
|
|
?LoggerInterface $logger = null |
42
|
|
|
) { |
43
|
1 |
|
$this->defaultCacheFolder = $defaultCacheFolder; |
44
|
1 |
|
$this->cache = $cache; |
45
|
1 |
|
$this->logger = $logger; |
46
|
|
|
|
47
|
1 |
|
parent::__construct(); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
1 |
|
protected function configure() : void |
51
|
|
|
{ |
52
|
|
|
$this |
53
|
1 |
|
->setName('browscap:check-update') |
54
|
1 |
|
->setDescription('Checks if an updated INI file is available.') |
55
|
1 |
|
->addOption( |
56
|
1 |
|
'debug', |
57
|
1 |
|
'd', |
58
|
1 |
|
InputOption::VALUE_NONE, |
59
|
1 |
|
'Should the debug mode entered?' |
60
|
|
|
) |
61
|
1 |
|
->addOption( |
62
|
1 |
|
'cache', |
63
|
1 |
|
'c', |
64
|
1 |
|
InputOption::VALUE_OPTIONAL, |
65
|
1 |
|
'Where the cache files are located', |
66
|
1 |
|
$this->defaultCacheFolder |
67
|
|
|
); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
71
|
|
|
{ |
72
|
|
|
$logger = $this->getLogger($input); |
73
|
|
|
|
74
|
|
|
$logger->debug('started checking for new version of remote file'); |
75
|
|
|
|
76
|
|
|
$browscap = new BrowscapUpdater($this->getCache($input), $logger); |
77
|
|
|
$browscap->checkUpdate(); |
78
|
|
|
|
79
|
|
|
$logger->debug('finished checking for new version of remote file'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function getCache(InputInterface $input) : CacheInterface |
83
|
|
|
{ |
84
|
|
|
if (null === $this->cache) { |
85
|
|
|
$fileCache = new FilesystemCache($input->getOption('cache')); |
86
|
|
|
$this->cache = new SimpleCacheAdapter($fileCache); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->cache; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getLogger(InputInterface $input) : LoggerInterface |
93
|
|
|
{ |
94
|
|
|
if (null === $this->logger) { |
95
|
|
|
$this->logger = LoggerHelper::createDefaultLogger($input->getOption('debug')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->logger; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|