You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Application.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Michael Weimann <mail@michael-weimann.eu>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Console;
  32. use OC\MemoryInfo;
  33. use OC\NeedsUpdateException;
  34. use OCP\App\AppPathNotFoundException;
  35. use OCP\App\IAppManager;
  36. use OCP\Console\ConsoleEvent;
  37. use OCP\EventDispatcher\IEventDispatcher;
  38. use OCP\IConfig;
  39. use OCP\IRequest;
  40. use Psr\Container\ContainerExceptionInterface;
  41. use Psr\Log\LoggerInterface;
  42. use Symfony\Component\Console\Application as SymfonyApplication;
  43. use Symfony\Component\Console\Input\InputInterface;
  44. use Symfony\Component\Console\Input\InputOption;
  45. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  46. use Symfony\Component\Console\Output\OutputInterface;
  47. class Application {
  48. private SymfonyApplication $application;
  49. public function __construct(
  50. private IConfig $config,
  51. private IEventDispatcher $dispatcher,
  52. private IRequest $request,
  53. private LoggerInterface $logger,
  54. private MemoryInfo $memoryInfo,
  55. private IAppManager $appManager,
  56. ) {
  57. $defaults = \OC::$server->get('ThemingDefaults');
  58. $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
  59. }
  60. /**
  61. * @throws \Exception
  62. */
  63. public function loadCommands(
  64. InputInterface $input,
  65. ConsoleOutputInterface $output
  66. ) {
  67. // $application is required to be defined in the register_command scripts
  68. $application = $this->application;
  69. $inputDefinition = $application->getDefinition();
  70. $inputDefinition->addOption(
  71. new InputOption(
  72. 'no-warnings',
  73. null,
  74. InputOption::VALUE_NONE,
  75. 'Skip global warnings, show command output only',
  76. null
  77. )
  78. );
  79. try {
  80. $input->bind($inputDefinition);
  81. } catch (\RuntimeException $e) {
  82. //expected if there are extra options
  83. }
  84. if ($input->getOption('no-warnings')) {
  85. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  86. }
  87. if ($this->memoryInfo->isMemoryLimitSufficient() === false) {
  88. $output->getErrorOutput()->writeln(
  89. '<comment>The current PHP memory limit ' .
  90. 'is below the recommended value of 512MB.</comment>'
  91. );
  92. }
  93. try {
  94. require_once __DIR__ . '/../../../core/register_command.php';
  95. if ($this->config->getSystemValueBool('installed', false)) {
  96. if (\OCP\Util::needUpgrade()) {
  97. throw new NeedsUpdateException();
  98. } elseif ($this->config->getSystemValueBool('maintenance')) {
  99. $this->writeMaintenanceModeInfo($input, $output);
  100. } else {
  101. $this->appManager->loadApps();
  102. foreach ($this->appManager->getInstalledApps() as $app) {
  103. try {
  104. $appPath = $this->appManager->getAppPath($app);
  105. } catch (AppPathNotFoundException) {
  106. continue;
  107. }
  108. // load commands using info.xml
  109. $info = $this->appManager->getAppInfo($app);
  110. if (isset($info['commands'])) {
  111. try {
  112. $this->loadCommandsFromInfoXml($info['commands']);
  113. } catch (\Throwable $e) {
  114. $output->writeln("<error>" . $e->getMessage() . "</error>");
  115. $this->logger->error($e->getMessage(), [
  116. 'exception' => $e,
  117. ]);
  118. }
  119. }
  120. // load from register_command.php
  121. \OC_App::registerAutoloading($app, $appPath);
  122. $file = $appPath . '/appinfo/register_command.php';
  123. if (file_exists($file)) {
  124. try {
  125. require $file;
  126. } catch (\Exception $e) {
  127. $this->logger->error($e->getMessage(), [
  128. 'exception' => $e,
  129. ]);
  130. }
  131. }
  132. }
  133. }
  134. } elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') {
  135. $errorOutput = $output->getErrorOutput();
  136. $errorOutput->writeln("Nextcloud is not installed - only a limited number of commands are available");
  137. }
  138. } catch (NeedsUpdateException $e) {
  139. if ($input->getArgument('command') !== '_completion') {
  140. $errorOutput = $output->getErrorOutput();
  141. $errorOutput->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  142. $errorOutput->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  143. }
  144. }
  145. if ($input->getFirstArgument() !== 'check') {
  146. $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
  147. if (!empty($errors)) {
  148. foreach ($errors as $error) {
  149. $output->writeln((string)$error['error']);
  150. $output->writeln((string)$error['hint']);
  151. $output->writeln('');
  152. }
  153. throw new \Exception("Environment not properly prepared.");
  154. }
  155. }
  156. }
  157. /**
  158. * Write a maintenance mode info.
  159. * The commands "_completion" and "maintenance:mode" are excluded.
  160. *
  161. * @param InputInterface $input The input implementation for reading inputs.
  162. * @param ConsoleOutputInterface $output The output implementation
  163. * for writing outputs.
  164. * @return void
  165. */
  166. private function writeMaintenanceModeInfo(InputInterface $input, ConsoleOutputInterface $output): void {
  167. if ($input->getArgument('command') !== '_completion'
  168. && $input->getArgument('command') !== 'maintenance:mode'
  169. && $input->getArgument('command') !== 'status') {
  170. $errOutput = $output->getErrorOutput();
  171. $errOutput->writeln('<comment>Nextcloud is in maintenance mode, no apps are loaded.</comment>');
  172. $errOutput->writeln('<comment>Commands provided by apps are unavailable.</comment>');
  173. }
  174. }
  175. /**
  176. * Sets whether to automatically exit after a command execution or not.
  177. *
  178. * @param bool $boolean Whether to automatically exit after a command execution or not
  179. */
  180. public function setAutoExit($boolean) {
  181. $this->application->setAutoExit($boolean);
  182. }
  183. /**
  184. * @param InputInterface $input
  185. * @param OutputInterface $output
  186. * @return int
  187. * @throws \Exception
  188. */
  189. public function run(?InputInterface $input = null, ?OutputInterface $output = null) {
  190. $event = new ConsoleEvent(
  191. ConsoleEvent::EVENT_RUN,
  192. $this->request->server['argv']
  193. );
  194. $this->dispatcher->dispatchTyped($event);
  195. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, $event);
  196. return $this->application->run($input, $output);
  197. }
  198. private function loadCommandsFromInfoXml($commands) {
  199. foreach ($commands as $command) {
  200. try {
  201. $c = \OCP\Server::get($command);
  202. } catch (ContainerExceptionInterface $e) {
  203. if (class_exists($command)) {
  204. try {
  205. $c = new $command();
  206. } catch (\ArgumentCountError $e2) {
  207. throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e);
  208. }
  209. } else {
  210. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  211. }
  212. }
  213. $this->application->add($c);
  214. }
  215. }
  216. }