vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\FrameworkBundle\Command;
       
    13 
       
    14 use Symfony\Component\Console\Input\InputArgument;
       
    15 use Symfony\Component\Console\Input\InputOption;
       
    16 use Symfony\Component\Console\Input\InputInterface;
       
    17 use Symfony\Component\Console\Output\OutputInterface;
       
    18 use Symfony\Component\Console\Output\Output;
       
    19 
       
    20 /**
       
    21  * Command that places bundle web assets into a given directory.
       
    22  *
       
    23  * @author Fabien Potencier <fabien@symfony.com>
       
    24  */
       
    25 class AssetsInstallCommand extends ContainerAwareCommand
       
    26 {
       
    27     /**
       
    28      * @see Command
       
    29      */
       
    30     protected function configure()
       
    31     {
       
    32         $this
       
    33             ->setDefinition(array(
       
    34                 new InputArgument('target', InputArgument::REQUIRED, 'The target directory (usually "web")'),
       
    35             ))
       
    36             ->addOption('symlink', null, InputOption::VALUE_NONE, 'Symlinks the assets instead of copying it')
       
    37             ->setDescription('Install bundles web assets under a public web directory')
       
    38             ->setHelp(<<<EOT
       
    39 The <info>assets:install</info> command installs bundle assets into a given
       
    40 directory (e.g. the web directory).
       
    41 
       
    42 <info>php app/console assets:install web [--symlink]</info>
       
    43 
       
    44 A "bundles" directory will be created inside the target directory, and the
       
    45 "Resources/public" directory of each bundle will be copied into it.
       
    46 
       
    47 To create a symlink to each bundle instead of copying its assets, use the
       
    48 <info>--symlink</info> option.
       
    49 
       
    50 EOT
       
    51             )
       
    52             ->setName('assets:install')
       
    53         ;
       
    54     }
       
    55 
       
    56     /**
       
    57      * @see Command
       
    58      *
       
    59      * @throws \InvalidArgumentException When the target directory does not exist
       
    60      */
       
    61     protected function execute(InputInterface $input, OutputInterface $output)
       
    62     {
       
    63         if (!is_dir($input->getArgument('target'))) {
       
    64             throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target')));
       
    65         }
       
    66 
       
    67         if (!function_exists('symlink') && $input->getOption('symlink')) {
       
    68             throw new \InvalidArgumentException('The symlink() function is not available on your system. You need to install the assets without the --symlink option.');
       
    69         }
       
    70 
       
    71         $filesystem = $this->getContainer()->get('filesystem');
       
    72 
       
    73         // Create the bundles directory otherwise symlink will fail.
       
    74         $filesystem->mkdir($input->getArgument('target').'/bundles/', 0777);
       
    75 
       
    76         foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
       
    77             if (is_dir($originDir = $bundle->getPath().'/Resources/public')) {
       
    78                 $targetDir = $input->getArgument('target').'/bundles/'.preg_replace('/bundle$/', '', strtolower($bundle->getName()));
       
    79 
       
    80                 $output->writeln(sprintf('Installing assets for <comment>%s</comment> into <comment>%s</comment>', $bundle->getNamespace(), $targetDir));
       
    81 
       
    82                 $filesystem->remove($targetDir);
       
    83 
       
    84                 if ($input->getOption('symlink')) {
       
    85                     $filesystem->symlink($originDir, $targetDir);
       
    86                 } else {
       
    87                     $filesystem->mkdir($targetDir, 0777);
       
    88                     $filesystem->mirror($originDir, $targetDir);
       
    89                 }
       
    90             }
       
    91         }
       
    92     }
       
    93 }