1 | <?php |
||
23 | class ConflictsCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand |
||
24 | { |
||
25 | |||
26 | const SETTING_CONFLICTS_RECORDED_CONFLICTS = 'conflicts.recorded-conflicts'; |
||
27 | |||
28 | const MODE_SHOW = 'show'; |
||
29 | |||
30 | const MODE_ADD = 'add'; |
||
31 | |||
32 | const MODE_REPLACE = 'replace'; |
||
33 | |||
34 | const MODE_ERASE = 'erase'; |
||
35 | |||
36 | /** |
||
37 | * Working copy conflict tracker. |
||
38 | * |
||
39 | * @var WorkingCopyConflictTracker |
||
40 | */ |
||
41 | private $_workingCopyConflictTracker; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 11 | protected function configure() |
|
47 | { |
||
48 | 11 | $mode_string = '<comment>' . implode('</comment>, <comment>', $this->getModes()) . '</comment>'; |
|
49 | |||
50 | $this |
||
51 | 11 | ->setName('conflicts') |
|
52 | 11 | ->setDescription( |
|
53 | 11 | 'Manage recorded conflicts in a working copy' |
|
54 | ) |
||
55 | 11 | ->setAliases(array('cf')) |
|
56 | 11 | ->addArgument( |
|
57 | 11 | 'path', |
|
58 | 11 | InputArgument::OPTIONAL, |
|
59 | 11 | 'Working copy path', |
|
60 | 11 | '.' |
|
61 | ) |
||
62 | 11 | ->addOption( |
|
63 | 11 | 'mode', |
|
64 | 11 | 'm', |
|
65 | 11 | InputOption::VALUE_REQUIRED, |
|
66 | 11 | 'Operation mode, e.g. ' . $mode_string, |
|
67 | 11 | self::MODE_SHOW |
|
68 | ); |
||
69 | |||
70 | 11 | parent::configure(); |
|
71 | 11 | } |
|
72 | |||
73 | /** |
||
74 | * Prepare dependencies. |
||
75 | * |
||
76 | * @return void |
||
77 | */ |
||
78 | protected function prepareDependencies() |
||
86 | |||
87 | /** |
||
88 | * Return possible values for the named option |
||
89 | * |
||
90 | * @param string $optionName Option name. |
||
91 | * @param CompletionContext $context Completion context. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function completeOptionValues($optionName, CompletionContext $context) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | * |
||
109 | * @throws \RuntimeException When invalid mode is specified. |
||
110 | */ |
||
111 | protected function execute(InputInterface $input, OutputInterface $output) |
||
112 | { |
||
113 | $mode = $this->io->getOption('mode'); |
||
114 | |||
115 | if ( !in_array($mode, $this->getModes()) ) { |
||
116 | throw new \RuntimeException( |
||
117 | 'The "' . $mode . '" mode is unknown.' |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | $wc_path = $this->getWorkingCopyPath(); |
||
122 | |||
123 | switch ( $mode ) { |
||
124 | case self::MODE_SHOW: |
||
125 | $recorded_conflicts = $this->_workingCopyConflictTracker->getRecordedConflicts($wc_path); |
||
126 | |||
127 | if ( !$recorded_conflicts ) { |
||
128 | $this->io->writeln('<info>The working copy doesn\'t have any recorded conflicts.</info>'); |
||
129 | } |
||
130 | else { |
||
131 | $this->io->writeln( |
||
132 | '<error>Recorded Conflicts (' . count($recorded_conflicts) . ' paths):</error>' |
||
133 | ); |
||
134 | |||
135 | foreach ( $recorded_conflicts as $conflicted_path ) { |
||
136 | $this->io->writeln(' * ' . $conflicted_path); |
||
137 | } |
||
138 | } |
||
139 | break; |
||
140 | |||
141 | case self::MODE_ADD: |
||
142 | $this->_workingCopyConflictTracker->add($wc_path); |
||
143 | $this->io->writeln('<info>Conflicts updated.</info>'); |
||
144 | break; |
||
145 | |||
146 | case self::MODE_REPLACE: |
||
147 | $this->_workingCopyConflictTracker->replace($wc_path); |
||
148 | $this->io->writeln('<info>Conflicts updated.</info>'); |
||
149 | break; |
||
150 | |||
151 | case self::MODE_ERASE: |
||
152 | $this->_workingCopyConflictTracker->erase($wc_path); |
||
153 | $this->io->writeln('<info>Conflicts erased.</info>'); |
||
154 | break; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns allowed modes. |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 11 | protected function getModes() |
|
167 | |||
168 | /** |
||
169 | * Returns list of config settings. |
||
170 | * |
||
171 | * @return AbstractConfigSetting[] |
||
172 | */ |
||
173 | public function getConfigSettings() |
||
174 | { |
||
175 | return array( |
||
176 | new ArrayConfigSetting(self::SETTING_CONFLICTS_RECORDED_CONFLICTS, array()), |
||
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns option names, that makes sense to use in aggregation mode. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function getAggregatedOptions() |
||
189 | |||
190 | } |
||
191 |