1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* For full license information, please view the LICENSE file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Phinx\Console\Command; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
|
15
|
|
|
class Breakpoint extends AbstractCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected static $defaultName = 'breakpoint'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
* |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
parent::configure(); |
30
|
|
|
|
31
|
|
|
$this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment.'); |
32
|
|
|
|
33
|
|
|
$this->setDescription('Manage breakpoints') |
34
|
|
|
->addOption('--target', '-t', InputOption::VALUE_REQUIRED, 'The version number to target for the breakpoint') |
35
|
|
|
->addOption('--set', '-s', InputOption::VALUE_NONE, 'Set the breakpoint') |
36
|
|
|
->addOption('--unset', '-u', InputOption::VALUE_NONE, 'Unset the breakpoint') |
37
|
|
|
->addOption('--remove-all', '-r', InputOption::VALUE_NONE, 'Remove all breakpoints') |
38
|
|
|
->setHelp( |
39
|
|
|
<<<EOT |
40
|
|
|
The <info>breakpoint</info> command allows you to toggle, set, or unset a breakpoint against a specific target to inhibit rollbacks beyond a certain target. |
41
|
35 |
|
If no target is supplied then the most recent migration will be used. |
42
|
|
|
You cannot specify un-migrated targets |
43
|
35 |
|
|
44
|
|
|
<info>phinx breakpoint -e development</info> |
45
|
35 |
|
<info>phinx breakpoint -e development -t 20110103081132</info> |
46
|
|
|
<info>phinx breakpoint -e development -r</info> |
47
|
35 |
|
EOT |
48
|
35 |
|
); |
49
|
35 |
|
} |
50
|
35 |
|
|
51
|
35 |
|
/** |
52
|
|
|
* Toggle the breakpoint. |
53
|
|
|
* |
54
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input Input |
55
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output Output |
56
|
|
|
* |
57
|
|
|
* @throws \InvalidArgumentException |
58
|
|
|
* |
59
|
|
|
* @return int integer 0 on success, or an error code. |
60
|
|
|
*/ |
61
|
35 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
35 |
|
{ |
63
|
|
|
$this->bootstrap($input, $output); |
64
|
|
|
|
65
|
|
|
$environment = $input->getOption('environment'); |
66
|
|
|
$version = (int)$input->getOption('target') ?: null; |
67
|
|
|
$removeAll = $input->getOption('remove-all'); |
68
|
|
|
$set = $input->getOption('set'); |
69
|
|
|
$unset = $input->getOption('unset'); |
70
|
|
|
|
71
|
|
View Code Duplication |
if ($environment === null) { |
|
|
|
|
72
|
|
|
$environment = $this->getConfig()->getDefaultEnvironment(); |
73
|
|
|
$output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment); |
74
|
|
|
} else { |
75
|
|
|
$output->writeln('<info>using environment</info> ' . $environment); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (!$this->getConfig()->hasEnvironment($environment)) { |
79
|
|
|
$output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment)); |
80
|
|
|
|
81
|
|
|
return self::CODE_ERROR; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($version && $removeAll) { |
|
|
|
|
85
|
|
|
throw new InvalidArgumentException('Cannot toggle a breakpoint and remove all breakpoints at the same time.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (($set && $unset) || ($set && $removeAll) || ($unset && $removeAll)) { |
89
|
|
|
throw new InvalidArgumentException('Cannot use more than one of --set, --unset, or --remove-all at the same time.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($removeAll) { |
93
|
|
|
// Remove all breakpoints. |
94
|
|
|
$this->getManager()->removeBreakpoints($environment); |
95
|
|
|
} elseif ($set) { |
96
|
|
|
// Set the breakpoint. |
97
|
|
|
$this->getManager()->setBreakpoint($environment, $version); |
98
|
|
|
} elseif ($unset) { |
99
|
|
|
// Unset the breakpoint. |
100
|
|
|
$this->getManager()->unsetBreakpoint($environment, $version); |
101
|
|
|
} else { |
102
|
|
|
// Toggle the breakpoint. |
103
|
|
|
$this->getManager()->toggleBreakpoint($environment, $version); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return self::CODE_SUCCESS; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.