console-helpers /
svn-buddy
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the SVN-Buddy library. |
||
| 4 | * For the full copyright and license information, please view |
||
| 5 | * the LICENSE file that was distributed with this source code. |
||
| 6 | * |
||
| 7 | * @copyright Alexander Obuhovich <[email protected]> |
||
| 8 | * @link https://github.com/console-helpers/svn-buddy |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace ConsoleHelpers\SVNBuddy\Command; |
||
| 12 | |||
| 13 | |||
| 14 | use ConsoleHelpers\ConsoleKit\Command\AbstractCommand as BaseCommand; |
||
| 15 | use ConsoleHelpers\SVNBuddy\Config\AbstractConfigSetting; |
||
| 16 | use ConsoleHelpers\ConsoleKit\Config\ConfigEditor; |
||
| 17 | use ConsoleHelpers\SVNBuddy\Repository\Connector\Connector; |
||
| 18 | use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLog; |
||
| 19 | use ConsoleHelpers\SVNBuddy\Repository\RevisionLog\RevisionLogFactory; |
||
| 20 | use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
||
| 21 | use Symfony\Component\Console\Input\InputInterface; |
||
| 22 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Base command class. |
||
| 26 | */ |
||
| 27 | abstract class AbstractCommand extends BaseCommand |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Whatever "path" argument accepts repository urls. |
||
| 32 | * |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | protected $pathAcceptsUrl = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Repository connector |
||
| 39 | * |
||
| 40 | * @var Connector |
||
| 41 | */ |
||
| 42 | protected $repositoryConnector; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Working directory. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $workingDirectory = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Revision logs by url |
||
| 53 | * |
||
| 54 | * @var RevisionLog[] |
||
| 55 | */ |
||
| 56 | private $_revisionLogs = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Working copy paths. |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | private $_workingCopyPaths = array(); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Revision log factory. |
||
| 67 | * |
||
| 68 | * @var RevisionLogFactory |
||
| 69 | */ |
||
| 70 | private $_revisionLogFactory; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Config editor. |
||
| 74 | * |
||
| 75 | * @var ConfigEditor |
||
| 76 | */ |
||
| 77 | private $_configEditor; |
||
| 78 | |||
| 79 | /** |
||
|
2 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 80 | * {@inheritdoc} |
||
|
1 ignored issue
–
show
|
|||
| 81 | */ |
||
|
1 ignored issue
–
show
|
|||
| 82 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 83 | { |
||
| 84 | $output->getFormatter()->setStyle('debug', new OutputFormatterStyle('white', 'magenta')); |
||
| 85 | |||
| 86 | parent::initialize($input, $output); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Prepare dependencies. |
||
| 91 | * |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | protected function prepareDependencies() |
||
| 95 | { |
||
| 96 | parent::prepareDependencies(); |
||
| 97 | |||
| 98 | $container = $this->getContainer(); |
||
| 99 | |||
| 100 | $this->repositoryConnector = $container['repository_connector']; |
||
| 101 | $this->_revisionLogFactory = $container['revision_log_factory']; |
||
| 102 | $this->workingDirectory = $container['working_directory']; |
||
| 103 | $this->_configEditor = $container['config_editor']; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns command setting value. |
||
| 108 | * |
||
| 109 | * @param string $name Name. |
||
| 110 | * @param string|null $command_name Command name to get settings from instead of current command. |
||
| 111 | * |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | protected function getSetting($name, $command_name = null) |
||
| 115 | { |
||
| 116 | return $this->_getConfigSetting($name, $command_name)->getValue(); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sets command setting value. |
||
| 121 | * |
||
| 122 | * @param string $name Name. |
||
| 123 | * @param mixed $value Value. |
||
| 124 | * @param string|null $command_name Command name to get settings from instead of current command. |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | protected function setSetting($name, $value, $command_name = null) |
||
| 129 | { |
||
| 130 | $this->_getConfigSetting($name, $command_name)->setValue($value); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Validates command setting usage. |
||
| 135 | * |
||
| 136 | * @param string $name Name. |
||
| 137 | * @param string|null $command_name Command name to get settings from instead of current command. |
||
| 138 | * |
||
| 139 | * @return AbstractConfigSetting |
||
| 140 | * @throws \LogicException When command don't have any config settings to provide. |
||
| 141 | * @throws \LogicException When config setting is not found. |
||
| 142 | */ |
||
| 143 | private function _getConfigSetting($name, $command_name = null) |
||
| 144 | { |
||
| 145 | // By default access own config settings. |
||
| 146 | if ( !isset($command_name) ) { |
||
| 147 | $command_name = $this->getName(); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** @var IConfigAwareCommand $command */ |
||
| 151 | $command = $this->getApplication()->get($command_name); |
||
| 152 | |||
| 153 | if ( !($command instanceof IConfigAwareCommand) ) { |
||
| 154 | throw new \LogicException('The "' . $command_name . '" command does not have any settings.'); |
||
| 155 | } |
||
| 156 | |||
| 157 | foreach ( $command->getConfigSettings() as $config_setting ) { |
||
| 158 | if ( $config_setting->getName() === $name ) { |
||
| 159 | if ( $config_setting->isWithinScope(AbstractConfigSetting::SCOPE_WORKING_COPY) ) { |
||
| 160 | $config_setting->setWorkingCopyUrl($this->getWorkingCopyUrl()); |
||
| 161 | } |
||
| 162 | |||
| 163 | $config_setting->setEditor($this->_configEditor); |
||
| 164 | |||
| 165 | return $config_setting; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | throw new \LogicException('The "' . $command_name . '" command doesn\'t have "' . $name . '" config setting.'); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Prepare setting prefix. |
||
| 174 | * |
||
| 175 | * @param boolean $is_global Return global setting prefix. |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | protected function getConfigScope($is_global) |
||
| 180 | { |
||
| 181 | if ( $is_global ) { |
||
| 182 | return 'global-settings.'; |
||
| 183 | } |
||
| 184 | |||
| 185 | $wc_path = $this->getWorkingCopyPath(); |
||
| 186 | $wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path); |
||
| 187 | $wc_hash = substr(hash_hmac('sha1', $wc_url, 'svn-buddy'), 0, 8); |
||
| 188 | |||
| 189 | return 'path-settings.' . $wc_hash . '.'; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns revision log. |
||
| 194 | * |
||
| 195 | * @param string $repository_url Repository url. |
||
| 196 | * |
||
| 197 | * @return RevisionLog |
||
| 198 | */ |
||
| 199 | protected function getRevisionLog($repository_url) |
||
| 200 | { |
||
| 201 | if ( !isset($this->_revisionLogs[$repository_url]) ) { |
||
| 202 | $this->_revisionLogs[$repository_url] = $this->_revisionLogFactory->getRevisionLog( |
||
| 203 | $repository_url, |
||
| 204 | $this->io |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $this->_revisionLogs[$repository_url]; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Transforms string into list. |
||
| 213 | * |
||
| 214 | * @param string $string String. |
||
| 215 | * @param string $separator Separator. |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function getList($string, $separator = ',') |
||
| 220 | { |
||
| 221 | return array_filter(array_map('trim', explode($separator, $string))); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns URL to the working copy. |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | protected function getWorkingCopyUrl() |
||
| 230 | { |
||
| 231 | return $this->repositoryConnector->getWorkingCopyUrl($this->getWorkingCopyPath()); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return working copy path. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | * @throws \RuntimeException When folder isn't a working copy. |
||
| 239 | */ |
||
| 240 | protected function getWorkingCopyPath() |
||
| 241 | { |
||
| 242 | $path = $this->getPath(); |
||
| 243 | |||
| 244 | if ( !in_array($path, $this->_workingCopyPaths) ) { |
||
| 245 | if ( !$this->repositoryConnector->isUrl($path) |
||
| 246 | && !$this->repositoryConnector->isWorkingCopy($path) |
||
| 247 | ) { |
||
| 248 | throw new \RuntimeException('The "' . $path . '" isn\'t a working copy.'); |
||
| 249 | } |
||
| 250 | |||
| 251 | $this->_workingCopyPaths[] = $path; |
||
| 252 | } |
||
| 253 | |||
| 254 | return $path; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns all refs. |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | protected function getAllRefs() |
||
| 263 | { |
||
| 264 | $wc_url = $this->getWorkingCopyUrl(); |
||
| 265 | $revision_log = $this->getRevisionLog($wc_url); |
||
| 266 | |||
| 267 | return $revision_log->find('refs', 'all_refs'); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return working copy path. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | * @throws \RuntimeException When url was given instead of path. |
||
| 275 | */ |
||
| 276 | protected function getPath() |
||
| 277 | { |
||
| 278 | // During auto-complete the IO isn't set. |
||
| 279 | if ( !isset($this->io) ) { |
||
| 280 | $path = '.'; |
||
| 281 | } |
||
| 282 | else { |
||
| 283 | $path = $this->io->getArgument('path'); |
||
| 284 | } |
||
| 285 | |||
| 286 | if ( !$this->repositoryConnector->isUrl($path) ) { |
||
| 287 | $path = realpath($path); |
||
| 288 | } |
||
| 289 | elseif ( !$this->pathAcceptsUrl ) { |
||
| 290 | throw new \RuntimeException('The "path" argument must be a working copy path and not URL.'); |
||
| 291 | } |
||
| 292 | |||
| 293 | return $path; |
||
| 294 | } |
||
| 295 | |||
| 296 | } |
||
| 297 |