DirectInstallCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * @package Grav\Console\Gpm
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Gpm;
  9. use Grav\Common\Cache;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Filesystem\Folder;
  12. use Grav\Common\GPM\GPM;
  13. use Grav\Common\GPM\Installer;
  14. use Grav\Common\GPM\Response;
  15. use Grav\Console\ConsoleCommand;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Question\ConfirmationQuestion;
  19. class DirectInstallCommand extends ConsoleCommand
  20. {
  21. /** @var string */
  22. protected $all_yes;
  23. /** @var string */
  24. protected $destination;
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('direct-install')
  29. ->setAliases(['directinstall'])
  30. ->addArgument(
  31. 'package-file',
  32. InputArgument::REQUIRED,
  33. 'Installable package local <path> or remote <URL>. Can install specific version'
  34. )
  35. ->addOption(
  36. 'all-yes',
  37. 'y',
  38. InputOption::VALUE_NONE,
  39. 'Assumes yes (or best approach) instead of prompting'
  40. )
  41. ->addOption(
  42. 'destination',
  43. 'd',
  44. InputOption::VALUE_OPTIONAL,
  45. 'The destination where the package should be installed at. By default this would be where the grav instance has been launched from',
  46. GRAV_ROOT
  47. )
  48. ->setDescription('Installs Grav, plugin, or theme directly from a file or a URL')
  49. ->setHelp('The <info>direct-install</info> command installs Grav, plugin, or theme directly from a file or a URL');
  50. }
  51. /**
  52. * @return bool
  53. */
  54. protected function serve()
  55. {
  56. // Making sure the destination is usable
  57. $this->destination = realpath($this->input->getOption('destination'));
  58. if (
  59. !Installer::isGravInstance($this->destination) ||
  60. !Installer::isValidDestination($this->destination, [Installer::EXISTS, Installer::IS_LINK])
  61. ) {
  62. $this->output->writeln('<red>ERROR</red>: ' . Installer::lastErrorMsg());
  63. exit;
  64. }
  65. $this->all_yes = $this->input->getOption('all-yes');
  66. $package_file = $this->input->getArgument('package-file');
  67. $helper = $this->getHelper('question');
  68. $question = new ConfirmationQuestion("Are you sure you want to direct-install <cyan>{$package_file}</cyan> [y|N] ", false);
  69. $answer = $this->all_yes ? true : $helper->ask($this->input, $this->output, $question);
  70. if (!$answer) {
  71. $this->output->writeln('exiting...');
  72. $this->output->writeln('');
  73. exit;
  74. }
  75. $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
  76. $tmp_zip = $tmp_dir . '/Grav-' . uniqid();
  77. $this->output->writeln('');
  78. $this->output->writeln("Preparing to install <cyan>{$package_file}</cyan>");
  79. if (Response::isRemote($package_file)) {
  80. $this->output->write(' |- Downloading package... 0%');
  81. try {
  82. $zip = GPM::downloadPackage($package_file, $tmp_zip);
  83. } catch (\RuntimeException $e) {
  84. $this->output->writeln('');
  85. $this->output->writeln(" `- <red>ERROR: {$e->getMessage()}</red>");
  86. $this->output->writeln('');
  87. exit;
  88. }
  89. if ($zip) {
  90. $this->output->write("\x0D");
  91. $this->output->write(' |- Downloading package... 100%');
  92. $this->output->writeln('');
  93. }
  94. } else {
  95. $this->output->write(' |- Copying package... 0%');
  96. $zip = GPM::copyPackage($package_file, $tmp_zip);
  97. if ($zip) {
  98. $this->output->write("\x0D");
  99. $this->output->write(' |- Copying package... 100%');
  100. $this->output->writeln('');
  101. }
  102. }
  103. if (file_exists($zip)) {
  104. $tmp_source = $tmp_dir . '/Grav-' . uniqid();
  105. $this->output->write(' |- Extracting package... ');
  106. $extracted = Installer::unZip($zip, $tmp_source);
  107. if (!$extracted) {
  108. $this->output->write("\x0D");
  109. $this->output->writeln(' |- Extracting package... <red>failed</red>');
  110. Folder::delete($tmp_source);
  111. Folder::delete($tmp_zip);
  112. exit;
  113. }
  114. $this->output->write("\x0D");
  115. $this->output->writeln(' |- Extracting package... <green>ok</green>');
  116. $type = GPM::getPackageType($extracted);
  117. if (!$type) {
  118. $this->output->writeln(" '- <red>ERROR: Not a valid Grav package</red>");
  119. $this->output->writeln('');
  120. Folder::delete($tmp_source);
  121. Folder::delete($tmp_zip);
  122. exit;
  123. }
  124. $blueprint = GPM::getBlueprints($extracted);
  125. if ($blueprint) {
  126. if (isset($blueprint['dependencies'])) {
  127. $dependencies = [];
  128. foreach ($blueprint['dependencies'] as $dependency) {
  129. if (is_array($dependency)){
  130. if (isset($dependency['name'])) {
  131. $dependencies[] = $dependency['name'];
  132. }
  133. if (isset($dependency['github'])) {
  134. $dependencies[] = $dependency['github'];
  135. }
  136. } else {
  137. $dependencies[] = $dependency;
  138. }
  139. }
  140. $this->output->writeln(' |- Dependencies found... <cyan>[' . implode(',', $dependencies) . ']</cyan>');
  141. $question = new ConfirmationQuestion(" | '- Dependencies will not be satisfied. Continue ? [y|N] ", false);
  142. $answer = $this->all_yes ? true : $helper->ask($this->input, $this->output, $question);
  143. if (!$answer) {
  144. $this->output->writeln('exiting...');
  145. $this->output->writeln('');
  146. Folder::delete($tmp_source);
  147. Folder::delete($tmp_zip);
  148. exit;
  149. }
  150. }
  151. }
  152. if ($type === 'grav') {
  153. $this->output->write(' |- Checking destination... ');
  154. Installer::isValidDestination(GRAV_ROOT . '/system');
  155. if (Installer::IS_LINK === Installer::lastErrorCode()) {
  156. $this->output->write("\x0D");
  157. $this->output->writeln(' |- Checking destination... <yellow>symbolic link</yellow>');
  158. $this->output->writeln(" '- <red>ERROR: symlinks found...</red> <yellow>" . GRAV_ROOT . '</yellow>');
  159. $this->output->writeln('');
  160. Folder::delete($tmp_source);
  161. Folder::delete($tmp_zip);
  162. exit;
  163. }
  164. $this->output->write("\x0D");
  165. $this->output->writeln(' |- Checking destination... <green>ok</green>');
  166. $this->output->write(' |- Installing package... ');
  167. static::upgradeGrav($zip, $extracted);
  168. } else {
  169. $name = GPM::getPackageName($extracted);
  170. if (!$name) {
  171. $this->output->writeln('<red>ERROR: Name could not be determined.</red> Please specify with --name|-n');
  172. $this->output->writeln('');
  173. Folder::delete($tmp_source);
  174. Folder::delete($tmp_zip);
  175. exit;
  176. }
  177. $install_path = GPM::getInstallPath($type, $name);
  178. $is_update = file_exists($install_path);
  179. $this->output->write(' |- Checking destination... ');
  180. Installer::isValidDestination(GRAV_ROOT . DS . $install_path);
  181. if (Installer::lastErrorCode() == Installer::IS_LINK) {
  182. $this->output->write("\x0D");
  183. $this->output->writeln(' |- Checking destination... <yellow>symbolic link</yellow>');
  184. $this->output->writeln(" '- <red>ERROR: symlink found...</red> <yellow>" . GRAV_ROOT . DS . $install_path . '</yellow>');
  185. $this->output->writeln('');
  186. Folder::delete($tmp_source);
  187. Folder::delete($tmp_zip);
  188. exit;
  189. }
  190. $this->output->write("\x0D");
  191. $this->output->writeln(' |- Checking destination... <green>ok</green>');
  192. $this->output->write(' |- Installing package... ');
  193. Installer::install(
  194. $zip,
  195. $this->destination,
  196. $options = [
  197. 'install_path' => $install_path,
  198. 'theme' => (($type === 'theme')),
  199. 'is_update' => $is_update
  200. ],
  201. $extracted
  202. );
  203. }
  204. Folder::delete($tmp_source);
  205. $this->output->write("\x0D");
  206. if(Installer::lastErrorCode()) {
  207. $this->output->writeln(" '- <red>" . Installer::lastErrorMsg() . '</red>');
  208. $this->output->writeln('');
  209. } else {
  210. $this->output->writeln(' |- Installing package... <green>ok</green>');
  211. $this->output->writeln(" '- <green>Success!</green> ");
  212. $this->output->writeln('');
  213. }
  214. } else {
  215. $this->output->writeln(" '- <red>ERROR: ZIP package could not be found</red>");
  216. }
  217. Folder::delete($tmp_zip);
  218. // clear cache after successful upgrade
  219. $this->clearCache();
  220. return true;
  221. }
  222. private function upgradeGrav($zip, $folder, $keepFolder = false)
  223. {
  224. static $ignores = [
  225. 'backup',
  226. 'cache',
  227. 'images',
  228. 'logs',
  229. 'tmp',
  230. 'user',
  231. '.htaccess',
  232. 'robots.txt'
  233. ];
  234. if (!is_dir($folder)) {
  235. Installer::setError('Invalid source folder');
  236. }
  237. try {
  238. $script = $folder . '/system/install.php';
  239. /** Install $installer */
  240. if ((file_exists($script) && $install = include $script) && is_callable($install)) {
  241. $install($zip);
  242. } else {
  243. Installer::install(
  244. $zip,
  245. GRAV_ROOT,
  246. ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => $ignores],
  247. $folder,
  248. $keepFolder
  249. );
  250. Cache::clearCache();
  251. }
  252. } catch (\Exception $e) {
  253. Installer::setError($e->getMessage());
  254. }
  255. }
  256. }