-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatcher.php
More file actions
142 lines (116 loc) · 4.54 KB
/
Patcher.php
File metadata and controls
142 lines (116 loc) · 4.54 KB
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
<?php
declare(strict_types=1);
/*
* CoreShop
*
* This source file is available under the terms of the
* CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.com)
* @license CoreShop Commercial License (CCL)
*
*/
namespace CoreShop\Bundle\ClassDefinitionPatchBundle;
use CoreShop\Component\Pimcore\DataObject\ClassUpdate;
class Patcher implements PatcherInterface
{
public function __construct(
protected Patches $patches,
) {
}
public function getPatches(): array
{
return $this->patches->getPatches();
}
public function patch(): void
{
foreach ($this->patches->getPatches() as $patch) {
$this->patchClass($patch);
}
}
public function old(PatchInterface $patch): array
{
return $this->patchWithClassUpdate($patch)->getOriginalJsonDefinition();
}
public function new(PatchInterface $patch): array
{
return $this->patchWithClassUpdate($patch)->getJsonDefinition();
}
public function patchClass(Patch $patch): void
{
$this->patchWithClassUpdate($patch)->save();
}
protected function patchWithClassUpdate(PatchInterface $patch): ClassUpdate
{
$classUpdater = new ClassUpdate($patch->getClassName());
if (null !== $patch->getParentClass()) {
$classUpdater->setProperty('parentClass', $patch->getParentClass());
}
if (null !== $patch->getGroup()) {
$classUpdater->setProperty('group', $patch->getGroup());
}
if (null !== $patch->getDescription()) {
$classUpdater->setProperty('description', $patch->getDescription());
}
if (null !== $patch->getListingParentClass()) {
$classUpdater->setProperty('listingParentClass', $patch->getListingUseTraits());
}
if (null !== $patch->getInterface()) {
$interfaces = $classUpdater->getProperty('implementsInterfaces');
foreach ($patch->getInterface() as $patchInterface) {
if (null === $interfaces || !str_contains($interfaces, $patchInterface)) {
if (!$interfaces) {
$interfaces = $patchInterface;
} else {
$interfaces .= sprintf(',%s', $patchInterface);
}
}
}
$classUpdater->setProperty('implementsInterfaces', $interfaces);
}
if (null !== $patch->getUseTraits()) {
$traits = $classUpdater->getProperty('useTraits');
foreach ($patch->getUseTraits() as $patchTrait) {
if (null === $traits || !str_contains($traits, $patchTrait)) {
if (!$traits) {
$traits = $patchTrait;
} else {
$traits .= sprintf(',%s', $patchTrait);
}
}
}
$classUpdater->setProperty('useTraits', $traits);
}
if (null !== $patch->getListingParentClass()) {
$traits = $classUpdater->getProperty('listingUseTraits');
foreach ($patch->getListingUseTraits() as $patchListingUseTrait) {
if (null === $traits || !str_contains($traits, $patchListingUseTrait)) {
if (!$traits) {
$traits = $patchListingUseTrait;
} else {
$traits .= sprintf(',%s', $patchListingUseTrait);
}
}
}
$classUpdater->setProperty('listingUseTraits', $traits);
}
foreach ($patch->getFields() as $field) {
if ($classUpdater->hasField($field->getFieldName())) {
if ($field->isReplace()) {
$classUpdater->replaceField($field->getFieldName(), $field->getDefinition());
} else {
$classUpdater->replaceFieldProperties($field->getFieldName(), $field->getDefinition());
}
} elseif ($field->getBefore()) {
$classUpdater->insertFieldBefore($field->getBefore(), $field->getDefinition());
} elseif ($field->getAfter()) {
$classUpdater->insertFieldAfter($field->getAfter(), $field->getDefinition());
} else {
$classUpdater->insertField($field->getDefinition());
}
}
return $classUpdater;
}
}