1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Component. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 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 2016 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
namespace SWP\Component\MultiTenancy\PathBuilder; |
15
|
|
|
|
16
|
|
|
use PHPCR\Util\PathHelper; |
17
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* TenantAwarePathBuilder class. |
21
|
|
|
*/ |
22
|
|
|
class TenantAwarePathBuilder implements TenantAwarePathBuilderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var TenantContextInterface |
26
|
|
|
*/ |
27
|
|
|
protected $tenantContext; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $rootPath; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $latestRootPath; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $defaultRootPath; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Construct. |
46
|
|
|
* |
47
|
|
|
* @param TenantContextInterface $tenantContext Tenant context |
48
|
|
|
* @param string $rootPath PHPCR root path (e.g. /swp) |
49
|
|
|
*/ |
50
|
|
|
public function __construct(TenantContextInterface $tenantContext, $rootPath) |
51
|
|
|
{ |
52
|
|
|
$this->tenantContext = $tenantContext; |
53
|
|
|
$this->rootPath = $rootPath; |
54
|
|
|
$this->defaultRootPath = $rootPath; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function build($data, $context = null) |
61
|
|
|
{ |
62
|
|
|
return $this->absolutize($data, $context); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Absolutize path or paths based on current context when provided. |
67
|
|
|
* |
68
|
|
|
* @param string|array $data Path or array of paths |
69
|
|
|
* @param string $context Path absolute context |
70
|
|
|
* |
71
|
|
|
* @return string|array Tenant aware paths |
72
|
|
|
*/ |
73
|
|
|
protected function absolutize($data, $context = null) |
74
|
|
|
{ |
75
|
|
|
$this->makePathTenantAware(); |
76
|
|
|
if (is_array($data)) { |
77
|
|
|
$tenantAwarePaths = []; |
78
|
|
|
foreach ($data as $path) { |
79
|
|
|
$tenantAwarePaths[] = $this->absolutizePath($path, $context); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $tenantAwarePaths; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->absolutizePath($data, $context); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Makes PHPCR tree root path to be tenant aware. |
90
|
|
|
* |
91
|
|
|
* When tenant is not available in tenant context it will resolve |
92
|
|
|
* the tenant from current request. |
93
|
|
|
*/ |
94
|
|
|
protected function makePathTenantAware() |
95
|
|
|
{ |
96
|
|
|
if ($this->latestRootPath === $this->rootPath) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$tenant = $this->tenantContext->getTenant(); |
101
|
|
|
$this->rootPath = $this->absolutizePath($tenant->getSubdomain()); |
102
|
|
|
$this->latestRootPath = $this->rootPath; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function absolutizePath($path, $context = null) |
106
|
|
|
{ |
107
|
|
|
if (null !== $context) { |
108
|
|
|
$context = $this->defaultRootPath.DIRECTORY_SEPARATOR.$context; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (isset($path[0]) && '/' === $path[0]) { |
112
|
|
|
$path = $context ?: $this->rootPath; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return (string) PathHelper::absolutizePath($path, $context ?: $this->rootPath); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|