1
0

Application.php 7.8 KB

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