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