From 2baeb515fc4f1a1319f354fbeb5e8c5a99e3a286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 4 Oct 2017 16:30:26 +0200 Subject: [PATCH] Adds a dumper option to the command line to dump SQL commands --- .../Commands/ApplyAllPatchesCommand.php | 6 +++++ .../Patcher/Commands/ApplyPatchCommand.php | 8 ++++++ .../Patcher/Commands/ResetPatchesCommand.php | 7 +++++ .../Patcher/Commands/RevertPatchCommand.php | 7 +++++ .../Patcher/Dumper/DumpableInterface.php | 12 +++++++++ src/Mouf/Utils/Patcher/Dumper/Dumper.php | 25 +++++++++++++++++ .../Utils/Patcher/Dumper/DumperInterface.php | 10 +++++++ src/Mouf/Utils/Patcher/PatchService.php | 27 ++++++++++++++++++- 8 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/Mouf/Utils/Patcher/Dumper/DumpableInterface.php create mode 100644 src/Mouf/Utils/Patcher/Dumper/Dumper.php create mode 100644 src/Mouf/Utils/Patcher/Dumper/DumperInterface.php diff --git a/src/Mouf/Utils/Patcher/Commands/ApplyAllPatchesCommand.php b/src/Mouf/Utils/Patcher/Commands/ApplyAllPatchesCommand.php index 66b4b05..54adfcf 100644 --- a/src/Mouf/Utils/Patcher/Commands/ApplyAllPatchesCommand.php +++ b/src/Mouf/Utils/Patcher/Commands/ApplyAllPatchesCommand.php @@ -1,6 +1,7 @@ registerOptions(); + + $this->addOption('dump', 'd', InputOption::VALUE_NONE, 'Dumps the patches to the output. Note: this is not a "dry" mode. The patches will still be applied.'); } /** @@ -43,6 +46,9 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + if ($input->getOption('dump')) { + $this->patchService->setDumper(new Dumper($output)); + } $this->applyAll($input, $output); } } diff --git a/src/Mouf/Utils/Patcher/Commands/ApplyPatchCommand.php b/src/Mouf/Utils/Patcher/Commands/ApplyPatchCommand.php index c7c7399..8989466 100644 --- a/src/Mouf/Utils/Patcher/Commands/ApplyPatchCommand.php +++ b/src/Mouf/Utils/Patcher/Commands/ApplyPatchCommand.php @@ -1,6 +1,7 @@ addOption('dump', 'd', InputOption::VALUE_NONE, 'Dumps the patch to the output. Note: this is not a "dry" mode. The patch will still be applied.'); } /** @@ -53,6 +56,11 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + if ($input->getOption('dump')) { + $this->patchService->setDumper(new Dumper($output)); + } + + $patchName = $input->getArgument('name'); $this->patchService->apply($patchName); diff --git a/src/Mouf/Utils/Patcher/Commands/ResetPatchesCommand.php b/src/Mouf/Utils/Patcher/Commands/ResetPatchesCommand.php index 1d70c07..5b438da 100644 --- a/src/Mouf/Utils/Patcher/Commands/ResetPatchesCommand.php +++ b/src/Mouf/Utils/Patcher/Commands/ResetPatchesCommand.php @@ -1,6 +1,7 @@ registerOptions(); + + $this->addOption('dump', 'd', InputOption::VALUE_NONE, 'Dumps the patch to the output. Note: this is not a "dry" mode. The database will still be reset.'); } /** @@ -41,6 +44,10 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + if ($input->getOption('dump')) { + $this->patchService->setDumper(new Dumper($output)); + } + $this->patchService->reset(); $this->applyAll($input, $output); diff --git a/src/Mouf/Utils/Patcher/Commands/RevertPatchCommand.php b/src/Mouf/Utils/Patcher/Commands/RevertPatchCommand.php index 99431b0..7a0c64b 100644 --- a/src/Mouf/Utils/Patcher/Commands/RevertPatchCommand.php +++ b/src/Mouf/Utils/Patcher/Commands/RevertPatchCommand.php @@ -1,6 +1,7 @@ addOption('dump', 'd', InputOption::VALUE_NONE, 'Dumps the patch to the output. Note: this is not a "dry" mode. The patch will still be reverted.'); } /** @@ -51,6 +54,10 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + if ($input->getOption('dump')) { + $this->patchService->setDumper(new Dumper($output)); + } + $patchName = $input->getArgument('name'); $this->patchService->revert($patchName); diff --git a/src/Mouf/Utils/Patcher/Dumper/DumpableInterface.php b/src/Mouf/Utils/Patcher/Dumper/DumpableInterface.php new file mode 100644 index 0000000..387ebde --- /dev/null +++ b/src/Mouf/Utils/Patcher/Dumper/DumpableInterface.php @@ -0,0 +1,12 @@ +output = $output; + } + + public function dumpPatch(string $code): void + { + $this->output->writeln($code); + } +} \ No newline at end of file diff --git a/src/Mouf/Utils/Patcher/Dumper/DumperInterface.php b/src/Mouf/Utils/Patcher/Dumper/DumperInterface.php new file mode 100644 index 0000000..0596713 --- /dev/null +++ b/src/Mouf/Utils/Patcher/Dumper/DumperInterface.php @@ -0,0 +1,10 @@ + * @ExtendedAction {"name":"View patches list", "url":"patcher/", "default":false} */ -class PatchService implements MoufValidatorInterface { +class PatchService implements MoufValidatorInterface, DumpableInterface { const IFEXISTS_EXCEPTION = "exception"; const IFEXISTS_IGNORE = "ignore"; @@ -55,6 +57,11 @@ class PatchService implements MoufValidatorInterface { */ private $listeners; + /** + * @var DumperInterface + */ + private $dumper; + /** * @param PatchType[] $types * @param PatchListenerInterface[] $listeners @@ -271,6 +278,10 @@ public function getView(): array { */ public function apply($uniqueName): void { $patch = $this->get($uniqueName); + if ($patch instanceof DumpableInterface && $this->dumper !== null) { + $patch->setDumper($this->dumper); + } + // TODO: in next major version, get rid of the DumpableInterface and pass the dumper right in the apply method. $patch->apply(); } @@ -280,6 +291,9 @@ public function apply($uniqueName): void { */ public function skip($uniqueName): void { $patch = $this->get($uniqueName); + if ($patch instanceof DumpableInterface && $this->dumper !== null) { + $patch->setDumper($this->dumper); + } $patch->skip(); } @@ -290,6 +304,9 @@ public function skip($uniqueName): void { */ public function revert($uniqueName): void { $patch = $this->get($uniqueName); + if ($patch instanceof DumpableInterface && $this->dumper !== null) { + $patch->setDumper($this->dumper); + } $patch->revert(); } @@ -338,7 +355,15 @@ public function applyAll(array $types = []): array { */ public function reset(): void { foreach ($this->listeners as $listener) { + if ($listener instanceof DumpableInterface && $this->dumper !== null) { + $listener->setDumper($this->dumper); + } $listener->onReset(); } } + + public function setDumper(DumperInterface $dumper) + { + $this->dumper = $dumper; + } }