1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\BrowserFilter\Route; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Spinen\BrowserFilter\Filter; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RouteFilter |
10
|
|
|
* |
11
|
|
|
* @package Spinen\BrowserFilter\Route |
12
|
|
|
*/ |
13
|
|
|
abstract class RouteFilter extends Filter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Loop through all of the parameters in the string and process them. |
17
|
|
|
* |
18
|
|
|
* @param string $filter The filter separated by '/' |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
4 |
|
private function extractRule($filter) |
23
|
|
|
{ |
24
|
4 |
|
list($device, $browser, $operator_versions) = array_pad(array_filter(explode('/', $filter, 3)), 3, '*'); |
25
|
|
|
|
26
|
|
|
// Apply rule to all browsers of the device |
27
|
4 |
|
if ('*' === $browser) { |
28
|
3 |
|
$this->rules[$device] = '*'; |
29
|
|
|
|
30
|
3 |
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Apply rule to all versions of the browser |
34
|
4 |
|
if ('*' === $operator_versions) { |
35
|
4 |
|
$this->rules[$device][$browser] = '*'; |
36
|
|
|
|
37
|
4 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
$this->rules[$device][$browser] = $this->extractVersions($device, $browser, $operator_versions); |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Loop through all of the versions in the string and process them. |
45
|
|
|
* |
46
|
|
|
* @param string $device The device |
47
|
|
|
* @param string $browser The browser |
48
|
|
|
* @param string $operator_versions The versions separated by '|' |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
4 |
|
private function extractVersions($device, $browser, $operator_versions) |
53
|
|
|
{ |
54
|
|
|
// Were there existing rules for the browser? |
55
|
4 |
|
$versions = empty($this->getRules()[$device][$browser]) ? [] : $this->getRules()[$device][$browser]; |
56
|
|
|
|
57
|
4 |
|
foreach (array_filter(explode('|', $operator_versions)) as $operator_version) { |
58
|
|
|
// Remove everything to the leading numbers |
59
|
4 |
|
$version = preg_replace("/^[^\\d]*/u", "", $operator_version); |
60
|
|
|
// Default no operator to equals |
61
|
4 |
|
$operator = str_replace($version, '', $operator_version) ?: '='; |
62
|
|
|
|
63
|
4 |
|
$versions[$operator] = $version; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $versions; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
1 |
|
public function generateCacheKey(Request $request) |
73
|
|
|
{ |
74
|
1 |
|
return parent::generateCacheKey($request) . ':' . $this->getFilterType() . ':' . md5($request->path()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Generate the key to use to cache the processed filter string into an array. |
79
|
|
|
* |
80
|
|
|
* @param string $filter_string The filters separated by ';' |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
5 |
|
private function generateFilterStringCacheKey($filter_string) |
85
|
|
|
{ |
86
|
5 |
|
return 'filter_string:' . md5($filter_string); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Loop through all of the filters in the string and process them. |
91
|
|
|
* |
92
|
|
|
* @param string $filter_string The filters separated by ';' |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
6 |
|
public function parseFilterString($filter_string) |
97
|
|
|
{ |
98
|
6 |
|
if (empty($filter_string)) { |
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
$cache_key = $this->generateFilterStringCacheKey($filter_string); |
103
|
|
|
|
104
|
5 |
|
$this->rules = $this->cache->get($cache_key, []); |
105
|
|
|
|
106
|
5 |
|
if ($this->rules !== []) { |
107
|
1 |
|
return; |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
array_map([$this, 'extractRule'], array_filter(explode(';', $filter_string))); |
111
|
|
|
|
112
|
4 |
|
$this->cache->put($cache_key, $this->rules, $this->getCacheTimeout()); |
113
|
4 |
|
} |
114
|
|
|
} |
115
|
|
|
|