ConsoleTrait.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @package Grav\Console
  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;
  9. use Grav\Common\Cache;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Composer;
  12. use Grav\Common\GravTrait;
  13. use Grav\Console\Cli\ClearCacheCommand;
  14. use RocketTheme\Toolbox\File\YamlFile;
  15. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  16. use Symfony\Component\Console\Input\ArrayInput;
  17. use Symfony\Component\Console\Input\InputInterface;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. trait ConsoleTrait
  20. {
  21. protected $argv;
  22. /* @var InputInterface $output */
  23. protected $input;
  24. /* @var OutputInterface $output */
  25. protected $output;
  26. /** @var array */
  27. protected $local_config;
  28. /**
  29. * Set colors style definition for the formatter.
  30. *
  31. * @param InputInterface $input
  32. * @param OutputInterface $output
  33. */
  34. public function setupConsole(InputInterface $input, OutputInterface $output)
  35. {
  36. // Initialize cache with CLI compatibility
  37. Grav::instance()['config']->set('system.cache.cli_compatibility', true);
  38. Grav::instance()['cache'];
  39. $this->argv = $_SERVER['argv'][0];
  40. $this->input = $input;
  41. $this->output = $output;
  42. $this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
  43. $this->output->getFormatter()->setStyle('yellow', new OutputFormatterStyle('yellow', null, array('bold')));
  44. $this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
  45. $this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
  46. $this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
  47. $this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
  48. $this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
  49. }
  50. /**
  51. * @param string $path
  52. */
  53. public function isGravInstance($path)
  54. {
  55. if (!file_exists($path)) {
  56. $this->output->writeln('');
  57. $this->output->writeln("<red>ERROR</red>: Destination doesn't exist:");
  58. $this->output->writeln(" <white>$path</white>");
  59. $this->output->writeln('');
  60. exit;
  61. }
  62. if (!is_dir($path)) {
  63. $this->output->writeln('');
  64. $this->output->writeln("<red>ERROR</red>: Destination chosen to install is not a directory:");
  65. $this->output->writeln(" <white>$path</white>");
  66. $this->output->writeln('');
  67. exit;
  68. }
  69. if (!file_exists($path . DS . 'index.php') || !file_exists($path . DS . '.dependencies') || !file_exists($path . DS . 'system' . DS . 'config' . DS . 'system.yaml')) {
  70. $this->output->writeln('');
  71. $this->output->writeln('<red>ERROR</red>: Destination chosen to install does not appear to be a Grav instance:');
  72. $this->output->writeln(" <white>$path</white>");
  73. $this->output->writeln('');
  74. exit;
  75. }
  76. }
  77. public function composerUpdate($path, $action = 'install')
  78. {
  79. $composer = Composer::getComposerExecutor();
  80. return system($composer . ' --working-dir="'.$path.'" --no-interaction --no-dev --prefer-dist -o '. $action);
  81. }
  82. /**
  83. * @param array $all
  84. *
  85. * @return int
  86. * @throws \Exception
  87. */
  88. public function clearCache($all = [])
  89. {
  90. if ($all) {
  91. $all = ['--all' => true];
  92. }
  93. $command = new ClearCacheCommand();
  94. $input = new ArrayInput($all);
  95. return $command->run($input, $this->output);
  96. }
  97. public function invalidateCache()
  98. {
  99. Cache::invalidateCache();
  100. }
  101. /**
  102. * Load the local config file
  103. *
  104. * @return mixed string the local config file name. false if local config does not exist
  105. */
  106. public function loadLocalConfig()
  107. {
  108. $home_folder = getenv('HOME') ?: getenv('HOMEDRIVE') . getenv('HOMEPATH');
  109. $local_config_file = $home_folder . '/.grav/config';
  110. if (file_exists($local_config_file)) {
  111. $file = YamlFile::instance($local_config_file);
  112. $this->local_config = $file->content();
  113. $file->free();
  114. return $local_config_file;
  115. }
  116. return false;
  117. }
  118. }