-
Notifications
You must be signed in to change notification settings - Fork 189
/
GeneratePagePartCommand.php
193 lines (164 loc) · 6.71 KB
/
GeneratePagePartCommand.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
<?php
namespace Kunstmaan\GeneratorBundle\Command;
use Kunstmaan\GeneratorBundle\Generator\PagePartGenerator;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
/**
* Generates a new pagepart
*/
class GeneratePagePartCommand extends KunstmaanGenerateCommand
{
/**
* @var BundleInterface
*/
private $bundle;
/**
* @var string
*/
private $prefix;
/**
* @var string
*/
private $pagepartName;
/**
* @var array
*/
private $fields;
/**
* @var array
*/
private $sections = [];
/**
* @var bool
*/
private $behatTest;
/**
* @see Command
*/
protected function configure()
{
$this->setDescription('Generates a new pagepart')
->setHelp(<<<'EOT'
The <info>kuma:generate:pagepart</info> command generates a new pagepart and the pagepart configuration.
<info>php bin/console kuma:generate:pagepart</info>
EOT
)
->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table name of the generated entity')
->setName('kuma:generate:pagepart');
}
/**
* {@inheritdoc}
*/
protected function getWelcomeText()
{
return 'Welcome to the Kunstmaan pagepart generator';
}
/**
* {@inheritdoc}
*/
protected function doExecute()
{
$this->assistant->writeSection('PagePart generation');
$this->createGenerator()->generate($this->bundle, $this->pagepartName, $this->prefix, $this->fields, $this->sections, $this->behatTest);
$this->assistant->writeSection('PagePart successfully created', 'bg=green;fg=black');
$this->assistant->writeLine([
'Make sure you update your database first before you test the pagepart:',
' Directly update your database: <comment>bin/console doctrine:schema:update --force</comment>',
' Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>',
($this->behatTest ? 'A new behat test is created, to run it: <comment>bin/behat --tags \'@' . $this->pagepartName . '\' @' . $this->bundle->getName() . '</comment>' : ''),
]);
return 0;
}
/**
* {@inheritdoc}
*/
protected function doInteract()
{
if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) {
$this->assistant->writeError('KunstmaanPagePartBundle not found', true);
}
$this->assistant->writeLine(["This command helps you to generate a new pagepart.\n"]);
/*
* Ask for which bundle we need to create the pagepart
*/
$this->bundle = $this->askForBundleName('pagepart');
/*
* Ask the database table prefix
*/
$this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
/*
* Ask the name of the pagepart
*/
$this->assistant->writeLine([
'',
'The name of your PagePart: For example: <comment>ContentBoxPagePart</comment>',
'',
]);
$generator = $this->getGenerator();
$bundlePath = $this->bundle->getPath();
$name = $this->assistant->askAndValidate(
'PagePart name',
function ($name) use ($generator, $bundlePath) {
// Check reserved words
if ($generator->isReservedKeyword($name)) {
throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name));
}
// Name should end on PagePart
if (!preg_match('/PagePart$/', $name)) {
throw new \InvalidArgumentException('The pagepart name must end with PagePart');
}
// Name should contain more characters than PagePart
if (strlen($name) <= strlen('PagePart') || !preg_match('/^[a-zA-Z]+$/', $name)) {
throw new \InvalidArgumentException('Invalid pagepart name');
}
// Check that entity does not already exist
if (file_exists($bundlePath . '/Entity/PageParts/' . $name . '.php')) {
throw new \InvalidArgumentException(sprintf('PagePart or entity "%s" already exists', $name));
}
return $name;
}
);
$this->pagepartName = $name;
/*
* Ask which fields need to be present
*/
$this->assistant->writeLine(["\nInstead of starting with a blank pagepart, you can add some fields now.\n"]);
$fields = $this->askEntityFields($this->bundle);
$this->fields = [];
foreach ($fields as $fieldInfo) {
if ($fieldInfo['type'] == 'image') {
$this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'],
$fieldInfo['extra'], true, $fieldInfo['minHeight'], $fieldInfo['maxHeight'], $fieldInfo['minWidth'], $fieldInfo['maxWidth'], $fieldInfo['mimeTypes']);
} elseif ($fieldInfo['type'] == 'media') {
$this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'],
$fieldInfo['extra'], true, null, null, null, null, $fieldInfo['mimeTypes']);
} else {
$this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], $fieldInfo['extra'], true);
}
}
/**
* Ask for which page sections we should enable this pagepart
*/
$question = 'In which page section configuration file(s) do you want to add the pagepart (multiple possible, separated by comma)';
$this->sections = $this->askForSections($question, $this->bundle, true);
/*
* Ask that you want to create behat tests for the new pagepart, if possible
*/
if (count($this->sections) > 0 && $this->canGenerateBehatTests($this->bundle)) {
$this->behatTest = $this->assistant->askConfirmation('Do you want to generate behat tests for this pagepart? (y/n)', 'y');
} else {
$this->behatTest = false;
}
}
/**
* Get the generator.
*
* @return PagePartGenerator
*/
protected function createGenerator()
{
$filesystem = $this->getContainer()->get('filesystem');
$registry = $this->getContainer()->get('doctrine');
return new PagePartGenerator($filesystem, $registry, '/pagepart', $this->assistant, $this->getContainer());
}
}