InfoCommand.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @package Grav\Console\Gpm
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Gpm;
  9. use Grav\Common\GPM\GPM;
  10. use Grav\Console\ConsoleCommand;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Question\ConfirmationQuestion;
  14. class InfoCommand extends ConsoleCommand
  15. {
  16. /** @var array */
  17. protected $data;
  18. /** @var GPM */
  19. protected $gpm;
  20. /** @var string */
  21. protected $all_yes;
  22. /**
  23. *
  24. */
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('info')
  29. ->addOption(
  30. 'force',
  31. 'f',
  32. InputOption::VALUE_NONE,
  33. 'Force fetching the new data remotely'
  34. )
  35. ->addOption(
  36. 'all-yes',
  37. 'y',
  38. InputOption::VALUE_NONE,
  39. 'Assumes yes (or best approach) instead of prompting'
  40. )
  41. ->addArgument(
  42. 'package',
  43. InputArgument::REQUIRED,
  44. 'The package of which more informations are desired. Use the "index" command for a list of packages'
  45. )
  46. ->setDescription('Shows more informations about a package')
  47. ->setHelp('The <info>info</info> shows more informations about a package');
  48. }
  49. /**
  50. * @return int|null|void
  51. */
  52. protected function serve()
  53. {
  54. $this->gpm = new GPM($this->input->getOption('force'));
  55. $this->all_yes = $this->input->getOption('all-yes');
  56. $this->displayGPMRelease();
  57. $foundPackage = $this->gpm->findPackage($this->input->getArgument('package'));
  58. if (!$foundPackage) {
  59. $this->output->writeln("The package <cyan>'{$this->input->getArgument('package')}'</cyan> was not found in the Grav repository.");
  60. $this->output->writeln('');
  61. $this->output->writeln('You can list all the available packages by typing:');
  62. $this->output->writeln(" <green>{$this->argv} index</green>");
  63. $this->output->writeln('');
  64. exit;
  65. }
  66. $this->output->writeln("Found package <cyan>'{$this->input->getArgument('package')}'</cyan> under the '<green>" . ucfirst($foundPackage->package_type) . "</green>' section");
  67. $this->output->writeln('');
  68. $this->output->writeln("<cyan>{$foundPackage->name}</cyan> [{$foundPackage->slug}]");
  69. $this->output->writeln(str_repeat('-', \strlen($foundPackage->name) + \strlen($foundPackage->slug) + 3));
  70. $this->output->writeln('<white>' . strip_tags($foundPackage->description_plain) . '</white>');
  71. $this->output->writeln('');
  72. $packageURL = '';
  73. if (isset($foundPackage->author['url'])) {
  74. $packageURL = '<' . $foundPackage->author['url'] . '>';
  75. }
  76. $this->output->writeln('<green>' . str_pad('Author',
  77. 12) . ':</green> ' . $foundPackage->author['name'] . ' <' . $foundPackage->author['email'] . '> ' . $packageURL);
  78. foreach ([
  79. 'version',
  80. 'keywords',
  81. 'date',
  82. 'homepage',
  83. 'demo',
  84. 'docs',
  85. 'guide',
  86. 'repository',
  87. 'bugs',
  88. 'zipball_url',
  89. 'license'
  90. ] as $info) {
  91. if (isset($foundPackage->{$info})) {
  92. $name = ucfirst($info);
  93. $data = $foundPackage->{$info};
  94. if ($info === 'zipball_url') {
  95. $name = 'Download';
  96. }
  97. if ($info === 'date') {
  98. $name = 'Last Update';
  99. $data = date('D, j M Y, H:i:s, P ', strtotime($data));
  100. }
  101. $name = str_pad($name, 12);
  102. $this->output->writeln("<green>{$name}:</green> {$data}");
  103. }
  104. }
  105. $type = rtrim($foundPackage->package_type, 's');
  106. $updatable = $this->gpm->{'is' . $type . 'Updatable'}($foundPackage->slug);
  107. $installed = $this->gpm->{'is' . $type . 'Installed'}($foundPackage->slug);
  108. // display current version if installed and different
  109. if ($installed && $updatable) {
  110. $local = $this->gpm->{'getInstalled'. $type}($foundPackage->slug);
  111. $this->output->writeln('');
  112. $this->output->writeln("Currently installed version: <magenta>{$local->version}</magenta>");
  113. $this->output->writeln('');
  114. }
  115. // display changelog information
  116. $questionHelper = $this->getHelper('question');
  117. $question = new ConfirmationQuestion('Would you like to read the changelog? [y|N] ',
  118. false);
  119. $answer = $this->all_yes ? true : $questionHelper->ask($this->input, $this->output, $question);
  120. if ($answer) {
  121. $changelog = $foundPackage->changelog;
  122. $this->output->writeln('');
  123. foreach ($changelog as $version => $log) {
  124. $title = $version . ' [' . $log['date'] . ']';
  125. $content = preg_replace_callback('/\d\.\s\[\]\(#(.*)\)/', function ($match) {
  126. return "\n" . ucfirst($match[1]) . ':';
  127. }, $log['content']);
  128. $this->output->writeln("<cyan>{$title}</cyan>");
  129. $this->output->writeln(str_repeat('-', \strlen($title)));
  130. $this->output->writeln($content);
  131. $this->output->writeln('');
  132. $question = new ConfirmationQuestion('Press [ENTER] to continue or [q] to quit ', true);
  133. $answer = $this->all_yes ? false : $questionHelper->ask($this->input, $this->output, $question);
  134. if (!$answer) {
  135. break;
  136. }
  137. $this->output->writeln('');
  138. }
  139. }
  140. $this->output->writeln('');
  141. if ($installed && $updatable) {
  142. $this->output->writeln('You can update this package by typing:');
  143. $this->output->writeln(" <green>{$this->argv} update</green> <cyan>{$foundPackage->slug}</cyan>");
  144. } else {
  145. $this->output->writeln("You can install this package by typing:");
  146. $this->output->writeln(" <green>{$this->argv} install</green> <cyan>{$foundPackage->slug}</cyan>");
  147. }
  148. $this->output->writeln('');
  149. }
  150. }