forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorEntityCommand.php
More file actions
135 lines (118 loc) · 4.11 KB
/
Copy pathGeneratorEntityCommand.php
File metadata and controls
135 lines (118 loc) · 4.11 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
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\GeneratorEntityCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Generator\EntityConfigGenerator;
use Drupal\AppConsole\Generator\EntityContentGenerator;
abstract class GeneratorEntityCommand 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()
{
$this
->setName($this->commandName)
->setDescription(sprintf(
$this->trans('commands.generate.entity.description'),
$this->entityType
))
->setHelp(sprintf(
$this->trans('commands.generate.entity.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.generate.entity.options.entity-class'))
->addOption('entity-name', null, InputOption::VALUE_REQUIRED,
$this->trans('commands.generate.entity.options.entity-name'));
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$entityType = $this->getStringUtils()->camelCaseToUnderscore($this->entityType);
$module = $input->getOption('module');
$entity_class = $input->getOption('entity-class');
$entity_name = $input->getOption('entity-name');
$this
->getGenerator()
->generate($module, $entity_name, $entity_class, $entityType);
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
$utils = $this->getStringUtils();
// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);
// --entity-class option
$entity_class = $input->getOption('entity-class');
if (!$entity_class) {
$entity_class = 'DefaultEntity';
$entity_class = $dialog->askAndValidate(
$output,
$dialog->getQuestion($this->trans('commands.generate.entity.questions.entity-class'), $entity_class),
function ($entity_class) {
return $this->validateSpaces($entity_class);
},
false,
$entity_class,
null
);
}
$input->setOption('entity-class', $entity_class);
$machine_name = $utils->camelCaseToMachineName($entity_class);
// --entity-name option
$entity_name = $input->getOption('entity-name');
if (!$entity_name) {
$entity_name = $dialog->askAndValidate(
$output,
$dialog->getQuestion($this->trans('commands.generate.entity.questions.entity-name'), $machine_name),
function ($machine_name) {
return $this->validateMachineName($machine_name);
},
false,
$machine_name,
null
);
}
$input->setOption('entity-name', $entity_name);
}
protected function createGenerator()
{
if ('EntityContent' == $this->entityType) {
return new EntityContentGenerator();
}
return new EntityConfigGenerator();
}
}