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\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Helper\Table; |
18
|
|
|
use Symfony\Component\Console\Helper\TableCell; |
19
|
|
|
use Symfony\Component\Console\Helper\TableSeparator; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Console\Question\Question; |
23
|
|
|
use Tardigrades\Entity\SectionInterface; |
24
|
|
|
use Tardigrades\SectionField\Service\SectionManagerInterface; |
25
|
|
|
use Tardigrades\SectionField\Service\SectionNotFoundException; |
26
|
|
|
use Tardigrades\SectionField\ValueObject\Id; |
27
|
|
|
|
28
|
|
|
abstract class SectionCommand extends Command |
29
|
|
|
{ |
30
|
|
|
const ALL = 'all'; |
31
|
|
|
|
32
|
|
|
/** @var SectionManagerInterface */ |
33
|
|
|
protected $sectionManager; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
SectionManagerInterface $sectionManager, |
37
|
|
|
string $name |
38
|
|
|
) { |
39
|
|
|
$this->sectionManager = $sectionManager; |
40
|
|
|
|
41
|
|
|
parent::__construct($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function renderTable(OutputInterface $output, array $sections, string $info) |
45
|
|
|
{ |
46
|
|
|
$table = new Table($output); |
47
|
|
|
|
48
|
|
|
$rows = []; |
49
|
|
|
/** @var SectionInterface $section */ |
50
|
|
|
foreach ($sections as $section) { |
51
|
|
|
$rows[] = [ |
52
|
|
|
$section->getId(), |
53
|
|
|
$section->getName(), |
54
|
|
|
$section->getHandle(), |
55
|
|
|
(string) $section->getConfig(), |
56
|
|
|
$section->getUpdated()->format('D-m-y'), |
57
|
|
|
$section->getVersion() |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$rows[] = new TableSeparator(); |
62
|
|
|
$rows[] = [ |
63
|
|
|
new TableCell('<info>' . $info . '</info>', ['colspan' => 5]) |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$table |
67
|
|
|
->setHeaders(['#id', 'name', 'handle', 'config', 'updated', 'version']) |
68
|
|
|
->setRows($rows) |
69
|
|
|
; |
70
|
|
|
$table->render(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getSection(InputInterface $input, OutputInterface $output): ?SectionInterface |
74
|
|
|
{ |
75
|
|
|
$question = new Question('<question>Choose record.</question> (#id): '); |
76
|
|
|
$question->setValidator(function ($id) { |
77
|
|
|
try { |
78
|
|
|
return $this->sectionManager->read(Id::fromInt((int) $id)); |
79
|
|
|
} catch (SectionNotFoundException $exception) { |
80
|
|
|
// Exceptions thrown from here seemingly can't be caught, so signal with a return value instead |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
}); |
84
|
|
|
$section = $this->getHelper('question')->ask($input, $output, $question); |
85
|
|
|
if (!$section) { |
86
|
|
|
throw new SectionNotFoundException(); |
87
|
|
|
} |
88
|
|
|
return $section; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param InputInterface $input |
93
|
|
|
* @param OutputInterface $output |
94
|
|
|
* @return SectionInterface[] |
95
|
|
|
* @throws SectionNotFoundException |
96
|
|
|
*/ |
97
|
|
|
protected function getSections(InputInterface $input, OutputInterface $output): array |
98
|
|
|
{ |
99
|
|
|
$question = new Question('<question>Choose record.</question> (#id): '); |
100
|
|
|
$question->setValidator(function ($id) { |
101
|
|
|
try { |
102
|
|
|
if ($id === self::ALL) { |
103
|
|
|
return $this->sectionManager->readAll(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (strpos($id, ',') !== false) { |
107
|
|
|
$ids = explode(',', $id); |
108
|
|
|
return $this->sectionManager->readByIds($ids); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return [$this->sectionManager->read(Id::fromInt((int) $id))]; |
112
|
|
|
} catch (SectionNotFoundException $exception) { |
113
|
|
|
// Exceptions thrown from here seemingly can't be caught, so signal with a return value instead |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
}); |
117
|
|
|
|
118
|
|
|
$section = $this->getHelper('question')->ask($input, $output, $question); |
119
|
|
|
if (!$section) { |
120
|
|
|
throw new SectionNotFoundException(); |
121
|
|
|
} |
122
|
|
|
return $section; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|