1
0

Application.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Console;
  8. use OC\MemoryInfo;
  9. use OC\NeedsUpdateException;
  10. use OCP\App\AppPathNotFoundException;
  11. use OCP\App\IAppManager;
  12. use OCP\Console\ConsoleEvent;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IConfig;
  15. use OCP\IRequest;
  16. use Psr\Container\ContainerExceptionInterface;
  17. use Psr\Log\LoggerInterface;
  18. use Symfony\Component\Console\Application as SymfonyApplication;
  19. use Symfony\Component\Console\Input\InputInterface;
  20. use Symfony\Component\Console\Input\InputOption;
  21. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. class Application {
  24. private SymfonyApplication $application;
  25. public function __construct(
  26. private IConfig $config,
  27. private IEventDispatcher $dispatcher,
  28. private IRequest $request,
  29. private LoggerInterface $logger,
  30. private MemoryInfo $memoryInfo,
  31. private IAppManager $appManager,
  32. ) {
  33. $defaults = \OC::$server->get('ThemingDefaults');
  34. $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
  35. }
  36. /**
  37. * @throws \Exception
  38. */
  39. public function loadCommands(
  40. InputInterface $input,
  41. ConsoleOutputInterface $output
  42. ) {
  43. // $application is required to be defined in the register_command scripts
  44. $application = $this->application;
  45. $inputDefinition = $application->getDefinition();
  46. $inputDefinition->addOption(
  47. new InputOption(
  48. 'no-warnings',
  49. null,
  50. InputOption::VALUE_NONE,
  51. 'Skip global warnings, show command output only',
  52. null
  53. )
  54. );
  55. try {
  56. $input->bind($inputDefinition);
  57. } catch (\RuntimeException $e) {
  58. //expected if there are extra options
  59. }
  60. if ($input->getOption('no-warnings')) {
  61. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  62. }
  63. if ($this->memoryInfo->isMemoryLimitSufficient() === false) {
  64. $output->getErrorOutput()->writeln(
  65. '<comment>The current PHP memory limit ' .
  66. 'is below the recommended value of 512MB.</comment>'
  67. );
  68. }
  69. try {
  70. require_once __DIR__ . '/../../../core/register_command.php';
  71. if ($this->config->getSystemValueBool('installed', false)) {
  72. if (\OCP\Util::needUpgrade()) {
  73. throw new NeedsUpdateException();
  74. } elseif ($this->config->getSystemValueBool('maintenance')) {
  75. $this->writeMaintenanceModeInfo($input, $output);
  76. } else {
  77. $this->appManager->loadApps();
  78. foreach ($this->appManager->getInstalledApps() as $app) {
  79. try {
  80. $appPath = $this->appManager->getAppPath($app);
  81. } catch (AppPathNotFoundException) {
  82. continue;
  83. }
  84. // load commands using info.xml
  85. $info = $this->appManager->getAppInfo($app);
  86. if (isset($info['commands'])) {
  87. try {
  88. $this->loadCommandsFromInfoXml($info['commands']);
  89. } catch (\Throwable $e) {
  90. $output->writeln("<error>" . $e->getMessage() . "</error>");
  91. $this->logger->error($e->getMessage(), [
  92. 'exception' => $e,
  93. ]);
  94. }
  95. }
  96. // load from register_command.php
  97. \OC_App::registerAutoloading($app, $appPath);
  98. $file = $appPath . '/appinfo/register_command.php';
  99. if (file_exists($file)) {
  100. try {
  101. require $file;
  102. } catch (\Exception $e) {
  103. $this->logger->error($e->getMessage(), [
  104. 'exception' => $e,
  105. ]);
  106. }
  107. }
  108. }
  109. }
  110. } elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') {
  111. $errorOutput = $output->getErrorOutput();
  112. $errorOutput->writeln("Nextcloud is not installed - only a limited number of commands are available");
  113. }
  114. } catch (NeedsUpdateException $e) {
  115. if ($input->getArgument('command') !== '_completion') {
  116. $errorOutput = $output->getErrorOutput();
  117. $errorOutput->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  118. $errorOutput->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  119. }
  120. }
  121. if ($input->getFirstArgument() !== 'check') {
  122. $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
  123. if (!empty($errors)) {
  124. foreach ($errors as $error) {
  125. $output->writeln((string)$error['error']);
  126. $output->writeln((string)$error['hint']);
  127. $output->writeln('');
  128. }
  129. throw new \Exception("Environment not properly prepared.");
  130. }
  131. }
  132. }
  133. /**
  134. * Write a maintenance mode info.
  135. * The commands "_completion" and "maintenance:mode" are excluded.
  136. *
  137. * @param InputInterface $input The input implementation for reading inputs.
  138. * @param ConsoleOutputInterface $output The output implementation
  139. * for writing outputs.
  140. * @return void
  141. */
  142. private function writeMaintenanceModeInfo(InputInterface $input, ConsoleOutputInterface $output): void {
  143. if ($input->getArgument('command') !== '_completion'
  144. && $input->getArgument('command') !== 'maintenance:mode'
  145. && $input->getArgument('command') !== 'status') {
  146. $errOutput = $output->getErrorOutput();
  147. $errOutput->writeln('<comment>Nextcloud is in maintenance mode, no apps are loaded.</comment>');
  148. $errOutput->writeln('<comment>Commands provided by apps are unavailable.</comment>');
  149. }
  150. }
  151. /**
  152. * Sets whether to automatically exit after a command execution or not.
  153. *
  154. * @param bool $boolean Whether to automatically exit after a command execution or not
  155. */
  156. public function setAutoExit($boolean) {
  157. $this->application->setAutoExit($boolean);
  158. }
  159. /**
  160. * @param InputInterface $input
  161. * @param OutputInterface $output
  162. * @return int
  163. * @throws \Exception
  164. */
  165. public function run(?InputInterface $input = null, ?OutputInterface $output = null) {
  166. $event = new ConsoleEvent(
  167. ConsoleEvent::EVENT_RUN,
  168. $this->request->server['argv']
  169. );
  170. $this->dispatcher->dispatchTyped($event);
  171. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, $event);
  172. return $this->application->run($input, $output);
  173. }
  174. private function loadCommandsFromInfoXml($commands) {
  175. foreach ($commands as $command) {
  176. try {
  177. $c = \OCP\Server::get($command);
  178. } catch (ContainerExceptionInterface $e) {
  179. if (class_exists($command)) {
  180. try {
  181. $c = new $command();
  182. } catch (\ArgumentCountError $e2) {
  183. throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e);
  184. }
  185. } else {
  186. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  187. }
  188. }
  189. $this->application->add($c);
  190. }
  191. }
  192. }