1
0

Application.php 6.7 KB

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