| 1 | <?php |
||
| 29 | abstract class AbstractCommand extends BaseCommand |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Raw path. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $_rawPath; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Whatever "path" argument accepts repository urls. |
||
| 41 | * |
||
| 42 | * @var boolean |
||
| 43 | */ |
||
| 44 | protected $pathAcceptsUrl = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Repository connector |
||
| 48 | * |
||
| 49 | * @var Connector |
||
| 50 | */ |
||
| 51 | protected $repositoryConnector; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Working directory. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $workingDirectory = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Working copy resolver. |
||
| 62 | * |
||
| 63 | * @var WorkingCopyResolver |
||
| 64 | */ |
||
| 65 | private $_workingCopyResolver = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Revision log factory. |
||
| 69 | * |
||
| 70 | * @var RevisionLogFactory |
||
| 71 | */ |
||
| 72 | private $_revisionLogFactory; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Command config. |
||
| 76 | * |
||
| 77 | * @var CommandConfig |
||
| 78 | */ |
||
| 79 | private $_commandConfig; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Update manager. |
||
| 83 | * |
||
| 84 | * @var UpdateManager |
||
| 85 | */ |
||
| 86 | private $_updateManager; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | * |
||
| 91 | * @throws \RuntimeException When url was given instead of path. |
||
| 92 | */ |
||
| 93 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 94 | { |
||
| 95 | $this->_rawPath = null; |
||
| 96 | $output->getFormatter()->setStyle('debug', new OutputFormatterStyle('white', 'magenta')); |
||
| 97 | |||
| 98 | parent::initialize($input, $output); |
||
| 99 | |||
| 100 | // Only apply check for commands, that accept working copy path. |
||
| 101 | if ( $input->hasArgument('path') ) { |
||
| 102 | if ( !$this->pathAcceptsUrl && $this->repositoryConnector->isUrl($this->getRawPath()) ) { |
||
| 103 | throw new \RuntimeException('The "path" argument must be a working copy path and not URL.'); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | if ( $this->checkForAppUpdates($input) ) { |
||
| 108 | $this->_showAppUpdateBanner($output); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Allow showing update banner. |
||
| 114 | * |
||
| 115 | * @param InputInterface $input Input. |
||
| 116 | * |
||
| 117 | * @return boolean |
||
| 118 | */ |
||
| 119 | protected function checkForAppUpdates(InputInterface $input) |
||
| 120 | { |
||
| 121 | // Show update banner only for outer command invoked by user and not sub-commands. |
||
| 122 | return $input instanceof ArgvInput; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Shows application update banner. |
||
| 127 | * |
||
| 128 | * @param OutputInterface $output Output. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | private function _showAppUpdateBanner(OutputInterface $output) |
||
|
1 ignored issue
–
show
|
|||
| 133 | { |
||
| 134 | $new_version = $this->_updateManager->getNewVersion(); |
||
| 135 | |||
| 136 | if ( !strlen($new_version) ) { |
||
| 137 | return; |
||
| 138 | } |
||
| 139 | |||
| 140 | $message = sprintf( |
||
| 141 | ' Update available. Run "%s self-update" to upgrade. ', |
||
| 142 | $_SERVER['argv'][0] |
||
| 143 | ); |
||
| 144 | $line_length = mb_strlen($message); |
||
| 145 | |||
| 146 | $output->writeln(array( |
||
| 147 | '<fg=white;bg=blue>' . str_repeat(' ', $line_length) . '</>', |
||
| 148 | '<fg=white;bg=blue>' . $message . '</>', |
||
| 149 | '<fg=white;bg=blue>' . str_repeat(' ', $line_length) . '</>', |
||
| 150 | '', |
||
| 151 | )); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Prepare dependencies. |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | protected function prepareDependencies() |
||
| 160 | { |
||
| 161 | parent::prepareDependencies(); |
||
| 162 | |||
| 163 | $container = $this->getContainer(); |
||
| 164 | |||
| 165 | $this->_workingCopyResolver = $container['working_copy_resolver']; |
||
| 166 | $this->repositoryConnector = $container['repository_connector']; |
||
| 167 | $this->_revisionLogFactory = $container['revision_log_factory']; |
||
| 168 | $this->workingDirectory = $container['working_directory']; |
||
| 169 | $this->_commandConfig = $container['command_config']; |
||
| 170 | $this->_updateManager = $container['update_manager']; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns command setting value. |
||
| 175 | * |
||
| 176 | * @param string $name Name. |
||
| 177 | * |
||
| 178 | * @return mixed |
||
| 179 | */ |
||
| 180 | protected function getSetting($name) |
||
| 181 | { |
||
| 182 | return $this->_commandConfig->getSettingValue($name, $this, $this->getRawPath()); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Sets command setting value. |
||
| 187 | * |
||
| 188 | * @param string $name Name. |
||
| 189 | * @param mixed $value Value. |
||
| 190 | * |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | protected function setSetting($name, $value) |
||
| 194 | { |
||
| 195 | $this->_commandConfig->setSettingValue($name, $this, $this->getRawPath(), $value); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns revision log. |
||
| 200 | * |
||
| 201 | * @param string $repository_url Repository url. |
||
| 202 | * |
||
| 203 | * @return RevisionLog |
||
| 204 | */ |
||
| 205 | protected function getRevisionLog($repository_url) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Transforms string into list. |
||
| 212 | * |
||
| 213 | * @param string $string String. |
||
| 214 | * @param string $separator Separator. |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | protected function getList($string, $separator = ',') |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns URL to the working copy. |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | protected function getWorkingCopyUrl() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Return working copy path. |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | protected function getWorkingCopyPath() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Returns all refs. |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | protected function getAllRefs() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns working copy path as used specified it. |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | protected function getRawPath() |
||
| 275 | |||
| 276 | } |
||
| 277 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: