AssetsInstallCommand.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Exception\InvalidArgumentException;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\Filesystem\Exception\IOException;
  20. use Symfony\Component\Filesystem\Filesystem;
  21. use Symfony\Component\Finder\Finder;
  22. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  23. use Symfony\Component\HttpKernel\KernelInterface;
  24. /**
  25. * Command that places bundle web assets into a given directory.
  26. *
  27. * @author Fabien Potencier <fabien@symfony.com>
  28. * @author Gábor Egyed <gabor.egyed@gmail.com>
  29. *
  30. * @final
  31. */
  32. class AssetsInstallCommand extends Command
  33. {
  34. public const METHOD_COPY = 'copy';
  35. public const METHOD_ABSOLUTE_SYMLINK = 'absolute symlink';
  36. public const METHOD_RELATIVE_SYMLINK = 'relative symlink';
  37. protected static $defaultName = 'assets:install';
  38. private $filesystem;
  39. private $projectDir;
  40. public function __construct(Filesystem $filesystem, string $projectDir)
  41. {
  42. parent::__construct();
  43. $this->filesystem = $filesystem;
  44. $this->projectDir = $projectDir;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function configure()
  50. {
  51. $this
  52. ->setDefinition([
  53. new InputArgument('target', InputArgument::OPTIONAL, 'The target directory', null),
  54. ])
  55. ->addOption('symlink', null, InputOption::VALUE_NONE, 'Symlink the assets instead of copying them')
  56. ->addOption('relative', null, InputOption::VALUE_NONE, 'Make relative symlinks')
  57. ->addOption('no-cleanup', null, InputOption::VALUE_NONE, 'Do not remove the assets of the bundles that no longer exist')
  58. ->setDescription('Install bundle\'s web assets under a public directory')
  59. ->setHelp(<<<'EOT'
  60. The <info>%command.name%</info> command installs bundle assets into a given
  61. directory (e.g. the <comment>public</comment> directory).
  62. <info>php %command.full_name% public</info>
  63. A "bundles" directory will be created inside the target directory and the
  64. "Resources/public" directory of each bundle will be copied into it.
  65. To create a symlink to each bundle instead of copying its assets, use the
  66. <info>--symlink</info> option (will fall back to hard copies when symbolic links aren't possible:
  67. <info>php %command.full_name% public --symlink</info>
  68. To make symlink relative, add the <info>--relative</info> option:
  69. <info>php %command.full_name% public --symlink --relative</info>
  70. EOT
  71. )
  72. ;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. protected function execute(InputInterface $input, OutputInterface $output): int
  78. {
  79. /** @var KernelInterface $kernel */
  80. $kernel = $this->getApplication()->getKernel();
  81. $targetArg = rtrim($input->getArgument('target'), '/');
  82. if (!$targetArg) {
  83. $targetArg = $this->getPublicDirectory($kernel->getContainer());
  84. }
  85. if (!is_dir($targetArg)) {
  86. $targetArg = $kernel->getProjectDir().'/'.$targetArg;
  87. if (!is_dir($targetArg)) {
  88. throw new InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $targetArg));
  89. }
  90. }
  91. $bundlesDir = $targetArg.'/bundles/';
  92. $io = new SymfonyStyle($input, $output);
  93. $io->newLine();
  94. if ($input->getOption('relative')) {
  95. $expectedMethod = self::METHOD_RELATIVE_SYMLINK;
  96. $io->text('Trying to install assets as <info>relative symbolic links</info>.');
  97. } elseif ($input->getOption('symlink')) {
  98. $expectedMethod = self::METHOD_ABSOLUTE_SYMLINK;
  99. $io->text('Trying to install assets as <info>absolute symbolic links</info>.');
  100. } else {
  101. $expectedMethod = self::METHOD_COPY;
  102. $io->text('Installing assets as <info>hard copies</info>.');
  103. }
  104. $io->newLine();
  105. $rows = [];
  106. $copyUsed = false;
  107. $exitCode = 0;
  108. $validAssetDirs = [];
  109. /** @var BundleInterface $bundle */
  110. foreach ($kernel->getBundles() as $bundle) {
  111. if (!is_dir($originDir = $bundle->getPath().'/Resources/public') && !is_dir($originDir = $bundle->getPath().'/public')) {
  112. continue;
  113. }
  114. $assetDir = preg_replace('/bundle$/', '', strtolower($bundle->getName()));
  115. $targetDir = $bundlesDir.$assetDir;
  116. $validAssetDirs[] = $assetDir;
  117. if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
  118. $message = sprintf("%s\n-> %s", $bundle->getName(), $targetDir);
  119. } else {
  120. $message = $bundle->getName();
  121. }
  122. try {
  123. $this->filesystem->remove($targetDir);
  124. if (self::METHOD_RELATIVE_SYMLINK === $expectedMethod) {
  125. $method = $this->relativeSymlinkWithFallback($originDir, $targetDir);
  126. } elseif (self::METHOD_ABSOLUTE_SYMLINK === $expectedMethod) {
  127. $method = $this->absoluteSymlinkWithFallback($originDir, $targetDir);
  128. } else {
  129. $method = $this->hardCopy($originDir, $targetDir);
  130. }
  131. if (self::METHOD_COPY === $method) {
  132. $copyUsed = true;
  133. }
  134. if ($method === $expectedMethod) {
  135. $rows[] = [sprintf('<fg=green;options=bold>%s</>', '\\' === \DIRECTORY_SEPARATOR ? 'OK' : "\xE2\x9C\x94" /* HEAVY CHECK MARK (U+2714) */), $message, $method];
  136. } else {
  137. $rows[] = [sprintf('<fg=yellow;options=bold>%s</>', '\\' === \DIRECTORY_SEPARATOR ? 'WARNING' : '!'), $message, $method];
  138. }
  139. } catch (\Exception $e) {
  140. $exitCode = 1;
  141. $rows[] = [sprintf('<fg=red;options=bold>%s</>', '\\' === \DIRECTORY_SEPARATOR ? 'ERROR' : "\xE2\x9C\x98" /* HEAVY BALLOT X (U+2718) */), $message, $e->getMessage()];
  142. }
  143. }
  144. // remove the assets of the bundles that no longer exist
  145. if (!$input->getOption('no-cleanup') && is_dir($bundlesDir)) {
  146. $dirsToRemove = Finder::create()->depth(0)->directories()->exclude($validAssetDirs)->in($bundlesDir);
  147. $this->filesystem->remove($dirsToRemove);
  148. }
  149. if ($rows) {
  150. $io->table(['', 'Bundle', 'Method / Error'], $rows);
  151. }
  152. if (0 !== $exitCode) {
  153. $io->error('Some errors occurred while installing assets.');
  154. } else {
  155. if ($copyUsed) {
  156. $io->note('Some assets were installed via copy. If you make changes to these assets you have to run this command again.');
  157. }
  158. $io->success($rows ? 'All assets were successfully installed.' : 'No assets were provided by any bundle.');
  159. }
  160. return $exitCode;
  161. }
  162. /**
  163. * Try to create relative symlink.
  164. *
  165. * Falling back to absolute symlink and finally hard copy.
  166. */
  167. private function relativeSymlinkWithFallback(string $originDir, string $targetDir): string
  168. {
  169. try {
  170. $this->symlink($originDir, $targetDir, true);
  171. $method = self::METHOD_RELATIVE_SYMLINK;
  172. } catch (IOException $e) {
  173. $method = $this->absoluteSymlinkWithFallback($originDir, $targetDir);
  174. }
  175. return $method;
  176. }
  177. /**
  178. * Try to create absolute symlink.
  179. *
  180. * Falling back to hard copy.
  181. */
  182. private function absoluteSymlinkWithFallback(string $originDir, string $targetDir): string
  183. {
  184. try {
  185. $this->symlink($originDir, $targetDir);
  186. $method = self::METHOD_ABSOLUTE_SYMLINK;
  187. } catch (IOException $e) {
  188. // fall back to copy
  189. $method = $this->hardCopy($originDir, $targetDir);
  190. }
  191. return $method;
  192. }
  193. /**
  194. * Creates symbolic link.
  195. *
  196. * @throws IOException if link can not be created
  197. */
  198. private function symlink(string $originDir, string $targetDir, bool $relative = false)
  199. {
  200. if ($relative) {
  201. $this->filesystem->mkdir(\dirname($targetDir));
  202. $originDir = $this->filesystem->makePathRelative($originDir, realpath(\dirname($targetDir)));
  203. }
  204. $this->filesystem->symlink($originDir, $targetDir);
  205. if (!file_exists($targetDir)) {
  206. throw new IOException(sprintf('Symbolic link "%s" was created but appears to be broken.', $targetDir), 0, null, $targetDir);
  207. }
  208. }
  209. /**
  210. * Copies origin to target.
  211. */
  212. private function hardCopy(string $originDir, string $targetDir): string
  213. {
  214. $this->filesystem->mkdir($targetDir, 0777);
  215. // We use a custom iterator to ignore VCS files
  216. $this->filesystem->mirror($originDir, $targetDir, Finder::create()->ignoreDotFiles(false)->in($originDir));
  217. return self::METHOD_COPY;
  218. }
  219. private function getPublicDirectory(ContainerInterface $container): string
  220. {
  221. $defaultPublicDir = 'public';
  222. if (null === $this->projectDir && !$container->hasParameter('kernel.project_dir')) {
  223. return $defaultPublicDir;
  224. }
  225. $composerFilePath = ($this->projectDir ?? $container->getParameter('kernel.project_dir')).'/composer.json';
  226. if (!file_exists($composerFilePath)) {
  227. return $defaultPublicDir;
  228. }
  229. $composerConfig = json_decode(file_get_contents($composerFilePath), true);
  230. if (isset($composerConfig['extra']['public-dir'])) {
  231. return $composerConfig['extra']['public-dir'];
  232. }
  233. return $defaultPublicDir;
  234. }
  235. }