1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Command; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\BrowscapUpdater; |
7
|
|
|
use BrowscapPHP\Cache\BrowscapCache; |
8
|
|
|
use BrowscapPHP\Cache\BrowscapCacheInterface; |
9
|
|
|
use BrowscapPHP\Helper\LoggerHelper; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use WurflCache\Adapter\File; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Command to convert a downloaded Browscap ini file and write it to the cache |
19
|
|
|
*/ |
20
|
|
|
class ConvertCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ?BrowscapCacheInterface |
24
|
|
|
*/ |
25
|
|
|
private $cache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $defaultIniFile; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $defaultCacheFolder; |
36
|
|
|
|
37
|
2 |
|
public function __construct( |
38
|
|
|
string $defaultCacheFolder, |
39
|
|
|
string $defaultIniFile, |
40
|
|
|
?BrowscapCacheInterface $cache = null |
41
|
|
|
) { |
42
|
2 |
|
$this->defaultCacheFolder = $defaultCacheFolder; |
43
|
2 |
|
$this->defaultIniFile = $defaultIniFile; |
44
|
2 |
|
$this->cache = $cache; |
45
|
|
|
|
46
|
2 |
|
parent::__construct(); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
protected function configure() : void |
50
|
|
|
{ |
51
|
|
|
$this |
52
|
2 |
|
->setName('browscap:convert') |
53
|
2 |
|
->setDescription('Converts an existing browscap.ini file to a cache.php file.') |
54
|
2 |
|
->addArgument( |
55
|
2 |
|
'file', |
56
|
2 |
|
InputArgument::OPTIONAL, |
57
|
2 |
|
'Path to the browscap.ini file', |
58
|
2 |
|
$this->defaultIniFile |
59
|
|
|
) |
60
|
2 |
|
->addOption( |
61
|
2 |
|
'debug', |
62
|
2 |
|
'd', |
63
|
2 |
|
InputOption::VALUE_NONE, |
64
|
2 |
|
'Should the debug mode entered?' |
65
|
|
|
) |
66
|
2 |
|
->addOption( |
67
|
2 |
|
'cache', |
68
|
2 |
|
'c', |
69
|
2 |
|
InputOption::VALUE_OPTIONAL, |
70
|
2 |
|
'Where the cache files are located', |
71
|
2 |
|
$this->defaultCacheFolder |
72
|
|
|
); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
76
|
|
|
{ |
77
|
1 |
|
$loggerHelper = new LoggerHelper(); |
78
|
1 |
|
$logger = $loggerHelper->create($input->getOption('debug')); |
79
|
|
|
|
80
|
1 |
|
$logger->info('initializing converting process'); |
81
|
|
|
|
82
|
1 |
|
$browscap = new BrowscapUpdater(); |
83
|
|
|
|
84
|
1 |
|
$browscap->setLogger($logger); |
85
|
1 |
|
$browscap->setCache($this->getCache($input)); |
86
|
|
|
|
87
|
1 |
|
$logger->info('started converting local file'); |
88
|
|
|
|
89
|
1 |
|
$file = ($input->getArgument('file') ? $input->getArgument('file') : ($this->defaultIniFile)); |
90
|
|
|
|
91
|
1 |
|
$browscap->convertFile($file); |
92
|
|
|
|
93
|
1 |
|
$logger->info('finished converting local file'); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
private function getCache(InputInterface $input) : BrowscapCacheInterface |
97
|
|
|
{ |
98
|
1 |
|
if (null === $this->cache) { |
99
|
|
|
$cacheAdapter = new File([File::DIR => $input->getOption('cache')]); |
100
|
|
|
$this->cache = new BrowscapCache($cacheAdapter); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $this->cache; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|