1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\PostProcessor\FileOverrider; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
8
|
|
|
use Symfony\Component\Console\Helper\Table; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
class OverridesUpdateCommand extends AbstractCommand |
14
|
|
|
{ |
15
|
|
|
public const OPT_OVERRIDE_ACTION = 'action'; |
16
|
|
|
public const OPT_OVERRIDE_ACTION_SHORT = 'a'; |
17
|
|
|
|
18
|
|
|
public const ACTION_TO_PROJECT = 'toProject'; |
19
|
|
|
public const ACTION_FROM_PROJECT = 'fromProject'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var FileOverrider |
23
|
|
|
*/ |
24
|
|
|
protected $fileOverrider; |
25
|
|
|
|
26
|
|
|
public function __construct(FileOverrider $fileOverrider, NamespaceHelper $namespaceHelper, ?string $name = null) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($namespaceHelper, $name); |
29
|
|
|
$this->fileOverrider = $fileOverrider; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): void |
33
|
|
|
{ |
34
|
|
|
$this->fileOverrider->setPathToProjectRoot($input->getOption(self::OPT_PROJECT_ROOT_PATH)); |
35
|
|
|
switch ($input->getOption(self::OPT_OVERRIDE_ACTION)) { |
36
|
|
|
case self::ACTION_TO_PROJECT: |
37
|
|
|
$this->renderTableOfUpdatedFiles($this->fileOverrider->applyOverrides(), $output); |
38
|
|
|
$output->writeln('<info>Overrides have been applied to project</info>'); |
39
|
|
|
|
40
|
|
|
return; |
41
|
|
|
case self::ACTION_FROM_PROJECT: |
42
|
|
|
$this->renderTableOfUpdatedFiles($this->fileOverrider->updateOverrideFiles(), $output); |
43
|
|
|
$output->writeln('<info>Overrides have been updated from the project</info>'); |
44
|
|
|
|
45
|
|
|
return; |
46
|
|
|
default: |
47
|
|
|
throw new \InvalidArgumentException( |
48
|
|
|
' Invalid action ' . $input->getOption(self::OPT_OVERRIDE_ACTION) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function renderTableOfUpdatedFiles(array $files, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$table = new Table($output); |
56
|
|
|
foreach ($files as $file) { |
57
|
|
|
$table->addRow([$file]); |
58
|
|
|
} |
59
|
|
|
$table->render(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws DoctrineStaticMetaException |
64
|
|
|
*/ |
65
|
|
|
protected function configure(): void |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
$this |
69
|
|
|
->setName(AbstractCommand::COMMAND_PREFIX . 'overrides:update') |
70
|
|
|
->setDefinition( |
71
|
|
|
[ |
72
|
|
|
new InputOption( |
73
|
|
|
self::OPT_OVERRIDE_ACTION, |
74
|
|
|
self::OPT_OVERRIDE_ACTION_SHORT, |
75
|
|
|
InputOption::VALUE_REQUIRED, |
76
|
|
|
'One of [ fromProject, toProject ]' |
77
|
|
|
), |
78
|
|
|
$this->getProjectRootPathOption(), |
79
|
|
|
] |
80
|
|
|
)->setDescription( |
81
|
|
|
'Update project overrides' |
82
|
|
|
); |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
throw new DoctrineStaticMetaException( |
85
|
|
|
'Exception in ' . __METHOD__ . ': ' . $e->getMessage(), |
86
|
|
|
$e->getCode(), |
87
|
|
|
$e |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|