1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 1998-2015 Browser Capabilities Project |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a |
6
|
|
|
* copy of this software and associated documentation files (the "Software"), |
7
|
|
|
* to deal in the Software without restriction, including without limitation |
8
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the |
10
|
|
|
* Software is furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included |
13
|
|
|
* in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* @category Browscap-PHP |
24
|
|
|
* @copyright 1998-2015 Browser Capabilities Project |
25
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
26
|
|
|
* @link https://github.com/browscap/browscap-php/ |
27
|
|
|
* @since added with version 3.0 |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace BrowscapPHP\Command; |
31
|
|
|
|
32
|
|
|
use BrowscapPHP\BrowscapUpdater; |
33
|
|
|
use BrowscapPHP\Cache\BrowscapCache; |
34
|
|
|
use BrowscapPHP\Cache\BrowscapCacheInterface; |
35
|
|
|
use BrowscapPHP\Helper\IniLoader; |
36
|
|
|
use BrowscapPHP\Helper\LoggerHelper; |
37
|
|
|
use Symfony\Component\Console\Command\Command; |
38
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
39
|
|
|
use Symfony\Component\Console\Input\InputOption; |
40
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
41
|
|
|
use WurflCache\Adapter\File; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* command to fetch a browscap ini file from the remote host, convert it into an array and store the content in a local |
45
|
|
|
* file |
46
|
|
|
* |
47
|
|
|
* @category Browscap-PHP |
48
|
|
|
* @author Dave Olsen, http://dmolsen.com |
49
|
|
|
* @author Thomas Müller <[email protected]> |
50
|
|
|
* @copyright Copyright (c) 1998-2015 Browser Capabilities Project |
51
|
|
|
* @version 3.0 |
52
|
|
|
* @license http://www.opensource.org/licenses/MIT MIT License |
53
|
|
|
* @link https://github.com/browscap/browscap-php/ |
54
|
|
|
*/ |
55
|
|
|
class UpdateCommand extends Command |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* @var BrowscapCacheInterface |
59
|
|
|
*/ |
60
|
|
|
private $cache = null; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
private $defaultCacheFolder; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $defaultCacheFolder |
69
|
|
|
*/ |
70
|
1 |
|
public function __construct($defaultCacheFolder) |
71
|
|
|
{ |
72
|
1 |
|
$this->defaultCacheFolder = $defaultCacheFolder; |
73
|
|
|
|
74
|
1 |
|
parent::__construct(); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
1 |
|
public function setCache(BrowscapCacheInterface $cache) |
83
|
|
|
{ |
84
|
1 |
|
$this->cache = $cache; |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Configures the current command. |
91
|
|
|
*/ |
92
|
1 |
|
protected function configure() |
93
|
|
|
{ |
94
|
|
|
$this |
95
|
1 |
|
->setName('browscap:update') |
96
|
1 |
|
->setDescription('Fetches an updated INI file for Browscap and overwrites the current PHP file.') |
97
|
1 |
|
->addOption( |
98
|
1 |
|
'remote-file', |
99
|
1 |
|
'r', |
100
|
1 |
|
InputOption::VALUE_OPTIONAL, |
101
|
1 |
|
'browscap.ini file to download from remote location (possible values are: ' . IniLoader::PHP_INI_LITE |
102
|
1 |
|
. ', ' . IniLoader::PHP_INI . ', ' . IniLoader::PHP_INI_FULL . ')', |
103
|
1 |
|
IniLoader::PHP_INI |
104
|
|
|
) |
105
|
1 |
|
->addOption( |
106
|
1 |
|
'no-backup', |
107
|
1 |
|
null, |
108
|
1 |
|
InputOption::VALUE_NONE, |
109
|
1 |
|
'Do not backup the previously existing file' |
110
|
|
|
) |
111
|
1 |
|
->addOption( |
112
|
1 |
|
'debug', |
113
|
1 |
|
'd', |
114
|
1 |
|
InputOption::VALUE_NONE, |
115
|
1 |
|
'Should the debug mode entered?' |
116
|
|
|
) |
117
|
1 |
|
->addOption( |
118
|
1 |
|
'cache', |
119
|
1 |
|
'c', |
120
|
1 |
|
InputOption::VALUE_OPTIONAL, |
121
|
1 |
|
'Where the cache files are located', |
122
|
1 |
|
$this->defaultCacheFolder |
123
|
|
|
); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param InputInterface $input |
128
|
|
|
* @param OutputInterface $output |
129
|
|
|
* |
130
|
|
|
* @return int|null|void |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$loggerHelper = new LoggerHelper(); |
135
|
|
|
$logger = $loggerHelper->create($input->getOption('debug')); |
136
|
|
|
|
137
|
|
|
$logger->info('started updating cache with remote file'); |
138
|
|
|
|
139
|
|
|
$browscap = new BrowscapUpdater(); |
140
|
|
|
|
141
|
|
|
$browscap->setLogger($logger); |
142
|
|
|
$browscap->setCache($this->getCache($input)); |
143
|
|
|
$browscap->update($input->getOption('remote-file')); |
144
|
|
|
|
145
|
|
|
$logger->info('finished updating cache with remote file'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param InputInterface $input |
150
|
|
|
* |
151
|
|
|
* @return BrowscapCacheInterface |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
private function getCache(InputInterface $input) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
if (null === $this->cache) { |
156
|
|
|
$cacheAdapter = new File([File::DIR => $input->getOption('cache')]); |
157
|
|
|
$this->cache = new BrowscapCache($cacheAdapter); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->cache; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.