Application.php 4.9 KB

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