SandboxCommand.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * @package Grav\Console\Cli
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Cli;
  9. use Grav\Common\Filesystem\Folder;
  10. use Grav\Common\Utils;
  11. use Grav\Console\GravCommand;
  12. use RuntimeException;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use function count;
  16. /**
  17. * Class SandboxCommand
  18. * @package Grav\Console\Cli
  19. */
  20. class SandboxCommand extends GravCommand
  21. {
  22. /** @var array */
  23. protected $directories = [
  24. '/assets',
  25. '/backup',
  26. '/cache',
  27. '/images',
  28. '/logs',
  29. '/tmp',
  30. '/user/accounts',
  31. '/user/config',
  32. '/user/data',
  33. '/user/pages',
  34. '/user/plugins',
  35. '/user/themes',
  36. ];
  37. /** @var array */
  38. protected $files = [
  39. '/.dependencies',
  40. '/.htaccess',
  41. '/user/config/site.yaml',
  42. '/user/config/system.yaml',
  43. ];
  44. /** @var array */
  45. protected $mappings = [
  46. '/.gitignore' => '/.gitignore',
  47. '/.editorconfig' => '/.editorconfig',
  48. '/CHANGELOG.md' => '/CHANGELOG.md',
  49. '/LICENSE.txt' => '/LICENSE.txt',
  50. '/README.md' => '/README.md',
  51. '/CONTRIBUTING.md' => '/CONTRIBUTING.md',
  52. '/index.php' => '/index.php',
  53. '/composer.json' => '/composer.json',
  54. '/bin' => '/bin',
  55. '/system' => '/system',
  56. '/vendor' => '/vendor',
  57. '/webserver-configs' => '/webserver-configs',
  58. ];
  59. /** @var string */
  60. protected $source;
  61. /** @var string */
  62. protected $destination;
  63. /**
  64. * @return void
  65. */
  66. protected function configure(): void
  67. {
  68. $this
  69. ->setName('sandbox')
  70. ->setDescription('Setup of a base Grav system in your webroot, good for development, playing around or starting fresh')
  71. ->addArgument(
  72. 'destination',
  73. InputArgument::REQUIRED,
  74. 'The destination directory to symlink into'
  75. )
  76. ->addOption(
  77. 'symlink',
  78. 's',
  79. InputOption::VALUE_NONE,
  80. 'Symlink the base grav system'
  81. )
  82. ->setHelp("The <info>sandbox</info> command help create a development environment that can optionally use symbolic links to link the core of grav to the git cloned repository.\nGood for development, playing around or starting fresh");
  83. $source = getcwd();
  84. if ($source === false) {
  85. throw new RuntimeException('Internal Error');
  86. }
  87. $this->source = $source;
  88. }
  89. /**
  90. * @return int
  91. */
  92. protected function serve(): int
  93. {
  94. $input = $this->getInput();
  95. $this->destination = $input->getArgument('destination');
  96. // Create Some core stuff if it doesn't exist
  97. $error = $this->createDirectories();
  98. if ($error) {
  99. return $error;
  100. }
  101. // Copy files or create symlinks
  102. $error = $input->getOption('symlink') ? $this->symlink() : $this->copy();
  103. if ($error) {
  104. return $error;
  105. }
  106. $error = $this->pages();
  107. if ($error) {
  108. return $error;
  109. }
  110. $error = $this->initFiles();
  111. if ($error) {
  112. return $error;
  113. }
  114. $error = $this->perms();
  115. if ($error) {
  116. return $error;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * @return int
  122. */
  123. private function createDirectories(): int
  124. {
  125. $io = $this->getIO();
  126. $io->newLine();
  127. $io->writeln('<comment>Creating Directories</comment>');
  128. $dirs_created = false;
  129. if (!file_exists($this->destination)) {
  130. Folder::create($this->destination);
  131. }
  132. foreach ($this->directories as $dir) {
  133. if (!file_exists($this->destination . $dir)) {
  134. $dirs_created = true;
  135. $io->writeln(' <cyan>' . $dir . '</cyan>');
  136. Folder::create($this->destination . $dir);
  137. }
  138. }
  139. if (!$dirs_created) {
  140. $io->writeln(' <red>Directories already exist</red>');
  141. }
  142. return 0;
  143. }
  144. /**
  145. * @return int
  146. */
  147. private function copy(): int
  148. {
  149. $io = $this->getIO();
  150. $io->newLine();
  151. $io->writeln('<comment>Copying Files</comment>');
  152. foreach ($this->mappings as $source => $target) {
  153. if ((string)(int)$source === (string)$source) {
  154. $source = $target;
  155. }
  156. $from = $this->source . $source;
  157. $to = $this->destination . $target;
  158. $io->writeln(' <cyan>' . $source . '</cyan> <comment>-></comment> ' . $to);
  159. @Folder::rcopy($from, $to);
  160. }
  161. return 0;
  162. }
  163. /**
  164. * @return int
  165. */
  166. private function symlink(): int
  167. {
  168. $io = $this->getIO();
  169. $io->newLine();
  170. $io->writeln('<comment>Resetting Symbolic Links</comment>');
  171. // Symlink also tests if using git.
  172. if (is_dir($this->source . '/tests')) {
  173. $this->mappings['/tests'] = '/tests';
  174. }
  175. foreach ($this->mappings as $source => $target) {
  176. if ((string)(int)$source === (string)$source) {
  177. $source = $target;
  178. }
  179. $from = $this->source . $source;
  180. $to = $this->destination . $target;
  181. $io->writeln(' <cyan>' . $source . '</cyan> <comment>-></comment> ' . $to);
  182. if (is_dir($to)) {
  183. @Folder::delete($to);
  184. } else {
  185. @unlink($to);
  186. }
  187. symlink($from, $to);
  188. }
  189. return 0;
  190. }
  191. /**
  192. * @return int
  193. */
  194. private function pages(): int
  195. {
  196. $io = $this->getIO();
  197. $io->newLine();
  198. $io->writeln('<comment>Pages Initializing</comment>');
  199. // get pages files and initialize if no pages exist
  200. $pages_dir = $this->destination . '/user/pages';
  201. $pages_files = array_diff(scandir($pages_dir), ['..', '.']);
  202. if (count($pages_files) === 0) {
  203. $destination = $this->source . '/user/pages';
  204. Folder::rcopy($destination, $pages_dir);
  205. $io->writeln(' <cyan>' . $destination . '</cyan> <comment>-></comment> Created');
  206. }
  207. return 0;
  208. }
  209. /**
  210. * @return int
  211. */
  212. private function initFiles(): int
  213. {
  214. if (!$this->check()) {
  215. return 1;
  216. }
  217. $io = $this->getIO();
  218. $io->newLine();
  219. $io->writeln('<comment>File Initializing</comment>');
  220. $files_init = false;
  221. // Copy files if they do not exist
  222. foreach ($this->files as $source => $target) {
  223. if ((string)(int)$source === (string)$source) {
  224. $source = $target;
  225. }
  226. $from = $this->source . $source;
  227. $to = $this->destination . $target;
  228. if (!file_exists($to)) {
  229. $files_init = true;
  230. copy($from, $to);
  231. $io->writeln(' <cyan>' . $target . '</cyan> <comment>-></comment> Created');
  232. }
  233. }
  234. if (!$files_init) {
  235. $io->writeln(' <red>Files already exist</red>');
  236. }
  237. return 0;
  238. }
  239. /**
  240. * @return int
  241. */
  242. private function perms(): int
  243. {
  244. $io = $this->getIO();
  245. $io->newLine();
  246. $io->writeln('<comment>Permissions Initializing</comment>');
  247. $dir_perms = 0755;
  248. $binaries = glob($this->destination . DS . 'bin' . DS . '*');
  249. foreach ($binaries as $bin) {
  250. chmod($bin, $dir_perms);
  251. $io->writeln(' <cyan>bin/' . Utils::basename($bin) . '</cyan> permissions reset to ' . decoct($dir_perms));
  252. }
  253. $io->newLine();
  254. return 0;
  255. }
  256. /**
  257. * @return bool
  258. */
  259. private function check(): bool
  260. {
  261. $success = true;
  262. $io = $this->getIO();
  263. if (!file_exists($this->destination)) {
  264. $io->writeln(' file: <red>' . $this->destination . '</red> does not exist!');
  265. $success = false;
  266. }
  267. foreach ($this->directories as $dir) {
  268. if (!file_exists($this->destination . $dir)) {
  269. $io->writeln(' directory: <red>' . $dir . '</red> does not exist!');
  270. $success = false;
  271. }
  272. }
  273. foreach ($this->mappings as $target => $link) {
  274. if (!file_exists($this->destination . $target)) {
  275. $io->writeln(' mappings: <red>' . $target . '</red> does not exist!');
  276. $success = false;
  277. }
  278. }
  279. if (!$success) {
  280. $io->newLine();
  281. $io->writeln('<comment>install should be run with --symlink|--s to symlink first</comment>');
  282. }
  283. return $success;
  284. }
  285. }