ConsoleTrait.php 4.4 KB

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