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