PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/src/Grav/Console/Gpm/UpdateCommand.php

https://gitlab.com/3dplex/3d-plex-main-site
PHP | 335 lines | 248 code | 39 blank | 48 comment | 33 complexity | a03467fc959f5216cd6884b410185fc1 MD5 | raw file
  1. <?php
  2. <<<<<<< HEAD
  3. =======
  4. /**
  5. * @package Grav.Console
  6. *
  7. * @copyright Copyright (C) 2014 - 2016 RocketTheme, LLC. All rights reserved.
  8. * @license MIT License; see LICENSE file for details.
  9. */
  10. >>>>>>> update grav cms
  11. namespace Grav\Console\Gpm;
  12. use Grav\Common\GPM\GPM;
  13. use Grav\Common\GPM\Installer;
  14. use Grav\Console\ConsoleCommand;
  15. use Symfony\Component\Console\Input\ArrayInput;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Question\ConfirmationQuestion;
  19. <<<<<<< HEAD
  20. /**
  21. * Class UpdateCommand
  22. * @package Grav\Console\Gpm
  23. */
  24. =======
  25. >>>>>>> update grav cms
  26. class UpdateCommand extends ConsoleCommand
  27. {
  28. /**
  29. * @var
  30. */
  31. protected $data;
  32. /**
  33. * @var
  34. */
  35. protected $extensions;
  36. /**
  37. * @var
  38. */
  39. protected $updatable;
  40. /**
  41. * @var
  42. */
  43. protected $destination;
  44. /**
  45. * @var
  46. */
  47. protected $file;
  48. /**
  49. * @var array
  50. */
  51. <<<<<<< HEAD
  52. protected $types = array('plugins', 'themes');
  53. =======
  54. protected $types = ['plugins', 'themes'];
  55. >>>>>>> update grav cms
  56. /**
  57. * @var GPM $gpm
  58. */
  59. protected $gpm;
  60. <<<<<<< HEAD
  61. =======
  62. protected $all_yes;
  63. protected $overwrite;
  64. >>>>>>> update grav cms
  65. /**
  66. *
  67. */
  68. protected function configure()
  69. {
  70. $this
  71. ->setName("update")
  72. ->addOption(
  73. 'force',
  74. 'f',
  75. InputOption::VALUE_NONE,
  76. 'Force re-fetching the data from remote'
  77. )
  78. ->addOption(
  79. 'destination',
  80. 'd',
  81. InputOption::VALUE_OPTIONAL,
  82. 'The grav instance location where the updates should be applied to. By default this would be where the grav cli has been launched from',
  83. GRAV_ROOT
  84. )
  85. ->addOption(
  86. 'all-yes',
  87. 'y',
  88. InputOption::VALUE_NONE,
  89. 'Assumes yes (or best approach) instead of prompting'
  90. )
  91. <<<<<<< HEAD
  92. =======
  93. ->addOption(
  94. 'overwrite',
  95. 'o',
  96. InputOption::VALUE_NONE,
  97. 'Option to overwrite packages if they already exist'
  98. )
  99. ->addOption(
  100. 'plugins',
  101. 'p',
  102. InputOption::VALUE_NONE,
  103. 'Update only plugins'
  104. )
  105. ->addOption(
  106. 'themes',
  107. 't',
  108. InputOption::VALUE_NONE,
  109. 'Update only themes'
  110. )
  111. >>>>>>> update grav cms
  112. ->addArgument(
  113. 'package',
  114. InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
  115. 'The package or packages that is desired to update. By default all available updates will be applied.'
  116. )
  117. ->setDescription("Detects and performs an update of plugins and themes when available")
  118. ->setHelp('The <info>update</info> command updates plugins and themes when a new version is available');
  119. }
  120. /**
  121. * @return int|null|void
  122. */
  123. protected function serve()
  124. {
  125. $this->gpm = new GPM($this->input->getOption('force'));
  126. <<<<<<< HEAD
  127. $this->destination = realpath($this->input->getOption('destination'));
  128. $skip_prompt = $this->input->getOption('all-yes');
  129. =======
  130. $this->all_yes = $this->input->getOption('all-yes');
  131. $this->overwrite = $this->input->getOption('overwrite');
  132. $this->displayGPMRelease();
  133. $this->destination = realpath($this->input->getOption('destination'));
  134. >>>>>>> update grav cms
  135. if (!Installer::isGravInstance($this->destination)) {
  136. $this->output->writeln("<red>ERROR</red>: " . Installer::lastErrorMsg());
  137. exit;
  138. }
  139. <<<<<<< HEAD
  140. $this->data = $this->gpm->getUpdatable();
  141. $only_packages = array_map('strtolower', $this->input->getArgument('package'));
  142. if (!$this->data['total']) {
  143. =======
  144. if ($this->input->getOption('plugins') === false && $this->input->getOption('themes') === false) {
  145. $list_type = ['plugins' => true, 'themes' => true];
  146. } else {
  147. $list_type['plugins'] = $this->input->getOption('plugins');
  148. $list_type['themes'] = $this->input->getOption('themes');
  149. }
  150. if ($this->overwrite) {
  151. $this->data = $this->gpm->getInstallable($list_type);
  152. $description = " can be overwritten";
  153. } else {
  154. $this->data = $this->gpm->getUpdatable($list_type);
  155. $description = " need updating";
  156. }
  157. $only_packages = array_map('strtolower', $this->input->getArgument('package'));
  158. if (!$this->overwrite && !$this->data['total']) {
  159. >>>>>>> update grav cms
  160. $this->output->writeln("Nothing to update.");
  161. exit;
  162. }
  163. <<<<<<< HEAD
  164. $this->output->write("Found <green>" . $this->gpm->countInstalled() . "</green> extensions installed of which <magenta>" . $this->data['total'] . "</magenta> need updating");
  165. =======
  166. $this->output->write("Found <green>" . $this->gpm->countInstalled() . "</green> packages installed of which <magenta>" . $this->data['total'] . "</magenta>" . $description);
  167. >>>>>>> update grav cms
  168. $limit_to = $this->userInputPackages($only_packages);
  169. $this->output->writeln('');
  170. unset($this->data['total']);
  171. unset($limit_to['total']);
  172. // updates review
  173. $slugs = [];
  174. $index = 0;
  175. foreach ($this->data as $packages) {
  176. foreach ($packages as $slug => $package) {
  177. if (count($limit_to) && !array_key_exists($slug, $limit_to)) {
  178. continue;
  179. }
  180. <<<<<<< HEAD
  181. =======
  182. if (!$package->available) {
  183. $package->available = $package->version;
  184. }
  185. >>>>>>> update grav cms
  186. $this->output->writeln(
  187. // index
  188. str_pad($index++ + 1, 2, '0', STR_PAD_LEFT) . ". " .
  189. // name
  190. "<cyan>" . str_pad($package->name, 15) . "</cyan> " .
  191. // version
  192. <<<<<<< HEAD
  193. "[v<magenta>" . $package->version . "</magenta> ➜ v<green>" . $package->available . "</green>]"
  194. =======
  195. "[v<magenta>" . $package->version . "</magenta> -> v<green>" . $package->available . "</green>]"
  196. >>>>>>> update grav cms
  197. );
  198. $slugs[] = $slug;
  199. }
  200. }
  201. <<<<<<< HEAD
  202. if (!$skip_prompt) {
  203. =======
  204. if (!$this->all_yes) {
  205. >>>>>>> update grav cms
  206. // prompt to continue
  207. $this->output->writeln("");
  208. $questionHelper = $this->getHelper('question');
  209. $question = new ConfirmationQuestion("Continue with the update process? [Y|n] ", true);
  210. $answer = $questionHelper->ask($this->input, $this->output, $question);
  211. if (!$answer) {
  212. <<<<<<< HEAD
  213. $this->output->writeln("Update aborted. Exiting...");
  214. =======
  215. $this->output->writeln("<red>Update aborted. Exiting...</red>");
  216. >>>>>>> update grav cms
  217. exit;
  218. }
  219. }
  220. // finally update
  221. $install_command = $this->getApplication()->find('install');
  222. <<<<<<< HEAD
  223. $args = new ArrayInput(array(
  224. 'command' => 'install',
  225. 'package' => $slugs,
  226. '-f' => $this->input->getOption('force'),
  227. '-d' => $this->destination,
  228. '-y' => true
  229. ));
  230. $command_exec = $install_command->run($args, $this->output);
  231. if ($command_exec != 0) {
  232. $this->output->writeln("<red>Error:</red> An error occurred while trying to install the extensions");
  233. =======
  234. $args = new ArrayInput([
  235. 'command' => 'install',
  236. 'package' => $slugs,
  237. '-f' => $this->input->getOption('force'),
  238. '-d' => $this->destination,
  239. '-y' => true
  240. ]);
  241. $command_exec = $install_command->run($args, $this->output);
  242. if ($command_exec != 0) {
  243. $this->output->writeln("<red>Error:</red> An error occurred while trying to install the packages");
  244. >>>>>>> update grav cms
  245. exit;
  246. }
  247. }
  248. /**
  249. * @param $only_packages
  250. *
  251. * @return array
  252. */
  253. private function userInputPackages($only_packages)
  254. {
  255. $found = ['total' => 0];
  256. $ignore = [];
  257. if (!count($only_packages)) {
  258. $this->output->writeln('');
  259. } else {
  260. foreach ($only_packages as $only_package) {
  261. $find = $this->gpm->findPackage($only_package);
  262. <<<<<<< HEAD
  263. if (!$find || !$this->gpm->isUpdatable($find->slug)) {
  264. =======
  265. if (!$find || (!$this->overwrite && !$this->gpm->isUpdatable($find->slug))) {
  266. >>>>>>> update grav cms
  267. $name = isset($find->slug) ? $find->slug : $only_package;
  268. $ignore[$name] = $name;
  269. } else {
  270. $found[$find->slug] = $find;
  271. $found['total']++;
  272. }
  273. }
  274. if ($found['total']) {
  275. $list = $found;
  276. unset($list['total']);
  277. $list = array_keys($list);
  278. if ($found['total'] !== $this->data['total']) {
  279. $this->output->write(", only <magenta>" . $found['total'] . "</magenta> will be updated");
  280. }
  281. $this->output->writeln('');
  282. $this->output->writeln("Limiting updates for only <cyan>" . implode('</cyan>, <cyan>',
  283. $list) . "</cyan>");
  284. }
  285. if (count($ignore)) {
  286. <<<<<<< HEAD
  287. =======
  288. $this->output->writeln('');
  289. >>>>>>> update grav cms
  290. $this->output->writeln("Packages not found or not requiring updates: <red>" . implode('</red>, <red>',
  291. $ignore) . "</red>");
  292. }
  293. }
  294. return $found;
  295. }
  296. }