/modules/system/console/OctoberMirror.php

https://gitlab.com/gideonmarked/yovelife · PHP · 174 lines · 119 code · 30 blank · 25 comment · 10 complexity · f2582fd46b624cb72add3d624ab3be2b MD5 · raw file

  1. <?php namespace System\Console;
  2. use File;
  3. use Illuminate\Console\Command;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. /**
  7. * This command will create symbolic links to files and directories
  8. * that are commonly required to be publicly available.
  9. *
  10. * It is experimental and currently undergoing testing,
  11. * see: https://github.com/octobercms/october/issues/1331
  12. */
  13. class OctoberMirror extends Command
  14. {
  15. /**
  16. * The console command name.
  17. */
  18. protected $name = 'october:mirror';
  19. /**
  20. * The console command description.
  21. */
  22. protected $description = '(Experimental) Generates a mirrored public folder using symbolic links.';
  23. protected $files = [
  24. '.htaccess',
  25. 'index.php',
  26. 'favicon.ico',
  27. 'robots.txt',
  28. 'sitemap.xml',
  29. ];
  30. protected $directories = [
  31. 'storage/app/uploads',
  32. 'storage/app/media',
  33. 'storage/temp/public',
  34. ];
  35. protected $wildcards = [
  36. 'modules/*/assets',
  37. 'modules/*/resources',
  38. 'modules/*/behaviors/*/assets',
  39. 'modules/*/behaviors/*/resources',
  40. 'modules/*/widgets/*/assets',
  41. 'modules/*/widgets/*/resources',
  42. 'modules/*/formwidgets/*/assets',
  43. 'modules/*/formwidgets/*/resources',
  44. 'modules/*/reportwidgets/*/assets',
  45. 'modules/*/reportwidgets/*/resources',
  46. 'plugins/*/*/assets',
  47. 'plugins/*/*/resources',
  48. 'plugins/*/*/behaviors/*/assets',
  49. 'plugins/*/*/behaviors/*/resources',
  50. 'plugins/*/*/reportwidgets/*/assets',
  51. 'plugins/*/*/reportwidgets/*/resources',
  52. 'plugins/*/*/formwidgets/*/assets',
  53. 'plugins/*/*/formwidgets/*/resources',
  54. 'plugins/*/*/widgets/*/assets',
  55. 'plugins/*/*/widgets/*/resources',
  56. 'themes/*/assets',
  57. 'themes/*/resources',
  58. ];
  59. protected $destinationPath;
  60. /**
  61. * Create a new command instance.
  62. */
  63. public function __construct()
  64. {
  65. parent::__construct();
  66. }
  67. /**
  68. * Execute the console command.
  69. */
  70. public function fire()
  71. {
  72. $this->getDestinationPath();
  73. foreach ($this->files as $file) {
  74. $this->mirrorFile($file);
  75. }
  76. foreach ($this->directories as $directory) {
  77. $this->mirrorDirectory($directory);
  78. }
  79. foreach ($this->wildcards as $wildcard) {
  80. $this->mirrorWildcard($wildcard);
  81. }
  82. $this->output->writeln('<info>Mirror complete!</info>');
  83. }
  84. protected function mirrorFile($file)
  85. {
  86. $this->output->writeln(sprintf('<info> - Mirroring: %s</info>', $file));
  87. $src = base_path().'/'.$file;
  88. $dest = $this->getDestinationPath().'/'.$file;
  89. if (!File::isFile($src) || File::isFile($dest)) return false;
  90. symlink($src, $dest);
  91. }
  92. protected function mirrorDirectory($directory)
  93. {
  94. $this->output->writeln(sprintf('<info> - Mirroring: %s</info>', $directory));
  95. $src = base_path().'/'.$directory;
  96. $dest = $this->getDestinationPath().'/'.$directory;
  97. if (!File::isDirectory($src) || File::isDirectory($dest)) return false;
  98. if (!File::isDirectory(dirname($dest))) File::makeDirectory(dirname($dest), 0755, true);
  99. symlink($src, $dest);
  100. }
  101. protected function mirrorWildcard($wildcard)
  102. {
  103. if (strpos($wildcard, '*') === false) {
  104. return $this->mirrorDirectory($wildcard);
  105. }
  106. list($start, $end) = explode('*', $wildcard, 2);
  107. $startDir = base_path().'/'.$start;
  108. if (!File::isDirectory($startDir)) return false;
  109. foreach (File::directories($startDir) as $directory) {
  110. $this->mirrorWildcard($start.basename($directory).$end);
  111. }
  112. }
  113. protected function getDestinationPath()
  114. {
  115. if ($this->destinationPath !== null) {
  116. return $this->destinationPath;
  117. }
  118. $destPath = $this->argument('destination');
  119. if (realpath($destPath) === false) {
  120. $destPath = base_path() . '/' . $destPath;
  121. }
  122. if (!File::isDirectory($destPath)) {
  123. File::makeDirectory($destPath, 0755, true);
  124. }
  125. $this->output->writeln(sprintf('<info>Destination: %s</info>', $destPath));
  126. return $this->destinationPath = $destPath;
  127. }
  128. /**
  129. * Get the console command arguments.
  130. */
  131. protected function getArguments()
  132. {
  133. return [
  134. ['destination', InputArgument::REQUIRED, 'The destination path relative to the current directory. Eg: public/'],
  135. ];
  136. }
  137. /**
  138. * Get the console command options.
  139. */
  140. protected function getOptions()
  141. {
  142. return [];
  143. }
  144. }