Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Config / ExportViewCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\ExportViewCommand.
6  */
7
8 namespace Drupal\Console\Command\Config;
9
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Command\Shared\ModuleTrait;
17 use Drupal\Core\Entity\EntityTypeManagerInterface;
18 use Drupal\Core\Config\CachedStorage;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Command\Shared\ExportTrait;
21 use Drupal\Console\Extension\Manager;
22
23 class ExportViewCommand extends Command
24 {
25     use CommandTrait;
26     use ModuleTrait;
27     use ExportTrait;
28
29     protected $configExport;
30
31
32     /**
33      * @var EntityTypeManagerInterface
34      */
35     protected $entityTypeManager;
36
37     /**
38      * @var CachedStorage
39      */
40     protected $configStorage;
41
42     /**
43      * @var Manager
44      */
45     protected $extensionManager;
46
47     /**
48      * ExportViewCommand constructor.
49      *
50      * @param EntityTypeManagerInterface $entityTypeManager
51      * @param CachedStorage              $configStorage
52      * @param Manager                    $extensionManager
53      */
54     public function __construct(
55         EntityTypeManagerInterface $entityTypeManager,
56         CachedStorage $configStorage,
57         Manager $extensionManager
58     ) {
59         $this->entityTypeManager = $entityTypeManager;
60         $this->configStorage = $configStorage;
61         $this->extensionManager = $extensionManager;
62         parent::__construct();
63     }
64
65
66     protected function configure()
67     {
68         $this
69             ->setName('config:export:view')
70             ->setDescription($this->trans('commands.config.export.view.description'))
71             ->addOption(
72                 'module', null,
73                 InputOption::VALUE_REQUIRED,
74                 $this->trans('commands.common.options.module')
75             )
76             ->addArgument(
77                 'view-id',
78                 InputArgument::OPTIONAL,
79                 $this->trans('commands.config.export.view.arguments.view-id')
80             )
81             ->addOption(
82                 'optional-config',
83                 null,
84                 InputOption::VALUE_OPTIONAL,
85                 $this->trans('commands.config.export.view.options.optional-config')
86             )
87             ->addOption(
88                 'include-module-dependencies',
89                 null,
90                 InputOption::VALUE_OPTIONAL,
91                 $this->trans('commands.config.export.view.options.include-module-dependencies')
92             );
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     protected function interact(InputInterface $input, OutputInterface $output)
99     {
100         $io = new DrupalStyle($input, $output);
101
102         // --module option
103         $module = $input->getOption('module');
104         if (!$module) {
105             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
106             $module = $this->moduleQuestion($io);
107             $input->setOption('module', $module);
108         }
109
110         // view-id argument
111         $viewId = $input->getArgument('view-id');
112         if (!$viewId) {
113             $views = $this->entityTypeManager->getStorage('view')->loadMultiple();
114
115             $viewList = [];
116             foreach ($views as $view) {
117                 $viewList[$view->get('id')] = $view->get('label');
118             }
119
120             $viewId = $io->choiceNoList(
121                 $this->trans('commands.config.export.view.questions.view'),
122                 $viewList
123             );
124             $input->setArgument('view-id', $viewId);
125         }
126
127         $optionalConfig = $input->getOption('optional-config');
128         if (!$optionalConfig) {
129             $optionalConfig = $io->confirm(
130                 $this->trans('commands.config.export.view.questions.optional-config'),
131                 true
132             );
133             $input->setOption('optional-config', $optionalConfig);
134         }
135
136         $includeModuleDependencies = $input->getOption('include-module-dependencies');
137         if (!$includeModuleDependencies) {
138             $includeModuleDependencies = $io->confirm(
139                 $this->trans('commands.config.export.view.questions.include-module-dependencies'),
140                 true
141             );
142             $input->setOption('include-module-dependencies', $includeModuleDependencies);
143         }
144     }
145
146     protected function execute(InputInterface $input, OutputInterface $output)
147     {
148         $io = new DrupalStyle($input, $output);
149
150         $module = $input->getOption('module');
151         $viewId = $input->getArgument('view-id');
152         $optionalConfig = $input->getOption('optional-config');
153         $includeModuleDependencies = $input->getOption('include-module-dependencies');
154
155         $viewTypeDefinition = $this->entityTypeManager->getDefinition('view');
156         $viewTypeName = $viewTypeDefinition->getConfigPrefix() . '.' . $viewId;
157
158         $viewNameConfig = $this->getConfiguration($viewTypeName);
159
160         $this->configExport[$viewTypeName] = ['data' => $viewNameConfig, 'optional' => $optionalConfig];
161
162         // Include config dependencies in export files
163         if ($dependencies = $this->fetchDependencies($viewNameConfig, 'config')) {
164             $this->resolveDependencies($dependencies, $optionalConfig);
165         }
166
167         // Include module dependencies in export files if export is not optional
168         if ($includeModuleDependencies) {
169             if ($dependencies = $this->fetchDependencies($viewNameConfig, 'module')) {
170                 $this->exportModuleDependencies($io, $module, $dependencies);
171             }
172         }
173
174         $this->exportConfigToModule($module, $io, $this->trans('commands.views.export.messages.view_exported'));
175     }
176 }