Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Mouf/Utils/Patcher/Commands/ApplyAllPatchesCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mouf\Utils\Patcher\Commands;

use Mouf\Utils\Patcher\Dumper\Dumper;
use Mouf\Utils\Patcher\PatchInterface;
use Mouf\Utils\Patcher\PatchService;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -36,13 +37,18 @@ protected function configure()
);

$this->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.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('dump')) {
$this->patchService->setDumper(new Dumper($output));
}
$this->applyAll($input, $output);
}
}
8 changes: 8 additions & 0 deletions src/Mouf/Utils/Patcher/Commands/ApplyPatchCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mouf\Utils\Patcher\Commands;

use Mouf\Utils\Patcher\Dumper\Dumper;
use Mouf\Utils\Patcher\PatchInterface;
use Mouf\Utils\Patcher\PatchService;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -46,13 +47,20 @@ protected function configure()
Use patches:apply-all to apply all pending patches.
EOT
);

$this->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.');
}

/**
* {@inheritdoc}
*/
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);

Expand Down
7 changes: 7 additions & 0 deletions src/Mouf/Utils/Patcher/Commands/ResetPatchesCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mouf\Utils\Patcher\Commands;

use Mouf\Utils\Patcher\Dumper\Dumper;
use Mouf\Utils\Patcher\PatchInterface;
use Mouf\Utils\Patcher\PatchService;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -34,13 +35,19 @@ protected function configure()
);

$this->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.');
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('dump')) {
$this->patchService->setDumper(new Dumper($output));
}

$this->patchService->reset();

$this->applyAll($input, $output);
Expand Down
7 changes: 7 additions & 0 deletions src/Mouf/Utils/Patcher/Commands/RevertPatchCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mouf\Utils\Patcher\Commands;

use Mouf\Utils\Patcher\Dumper\Dumper;
use Mouf\Utils\Patcher\PatchInterface;
use Mouf\Utils\Patcher\PatchService;
use Symfony\Component\Console\Helper\Table;
Expand Down Expand Up @@ -44,13 +45,19 @@ protected function configure()
Reverts a patch. You must pass in parameter the name of the patch.
EOT
);

$this->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.');
}

/**
* {@inheritdoc}
*/
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);

Expand Down
12 changes: 12 additions & 0 deletions src/Mouf/Utils/Patcher/Dumper/DumpableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php


namespace Mouf\Utils\Patcher\Dumper;

/**
* Patches implementing this interface can dump their content (probably SQL) to the output.
*/
interface DumpableInterface
{
public function setDumper(DumperInterface $dumper);
}
25 changes: 25 additions & 0 deletions src/Mouf/Utils/Patcher/Dumper/Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php


namespace Mouf\Utils\Patcher\Dumper;


use Symfony\Component\Console\Output\OutputInterface;

class Dumper implements DumperInterface
{
/**
* @var OutputInterface
*/
private $output;

public function __construct(OutputInterface $output)
{
$this->output = $output;
}

public function dumpPatch(string $code): void
{
$this->output->writeln($code);
}
}
10 changes: 10 additions & 0 deletions src/Mouf/Utils/Patcher/Dumper/DumperInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


namespace Mouf\Utils\Patcher\Dumper;


interface DumperInterface
{
public function dumpPatch(string $code): void;
}
27 changes: 26 additions & 1 deletion src/Mouf/Utils/Patcher/PatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Mouf\Utils\Patcher;

use Mouf\Utils\Patcher\Dumper\DumpableInterface;
use Mouf\Utils\Patcher\Dumper\DumperInterface;
use Mouf\Validator\MoufValidatorInterface;
use Mouf\MoufManager;
use Mouf\Validator\MoufValidatorResult;
Expand All @@ -29,7 +31,7 @@
* @author David Negrier <david@mouf-php.com>
* @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";

Expand All @@ -55,6 +57,11 @@ class PatchService implements MoufValidatorInterface {
*/
private $listeners;

/**
* @var DumperInterface
*/
private $dumper;

/**
* @param PatchType[] $types
* @param PatchListenerInterface[] $listeners
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}
}