-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bootstrap.php
165 lines (137 loc) · 4.51 KB
/
Bootstrap.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
<?php
/**
* (c) shopware AG <info@shopware.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
class Shopware_Plugins_Frontend_SwagDemoDataEN_Bootstrap extends Shopware_Components_Plugin_Bootstrap
{
public function getCapabilities()
{
return [
'install' => true,
'update' => true,
'enable' => true,
'secureUninstall' => false
];
}
public function install()
{
if (!$this->assertMinimumVersion('5.4.0')) {
return false;
}
try {
if ($this->isInstallAllowed()) {
$this->importMedia();
$this->importDatabase();
}
} catch (Exception $e) {
return ['success' => false, 'message' => $e->getMessage()];
}
return true;
}
private function isInstallAllowed(): bool
{
if (PHP_SAPI === 'cli') {
return true;
}
$session = $this->get('backendsession');
if ($session->offsetExists('updateException')) {
$session->offsetUnset('updateException');
return true;
}
$session->offsetSet('updateException', true);
throw new RuntimeException('All your data will be lost after the installation. Please start the installation again to confirm these changes.');
}
/**
* Returns a marketing friendly name of the plugin.
*
* @return string
*/
public function getLabel()
{
return 'Shopware 5 Demo Data EN';
}
/**
* Returns the version of the plugin
*
* @return string|void
* @throws Exception
*/
public function getVersion()
{
$info = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'plugin.json'), true);
if ($info) {
return $info['currentVersion'];
}
throw new Exception('The plugin has an invalid version file.');
}
private function importMedia(): void
{
$rootDir = Shopware()->Container()->getParameter('kernel.root_dir');
if (Shopware()->Container()->hasParameter('shopware.app.rootdir')) {
$rootDir = Shopware()->Container()->getParameter('shopware.app.rootdir');
}
if (Shopware()->Container()->hasParameter('shopware.app.rootDir')) {
$rootDir = Shopware()->Container()->getParameter('shopware.app.rootDir');
}
$source = __DIR__ . '/resources/media';
$destination = $rootDir . '/media';
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $item) {
if ($item->isDir()) {
mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
}
/**
* Imports demo data scripts into the database
*
* @return void
* @throws \Exception
*/
private function importDatabase(): void
{
/** @var $connection \Doctrine\DBAL\Connection */
$connection = $this->get('dbal_connection');
$dataPaths = $this->getDataFilesList();
$connection->executeUpdate('SET FOREIGN_KEY_CHECKS = 0;');
try {
foreach ($dataPaths as $dataFile) {
$sql = file_get_contents($dataFile);
$connection->executeUpdate($sql);
}
} catch (\Exception $exception) {
}
$connection->executeUpdate('SET FOREIGN_KEY_CHECKS = 1;');
if ($exception) {
throw $exception;
}
}
/**
* Returns an ordered list of data files, in the order in which they should be imported
*
* @return array
*/
private function getDataFilesList(): array
{
$regexPattern = '/^([0-9]*)-.+\.sql/i';
$dataPath = __DIR__ . '/resources/data/';
$directoryIterator = new \DirectoryIterator($dataPath);
$regex = new \RegexIterator($directoryIterator, $regexPattern, \RecursiveRegexIterator::GET_MATCH);
$dataPaths = [];
foreach ($regex as $result) {
$dataPriority = intval($result['1']);
$dataPaths[$dataPriority] = $dataPath . $result['0'];
}
ksort($dataPaths);
return $dataPaths;
}
}