-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Installer.php
447 lines (390 loc) · 14.3 KB
/
Installer.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace Pimcore\Bundle\EcommerceFrameworkBundle\Tools;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Pimcore\Db\ConnectionInterface;
use Pimcore\Extension\Bundle\Installer\AbstractInstaller;
use Pimcore\Extension\Bundle\Installer\Exception\InstallationException;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\Service;
use Pimcore\Model\DataObject\Fieldcollection;
use Pimcore\Model\DataObject\Objectbrick;
use Pimcore\Model\Translation\Admin;
use Pimcore\Model\User\Permission;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
class Installer extends AbstractInstaller
{
/**
* @var string
*/
private $installSourcesPath;
/**
* @var array
*/
private $tablesToInstall = [
'ecommerceframework_cart' =>
'CREATE TABLE IF NOT EXISTS `ecommerceframework_cart` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`userid` int(20) NOT NULL,
`name` varchar(250) COLLATE utf8_bin DEFAULT NULL,
`creationDateTimestamp` int(10) NOT NULL,
`modificationDateTimestamp` int(10) NOT NULL,
PRIMARY KEY (`id`),
KEY `ecommerceframework_cart_userid_index` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;',
'ecommerceframework_cartcheckoutdata' =>
'CREATE TABLE IF NOT EXISTS `ecommerceframework_cartcheckoutdata` (
`cartId` int(20) NOT NULL,
`key` varchar(150) COLLATE utf8_bin NOT NULL,
`data` longtext,
PRIMARY KEY (`cartId`,`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;',
'ecommerceframework_cartitem' =>
"CREATE TABLE IF NOT EXISTS `ecommerceframework_cartitem` (
`productId` int(20) NOT NULL,
`cartId` int(20) NOT NULL,
`count` int(20) NOT NULL,
`itemKey` varchar(100) COLLATE utf8_bin NOT NULL,
`parentItemKey` varchar(100) COLLATE utf8_bin NOT NULL DEFAULT '0',
`comment` LONGTEXT ASCII,
`addedDateTimestamp` int(10) NOT NULL,
`sortIndex` INT(10) UNSIGNED NULL DEFAULT '0',
PRIMARY KEY (`itemKey`,`cartId`,`parentItemKey`),
KEY `cartId_parentItemKey` (`cartId`,`parentItemKey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;",
'ecommerceframework_vouchertoolkit_statistics' =>
"CREATE TABLE IF NOT EXISTS `ecommerceframework_vouchertoolkit_statistics` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`voucherSeriesId` BIGINT(20) NOT NULL,
`date` DATE NOT NULL,
PRIMARY KEY (`id`)
) COLLATE='utf8_general_ci' ENGINE=InnoDB;",
'ecommerceframework_vouchertoolkit_tokens' =>
"CREATE TABLE IF NOT EXISTS `ecommerceframework_vouchertoolkit_tokens` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`voucherSeriesId` BIGINT(20) NULL DEFAULT NULL,
`token` VARCHAR(250) NULL DEFAULT NULL COLLATE 'latin1_bin',
`length` INT(11) NULL DEFAULT NULL,
`type` VARCHAR(50) NULL DEFAULT NULL,
`usages` BIGINT(20) NULL DEFAULT '0',
`timestamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `token` (`token`)
) COLLATE='utf8_general_ci' ENGINE=InnoDB;",
'ecommerceframework_vouchertoolkit_reservations' =>
"CREATE TABLE IF NOT EXISTS `ecommerceframework_vouchertoolkit_reservations` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`token` VARCHAR(250) NOT NULL,
`cart_id` VARCHAR(250) NOT NULL,
`timestamp` DATETIME NOT NULL,
PRIMARY KEY (`id`),
INDEX `token` (`token`)
) COLLATE='utf8_general_ci' ENGINE=InnoDB;",
'ecommerceframework_pricing_rule' =>
"CREATE TABLE IF NOT EXISTS `ecommerceframework_pricing_rule` (
`id` INT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT NULL,
`label` TEXT NULL,
`description` TEXT NULL,
`behavior` ENUM('additiv','stopExecute') NULL DEFAULT NULL,
`active` TINYINT(1) UNSIGNED NULL DEFAULT NULL,
`prio` TINYINT(3) UNSIGNED NOT NULL,
`condition` TEXT NOT NULL COMMENT 'configuration der condition',
`actions` TEXT NOT NULL COMMENT 'configuration der action',
PRIMARY KEY (`id`),
UNIQUE INDEX `name` (`name`),
INDEX `active` (`active`)
) ENGINE=InnoDB AUTO_INCREMENT=0;",
];
/**
* @var array
*/
private $classesToInstall = [
'FilterDefinition' => 'EF_FD',
'OfferToolCustomProduct' => 'EF_OTCP',
'OfferToolOffer' => 'EF_OTO',
'OfferToolOfferItem' => 'EF_OTOI',
'OnlineShopOrder' => 'EF_OSO',
'OnlineShopOrderItem' => 'EF_OSOI',
'OnlineShopTaxClass' => 'EF_OSTC',
'OnlineShopVoucherSeries' => 'EF_OSVS',
'OnlineShopVoucherToken' => 'EF_OSVT',
];
/**
* @var array
*/
private $permissionsToInstall = [
'bundle_ecommerce_pricing_rules',
'bundle_ecommerce_back-office_order',
];
/**
* @var BundleInterface
*/
protected $bundle;
/**
* @var ConnectionInterface
*/
protected $db;
/**
* @var Schema
*/
protected $schema;
public function __construct(
BundleInterface $bundle,
ConnectionInterface $connection
) {
$this->installSourcesPath = __DIR__ . '/../Resources/install';
$this->bundle = $bundle;
$this->db = $connection;
if ($this->db instanceof Connection) {
$this->schema = $this->db->getSchemaManager()->createSchema();
}
parent::__construct();
}
public function install()
{
$this->installFieldCollections();
$this->installClasses();
$this->installObjectBricks();
$this->installTables();
$this->installTranslations();
$this->installPermissions();
}
public function uninstall()
{
$this->uninstallPermissions();
$this->uninstallTables();
}
/**
* {@inheritdoc}
*/
public function isInstalled()
{
$installed = false;
try {
// check if if first permission is installed
$installed = $this->db->fetchOne('SELECT `key` FROM users_permission_definitions WHERE `key` = :key', [
'key' => $this->permissionsToInstall[0],
]);
} catch (\Exception $e) {
// nothing to do
}
return (bool) $installed;
}
/**
* {@inheritdoc}
*/
public function canBeInstalled()
{
return !$this->isInstalled();
}
/**
* {@inheritdoc}
*/
public function canBeUninstalled()
{
return $this->isInstalled();
}
private function getClassesToInstall(): array
{
$result = [];
foreach (array_keys($this->classesToInstall) as $className) {
$filename = sprintf('class_%s_export.json', $className);
$path = $this->installSourcesPath . '/class_sources/' . $filename;
$path = realpath($path);
if (false === $path || !is_file($path)) {
throw new InstallationException(sprintf(
'Class export for class "%s" was expected in "%s" but file does not exist',
$className,
$path
));
}
$result[$className] = $path;
}
return $result;
}
private function installClasses()
{
$classes = $this->getClassesToInstall();
$mapping = $this->classesToInstall;
foreach ($classes as $key => $path) {
$class = ClassDefinition::getByName($key);
if ($class) {
$this->output->write(sprintf(
' <comment>WARNING:</comment> Skipping class "%s" as it already exists',
$key
));
continue;
}
$class = new ClassDefinition();
$classId = $mapping[$key];
$class->setName($key);
$class->setId($classId);
$data = file_get_contents($path);
$success = Service::importClassDefinitionFromJson($class, $data, false, true);
if (!$success) {
throw new InstallationException(sprintf(
'Failed to create class "%s"',
$key
));
}
}
}
private function installFieldCollections()
{
$fieldCollections = $this->findInstallFiles(
$this->installSourcesPath . '/fieldcollection_sources',
'/^fieldcollection_(.*)_export\.json$/'
);
foreach ($fieldCollections as $key => $path) {
if ($fieldCollection = Fieldcollection\Definition::getByKey($key)) {
if ($fieldCollection) {
$this->output->write(sprintf(
' <comment>WARNING:</comment> Skipping field collection "%s" as it already exists',
$key
));
continue;
}
} else {
$fieldCollection = new Fieldcollection\Definition();
$fieldCollection->setKey($key);
}
$data = file_get_contents($path);
$success = Service::importFieldCollectionFromJson($fieldCollection, $data);
if (!$success) {
throw new InstallationException(sprintf(
'Failed to create field collection "%s"',
$key
));
}
}
}
private function installObjectBricks()
{
$bricks = $this->findInstallFiles(
$this->installSourcesPath . '/objectbrick_sources',
'/^objectbrick_(.*)_export\.json$/'
);
foreach ($bricks as $key => $path) {
if ($brick = Objectbrick\Definition::getByKey($key)) {
$this->output->write(sprintf(
' <comment>WARNING:</comment> Skipping object brick "%s" as it already exists',
$key
));
continue;
} else {
$brick = new Objectbrick\Definition();
$brick->setKey($key);
}
$data = file_get_contents($path);
$success = Service::importObjectBrickFromJson($brick, $data);
if (!$success) {
throw new InstallationException(sprintf(
'Failed to create object brick "%s"',
$key
));
}
}
}
private function installPermissions()
{
foreach ($this->permissionsToInstall as $permission) {
$definition = Permission\Definition::getByKey($permission);
if ($definition) {
$this->output->write(sprintf(
' <comment>WARNING:</comment> Skipping permission "%s" as it already exists',
$permission
));
continue;
}
try {
Permission\Definition::create($permission);
} catch (\Throwable $e) {
throw new InstallationException(sprintf(
'Failed to create permission "%s": %s',
$permission, $e->getMessage()
));
}
}
}
private function uninstallPermissions()
{
foreach ($this->permissionsToInstall as $permission) {
$this->db->executeQuery('DELETE FROM users_permission_definitions WHERE `key` = :key', [
'key' => $permission,
]);
}
}
private function installTables()
{
foreach ($this->tablesToInstall as $name => $statement) {
if ($this->schema->hasTable($name)) {
$this->output->write(sprintf(
' <comment>WARNING:</comment> Skipping table "%s" as it already exists',
$name
));
continue;
}
$this->db->executeQuery($statement);
}
}
private function uninstallTables()
{
foreach (array_keys($this->tablesToInstall) as $table) {
if (!$this->schema->hasTable($table)) {
$this->output->write(sprintf(
' <comment>WARNING:</comment> Not dropping table "%s" as it doesn\'t exist',
$table
));
continue;
}
$this->schema->dropTable($table);
}
}
private function installTranslations()
{
Admin::importTranslationsFromFile($this->installSourcesPath . '/admin-translations/init.csv');
}
/**
* Finds objectbrick/fieldcollection sources by path returns a result list
* indexed by element name.
*
* @param string $directory
* @param string $pattern
*
* @return array
*/
private function findInstallFiles(string $directory, string $pattern): array
{
$finder = new Finder();
$finder
->files()
->in($directory)
->name($pattern);
$results = [];
foreach ($finder as $file) {
if (preg_match($pattern, $file->getFilename(), $matches)) {
$key = $matches[1];
$results[$key] = $file->getRealPath();
}
}
return $results;
}
public function needsReloadAfterInstall()
{
return true;
}
}