|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Component. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Component\MultiTenancy\Resolver; |
|
16
|
|
|
|
|
17
|
|
|
use LayerShifter\TLDExtract\Extract; |
|
18
|
|
|
use SWP\Component\MultiTenancy\Exception\TenantNotFoundException; |
|
19
|
|
|
use SWP\Component\MultiTenancy\Model\TenantInterface; |
|
20
|
|
|
use SWP\Component\MultiTenancy\Repository\TenantRepositoryInterface; |
|
21
|
|
|
|
|
22
|
|
|
class TenantResolver implements TenantResolverInterface |
|
23
|
|
|
{ |
|
24
|
|
|
private $tenantRepository; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(TenantRepositoryInterface $tenantRepository) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->tenantRepository = $tenantRepository; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function resolve(string $host = null): TenantInterface |
|
32
|
|
|
{ |
|
33
|
|
|
$domain = $this->extractDomain($host); |
|
34
|
|
|
$subdomain = $this->extractSubdomain($host); |
|
35
|
|
|
|
|
36
|
|
|
if (null !== $subdomain) { |
|
37
|
|
|
$tenant = $this->tenantRepository->findOneBySubdomainAndDomain($subdomain, $domain); |
|
38
|
|
|
} else { |
|
39
|
|
|
$tenant = $this->tenantRepository->findOneByDomain($domain); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (null === $tenant) { |
|
43
|
|
|
throw new TenantNotFoundException($host); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $tenant; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function extractDomain(string $host = null): string |
|
50
|
|
|
{ |
|
51
|
|
|
if (null === $host || TenantResolverInterface::LOCALHOST === $host) { |
|
52
|
|
|
return TenantResolverInterface::LOCALHOST; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$result = $this->extractHost($host); |
|
56
|
|
|
|
|
57
|
|
|
// handle case for ***.localhost |
|
58
|
|
|
if (TenantResolverInterface::LOCALHOST === $result->getSuffix() && |
|
59
|
|
|
null !== $result->getHostname() && |
|
60
|
|
|
null === $result->getSubdomain() |
|
61
|
|
|
) { |
|
62
|
|
|
return $result->getSuffix(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$domainString = $result->getHostname(); |
|
66
|
|
|
if (null !== $result->getSuffix()) { |
|
67
|
|
|
$domainString = $domainString.'.'.$result->getSuffix(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $domainString; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function extractSubdomain(string $host = null): ?string |
|
74
|
|
|
{ |
|
75
|
|
|
$result = $this->extractHost($host); |
|
76
|
|
|
|
|
77
|
|
|
// handle case for ***.localhost |
|
78
|
|
|
if (TenantResolverInterface::LOCALHOST === $result->getSuffix() && |
|
79
|
|
|
null !== $result->getHostname() && |
|
80
|
|
|
null === $result->getSubdomain() |
|
81
|
|
|
) { |
|
82
|
|
|
return $result->getHostname(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$subdomain = $result->getSubdomain(); |
|
86
|
|
|
if (null !== $subdomain) { |
|
87
|
|
|
return $subdomain; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function extractHost($host) |
|
94
|
|
|
{ |
|
95
|
|
|
$extract = new Extract(); |
|
96
|
|
|
|
|
97
|
|
|
return $extract->parse($host); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|