AssetsInstallCommand.php 3.0 KB

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