-
-
Notifications
You must be signed in to change notification settings - Fork 547
Expand file tree
/
Copy pathEntityContentCommand.php
More file actions
189 lines (164 loc) · 5.61 KB
/
EntityContentCommand.php
File metadata and controls
189 lines (164 loc) · 5.61 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
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
<?php
/**
* @file
* Contains \Drupal\Console\Command\Generate\EntityContentCommand.
*/
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\Generator\EntityContentGenerator;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Utils\StringConverter;
use Drupal\Console\Core\Utils\ChainQueue;
use Drupal\Console\Utils\Validator;
use Drupal\Console\Core\Style\DrupalStyle;
class EntityContentCommand extends EntityCommand
{
/**
* @var ChainQueue
*/
protected $chainQueue;
/**
* @var EntityContentGenerator
*/
protected $generator;
/**
* @var StringConverter
*/
protected $stringConverter;
/**
* @var Manager
*/
protected $extensionManager;
/**
* @var Validator
*/
protected $validator;
/**
* EntityContentCommand constructor.
*
* @param ChainQueue $chainQueue
* @param EntityContentGenerator $generator
* @param StringConverter $stringConverter
* @param Manager $extensionManager
* @param Validator $validator
*/
public function __construct(
ChainQueue $chainQueue,
EntityContentGenerator $generator,
StringConverter $stringConverter,
Manager $extensionManager,
Validator $validator
) {
$this->chainQueue = $chainQueue;
$this->generator = $generator;
$this->stringConverter = $stringConverter;
$this->extensionManager = $extensionManager;
$this->validator = $validator;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setEntityType('EntityContent');
$this->setCommandName('generate:entity:content');
parent::configure();
$this->addOption(
'has-bundles',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.entity.content.options.has-bundles')
);
$this->addOption(
'is-translatable',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.entity.content.options.is-translatable')
);
$this->addOption(
'revisionable',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.entity.content.options.revisionable')
);
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
parent::interact($input, $output);
$io = new DrupalStyle($input, $output);
// --has-bundles option.
$this->interactBooleanQuestion($input, $io, 'has-bundles', false);
// --is-translatable option
$this->interactBooleanQuestion($input, $io, 'is-translatable', true);
// --revisionable option
$this->interactBooleanQuestion($input, $io, 'revisionable', true);
}
/**
* Helper to ask for boolean option.
*
* @param InputInterface $input
* @param DrupalStyle $io
* @param string $option
* @param bool $default
*/
protected function interactBooleanQuestion(InputInterface $input, DrupalStyle $io, $option, $default = true) {
// If no option flag has been set, we ask for manual input.
if (!$input->hasOption($option)) {
$set_value = $io->confirm(
$this->trans('commands.generate.entity.content.questions.' . $option),
(bool) $default
);
}
else {
$value = $input->getOption($option);
// When the value of the option is "0" or "false" the option is disabled.
if (isset($value) && $value === "0" || strtolower($value) === "false") {
$set_value = FALSE;
}
// ... otherwise the option is enabled.
else {
$set_value = TRUE;
}
}
$input->setOption($option, $set_value);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$module = $input->getOption('module');
$entity_class = $input->getOption('entity-class');
$entity_name = $input->getOption('entity-name');
$label = $input->getOption('label');
$has_bundles = $input->getOption('has-bundles');
$base_path = $input->getOption('base-path');
$learning = $input->hasOption('learning')?$input->getOption('learning'):false;
$bundle_entity_name = $has_bundles ? $entity_name . '_type' : null;
$is_translatable = $input->hasOption('is-translatable') ? $input->getOption('is-translatable') : true;
$revisionable = $input->hasOption('revisionable') ? $input->getOption('revisionable') : false;
$io = new DrupalStyle($input, $output);
$generator = $this->generator;
$generator->setIo($io);
//@TODO:
//$generator->setLearning($learning);
$generator->generate($module, $entity_name, $entity_class, $label, $base_path, $is_translatable, $bundle_entity_name, $revisionable);
if ($has_bundles) {
$this->chainQueue->addCommand(
'generate:entity:config', [
'--module' => $module,
'--entity-class' => $entity_class . 'Type',
'--entity-name' => $entity_name . '_type',
'--label' => $label . ' type',
'--bundle-of' => $entity_name
]
);
}
}
}