Application.php 7.2 KB

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