Application.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $output->writeln("Nextcloud is in maintenance mode - no apps have been loaded");
  97. }
  98. } else {
  99. OC_App::loadApps();
  100. foreach (\OC::$server->getAppManager()->getInstalledApps() as $app) {
  101. $appPath = \OC_App::getAppPath($app);
  102. if ($appPath === false) {
  103. continue;
  104. }
  105. // load commands using info.xml
  106. $info = \OC_App::getAppInfo($app);
  107. if (isset($info['commands'])) {
  108. $this->loadCommandsFromInfoXml($info['commands']);
  109. }
  110. // load from register_command.php
  111. \OC_App::registerAutoloading($app, $appPath);
  112. $file = $appPath . '/appinfo/register_command.php';
  113. if (file_exists($file)) {
  114. try {
  115. require $file;
  116. } catch (\Exception $e) {
  117. $this->logger->logException($e);
  118. }
  119. }
  120. }
  121. }
  122. } else if ($input->getArgument('command') !== '_completion') {
  123. $output->writeln("Nextcloud is not installed - only a limited number of commands are available");
  124. }
  125. } catch(NeedsUpdateException $e) {
  126. if ($input->getArgument('command') !== '_completion') {
  127. $output->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  128. $output->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  129. }
  130. }
  131. if ($input->getFirstArgument() !== 'check') {
  132. $errors = \OC_Util::checkServer(\OC::$server->getConfig());
  133. if (!empty($errors)) {
  134. foreach ($errors as $error) {
  135. $output->writeln((string)$error['error']);
  136. $output->writeln((string)$error['hint']);
  137. $output->writeln('');
  138. }
  139. throw new \Exception("Environment not properly prepared.");
  140. }
  141. }
  142. }
  143. /**
  144. * Sets whether to automatically exit after a command execution or not.
  145. *
  146. * @param bool $boolean Whether to automatically exit after a command execution or not
  147. */
  148. public function setAutoExit($boolean) {
  149. $this->application->setAutoExit($boolean);
  150. }
  151. /**
  152. * @param InputInterface $input
  153. * @param OutputInterface $output
  154. * @return int
  155. * @throws \Exception
  156. */
  157. public function run(InputInterface $input = null, OutputInterface $output = null) {
  158. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
  159. ConsoleEvent::EVENT_RUN,
  160. $this->request->server['argv']
  161. ));
  162. return $this->application->run($input, $output);
  163. }
  164. private function loadCommandsFromInfoXml($commands) {
  165. foreach ($commands as $command) {
  166. try {
  167. $c = \OC::$server->query($command);
  168. } catch (QueryException $e) {
  169. if (class_exists($command)) {
  170. $c = new $command();
  171. } else {
  172. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  173. }
  174. }
  175. $this->application->add($c);
  176. }
  177. }
  178. }