ConsoleTrait.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Grav\Console;
  3. use Grav\Common\GravTrait;
  4. use Grav\Common\Composer;
  5. use Grav\Console\Cli\ClearCacheCommand;
  6. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  7. use Symfony\Component\Console\Input\ArrayInput;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. /**
  11. * Class ConsoleTrait
  12. * @package Grav\Console
  13. */
  14. trait ConsoleTrait
  15. {
  16. use GravTrait;
  17. /**
  18. * @var
  19. */
  20. protected $argv;
  21. /* @var InputInterface $output */
  22. protected $input;
  23. /* @var OutputInterface $output */
  24. protected $output;
  25. /**
  26. * Set colors style definition for the formatter.
  27. *
  28. * @param InputInterface $input
  29. * @param OutputInterface $output
  30. */
  31. public function setupConsole(InputInterface $input, OutputInterface $output)
  32. {
  33. if (self::getGrav()) {
  34. self::getGrav()['config']->set('system.cache.driver', 'default');
  35. }
  36. $this->argv = $_SERVER['argv'][0];
  37. $this->input = $input;
  38. $this->output = $output;
  39. $this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
  40. $this->output->getFormatter()->setStyle('yellow', new OutputFormatterStyle('yellow', null, array('bold')));
  41. $this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
  42. $this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
  43. $this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
  44. $this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
  45. $this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
  46. }
  47. /**
  48. * @param $path
  49. */
  50. public function isGravInstance($path)
  51. {
  52. if (!file_exists($path)) {
  53. $this->output->writeln('');
  54. $this->output->writeln("<red>ERROR</red>: Destination doesn't exist:");
  55. $this->output->writeln(" <white>$path</white>");
  56. $this->output->writeln('');
  57. exit;
  58. }
  59. if (!is_dir($path)) {
  60. $this->output->writeln('');
  61. $this->output->writeln("<red>ERROR</red>: Destination chosen to install is not a directory:");
  62. $this->output->writeln(" <white>$path</white>");
  63. $this->output->writeln('');
  64. exit;
  65. }
  66. if (!file_exists($path . DS . 'index.php') || !file_exists($path . DS . '.dependencies') || !file_exists($path . DS . 'system' . DS . 'config' . DS . 'system.yaml')) {
  67. $this->output->writeln('');
  68. $this->output->writeln("<red>ERROR</red>: Destination chosen to install does not appear to be a Grav instance:");
  69. $this->output->writeln(" <white>$path</white>");
  70. $this->output->writeln('');
  71. exit;
  72. }
  73. }
  74. public function composerUpdate($path, $action = 'install')
  75. {
  76. $composer = Composer::getComposerExecutor();
  77. return system($composer . ' --working-dir="'.$path.'" --no-interaction --no-dev --prefer-dist -o '. $action);
  78. }
  79. /**
  80. * @param array $all
  81. *
  82. * @return int
  83. * @throws \Exception
  84. */
  85. public function clearCache($all = [])
  86. {
  87. if ($all) {
  88. $all = ['--all' => true];
  89. }
  90. $command = new ClearCacheCommand();
  91. $input = new ArrayInput($all);
  92. return $command->run($input, $this->output);
  93. }
  94. /**
  95. * Validate if the system is based on windows or not.
  96. *
  97. * @return bool
  98. */
  99. public function isWindows()
  100. {
  101. $keys = [
  102. 'CYGWIN_NT-5.1',
  103. 'WIN32',
  104. 'WINNT',
  105. 'Windows'
  106. ];
  107. return array_key_exists(PHP_OS, $keys);
  108. }
  109. }