forked from pimcore/pimcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassesRebuildCommand.php
164 lines (144 loc) · 5.68 KB
/
ClassesRebuildCommand.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
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* 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 PCL
*/
namespace Pimcore\Bundle\CoreBundle\Command;
use Pimcore\Console\AbstractCommand;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\ClassDefinition\ClassDefinitionManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Contracts\Service\Attribute\Required;
/**
* @internal
*/
#[AsCommand(
name: 'pimcore:deployment:classes-rebuild',
description: 'rebuilds db structure for classes, field collections and object bricks
based on updated var/classes/definition_*.php files',
aliases: ['deployment:classes-rebuild']
)]
class ClassesRebuildCommand extends AbstractCommand
{
protected ClassDefinitionManager $classDefinitionManager;
protected function configure(): void
{
$this
->addOption(
'create-classes',
'c',
InputOption::VALUE_NONE,
'Create missing Classes (Classes that exists in var/classes but not in the database)'
)
->addOption(
'delete-classes',
'd',
InputOption::VALUE_NONE,
'Delete missing Classes (Classes that don\'t exists in var/classes anymore but in the database)'
);
}
#[Required]
public function setClassDefinitionManager(ClassDefinitionManager $classDefinitionManager): void
{
$this->classDefinitionManager = $classDefinitionManager;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('delete-classes')) {
$questionResult = true;
if ($input->isInteractive()) {
$questionResult = $this->io->confirm(
'<error>You are going to delete classes that don\'t have class-definitions anymore. This could lead to data loss! Do you want to continue?</error>',
false
);
}
if ($questionResult) {
if ($output->isVerbose()) {
$output->writeln('---------------------');
$output->writeln('Delete Classes that don\'t have class-definitions anymore.');
}
foreach ($this->classDefinitionManager->cleanUpDeletedClassDefinitions() as $deleted) {
if ($output->isVerbose()) {
[$class, $id, $action] = $deleted;
$output->writeln(sprintf('%s [%s] %s', $class, $id, $action));
}
}
}
}
if ($output->isVerbose()) {
$output->writeln('---------------------');
$output->writeln('Saving all classes');
}
if ($input->getOption('create-classes')) {
foreach ($this->classDefinitionManager->createOrUpdateClassDefinitions() as $changes) {
if ($output->isVerbose()) {
[$class, $id, $action] = $changes;
$output->writeln(sprintf('%s [%s] %s', $class, $id, $action));
}
}
} else {
$list = new ClassDefinition\Listing();
foreach ($list->getData() as $class) {
if ($class instanceof DataObject\ClassDefinitionInterface) {
if ($output->isVerbose()) {
$output->writeln(sprintf('%s [%s] saved', $class->getName(), $class->getId()));
}
$class->save(false);
}
}
}
if ($output->isVerbose()) {
$output->writeln('---------------------');
$output->writeln('Saving all object bricks');
}
$list = new DataObject\Objectbrick\Definition\Listing();
$list = $list->load();
foreach ($list as $brickDefinition) {
if ($output->isVerbose()) {
$output->writeln(sprintf('%s saved', $brickDefinition->getKey()));
}
try {
$brickDefinition->save(false);
} catch (\Exception $e) {
$output->write((string)$e);
}
}
if ($output->isVerbose()) {
$output->writeln('---------------------');
$output->writeln('Saving all field collections');
}
$list = new DataObject\Fieldcollection\Definition\Listing();
$list = $list->load();
foreach ($list as $fc) {
if ($output->isVerbose()) {
$output->writeln(sprintf('%s saved', $fc->getKey()));
}
$fc->save(false);
}
if ($output->isVerbose()) {
$output->writeln('---------------------');
$output->writeln('Saving all select options');
}
$selectOptionConfigurations = new DataObject\SelectOptions\Config\Listing();
foreach ($selectOptionConfigurations as $selectOptionConfiguration) {
if ($output->isVerbose()) {
$output->writeln(sprintf('%s saved', $selectOptionConfiguration->getId()));
}
$selectOptionConfiguration->generateEnumFiles();
}
return 0;
}
}