1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * This file is part of the SexyField package. |
||||
5 | * |
||||
6 | * (c) Dion Snoeijen <[email protected]> |
||||
7 | * |
||||
8 | * For the full copyright and license information, please view the LICENSE |
||||
9 | * file that was distributed with this source code. |
||||
10 | */ |
||||
11 | |||||
12 | declare (strict_types = 1); |
||||
13 | |||||
14 | namespace Tardigrades\Command; |
||||
15 | |||||
16 | use Symfony\Component\Console\Helper\QuestionHelper; |
||||
17 | use Symfony\Component\Console\Input\InputArgument; |
||||
18 | use Symfony\Component\Console\Input\InputInterface; |
||||
19 | use Symfony\Component\Console\Input\InputOption; |
||||
20 | use Symfony\Component\Console\Output\OutputInterface; |
||||
21 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||||
22 | use Symfony\Component\Yaml\Yaml; |
||||
23 | use Tardigrades\SectionField\Service\SectionManagerInterface; |
||||
24 | use Tardigrades\SectionField\Service\SectionNotFoundException; |
||||
25 | use Tardigrades\SectionField\ValueObject\SectionConfig; |
||||
26 | |||||
27 | class UpdateSectionCommand extends SectionCommand |
||||
28 | { |
||||
29 | /** @var QuestionHelper */ |
||||
30 | private $questionHelper; |
||||
31 | |||||
32 | public function __construct( |
||||
33 | SectionManagerInterface $sectionManager |
||||
34 | ) { |
||||
35 | parent::__construct($sectionManager, 'sf:update-section'); |
||||
36 | } |
||||
37 | |||||
38 | protected function configure(): void |
||||
39 | { |
||||
40 | // @codingStandardsIgnoreStart |
||||
41 | $this |
||||
42 | ->setDescription('Updates an existing section.') |
||||
43 | ->setHelp(<<<EOF |
||||
44 | The <info>%command.name%</info> command allows you to update a section based on a yml section cofiguration. Pass along the path to a section cofiguration yml. Something like: section/blog.yml |
||||
45 | |||||
46 | You can automatically continue with flags: |
||||
47 | |||||
48 | <info>--yes-mode --in-history</info> |
||||
49 | |||||
50 | Will store the section config that will be replaced in history. |
||||
51 | |||||
52 | <info>--yes-mode --not-in-history</info> |
||||
53 | |||||
54 | Will not store anything in history. |
||||
55 | |||||
56 | EOF |
||||
57 | ) |
||||
58 | ->addArgument('config', InputArgument::REQUIRED, 'The section configuration yml') |
||||
59 | ->addOption( |
||||
60 | 'yes-mode', |
||||
61 | 'ym', |
||||
62 | InputOption::VALUE_NONE, |
||||
63 | 'Automatically say yes when a field handle is found' |
||||
64 | ) |
||||
65 | ->addOption( |
||||
66 | 'in-history', |
||||
67 | null, |
||||
68 | InputOption::VALUE_NONE, |
||||
69 | 'Set this flag if you want to store the section in history automatically' |
||||
70 | ) |
||||
71 | ->addOption( |
||||
72 | 'not-in-history', |
||||
73 | null, |
||||
74 | InputOption::VALUE_NONE, |
||||
75 | 'Set this flag if you don\'t want to store the section in history automatically' |
||||
76 | ) |
||||
77 | ; |
||||
78 | // @codingStandardsIgnoreEnd |
||||
79 | } |
||||
80 | |||||
81 | protected function execute(InputInterface $input, OutputInterface $output): void |
||||
82 | { |
||||
83 | try { |
||||
84 | $this->questionHelper = $this->getHelper('question'); |
||||
85 | $this->showInstalledSections($input, $output); |
||||
86 | } catch (SectionNotFoundException $exception) { |
||||
87 | $output->writeln("Section not found"); |
||||
88 | } |
||||
89 | } |
||||
90 | |||||
91 | private function showInstalledSections(InputInterface $input, OutputInterface $output): void |
||||
92 | { |
||||
93 | if (!$input->getOption('yes-mode')) { |
||||
94 | $this->renderTable($output, $this->sectionManager->readAll(), 'All installed Sections'); |
||||
95 | } |
||||
96 | $this->updateWhatRecord($input, $output); |
||||
97 | } |
||||
98 | |||||
99 | private function getConfig(InputInterface $input, OutputInterface $output): ?SectionConfig |
||||
100 | { |
||||
101 | try { |
||||
102 | $config = $input->getArgument('config'); |
||||
103 | $sectionConfig = SectionConfig::fromArray( |
||||
104 | Yaml::parse( |
||||
105 | file_get_contents($config) |
||||
106 | ) |
||||
107 | ); |
||||
108 | return $sectionConfig; |
||||
109 | } catch (\Exception $exception) { |
||||
110 | $output->writeln("<error>Invalid configuration file. {$exception->getMessage()}</error>"); |
||||
111 | return null; |
||||
112 | } |
||||
113 | } |
||||
114 | |||||
115 | private function updateWhatRecord(InputInterface $input, OutputInterface $output): void |
||||
116 | { |
||||
117 | if (is_null($sectionConfig = $this->getConfig($input, $output))) { |
||||
118 | return; |
||||
119 | } |
||||
120 | |||||
121 | try { |
||||
122 | $section = $this->sectionManager->readByHandle($sectionConfig->getHandle()); |
||||
123 | if (!$input->getOption('yes-mode')) { |
||||
124 | $sure = new ConfirmationQuestion( |
||||
125 | '<comment>Do you want to update the section with id: ' . $section->getId() . '?</comment> (y/n) ', |
||||
126 | false |
||||
127 | ); |
||||
128 | |||||
129 | if (!$this->questionHelper->ask($input, $output, $sure)) { |
||||
130 | $output->writeln('<comment>Cancelled, nothing updated.</comment>', false); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
131 | return; |
||||
132 | } |
||||
133 | } |
||||
134 | } catch (SectionNotFoundException $exception) { |
||||
135 | $output->writeln( |
||||
136 | 'You are trying to update a section with handle: ' . $sectionConfig->getHandle() . '. No field with ' . |
||||
137 | 'that handle exists in the database, use sf:create-section is you actually need a new section, or' . |
||||
138 | 'select an existing section id that will be overwritten with this config.' |
||||
139 | ); |
||||
140 | |||||
141 | $sure = new ConfirmationQuestion( |
||||
142 | '<comment>Do you want to continue to select a section that will be overwritten?</comment> (y/n) ', |
||||
143 | false |
||||
144 | ); |
||||
145 | |||||
146 | if (!$this->getHelper('question')->ask($input, $output, $sure)) { |
||||
147 | $output->writeln('<comment>Cancelled, nothing updated</comment>'); |
||||
148 | return; |
||||
149 | } |
||||
150 | |||||
151 | $section = $this->getSection($input, $output); |
||||
152 | } |
||||
153 | |||||
154 | $inHistory = $input->getOption('in-history'); |
||||
155 | $notInHistory = $input->getOption('not-in-history'); |
||||
156 | |||||
157 | if (!$inHistory && !$notInHistory) { |
||||
158 | $inHistory = false; |
||||
159 | // @todo: Make it a section config (if it can be manually told to go to history) |
||||
160 | // $inHistory = $this->getHelper('question')->ask( |
||||
161 | // $input, |
||||
162 | // $output, |
||||
163 | // new ConfirmationQuestion( |
||||
164 | // '<comment>Do you want to store the current version in history?</comment> (y/n) ', |
||||
165 | // false |
||||
166 | // ) |
||||
167 | // ); |
||||
168 | } |
||||
169 | |||||
170 | if ($notInHistory) { |
||||
171 | $inHistory = false; |
||||
172 | } |
||||
173 | |||||
174 | $this->sectionManager->updateByConfig($sectionConfig, $section, $inHistory); |
||||
0 ignored issues
–
show
It seems like
$section can also be of type null ; however, parameter $section of Tardigrades\SectionField...rface::updateByConfig() does only seem to accept Tardigrades\Entity\SectionInterface , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
175 | if (!$input->getOption('yes-mode')) { |
||||
176 | $this->renderTable($output, $this->sectionManager->readAll(), 'Section updated!'); |
||||
177 | } else { |
||||
178 | $output->writeln( |
||||
179 | 'Section updated! ' . |
||||
180 | ($inHistory ? 'Old version stored in history.' : 'Nothing stored in history.') |
||||
181 | ); |
||||
182 | } |
||||
183 | } |
||||
184 | } |
||||
185 |