1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\Application\Environment\Detector; |
6
|
|
|
|
7
|
|
|
use Nette\Http\IRequest; |
8
|
|
|
use Nette\Utils\Strings; |
9
|
|
|
use SixtyEightPublishers\Application\Environment\IEnvironmentDetector; |
10
|
|
|
use SixtyEightPublishers\Application\Environment\Profile; |
11
|
|
|
use SixtyEightPublishers\Application\Environment\ProfileContainer; |
12
|
|
|
|
13
|
|
|
class NetteRequestDetector implements IEnvironmentDetector |
14
|
|
|
{ |
15
|
|
|
/** @var \Nette\Http\IRequest */ |
16
|
|
|
private $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param \Nette\Http\IRequest $request |
20
|
|
|
*/ |
21
|
|
|
public function __construct(IRequest $request) |
22
|
|
|
{ |
23
|
|
|
$this->request = $request; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function detect(ProfileContainer $profileContainer) |
30
|
|
|
{ |
31
|
|
|
$url = $this->request->getUrl()->getHost(); |
32
|
|
|
|
33
|
|
|
/** @var Profile $profile */ |
34
|
|
|
foreach ($profileContainer as $profile) { |
35
|
|
|
if ($profile->isEnabled()) { |
36
|
|
|
foreach ($profile->getDomains() as $domain) { |
37
|
|
|
if ((Strings::contains($domain, '*') && $this->match($domain, $url)) || ($domain === $url)) { |
38
|
|
|
return $profile; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return NULL; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $domain |
49
|
|
|
* @param string $url |
50
|
|
|
* |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
private function match($domain, $url) |
54
|
|
|
{ |
55
|
|
|
$count = substr_count($domain, '*'); |
56
|
|
|
foreach (explode('*', $domain) as $e) { |
57
|
|
|
if (empty($e)) { |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (strpos($url, $e) === FALSE) { |
62
|
|
|
return FALSE; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$url = str_replace($e, '@', $url); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$params = []; |
69
|
|
|
foreach (explode('@', $url) as $t) { |
70
|
|
|
if (!empty($t)) { |
71
|
|
|
$params[] = $t; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return ($count === count($params)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|