Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / EntityCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Generate\EntityCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17
18 abstract class EntityCommand extends Command
19 {
20     use CommandTrait;
21     use ModuleTrait;
22     private $entityType;
23     private $commandName;
24
25     /**
26      * @param $entityType
27      */
28     protected function setEntityType($entityType)
29     {
30         $this->entityType = $entityType;
31     }
32
33     /**
34      * @param $commandName
35      */
36     protected function setCommandName($commandName)
37     {
38         $this->commandName = $commandName;
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     protected function configure()
45     {
46         $commandKey = str_replace(':', '.', $this->commandName);
47
48         $this
49             ->setName($this->commandName)
50             ->setDescription(
51                 sprintf(
52                     $this->trans('commands.'.$commandKey.'.description'),
53                     $this->entityType
54                 )
55             )
56             ->setHelp(
57                 sprintf(
58                     $this->trans('commands.'.$commandKey.'.help'),
59                     $this->commandName,
60                     $this->entityType
61                 )
62             )
63             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
64             ->addOption(
65                 'entity-class',
66                 null,
67                 InputOption::VALUE_REQUIRED,
68                 $this->trans('commands.'.$commandKey.'.options.entity-class')
69             )
70             ->addOption(
71                 'entity-name',
72                 null,
73                 InputOption::VALUE_REQUIRED,
74                 $this->trans('commands.'.$commandKey.'.options.entity-name')
75             )
76             ->addOption(
77                 'base-path',
78                 null,
79                 InputOption::VALUE_OPTIONAL,
80                 $this->trans('commands.' . $commandKey . '.options.base-path')
81             )
82             ->addOption(
83                 'label',
84                 null,
85                 InputOption::VALUE_REQUIRED,
86                 $this->trans('commands.'.$commandKey.'.options.label')
87             );
88     }
89
90     protected function execute(InputInterface $input, OutputInterface $output)
91     {
92         // Operations defined in EntityConfigCommand and EntityContentCommand.
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     protected function interact(InputInterface $input, OutputInterface $output)
99     {
100         $io = new DrupalStyle($input, $output);
101
102         $commandKey = str_replace(':', '.', $this->commandName);
103         $utils = $this->stringConverter;
104
105         // --module option
106         $module = $input->getOption('module');
107         if (!$module) {
108             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
109             $module = $this->moduleQuestion($io);
110             $input->setOption('module', $module);
111         }
112
113         // --entity-class option
114         $entityClass = $input->getOption('entity-class');
115         if (!$entityClass) {
116             $entityClass = $io->ask(
117                 $this->trans('commands.'.$commandKey.'.questions.entity-class'),
118                 'DefaultEntity',
119                 function ($entityClass) {
120                     return $this->validator->validateSpaces($entityClass);
121                 }
122             );
123
124             $input->setOption('entity-class', $entityClass);
125         }
126
127         // --entity-name option
128         $entityName = $input->getOption('entity-name');
129         if (!$entityName) {
130             $entityName = $io->ask(
131                 $this->trans('commands.'.$commandKey.'.questions.entity-name'),
132                 $utils->camelCaseToMachineName($entityClass),
133                 function ($entityName) {
134                     return $this->validator->validateMachineName($entityName);
135                 }
136             );
137             $input->setOption('entity-name', $entityName);
138         }
139
140         // --label option
141         $label = $input->getOption('label');
142         if (!$label) {
143             $label = $io->ask(
144                 $this->trans('commands.'.$commandKey.'.questions.label'),
145                 $utils->camelCaseToHuman($entityClass)
146             );
147             $input->setOption('label', $label);
148         }
149
150         // --base-path option
151         $base_path = $input->getOption('base-path');
152         if (!$base_path) {
153             $base_path = $this->getDefaultBasePath();
154         }
155         $base_path = $io->ask(
156             $this->trans('commands.'.$commandKey.'.questions.base-path'),
157             $base_path
158         );
159         if (substr($base_path, 0, 1) !== '/') {
160             // Base path must start with a leading '/'.
161             $base_path = '/' . $base_path;
162         }
163         $input->setOption('base-path', $base_path);
164     }
165
166     /**
167      * Gets default base path.
168      *
169      * @return string
170      *   Default base path.
171      */
172     protected function getDefaultBasePath()
173     {
174         return '/admin/structure';
175     }
176 }