1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\GeneratorBundle\Command; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\GeneratorBundle\Generator\PageGenerator; |
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
7
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Generates a new page |
11
|
|
|
*/ |
12
|
|
|
class GeneratePageCommand extends KunstmaanGenerateCommand |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var BundleInterface |
16
|
|
|
*/ |
17
|
|
|
private $bundle; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $prefix; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $pageName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $fields; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $template; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $sections = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $parentPages = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @see Command |
51
|
|
|
*/ |
52
|
|
|
protected function configure() |
53
|
|
|
{ |
54
|
|
|
$this->setDescription('Generates a new page') |
55
|
|
|
->setHelp(<<<'EOT' |
56
|
|
|
The <info>kuma:generate:page</info> command generates a new page and its configuration. |
57
|
|
|
|
58
|
|
|
<info>php bin/console kuma:generate:page</info> |
59
|
|
|
EOT |
60
|
|
|
) |
61
|
|
|
->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table name of the generated entity') |
62
|
|
|
->setName('kuma:generate:page'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
protected function getWelcomeText() |
69
|
|
|
{ |
70
|
|
|
return 'Welcome to the Kunstmaan page generator'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
protected function doExecute() |
77
|
|
|
{ |
78
|
|
|
$this->assistant->writeSection('Page generation'); |
79
|
|
|
|
80
|
|
|
$this->createGenerator()->generate($this->bundle, $this->pageName, $this->prefix, $this->fields, $this->template, $this->sections, $this->parentPages); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$this->assistant->writeSection('Page successfully created', 'bg=green;fg=black'); |
83
|
|
|
|
84
|
|
View Code Duplication |
if (count($this->parentPages) == 0) { |
|
|
|
|
85
|
|
|
$this->assistant->writeLine(array( |
86
|
|
|
'To use this page you must first add the definition below to the <comment>getPossibleChildTypes</comment> funtion of the parent page:', |
87
|
|
|
'<comment> [</comment>', |
88
|
|
|
"<comment> 'name' => '".$this->pageName."',</comment>", |
89
|
|
|
"<comment> 'class'=> '".$this->bundle->getNamespace().'\\Entity\\Pages\\'.$this->pageName."'</comment>", |
90
|
|
|
'<comment> ],</comment>', |
91
|
|
|
'', |
92
|
|
|
)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->assistant->writeLine(array( |
96
|
|
|
'Make sure you update your database first before you use the page:', |
97
|
|
|
' Directly update your database: <comment>bin/console doctrine:schema:update --force</comment>', |
98
|
|
|
' Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>', |
99
|
|
|
'', |
100
|
|
|
)); |
101
|
|
|
|
102
|
|
|
return 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
protected function doInteract() |
109
|
|
|
{ |
110
|
|
|
if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) { |
111
|
|
|
$this->assistant->writeError('KunstmaanPagePartBundle not found', true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->assistant->writeLine(array("This command helps you to generate a new page.\n")); |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Ask for which bundle we need to create the pagepart |
118
|
|
|
*/ |
119
|
|
|
$this->bundle = $this->askForBundleName('page'); |
120
|
|
|
|
121
|
|
|
/* |
122
|
|
|
* Ask the database table prefix |
123
|
|
|
*/ |
124
|
|
|
$this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace()); |
125
|
|
|
|
126
|
|
|
/* |
127
|
|
|
* Ask the name of the pagepart |
128
|
|
|
*/ |
129
|
|
|
$this->assistant->writeLine(array( |
130
|
|
|
'', |
131
|
|
|
'The name of your Page: For example: <comment>SponsorPage</comment>, <comment>NewsOverviewPage</comment>', |
132
|
|
|
'', |
133
|
|
|
)); |
134
|
|
|
$generator = $this->getGenerator(); |
135
|
|
|
$bundlePath = $this->bundle->getPath(); |
136
|
|
|
|
137
|
|
|
$name = $this->assistant->askAndValidate( |
138
|
|
|
'Page name', |
139
|
|
View Code Duplication |
function ($name) use ($generator, $bundlePath) { |
|
|
|
|
140
|
|
|
// Check reserved words |
141
|
|
|
if ($generator->isReservedKeyword($name)) { |
142
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Name should end on Page |
146
|
|
|
if (!preg_match('/Page$/', $name)) { |
147
|
|
|
throw new \InvalidArgumentException('The page name must end with Page'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Name should contain more characters than Page |
151
|
|
|
if (strlen($name) <= strlen('Page') || !preg_match('/^[a-zA-Z]+$/', $name)) { |
152
|
|
|
throw new \InvalidArgumentException('Invalid page name'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Check that entity does not already exist |
156
|
|
|
if (file_exists($bundlePath . '/Entity/Pages/' . $name . '.php')) { |
157
|
|
|
throw new \InvalidArgumentException(sprintf('Page or entity "%s" already exists', $name)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $name; |
161
|
|
|
} |
162
|
|
|
); |
163
|
|
|
$this->pageName = $name; |
164
|
|
|
|
165
|
|
|
/* |
166
|
|
|
* Ask which fields need to be present |
167
|
|
|
*/ |
168
|
|
|
$this->assistant->writeLine(array("\nInstead of starting with a blank page, you can add some fields now.\n")); |
169
|
|
|
$fields = $this->askEntityFields($this->bundle, array('title', 'pageTitle', 'parent', 'id')); |
170
|
|
|
$this->fields = array(); |
171
|
|
|
foreach ($fields as $fieldInfo) { |
172
|
|
|
$this->fields[] = $this->getEntityFields( |
173
|
|
|
$this->bundle, |
174
|
|
|
$this->pageName, |
175
|
|
|
$this->prefix, |
176
|
|
|
$fieldInfo['name'], |
177
|
|
|
$fieldInfo['type'], |
178
|
|
|
$fieldInfo['extra'], |
179
|
|
|
true, |
180
|
|
|
$fieldInfo['minHeight'], |
181
|
|
|
$fieldInfo['maxHeight'], |
182
|
|
|
$fieldInfo['minWidth'], |
183
|
|
|
$fieldInfo['maxWidth'], |
184
|
|
|
$fieldInfo['mimeTypes'] |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Ask which default page template we need to use |
190
|
|
|
*/ |
191
|
|
|
$templateSelect = $this->getTemplateList(); |
192
|
|
|
if (empty($templateSelect)) { |
193
|
|
|
throw new \RuntimeException('You need to define at least one page template before running the page generator!'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$this->assistant->writeLine(''); |
197
|
|
|
$templateId = $this->assistant->askSelect('Which page template do you want to use', $templateSelect); |
198
|
|
|
$templateConfigs = $this->getAvailableTemplates($this->bundle); |
199
|
|
|
$templateConfig = $templateConfigs[$templateId]; |
200
|
|
|
$this->template = $templateConfig['file']; |
201
|
|
|
|
202
|
|
|
/* |
203
|
|
|
* Ask for which sections pagepart configuration the end user wants to use for the different sections |
204
|
|
|
*/ |
205
|
|
|
$this->assistant->writeLine(array("\nThe select page template consists of these contexts: " . implode(', ', $templateConfig['contexts']))); |
206
|
|
|
$this->sections = array(); |
207
|
|
|
foreach ($templateConfig['contexts'] as $context) { |
208
|
|
|
$question = "Which pagepart configuration would you like to use for the '$context' context"; |
209
|
|
|
$section = $this->askForSections($question, $this->bundle, false, $context); |
210
|
|
|
if (is_null($section)) { |
211
|
|
|
$this->assistant->writeError(sprintf('No section pagepart configuration found for context "%s"', $context), true); |
212
|
|
|
} |
213
|
|
|
$this->sections[] = $section; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Ask the parent pages |
218
|
|
|
*/ |
219
|
|
|
$parentPages = $this->getAvailablePages($this->bundle); |
220
|
|
|
$pagesSelect = array_map(function ($item) { |
221
|
|
|
return $item['name']; |
222
|
|
|
}, $parentPages); |
223
|
|
View Code Duplication |
if (count($pagesSelect) > 0) { |
|
|
|
|
224
|
|
|
$this->assistant->writeLine(''); |
225
|
|
|
$parentPageIds = $this->assistant->askSelect('Which existing page(s) can have the new page as sub-page (multiple possible, separated by comma)', $pagesSelect, null, true); |
226
|
|
|
foreach ($parentPageIds as $id) { |
|
|
|
|
227
|
|
|
$this->parentPages[] = $parentPages[$id]['path']; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get the generator. |
234
|
|
|
* |
235
|
|
|
* @return PageGenerator |
236
|
|
|
*/ |
237
|
|
View Code Duplication |
protected function createGenerator() |
|
|
|
|
238
|
|
|
{ |
239
|
|
|
$filesystem = $this->getContainer()->get('filesystem'); |
240
|
|
|
$registry = $this->getContainer()->get('doctrine'); |
241
|
|
|
|
242
|
|
|
return new PageGenerator($filesystem, $registry, '/page', $this->assistant, $this->getContainer()); |
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get all the available default templates. |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
private function getTemplateList() |
251
|
|
|
{ |
252
|
|
|
$templates = $this->getAvailableTemplates($this->bundle); |
253
|
|
|
|
254
|
|
|
$types = array(); |
255
|
|
|
foreach ($templates as $key => $template) { |
256
|
|
|
$types[$key] = $template['name']; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $types; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: