|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Tahoe\Bundle\MultiTenancyBundle\Service; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
7
|
|
|
use Symfony\Component\Routing\Router; |
|
8
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantTenantInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class TenantAwareRouter that cares about tenants! :-) |
|
12
|
|
|
* |
|
13
|
|
|
* @author Konrad Podgórski <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class TenantAwareRouter |
|
16
|
|
|
{ |
|
17
|
|
|
const ABSOLUTE_URL_FALSE = false; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var RequestStack |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $requestStack; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $domain; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Router |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $router; |
|
30
|
|
|
|
|
31
|
|
|
protected $strategy; |
|
32
|
|
|
|
|
33
|
|
|
protected $redirectRoute; |
|
34
|
|
|
|
|
35
|
|
|
function __construct($requestStack, $domain , $router, $strategy, $route = '') |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$this->requestStack = $requestStack; |
|
38
|
|
|
$this->domain = $domain; |
|
39
|
|
|
$this->router = $router; |
|
40
|
|
|
$this->strategy = $strategy; |
|
41
|
|
|
$this->redirectRoute = $route; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param MultiTenantTenantInterface $tenant |
|
46
|
|
|
* @param string $name |
|
|
|
|
|
|
47
|
|
|
* @param array $parameters |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
public function generateUrl(MultiTenantTenantInterface $tenant, $parameters = array()) |
|
52
|
|
|
{ |
|
53
|
|
|
$url = ''; |
|
|
|
|
|
|
54
|
|
|
$name = $this->redirectRoute; |
|
55
|
|
|
|
|
56
|
|
|
if ($this->strategy == TenantResolver::STRATEGY_TENANT_AWARE_SUBDOMAIN ) { |
|
57
|
|
|
$scheme = $this->requestStack->getCurrentRequest()->getScheme(); |
|
58
|
|
|
$requestPort = $this->requestStack->getCurrentRequest()->getPort(); |
|
59
|
|
|
|
|
60
|
|
|
$host = $scheme . '://' . $tenant->getSubdomain() . '.' . $this->domain; |
|
61
|
|
|
|
|
62
|
|
|
$port = ''; |
|
63
|
|
|
if ('http' === $scheme && 80 != $requestPort) { |
|
64
|
|
|
$port = ':'.$requestPort; |
|
65
|
|
|
} elseif ('https' === $scheme && 443 != $requestPort) { |
|
66
|
|
|
$port = ':'.$requestPort; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$url = $host . $port; |
|
70
|
|
|
$url .= $this->router->generate($name, $parameters, self::ABSOLUTE_URL_FALSE); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
return $url; |
|
73
|
|
|
} else { |
|
74
|
|
|
return $this->router->generate($name, $parameters); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.