ConsoleCommand.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Grav;
  10. use Grav\Common\Language\Language;
  11. use Grav\Common\Page\Page;
  12. use Grav\Common\Processors\InitializeProcessor;
  13. use RocketTheme\Toolbox\Event\Event;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class ConsoleCommand extends Command
  18. {
  19. use ConsoleTrait;
  20. /** @var bool */
  21. private $plugins_initialized = false;
  22. /** @var bool */
  23. private $themes_initialized = false;
  24. /** @var bool */
  25. private $pages_initialized = false;
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. *
  30. * @return int|null|void
  31. */
  32. protected function execute(InputInterface $input, OutputInterface $output)
  33. {
  34. $this->setupConsole($input, $output);
  35. $this->serve();
  36. }
  37. /**
  38. * Override with your implementation.
  39. */
  40. protected function serve()
  41. {
  42. }
  43. /**
  44. * Initialize Grav.
  45. *
  46. * - Load configuration
  47. * - Disable debugger
  48. * - Set timezone, locale
  49. * - Load plugins
  50. * - Set Users type to be used in the site
  51. *
  52. * Safe to be called multiple times.
  53. *
  54. * @return $this
  55. */
  56. final protected function initializeGrav()
  57. {
  58. InitializeProcessor::initializeCli(Grav::instance());
  59. return $this;
  60. }
  61. /**
  62. * Set language to be used in CLI.
  63. *
  64. * @param string|null $code
  65. */
  66. final protected function setLanguage(string $code = null)
  67. {
  68. $this->initializeGrav();
  69. $grav = Grav::instance();
  70. /** @var Language $language */
  71. $language = $grav['language'];
  72. if ($language->enabled()) {
  73. if ($code && $language->validate($code)) {
  74. $language->setActive($code);
  75. } else {
  76. $language->setActive($language->getDefault());
  77. }
  78. }
  79. }
  80. /**
  81. * Properly initialize plugins.
  82. *
  83. * - call $this->initializeGrav()
  84. * - call onPluginsInitialized event
  85. *
  86. * Safe to be called multiple times.
  87. *
  88. * @return $this
  89. */
  90. final protected function initializePlugins()
  91. {
  92. if (!$this->plugins_initialized) {
  93. $this->plugins_initialized = true;
  94. $this->initializeGrav();
  95. // Initialize plugins.
  96. $grav = Grav::instance();
  97. $grav->fireEvent('onPluginsInitialized');
  98. }
  99. return $this;
  100. }
  101. /**
  102. * Properly initialize themes.
  103. *
  104. * - call $this->initializePlugins()
  105. * - initialize theme (call onThemeInitialized event)
  106. *
  107. * Safe to be called multiple times.
  108. *
  109. * @return $this
  110. */
  111. final protected function initializeThemes()
  112. {
  113. if (!$this->themes_initialized) {
  114. $this->themes_initialized = true;
  115. $this->initializePlugins();
  116. // Initialize themes.
  117. $grav = Grav::instance();
  118. $grav['themes']->init();
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Properly initialize pages.
  124. *
  125. * - call $this->initializeThemes()
  126. * - initialize assets (call onAssetsInitialized event)
  127. * - initialize twig (calls the twig events)
  128. * - initialize pages (calls onPagesInitialized event)
  129. *
  130. * Safe to be called multiple times.
  131. *
  132. * @return $this
  133. */
  134. final protected function initializePages()
  135. {
  136. if (!$this->pages_initialized) {
  137. $this->pages_initialized = true;
  138. $this->initializeThemes();
  139. $grav = Grav::instance();
  140. // Initialize assets.
  141. $grav['assets']->init();
  142. $grav->fireEvent('onAssetsInitialized');
  143. // Initialize twig.
  144. $grav['twig']->init();
  145. // Initialize pages.
  146. $pages = $grav['pages'];
  147. $pages->init();
  148. $grav->fireEvent('onPagesInitialized', new Event(['pages' => $pages]));
  149. }
  150. return $this;
  151. }
  152. protected function displayGPMRelease()
  153. {
  154. $this->output->writeln('');
  155. $this->output->writeln('GPM Releases Configuration: <yellow>' . ucfirst(Grav::instance()['config']->get('system.gpm.releases')) . '</yellow>');
  156. $this->output->writeln('');
  157. }
  158. }