forked from hechoendrupal/drupal-console
/
EntityCommand.php
178 lines (159 loc) · 5.13 KB
/
EntityCommand.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
<?php
/**
* @file
* Contains \Drupal\Console\Command\Generate\EntityCommand.
*/
namespace Drupal\Console\Command\Generate;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Command\ModuleTrait;
use Drupal\Console\Command\GeneratorCommand;
use Drupal\Console\Style\DrupalStyle;
abstract class EntityCommand extends GeneratorCommand
{
use ModuleTrait;
private $entityType;
private $commandName;
/**
* @param $entityType
*/
protected function setEntityType($entityType)
{
$this->entityType = $entityType;
}
/**
* @param $commandName
*/
protected function setCommandName($commandName)
{
$this->commandName = $commandName;
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$commandKey = str_replace(':', '.', $this->commandName);
$this
->setName($this->commandName)
->setDescription(
sprintf(
$this->trans('commands.'.$commandKey.'.description'),
$this->entityType
)
)
->setHelp(
sprintf(
$this->trans('commands.'.$commandKey.'.help'),
$this->commandName,
$this->entityType
)
)
->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
->addOption(
'entity-class',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.'.$commandKey.'.options.entity-class')
)
->addOption(
'entity-name',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.'.$commandKey.'.options.entity-name')
)
->addOption(
'base-path',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.' . $commandKey . '.options.base-path')
)
->addOption(
'label',
null,
InputOption::VALUE_REQUIRED,
$this->trans('commands.'.$commandKey.'.options.label')
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Operations defined in EntityConfigCommand and EntityContentCommand.
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$commandKey = str_replace(':', '.', $this->commandName);
$utils = $this->getStringHelper();
// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\Console\Command\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($io);
$input->setOption('module', $module);
}
// --entity-class option
$entityClass = $input->getOption('entity-class');
if (!$entityClass) {
$entityClass = $io->ask(
$this->trans('commands.'.$commandKey.'.questions.entity-class'),
'DefaultEntity',
function ($entityClass) {
return $this->validateSpaces($entityClass);
}
);
$input->setOption('entity-class', $entityClass);
}
// --entity-name option
$entityName = $input->getOption('entity-name');
if (!$entityName) {
$entityName = $io->ask(
$this->trans('commands.'.$commandKey.'.questions.entity-name'),
$utils->camelCaseToMachineName($entityClass),
function ($entityName) {
return $this->validateMachineName($entityName);
}
);
$input->setOption('entity-name', $entityName);
}
// --label option
$label = $input->getOption('label');
if (!$label) {
$label = $io->ask(
$this->trans('commands.'.$commandKey.'.questions.label'),
$utils->camelCaseToHuman($entityClass)
);
$input->setOption('label', $label);
}
// --base-path option
$base_path = $input->getOption('base-path');
if (!$base_path) {
$base_path = $this->getDefaultBasePath();
}
$base_path = $io->ask(
$this->trans('commands.'.$commandKey.'.questions.base_path'),
$base_path
);
if (substr($base_path, 0, 1) !== '/') {
// Base path must start with a leading '/'.
$base_path = '/' . $base_path;
}
$input->setOption('base-path', $base_path);
}
protected function createGenerator()
{
}
/**
* Gets default base path.
*
* @return string
* Default base path.
*/
protected function getDefaultBasePath()
{
return '/admin/structure';
}
}