|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Console\Command; |
| 4 | +use Symfony\Component\Console\Input\InputOption; |
| 5 | +use Symfony\Component\Console\Input\InputArgument; |
| 6 | +use Symfony\Component\Console\Output\StreamOutput; |
| 7 | + |
| 8 | +class UpdateForumCommand extends Command { |
| 9 | + |
| 10 | + /** |
| 11 | + * The console command name. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $name = 'syntax:update-forum'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Perform any steps needed for syntax\chat updates.'; |
| 23 | + |
| 24 | + /** |
| 25 | + * The output stream for any artisan commands |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + protected $stream; |
| 30 | + |
| 31 | + /** |
| 32 | + * Create a new command instance. |
| 33 | + * |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + public function __construct() |
| 37 | + { |
| 38 | + parent::__construct(); |
| 39 | + |
| 40 | + $this->stream = fopen('php://output', 'w'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Execute the console command. |
| 45 | + * |
| 46 | + * @return mixed |
| 47 | + */ |
| 48 | + public function fire() |
| 49 | + { |
| 50 | + // Handle Forum |
| 51 | + if ($this->confirm('Do you want to update Syntax\Forum? [yes|no]')) { |
| 52 | + $requestedVersion = $this->ask('What version of forum do you require? [Hit enter to leave as dev-master]', 'dev-master'); |
| 53 | + |
| 54 | + $this->comment('Running composer update...'); |
| 55 | + $this->composerUpdate($requestedVersion); |
| 56 | + |
| 57 | + $currentVersion = Config::get('forum::version'); |
| 58 | + $newVersion = Syntax\Forum\ForumServiceProvider::version; |
| 59 | + |
| 60 | + $this->comment('Checking for update steps to perform...'); |
| 61 | + $this->update($currentVersion, $newVersion); |
| 62 | + |
| 63 | + $this->comment('Updating the version in the config...'); |
| 64 | + $this->updateVersion($newVersion); |
| 65 | + } |
| 66 | + |
| 67 | + $this->comment('Done!'); |
| 68 | + } |
| 69 | + |
| 70 | + /******************************************************************** |
| 71 | + * Update Methods |
| 72 | + *******************************************************************/ |
| 73 | + public function update($start, $end) |
| 74 | + { |
| 75 | + $versionRange = range($this->cleanVersion($start), $this->cleanVersion($end)); |
| 76 | + |
| 77 | + if (count($versionRange) == 1) { |
| 78 | + $this->comment('Already on the latest version: '. $end); |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + $performedUpdate = false; |
| 83 | + |
| 84 | + foreach ($versionRange as $version) { |
| 85 | + if ($version == $start) { |
| 86 | + $previousVersion = $version; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $method = 'update'. $previousVersion .'To'. $version; |
| 91 | + |
| 92 | + if (method_exists($this, $method)) { |
| 93 | + $performedUpdate = true; |
| 94 | + |
| 95 | + $this->comment('Updating from version '. $previousVersion .' to '. $version .'...'); |
| 96 | + } |
| 97 | + |
| 98 | + $previousVersion = $version; |
| 99 | + } |
| 100 | + |
| 101 | + if ($performedUpdate === false) { |
| 102 | + $this->comment('No updates needed...'); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /******************************************************************** |
| 107 | + * Helper Methods |
| 108 | + *******************************************************************/ |
| 109 | + protected function composerUpdate($version) |
| 110 | + { |
| 111 | + $commands = [ |
| 112 | + 'cd '. base_path(), |
| 113 | + 'composer update syntax/forum:'. $version, |
| 114 | + ]; |
| 115 | + |
| 116 | + SSH::run($commands, function ($line) { |
| 117 | + echo $line.PHP_EOL; |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + protected function cleanVersion($version) |
| 122 | + { |
| 123 | + return str_replace('.', '', $version); |
| 124 | + } |
| 125 | + |
| 126 | + protected function updateVersion($version) |
| 127 | + { |
| 128 | + list($path, $contents) = $this->getConfig('packages/syntax/forum/config.php'); |
| 129 | + |
| 130 | + $contents = str_replace($this->laravel['config']['forum::version'], $version, $contents); |
| 131 | + |
| 132 | + File::put($path, $contents); |
| 133 | + } |
| 134 | + |
| 135 | + /******************************************************************** |
| 136 | + * Extra Methods |
| 137 | + *******************************************************************/ |
| 138 | + protected function getConfig($file) |
| 139 | + { |
| 140 | + $path = $this->laravel['path'].'/config/'. $file; |
| 141 | + |
| 142 | + $contents = File::get($path); |
| 143 | + |
| 144 | + return array($path, $contents); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get the console command arguments. |
| 149 | + * |
| 150 | + * @return array |
| 151 | + */ |
| 152 | + protected function getArguments() |
| 153 | + { |
| 154 | + return array( |
| 155 | + // array('example', InputArgument::REQUIRED, 'An example argument.'), |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Get the console command options. |
| 161 | + * |
| 162 | + * @return array |
| 163 | + */ |
| 164 | + protected function getOptions() |
| 165 | + { |
| 166 | + return array( |
| 167 | + // array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null), |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments