1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BrowscapPHP\Command; |
5
|
|
|
|
6
|
|
|
use BrowscapPHP\Browscap; |
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
|
|
|
* commands to parse a given useragent |
19
|
|
|
*/ |
20
|
|
|
class ParserCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ?BrowscapCacheInterface |
24
|
|
|
*/ |
25
|
|
|
private $cache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $defaultCacheFolder; |
31
|
|
|
|
32
|
1 |
|
public function __construct($defaultCacheFolder, ?BrowscapCacheInterface $cache = null) |
33
|
|
|
{ |
34
|
1 |
|
$this->defaultCacheFolder = $defaultCacheFolder; |
35
|
1 |
|
$this->cache = $cache; |
36
|
|
|
|
37
|
1 |
|
parent::__construct(); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
2 |
|
protected function configure() : void |
41
|
|
|
{ |
42
|
|
|
$this |
43
|
2 |
|
->setName('browscap:parse') |
44
|
2 |
|
->setDescription('Parses a user agent string and dumps the results.') |
45
|
2 |
|
->addArgument( |
46
|
2 |
|
'user-agent', |
47
|
2 |
|
InputArgument::REQUIRED, |
48
|
2 |
|
'User agent string to analyze', |
49
|
2 |
|
null |
50
|
|
|
) |
51
|
2 |
|
->addOption( |
52
|
2 |
|
'debug', |
53
|
2 |
|
'd', |
54
|
2 |
|
InputOption::VALUE_NONE, |
55
|
2 |
|
'Should the debug mode entered?' |
56
|
|
|
) |
57
|
2 |
|
->addOption( |
58
|
2 |
|
'cache', |
59
|
2 |
|
'c', |
60
|
2 |
|
InputOption::VALUE_OPTIONAL, |
61
|
2 |
|
'Where the cache files are located', |
62
|
2 |
|
$this->defaultCacheFolder |
63
|
|
|
); |
64
|
2 |
|
} |
65
|
|
|
|
66
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) : void |
67
|
|
|
{ |
68
|
1 |
|
$loggerHelper = new LoggerHelper(); |
69
|
1 |
|
$logger = $loggerHelper->create($input->getOption('debug')); |
70
|
|
|
|
71
|
1 |
|
$browscap = new Browscap(); |
72
|
|
|
|
73
|
|
|
$browscap |
74
|
1 |
|
->setLogger($logger) |
75
|
1 |
|
->setCache($this->getCache($input)); |
76
|
|
|
|
77
|
1 |
|
$result = $browscap->getBrowser($input->getArgument('user-agent')); |
78
|
|
|
|
79
|
1 |
|
$output->writeln(json_encode($result, JSON_PRETTY_PRINT)); |
80
|
1 |
|
} |
81
|
|
|
|
82
|
1 |
|
private function getCache(InputInterface $input) : BrowscapCacheInterface |
83
|
|
|
{ |
84
|
1 |
|
if (null === $this->cache) { |
85
|
|
|
$cacheAdapter = new File([File::DIR => $input->getOption('cache')]); |
86
|
|
|
$this->cache = new BrowscapCache($cacheAdapter); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return $this->cache; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|