2010-02-17 13:55:05 +00:00
< ? php
2011-01-15 13:29:43 +00:00
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-01-15 13:29:43 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-07-09 09:05:26 +01:00
namespace Symfony\Bundle\FrameworkBundle\Command ;
2010-02-17 13:55:05 +00:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\Console\Input\InputArgument ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Output\OutputInterface ;
use Symfony\Component\Console\Output\Output ;
2011-08-25 12:31:27 +01:00
use Symfony\Component\Finder\Finder ;
2010-02-17 13:55:05 +00:00
/**
2011-04-09 15:13:24 +01:00
* Command that places bundle web assets into a given directory .
2010-02-17 13:55:05 +00:00
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2010-02-17 13:55:05 +00:00
*/
2011-06-20 20:04:55 +01:00
class AssetsInstallCommand extends ContainerAwareCommand
2010-02-17 13:55:05 +00:00
{
2010-05-06 12:25:53 +01:00
/**
* @ see Command
*/
protected function configure ()
2010-02-17 13:55:05 +00:00
{
2010-05-06 12:25:53 +01:00
$this
-> setDefinition ( array (
2011-04-09 15:13:24 +01:00
new InputArgument ( 'target' , InputArgument :: REQUIRED , 'The target directory (usually "web")' ),
2010-05-06 12:25:53 +01:00
))
2010-11-27 16:56:55 +00:00
-> addOption ( 'symlink' , null , InputOption :: VALUE_NONE , 'Symlinks the assets instead of copying it' )
2011-07-28 08:52:34 +01:00
-> setDescription ( 'Install bundles web assets under a public web directory' )
2011-04-09 15:13:24 +01:00
-> setHelp ( <<< EOT
The < info > assets : install </ info > command installs bundle assets into a given
directory ( e . g . the web directory ) .
2011-07-28 08:52:34 +01:00
< info > php app / console assets : install web [ -- symlink ] </ info >
2011-04-09 15:13:24 +01:00
A " bundles " directory will be created inside the target directory , and the
" Resources/public " directory of each bundle will be copied into it .
To create a symlink to each bundle instead of copying its assets , use the
< info >-- symlink </ info > option .
EOT
)
2010-05-06 12:25:53 +01:00
-> setName ( 'assets:install' )
;
2010-02-17 13:55:05 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* @ see Command
*
* @ throws \InvalidArgumentException When the target directory does not exist
*/
protected function execute ( InputInterface $input , OutputInterface $output )
2010-02-17 13:55:05 +00:00
{
2010-05-07 15:09:11 +01:00
if ( ! is_dir ( $input -> getArgument ( 'target' ))) {
2010-05-06 12:25:53 +01:00
throw new \InvalidArgumentException ( sprintf ( 'The target directory "%s" does not exist.' , $input -> getArgument ( 'target' )));
}
2011-05-09 11:11:54 +01:00
if ( ! function_exists ( 'symlink' ) && $input -> getOption ( 'symlink' )) {
2011-05-09 12:52:49 +01:00
throw new \InvalidArgumentException ( 'The symlink() function is not available on your system. You need to install the assets without the --symlink option.' );
2011-05-09 11:11:54 +01:00
}
2010-05-06 12:25:53 +01:00
2011-06-20 20:04:55 +01:00
$filesystem = $this -> getContainer () -> get ( 'filesystem' );
2010-05-06 12:25:53 +01:00
2010-12-03 19:00:11 +00:00
// Create the bundles directory otherwise symlink will fail.
2011-03-26 07:37:10 +00:00
$filesystem -> mkdir ( $input -> getArgument ( 'target' ) . '/bundles/' , 0777 );
2010-12-03 19:00:11 +00:00
2011-06-20 20:04:55 +01:00
foreach ( $this -> getContainer () -> get ( 'kernel' ) -> getBundles () as $bundle ) {
2010-05-19 09:24:36 +01:00
if ( is_dir ( $originDir = $bundle -> getPath () . '/Resources/public' )) {
$targetDir = $input -> getArgument ( 'target' ) . '/bundles/' . preg_replace ( '/bundle$/' , '' , strtolower ( $bundle -> getName ()));
2010-02-17 13:55:05 +00:00
2011-04-09 15:13:24 +01:00
$output -> writeln ( sprintf ( 'Installing assets for <comment>%s</comment> into <comment>%s</comment>' , $bundle -> getNamespace (), $targetDir ));
2010-05-06 12:25:53 +01:00
$filesystem -> remove ( $targetDir );
2010-10-07 20:26:30 +01:00
if ( $input -> getOption ( 'symlink' )) {
$filesystem -> symlink ( $originDir , $targetDir );
} else {
2011-03-26 07:37:10 +00:00
$filesystem -> mkdir ( $targetDir , 0777 );
2011-08-25 12:31:27 +01:00
// We use a custom iterator to ignore VCS files
$filesystem -> mirror ( $originDir , $targetDir , Finder :: create () -> in ( $originDir ));
2010-10-07 20:26:30 +01:00
}
2010-05-06 12:25:53 +01:00
}
}
2010-02-17 13:55:05 +00:00
}
}