ConsoleTrait.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * @package Grav\Console
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console;
  9. use Exception;
  10. use Grav\Common\Cache;
  11. use Grav\Common\Grav;
  12. use Grav\Common\Composer;
  13. use Grav\Common\Language\Language;
  14. use Grav\Common\Processors\InitializeProcessor;
  15. use Grav\Console\Cli\ClearCacheCommand;
  16. use RocketTheme\Toolbox\Event\Event;
  17. use RocketTheme\Toolbox\File\YamlFile;
  18. use Symfony\Component\Console\Exception\InvalidArgumentException;
  19. use Symfony\Component\Console\Input\ArrayInput;
  20. use Symfony\Component\Console\Input\InputInterface;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\Console\Style\SymfonyStyle;
  23. /**
  24. * Trait ConsoleTrait
  25. * @package Grav\Console
  26. */
  27. trait ConsoleTrait
  28. {
  29. /** @var string */
  30. protected $argv;
  31. /** @var InputInterface */
  32. protected $input;
  33. /** @var SymfonyStyle */
  34. protected $output;
  35. /** @var array */
  36. protected $local_config;
  37. /** @var bool */
  38. private $plugins_initialized = false;
  39. /** @var bool */
  40. private $themes_initialized = false;
  41. /** @var bool */
  42. private $pages_initialized = false;
  43. /**
  44. * Set colors style definition for the formatter.
  45. *
  46. * @param InputInterface $input
  47. * @param OutputInterface $output
  48. * @return void
  49. */
  50. public function setupConsole(InputInterface $input, OutputInterface $output)
  51. {
  52. $this->argv = $_SERVER['argv'][0];
  53. $this->input = $input;
  54. $this->output = new SymfonyStyle($input, $output);
  55. $this->setupGrav();
  56. }
  57. public function getInput(): InputInterface
  58. {
  59. return $this->input;
  60. }
  61. /**
  62. * @return SymfonyStyle
  63. */
  64. public function getIO(): SymfonyStyle
  65. {
  66. return $this->output;
  67. }
  68. /**
  69. * Adds an option.
  70. *
  71. * @param string $name The option name
  72. * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
  73. * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
  74. * @param string $description A description text
  75. * @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
  76. * @return $this
  77. * @throws InvalidArgumentException If option mode is invalid or incompatible
  78. */
  79. public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
  80. {
  81. if ($name !== 'env' && $name !== 'lang') {
  82. parent::addOption($name, $shortcut, $mode, $description, $default);
  83. }
  84. return $this;
  85. }
  86. /**
  87. * @return void
  88. */
  89. final protected function setupGrav(): void
  90. {
  91. try {
  92. $language = $this->input->getOption('lang');
  93. if ($language) {
  94. // Set used language.
  95. $this->setLanguage($language);
  96. }
  97. } catch (InvalidArgumentException $e) {}
  98. // Initialize cache with CLI compatibility
  99. $grav = Grav::instance();
  100. $grav['config']->set('system.cache.cli_compatibility', true);
  101. }
  102. /**
  103. * Initialize Grav.
  104. *
  105. * - Load configuration
  106. * - Initialize logger
  107. * - Disable debugger
  108. * - Set timezone, locale
  109. * - Load plugins (call PluginsLoadedEvent)
  110. * - Set Pages and Users type to be used in the site
  111. *
  112. * Safe to be called multiple times.
  113. *
  114. * @return $this
  115. */
  116. final protected function initializeGrav()
  117. {
  118. InitializeProcessor::initializeCli(Grav::instance());
  119. return $this;
  120. }
  121. /**
  122. * Set language to be used in CLI.
  123. *
  124. * @param string|null $code
  125. * @return $this
  126. */
  127. final protected function setLanguage(string $code = null)
  128. {
  129. $this->initializeGrav();
  130. $grav = Grav::instance();
  131. /** @var Language $language */
  132. $language = $grav['language'];
  133. if ($language->enabled()) {
  134. if ($code && $language->validate($code)) {
  135. $language->setActive($code);
  136. } else {
  137. $language->setActive($language->getDefault());
  138. }
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Properly initialize plugins.
  144. *
  145. * - call $this->initializeGrav()
  146. * - call onPluginsInitialized event
  147. *
  148. * Safe to be called multiple times.
  149. *
  150. * @return $this
  151. */
  152. final protected function initializePlugins()
  153. {
  154. if (!$this->plugins_initialized) {
  155. $this->plugins_initialized = true;
  156. $this->initializeGrav();
  157. // Initialize plugins.
  158. $grav = Grav::instance();
  159. $grav['plugins']->init();
  160. $grav->fireEvent('onPluginsInitialized');
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Properly initialize themes.
  166. *
  167. * - call $this->initializePlugins()
  168. * - initialize theme (call onThemeInitialized event)
  169. *
  170. * Safe to be called multiple times.
  171. *
  172. * @return $this
  173. */
  174. final protected function initializeThemes()
  175. {
  176. if (!$this->themes_initialized) {
  177. $this->themes_initialized = true;
  178. $this->initializePlugins();
  179. // Initialize themes.
  180. $grav = Grav::instance();
  181. $grav['themes']->init();
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Properly initialize pages.
  187. *
  188. * - call $this->initializeThemes()
  189. * - initialize assets (call onAssetsInitialized event)
  190. * - initialize twig (calls the twig events)
  191. * - initialize pages (calls onPagesInitialized event)
  192. *
  193. * Safe to be called multiple times.
  194. *
  195. * @return $this
  196. */
  197. final protected function initializePages()
  198. {
  199. if (!$this->pages_initialized) {
  200. $this->pages_initialized = true;
  201. $this->initializeThemes();
  202. $grav = Grav::instance();
  203. // Initialize assets.
  204. $grav['assets']->init();
  205. $grav->fireEvent('onAssetsInitialized');
  206. // Initialize twig.
  207. $grav['twig']->init();
  208. // Initialize pages.
  209. $pages = $grav['pages'];
  210. $pages->init();
  211. $grav->fireEvent('onPagesInitialized', new Event(['pages' => $pages]));
  212. }
  213. return $this;
  214. }
  215. /**
  216. * @param string $path
  217. * @return void
  218. */
  219. public function isGravInstance($path)
  220. {
  221. $io = $this->getIO();
  222. if (!file_exists($path)) {
  223. $io->writeln('');
  224. $io->writeln("<red>ERROR</red>: Destination doesn't exist:");
  225. $io->writeln(" <white>$path</white>");
  226. $io->writeln('');
  227. exit;
  228. }
  229. if (!is_dir($path)) {
  230. $io->writeln('');
  231. $io->writeln("<red>ERROR</red>: Destination chosen to install is not a directory:");
  232. $io->writeln(" <white>$path</white>");
  233. $io->writeln('');
  234. exit;
  235. }
  236. if (!file_exists($path . DS . 'index.php') || !file_exists($path . DS . '.dependencies') || !file_exists($path . DS . 'system' . DS . 'config' . DS . 'system.yaml')) {
  237. $io->writeln('');
  238. $io->writeln('<red>ERROR</red>: Destination chosen to install does not appear to be a Grav instance:');
  239. $io->writeln(" <white>$path</white>");
  240. $io->writeln('');
  241. exit;
  242. }
  243. }
  244. /**
  245. * @param string $path
  246. * @param string $action
  247. * @return string|false
  248. */
  249. public function composerUpdate($path, $action = 'install')
  250. {
  251. $composer = Composer::getComposerExecutor();
  252. return system($composer . ' --working-dir=' . escapeshellarg($path) . ' --no-interaction --no-dev --prefer-dist -o '. $action);
  253. }
  254. /**
  255. * @param array $all
  256. * @return int
  257. * @throws Exception
  258. */
  259. public function clearCache($all = [])
  260. {
  261. if ($all) {
  262. $all = ['--all' => true];
  263. }
  264. $command = new ClearCacheCommand();
  265. $input = new ArrayInput($all);
  266. return $command->run($input, $this->output);
  267. }
  268. /**
  269. * @return void
  270. */
  271. public function invalidateCache()
  272. {
  273. Cache::invalidateCache();
  274. }
  275. /**
  276. * Load the local config file
  277. *
  278. * @return string|false The local config file name. false if local config does not exist
  279. */
  280. public function loadLocalConfig()
  281. {
  282. $home_folder = getenv('HOME') ?: getenv('HOMEDRIVE') . getenv('HOMEPATH');
  283. $local_config_file = $home_folder . '/.grav/config';
  284. if (file_exists($local_config_file)) {
  285. $file = YamlFile::instance($local_config_file);
  286. $this->local_config = $file->content();
  287. $file->free();
  288. return $local_config_file;
  289. }
  290. return false;
  291. }
  292. }