1
0

Application.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Console;
  28. use OC\NeedsUpdateException;
  29. use OC_App;
  30. use OCP\AppFramework\QueryException;
  31. use OCP\Console\ConsoleEvent;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use OCP\IRequest;
  35. use Symfony\Component\Console\Application as SymfonyApplication;
  36. use Symfony\Component\Console\Input\InputInterface;
  37. use Symfony\Component\Console\Input\InputOption;
  38. use Symfony\Component\Console\Output\OutputInterface;
  39. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  40. class Application {
  41. /** @var IConfig */
  42. private $config;
  43. /** @var EventDispatcherInterface */
  44. private $dispatcher;
  45. /** @var IRequest */
  46. private $request;
  47. /** @var ILogger */
  48. private $logger;
  49. /**
  50. * @param IConfig $config
  51. * @param EventDispatcherInterface $dispatcher
  52. * @param IRequest $request
  53. * @param ILogger $logger
  54. */
  55. public function __construct(IConfig $config, EventDispatcherInterface $dispatcher, IRequest $request, ILogger $logger) {
  56. $defaults = \OC::$server->getThemingDefaults();
  57. $this->config = $config;
  58. $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
  59. $this->dispatcher = $dispatcher;
  60. $this->request = $request;
  61. $this->logger = $logger;
  62. }
  63. /**
  64. * @param InputInterface $input
  65. * @param OutputInterface $output
  66. * @throws \Exception
  67. */
  68. public function loadCommands(InputInterface $input, OutputInterface $output) {
  69. // $application is required to be defined in the register_command scripts
  70. $application = $this->application;
  71. $inputDefinition = $application->getDefinition();
  72. $inputDefinition->addOption(
  73. new InputOption(
  74. 'no-warnings',
  75. null,
  76. InputOption::VALUE_NONE,
  77. 'Skip global warnings, show command output only',
  78. null
  79. )
  80. );
  81. try {
  82. $input->bind($inputDefinition);
  83. } catch (\RuntimeException $e) {
  84. //expected if there are extra options
  85. }
  86. if ($input->getOption('no-warnings')) {
  87. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  88. }
  89. try {
  90. require_once __DIR__ . '/../../../core/register_command.php';
  91. if ($this->config->getSystemValue('installed', false)) {
  92. if (\OCP\Util::needUpgrade()) {
  93. throw new NeedsUpdateException();
  94. } elseif ($this->config->getSystemValue('maintenance', false)) {
  95. if ($input->getArgument('command') !== '_completion') {
  96. $errOutput = $output->getErrorOutput();
  97. $errOutput->writeln('<comment>Nextcloud is in maintenance mode - no app have been loaded</comment>' . PHP_EOL);
  98. }
  99. } else {
  100. OC_App::loadApps();
  101. foreach (\OC::$server->getAppManager()->getInstalledApps() as $app) {
  102. $appPath = \OC_App::getAppPath($app);
  103. if ($appPath === false) {
  104. continue;
  105. }
  106. // load commands using info.xml
  107. $info = \OC_App::getAppInfo($app);
  108. if (isset($info['commands'])) {
  109. $this->loadCommandsFromInfoXml($info['commands']);
  110. }
  111. // load from register_command.php
  112. \OC_App::registerAutoloading($app, $appPath);
  113. $file = $appPath . '/appinfo/register_command.php';
  114. if (file_exists($file)) {
  115. try {
  116. require $file;
  117. } catch (\Exception $e) {
  118. $this->logger->logException($e);
  119. }
  120. }
  121. }
  122. }
  123. } else if ($input->getArgument('command') !== '_completion') {
  124. $output->writeln("Nextcloud is not installed - only a limited number of commands are available");
  125. }
  126. } catch(NeedsUpdateException $e) {
  127. if ($input->getArgument('command') !== '_completion') {
  128. $output->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  129. $output->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  130. }
  131. }
  132. if ($input->getFirstArgument() !== 'check') {
  133. $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
  134. if (!empty($errors)) {
  135. foreach ($errors as $error) {
  136. $output->writeln((string)$error['error']);
  137. $output->writeln((string)$error['hint']);
  138. $output->writeln('');
  139. }
  140. throw new \Exception("Environment not properly prepared.");
  141. }
  142. }
  143. }
  144. /**
  145. * Sets whether to automatically exit after a command execution or not.
  146. *
  147. * @param bool $boolean Whether to automatically exit after a command execution or not
  148. */
  149. public function setAutoExit($boolean) {
  150. $this->application->setAutoExit($boolean);
  151. }
  152. /**
  153. * @param InputInterface $input
  154. * @param OutputInterface $output
  155. * @return int
  156. * @throws \Exception
  157. */
  158. public function run(InputInterface $input = null, OutputInterface $output = null) {
  159. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
  160. ConsoleEvent::EVENT_RUN,
  161. $this->request->server['argv']
  162. ));
  163. return $this->application->run($input, $output);
  164. }
  165. private function loadCommandsFromInfoXml($commands) {
  166. foreach ($commands as $command) {
  167. try {
  168. $c = \OC::$server->query($command);
  169. } catch (QueryException $e) {
  170. if (class_exists($command)) {
  171. $c = new $command();
  172. } else {
  173. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  174. }
  175. }
  176. $this->application->add($c);
  177. }
  178. }
  179. }