Application.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Console\Output\ConsoleOutputInterface;
  40. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  41. class Application {
  42. /** @var IConfig */
  43. private $config;
  44. /** @var EventDispatcherInterface */
  45. private $dispatcher;
  46. /** @var IRequest */
  47. private $request;
  48. /** @var ILogger */
  49. private $logger;
  50. /**
  51. * @param IConfig $config
  52. * @param EventDispatcherInterface $dispatcher
  53. * @param IRequest $request
  54. * @param ILogger $logger
  55. */
  56. public function __construct(IConfig $config, EventDispatcherInterface $dispatcher, IRequest $request, ILogger $logger) {
  57. $defaults = \OC::$server->getThemingDefaults();
  58. $this->config = $config;
  59. $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
  60. $this->dispatcher = $dispatcher;
  61. $this->request = $request;
  62. $this->logger = $logger;
  63. }
  64. /**
  65. * @param InputInterface $input
  66. * @param OutputInterface $output
  67. * @throws \Exception
  68. */
  69. public function loadCommands(InputInterface $input, OutputInterface $output) {
  70. // $application is required to be defined in the register_command scripts
  71. $application = $this->application;
  72. $inputDefinition = $application->getDefinition();
  73. $inputDefinition->addOption(
  74. new InputOption(
  75. 'no-warnings',
  76. null,
  77. InputOption::VALUE_NONE,
  78. 'Skip global warnings, show command output only',
  79. null
  80. )
  81. );
  82. try {
  83. $input->bind($inputDefinition);
  84. } catch (\RuntimeException $e) {
  85. //expected if there are extra options
  86. }
  87. if ($input->getOption('no-warnings')) {
  88. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  89. }
  90. try {
  91. require_once __DIR__ . '/../../../core/register_command.php';
  92. if ($this->config->getSystemValue('installed', false)) {
  93. if (\OCP\Util::needUpgrade()) {
  94. throw new NeedsUpdateException();
  95. } elseif ($this->config->getSystemValue('maintenance', false)) {
  96. if ($input->getArgument('command') !== '_completion') {
  97. $errOutput = $output->getErrorOutput();
  98. $errOutput->writeln('<comment>Nextcloud is in maintenance mode - no app have been loaded</comment>' . PHP_EOL);
  99. }
  100. } else {
  101. OC_App::loadApps();
  102. foreach (\OC::$server->getAppManager()->getInstalledApps() as $app) {
  103. $appPath = \OC_App::getAppPath($app);
  104. if ($appPath === false) {
  105. continue;
  106. }
  107. // load commands using info.xml
  108. $info = \OC_App::getAppInfo($app);
  109. if (isset($info['commands'])) {
  110. $this->loadCommandsFromInfoXml($info['commands']);
  111. }
  112. // load from register_command.php
  113. \OC_App::registerAutoloading($app, $appPath);
  114. $file = $appPath . '/appinfo/register_command.php';
  115. if (file_exists($file)) {
  116. try {
  117. require $file;
  118. } catch (\Exception $e) {
  119. $this->logger->logException($e);
  120. }
  121. }
  122. }
  123. }
  124. } else if ($input->getArgument('command') !== '_completion') {
  125. $output->writeln("Nextcloud is not installed - only a limited number of commands are available");
  126. }
  127. } catch(NeedsUpdateException $e) {
  128. if ($input->getArgument('command') !== '_completion') {
  129. $output->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  130. $output->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  131. }
  132. }
  133. if ($input->getFirstArgument() !== 'check') {
  134. $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
  135. if (!empty($errors)) {
  136. foreach ($errors as $error) {
  137. $output->writeln((string)$error['error']);
  138. $output->writeln((string)$error['hint']);
  139. $output->writeln('');
  140. }
  141. throw new \Exception("Environment not properly prepared.");
  142. }
  143. }
  144. }
  145. /**
  146. * Sets whether to automatically exit after a command execution or not.
  147. *
  148. * @param bool $boolean Whether to automatically exit after a command execution or not
  149. */
  150. public function setAutoExit($boolean) {
  151. $this->application->setAutoExit($boolean);
  152. }
  153. /**
  154. * @param InputInterface $input
  155. * @param OutputInterface $output
  156. * @return int
  157. * @throws \Exception
  158. */
  159. public function run(InputInterface $input = null, OutputInterface $output = null) {
  160. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
  161. ConsoleEvent::EVENT_RUN,
  162. $this->request->server['argv']
  163. ));
  164. return $this->application->run($input, $output);
  165. }
  166. private function loadCommandsFromInfoXml($commands) {
  167. foreach ($commands as $command) {
  168. try {
  169. $c = \OC::$server->query($command);
  170. } catch (QueryException $e) {
  171. if (class_exists($command)) {
  172. $c = new $command();
  173. } else {
  174. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  175. }
  176. }
  177. $this->application->add($c);
  178. }
  179. }
  180. }