1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Bundle. |
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
|
|
|
namespace SWP\Bundle\MultiTenancyBundle\Initializer; |
15
|
|
|
|
16
|
|
|
use PHPCR\Util\NodeHelper; |
17
|
|
|
use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface; |
18
|
|
|
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry; |
19
|
|
|
use PHPCR\SessionInterface; |
20
|
|
|
use SWP\Component\MultiTenancy\Provider\TenantProviderInterface; |
21
|
|
|
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface; |
22
|
|
|
use SWP\Component\MultiTenancy\Model\SiteDocumentInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\UnexpectedTypeException; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PHPCR Base Paths Repository Initializer. |
27
|
|
|
* |
28
|
|
|
* It creates based paths in content repository based on provided |
29
|
|
|
* tenants and config. Disabled by default, can be enabled in config. |
30
|
|
|
* Requires DoctrinePHPCRBundle to be configured in the system. |
31
|
|
|
*/ |
32
|
|
|
class PHPCRBasePathsInitializer implements InitializerInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $paths; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TenantProviderInterface |
41
|
|
|
*/ |
42
|
|
|
private $tenantProvider; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var TenantAwarePathBuilderInterface |
46
|
|
|
*/ |
47
|
|
|
private $pathBuilder; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $siteClass; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $documentClass; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Construct. |
61
|
|
|
* |
62
|
|
|
* @param array $paths Content paths |
63
|
|
|
* @param TenantProviderInterface $tenantProvider Tenants provider |
64
|
|
|
* @param TenantAwarePathBuilderInterface $pathBuilder Path builder |
65
|
|
|
* @param string $siteClass Site document class |
66
|
|
|
* @param string $documentClass Page document FQCN |
67
|
|
|
*/ |
68
|
|
|
public function __construct( |
69
|
|
|
array $paths, |
70
|
|
|
TenantProviderInterface $tenantProvider, |
71
|
|
|
TenantAwarePathBuilderInterface $pathBuilder, |
72
|
|
|
$siteClass, |
73
|
|
|
$documentClass |
74
|
|
|
) { |
75
|
|
|
$this->paths = $paths; |
76
|
|
|
$this->tenantProvider = $tenantProvider; |
77
|
|
|
$this->pathBuilder = $pathBuilder; |
78
|
|
|
$this->siteClass = $siteClass; |
79
|
|
|
$this->documentClass = $documentClass; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function init(ManagerRegistry $registry) |
86
|
|
|
{ |
87
|
|
|
$session = $registry->getConnection(); |
88
|
|
|
$this->dm = $registry->getManager(); |
|
|
|
|
89
|
|
|
$tenants = $this->tenantProvider->getAvailableTenants(); |
90
|
|
|
|
91
|
|
|
$this->generateBasePaths($session, $tenants); |
92
|
|
|
$this->dm->flush(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function generateBasePaths(SessionInterface $session, array $tenants = []) |
96
|
|
|
{ |
97
|
|
|
$basePaths = []; |
98
|
|
|
foreach ($tenants as $tenant) { |
99
|
|
|
$subdomain = $tenant['subdomain']; |
100
|
|
|
$site = $this->dm->find($this->siteClass, $this->pathBuilder->build('/', $subdomain)); |
101
|
|
|
if (!$site) { |
102
|
|
|
$site = new $this->siteClass(); |
103
|
|
|
if (!$site instanceof SiteDocumentInterface) { |
104
|
|
|
throw new UnexpectedTypeException($site, 'SWP\Component\MultiTenancy\Model\SiteDocumentInterface'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$site->setId((string) $this->pathBuilder->build('/', $subdomain)); |
108
|
|
|
$this->dm->persist($site); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach ($this->paths as $path) { |
112
|
|
|
$basePaths[] = $this->pathBuilder->build($path, $subdomain); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->dm->flush(); |
117
|
|
|
|
118
|
|
|
if (count($basePaths) > 0) { |
119
|
|
|
$this->createBasePaths($session, $basePaths, $tenants); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function createBasePaths(SessionInterface $session, array $basePaths, array $tenants) |
124
|
|
|
{ |
125
|
|
|
$route = null; |
126
|
|
|
$home = 'homepage'; |
127
|
|
|
foreach ($basePaths as $path) { |
128
|
|
|
NodeHelper::createPath($session, $path); |
129
|
|
|
$homepage = $this->dm->find(null, $path.'/'.$home); |
130
|
|
|
if (null === $homepage) { |
131
|
|
|
$route = new $this->documentClass(); |
132
|
|
|
$route->setParentDocument($this->dm->find(null, $path)); |
133
|
|
|
$route->setName($home); |
134
|
|
|
$this->dm->persist($route); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$session->save(); |
139
|
|
|
foreach ($tenants as $tenant) { |
140
|
|
|
$site = $this->dm->find($this->siteClass, $this->pathBuilder->build('/', $tenant['subdomain'])); |
141
|
|
|
if (null !== $site && null === $site->getHomepage()) { |
142
|
|
|
$site->setHomepage($route); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
|
|
public function getName() |
151
|
|
|
{ |
152
|
|
|
return 'Multi-tenancy base paths'; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: