-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
SiteImportService.php
253 lines (219 loc) · 8.21 KB
/
SiteImportService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Neos\Neos\Domain\Service;
/*
* This file is part of the Neos.Neos package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Flow\Package\Exception\InvalidPackageStateException;
use Neos\Flow\Package\Exception\UnknownPackageException;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\Reflection\ReflectionService;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\ImageVariant;
use Neos\Neos\Domain\Model\Site;
use Neos\Neos\Domain\Repository\SiteRepository;
use Neos\Neos\EventLog\Domain\Service\EventEmittingService;
use Neos\Neos\Exception as NeosException;
use Neos\ContentRepository\Domain\Model\Workspace;
use Neos\ContentRepository\Domain\Repository\WorkspaceRepository;
use Neos\ContentRepository\Domain\Service\ContextFactoryInterface;
use Neos\ContentRepository\Domain\Service\ImportExport\NodeImportService;
use Neos\ContentRepository\Domain\Utility\NodePaths;
/**
* The Site Import Service
*
* @Flow\Scope("singleton")
* @api
*/
class SiteImportService
{
/**
* @Flow\Inject
* @var PackageManager
*/
protected $packageManager;
/**
* @Flow\Inject
* @var SiteRepository
*/
protected $siteRepository;
/**
* @Flow\Inject
* @var ContextFactoryInterface
*/
protected $contextFactory;
/**
* @Flow\Inject
* @var NodeImportService
*/
protected $nodeImportService;
/**
* @Flow\Inject
* @var WorkspaceRepository
*/
protected $workspaceRepository;
/**
* @Flow\Inject
* @var ReflectionService
*/
protected $reflectionService;
/**
* @Flow\Inject
* @var ObjectManagerInterface
*/
protected $objectManager;
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* @Flow\Inject
* @var EventEmittingService
*/
protected $eventEmittingService;
/**
* @var string
*/
protected $resourcesPath = null;
/**
* An array that contains all fully qualified class names that extend ImageVariant including ImageVariant itself
*
* @var array<string>
*/
protected $imageVariantClassNames = [];
/**
* An array that contains all fully qualified class names that implement AssetInterface
*
* @var array<string>
*/
protected $assetClassNames = [];
/**
* An array that contains all fully qualified class names that extend \DateTime including \DateTime itself
*
* @var array<string>
*/
protected $dateTimeClassNames = [];
/**
* @return void
*/
public function initializeObject()
{
$this->imageVariantClassNames = $this->reflectionService->getAllSubClassNamesForClass(ImageVariant::class);
array_unshift($this->imageVariantClassNames, ImageVariant::class);
$this->assetClassNames = $this->reflectionService->getAllImplementationClassNamesForInterface(AssetInterface::class);
$this->dateTimeClassNames = $this->reflectionService->getAllSubClassNamesForClass('DateTime');
array_unshift($this->dateTimeClassNames, 'DateTime');
}
/**
* Checks for the presence of Sites.xml in the given package and imports it if found.
*
* @param string $packageKey
* @return Site the imported site
* @throws NeosException
*/
public function importFromPackage($packageKey)
{
if (!$this->packageManager->isPackageAvailable($packageKey)) {
throw new NeosException(sprintf('Error: Package "%s" is not active.', $packageKey), 1384192950);
}
$contentPathAndFilename = sprintf('resource://%s/Private/Content/Sites.xml', $packageKey);
if (!file_exists($contentPathAndFilename)) {
throw new NeosException(sprintf('Error: No content found in package "%s".', $packageKey), 1384192955);
}
try {
return $this->importFromFile($contentPathAndFilename);
} catch (\Exception $exception) {
throw new NeosException(sprintf('Error: During import an exception occurred: "%s".', $exception->getMessage()), 1300360480, $exception);
}
}
/**
* Imports one or multiple sites from the XML file at $pathAndFilename
*
* @param string $pathAndFilename
* @return Site The imported site
* @throws UnknownPackageException|InvalidPackageStateException|NeosException
*/
public function importFromFile($pathAndFilename)
{
if (!file_exists($pathAndFilename)) {
throw new NeosException(sprintf('Error: File "%s" does not exist.', $pathAndFilename), 1540934412);
}
/** @var Site $importedSite */
$site = null;
$xmlReader = new \XMLReader();
if ($xmlReader->open($pathAndFilename, null, LIBXML_PARSEHUGE) === false) {
throw new NeosException(sprintf('Error: XMLReader could not open "%s".', $pathAndFilename), 1540934199);
}
if ($this->workspaceRepository->findOneByName('live') === null) {
$this->workspaceRepository->add(new Workspace('live'));
$this->persistenceManager->persistAll();
}
while ($xmlReader->read()) {
if ($xmlReader->nodeType != \XMLReader::ELEMENT || $xmlReader->name !== 'site') {
continue;
}
$site = $this->getSiteByNodeName($xmlReader->getAttribute('siteNodeName'));
$site->setName($xmlReader->getAttribute('name'));
$site->setState((integer)$xmlReader->getAttribute('state'));
$siteResourcesPackageKey = $xmlReader->getAttribute('siteResourcesPackageKey');
if (!$this->packageManager->isPackageAvailable($siteResourcesPackageKey)) {
throw new UnknownPackageException(sprintf('Package "%s" specified in the XML as site resources package does not exist.', $siteResourcesPackageKey), 1303891443);
}
if (!$this->packageManager->isPackageAvailable($siteResourcesPackageKey)) {
throw new InvalidPackageStateException(sprintf('Package "%s" specified in the XML as site resources package is not active.', $siteResourcesPackageKey), 1303898135);
}
$site->setSiteResourcesPackageKey($siteResourcesPackageKey);
$rootNode = $this->contextFactory->create()->getRootNode();
// We fetch the workspace to be sure it's known to the persistence manager and persist all
// so the workspace and site node are persisted before we import any nodes to it.
$rootNode->getContext()->getWorkspace();
$this->persistenceManager->persistAll();
$sitesNode = $rootNode->getNode(SiteService::SITES_ROOT_PATH);
if ($sitesNode === null) {
$sitesNode = $rootNode->createNode(NodePaths::getNodeNameFromPath(SiteService::SITES_ROOT_PATH));
}
$this->nodeImportService->import($xmlReader, $sitesNode->getPath(), dirname($pathAndFilename) . '/Resources');
}
if ($site === null) {
throw new NeosException(sprintf('The XML file did not contain a valid site node.'), 1418999522);
}
$this->emitSiteImported($site);
return $site;
}
/**
* Updates or creates a site with the given $siteNodeName
*
* @param string $siteNodeName
* @return Site
*/
protected function getSiteByNodeName($siteNodeName)
{
$site = $this->siteRepository->findOneByNodeName($siteNodeName);
if ($site === null) {
$site = new Site($siteNodeName);
$this->siteRepository->add($site);
} else {
$this->siteRepository->update($site);
}
return $site;
}
/**
* Signal that is triggered when a site has been imported successfully
*
* @Flow\Signal
* @param Site $site The site that has been imported
* @return void
*/
protected function emitSiteImported(Site $site)
{
}
}