|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the browscap package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 1998-2017, Browser Capabilities Project |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types = 1); |
|
12
|
|
|
namespace Browscap\Helper; |
|
13
|
|
|
|
|
14
|
|
|
use Browscap\Data\DataCollection; |
|
15
|
|
|
use Psr\Log\LoggerInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class CollectionCreator |
|
19
|
|
|
* |
|
20
|
|
|
* @category Browscap |
|
21
|
|
|
* |
|
22
|
|
|
* @author James Titcumb <[email protected]> |
|
23
|
|
|
* @author Thomas Müller <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class CollectionCreator |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Browscap\Data\DataCollection |
|
29
|
|
|
*/ |
|
30
|
|
|
private $collection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Psr\Log\LoggerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $logger; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Create a new collection creator |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function __construct(LoggerInterface $logger) |
|
43
|
|
|
{ |
|
44
|
3 |
|
$this->logger = $logger; |
|
45
|
3 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param \Browscap\Data\DataCollection $collection |
|
49
|
|
|
*/ |
|
50
|
7 |
|
public function setDataCollection(DataCollection $collection) : void |
|
51
|
|
|
{ |
|
52
|
7 |
|
$this->collection = $collection; |
|
53
|
7 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create and populate a data collection object from a resource folder |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $resourceFolder |
|
59
|
|
|
* |
|
60
|
|
|
* @throws \LogicException |
|
61
|
|
|
* |
|
62
|
|
|
* @return \Browscap\Data\DataCollection |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function createDataCollection(string $resourceFolder) : DataCollection |
|
65
|
|
|
{ |
|
66
|
3 |
|
if (null === $this->collection) { |
|
67
|
1 |
|
throw new \LogicException( |
|
68
|
|
|
'An instance of \Browscap\Data\DataCollection is required for this function. ' |
|
69
|
1 |
|
. 'Please set it with setDataCollection' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
$this->logger->debug('add platform file'); |
|
74
|
2 |
|
$this->collection->addPlatformsFile($resourceFolder . '/platforms.json'); |
|
75
|
1 |
|
$this->collection->addEnginesFile($resourceFolder . '/engines.json'); |
|
76
|
1 |
|
$this->collection->addDefaultProperties($resourceFolder . '/core/default-properties.json'); |
|
77
|
1 |
|
$this->collection->addDefaultBrowser($resourceFolder . '/core/default-browser.json'); |
|
78
|
|
|
|
|
79
|
1 |
|
$deviceDirectory = $resourceFolder . '/devices'; |
|
80
|
|
|
|
|
81
|
1 |
|
$iterator = new \RecursiveDirectoryIterator($deviceDirectory); |
|
82
|
|
|
|
|
83
|
1 |
View Code Duplication |
foreach (new \RecursiveIteratorIterator($iterator) as $file) { |
|
|
|
|
|
|
84
|
|
|
/** @var $file \SplFileInfo */ |
|
85
|
1 |
|
if (!$file->isFile() || 'json' !== $file->getExtension()) { |
|
86
|
1 |
|
continue; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
$this->logger->debug('add device file ' . $file->getPathname()); |
|
90
|
1 |
|
$this->collection->addDevicesFile($file->getPathname()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
$uaSourceDirectory = $resourceFolder . '/user-agents'; |
|
94
|
|
|
|
|
95
|
1 |
|
$iterator = new \RecursiveDirectoryIterator($uaSourceDirectory); |
|
96
|
|
|
|
|
97
|
1 |
View Code Duplication |
foreach (new \RecursiveIteratorIterator($iterator) as $file) { |
|
|
|
|
|
|
98
|
|
|
/** @var $file \SplFileInfo */ |
|
99
|
1 |
|
if (!$file->isFile() || 'json' !== $file->getExtension()) { |
|
100
|
1 |
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$this->logger->debug('add source file ' . $file->getPathname()); |
|
104
|
1 |
|
$this->collection->addSourceFile($file->getPathname()); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return $this->collection; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
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.