1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Command; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\Browscap; |
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\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* commands to parse a given useragent |
20
|
|
|
*/ |
21
|
|
|
class ParserCommand 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
|
|
|
public function __construct( |
39
|
|
|
string $defaultCacheFolder, |
40
|
|
|
?CacheInterface $cache = null, |
41
|
|
|
?LoggerInterface $logger = null |
42
|
|
|
) { |
43
|
|
|
$this->defaultCacheFolder = $defaultCacheFolder; |
44
|
|
|
$this->cache = $cache; |
45
|
|
|
$this->logger = $logger; |
46
|
|
|
|
47
|
|
|
parent::__construct(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
protected function configure() : void |
51
|
|
|
{ |
52
|
|
|
$this |
53
|
1 |
|
->setName('browscap:parse') |
54
|
1 |
|
->setDescription('Parses a user agent string and dumps the results.') |
55
|
1 |
|
->addArgument( |
56
|
1 |
|
'user-agent', |
57
|
1 |
|
InputArgument::REQUIRED, |
58
|
1 |
|
'User agent string to analyze', |
59
|
1 |
|
null |
60
|
|
|
) |
61
|
1 |
|
->addOption( |
62
|
1 |
|
'debug', |
63
|
1 |
|
'd', |
64
|
1 |
|
InputOption::VALUE_NONE, |
65
|
1 |
|
'Should the debug mode entered?' |
66
|
|
|
) |
67
|
1 |
|
->addOption( |
68
|
1 |
|
'cache', |
69
|
1 |
|
'c', |
70
|
1 |
|
InputOption::VALUE_OPTIONAL, |
71
|
1 |
|
'Where the cache files are located', |
72
|
1 |
|
$this->defaultCacheFolder |
73
|
|
|
); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
77
|
|
|
{ |
78
|
|
|
$logger = $this->getLogger($input); |
79
|
|
|
|
80
|
|
|
$browscap = new Browscap($this->getCache($input), $logger); |
81
|
|
|
|
82
|
|
|
$result = $browscap->getBrowser($input->getArgument('user-agent')); |
83
|
|
|
|
84
|
|
|
$output->writeln(json_encode($result, JSON_PRETTY_PRINT)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getCache(InputInterface $input) : CacheInterface |
88
|
|
|
{ |
89
|
|
|
if (null === $this->cache) { |
90
|
|
|
$fileCache = new FilesystemCache($input->getOption('cache')); |
91
|
|
|
$this->cache = new SimpleCacheAdapter($fileCache); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->cache; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function getLogger(InputInterface $input) : LoggerInterface |
98
|
|
|
{ |
99
|
|
|
if (null === $this->logger) { |
100
|
|
|
$this->logger = LoggerHelper::createDefaultLogger($input->getOption('debug')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->logger; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|