AssetsInstallCommand.php 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Component\Finder\Finder;
  17. /**
  18. * Command that places bundle web assets into a given directory.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class AssetsInstallCommand extends ContainerAwareCommand
  23. {
  24. /**
  25. * @see Command
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setDefinition(array(
  31. new InputArgument('target', InputArgument::REQUIRED, 'The target directory (usually "web")'),
  32. ))
  33. ->addOption('symlink', null, InputOption::VALUE_NONE, 'Symlinks the assets instead of copying it')
  34. ->setDescription('Installs bundles web assets under a public web directory')
  35. ->setHelp(<<<EOT
  36. The <info>assets:install</info> command installs bundle assets into a given
  37. directory (e.g. the web directory).
  38. <info>php app/console assets:install web [--symlink]</info>
  39. A "bundles" directory will be created inside the target directory, and the
  40. "Resources/public" directory of each bundle will be copied into it.
  41. To create a symlink to each bundle instead of copying its assets, use the
  42. <info>--symlink</info> option.
  43. EOT
  44. )
  45. ->setName('assets:install')
  46. ;
  47. }
  48. /**
  49. * @see Command
  50. *
  51. * @throws \InvalidArgumentException When the target directory does not exist
  52. */
  53. protected function execute(InputInterface $input, OutputInterface $output)
  54. {
  55. $targetArg = rtrim($input->getArgument('target'), '/');
  56. if (!is_dir($targetArg)) {
  57. throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target')));
  58. }
  59. if (!function_exists('symlink') && $input->getOption('symlink')) {
  60. throw new \InvalidArgumentException('The symlink() function is not available on your system. You need to install the assets without the --symlink option.');
  61. }
  62. $filesystem = $this->getContainer()->get('filesystem');
  63. // Create the bundles directory otherwise symlink will fail.
  64. $filesystem->mkdir($targetArg.'/bundles/', 0777);
  65. foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
  66. if (is_dir($originDir = $bundle->getPath().'/Resources/public')) {
  67. $targetDir = $targetArg.'/bundles/'.preg_replace('/bundle$/', '', strtolower($bundle->getName()));
  68. $output->writeln(sprintf('Installing assets for <comment>%s</comment> into <comment>%s</comment>', $bundle->getNamespace(), $targetDir));
  69. $filesystem->remove($targetDir);
  70. if ($input->getOption('symlink')) {
  71. $filesystem->symlink($originDir, $targetDir);
  72. } else {
  73. $filesystem->mkdir($targetDir, 0777);
  74. // We use a custom iterator to ignore VCS files
  75. $filesystem->mirror($originDir, $targetDir, Finder::create()->in($originDir));
  76. }
  77. }
  78. }
  79. }
  80. }