|
<?php |
|
|
|
namespace remiii\UtilsBundle\Command\Assets ; |
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand ; |
|
use Symfony\Component\Console\Input\InputArgument ; |
|
use Symfony\Component\Console\Input\InputInterface ; |
|
use Symfony\Component\Console\Input\InputOption ; |
|
use Symfony\Component\Console\Output\OutputInterface ; |
|
|
|
use Symfony\Component\Yaml\Parser ; |
|
|
|
use Aws\Common\Aws ; |
|
|
|
class AssetsUpdateStaticFilesCommand extends ContainerAwareCommand |
|
{ |
|
|
|
protected $filename ; |
|
|
|
protected function configure ( ) |
|
{ |
|
$this |
|
-> setname ( 'remiii:utils:assets:update-static-files' ) |
|
-> setDescription ( 'Utils: Update assets static files on S3 storage' ) ; |
|
} |
|
|
|
/** |
|
* @see Command |
|
*/ |
|
protected function execute ( InputInterface $input , OutputInterface $output ) |
|
{ |
|
|
|
// AWS Doc: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-s3.html |
|
|
|
// Confirmation part |
|
$dialog = $this -> getHelperSet ( ) -> get ( 'dialog' ) ; |
|
if ( ! $dialog -> askConfirmation ( |
|
$output , |
|
'<error>This command do fat stuff on your assets S3 storage! Are you sure to continue?</error>' , |
|
false |
|
) ) |
|
{ |
|
return ; |
|
} |
|
$output -> writeln ( '<info>Update assets static files on S3 storage</info>' ) ; |
|
|
|
$yaml = new Parser ( ) ; |
|
$datas = $yaml -> parse ( file_get_contents ( __DIR__ . '/../../Resources/config/staticFiles.yml' ) ) ; |
|
$staticFiles = $datas [ 'staticFiles' ] ; |
|
|
|
$bucket = $this -> getContainer ( ) -> getParameter ( 'aws_assets_bucket_name' ) ; |
|
|
|
$s3client = $this -> getContainer ( ) -> get ( 'remiii.amazon.s3' ) ; |
|
$s3client -> registerStreamWrapper ( ) ; |
|
|
|
foreach ( $staticFiles as $staticFile ) |
|
{ |
|
$pathFile = $this -> getContainer ( ) -> get ( 'kernel' ) -> getRootDir ( ) . '/../web/' . $staticFile ; |
|
$output -> writeln ( '<info>Upload ' . $staticFile . ' (' . filesize ( $pathFile ) . 'bytes)</info>' ) ; |
|
try { |
|
$handle = fopen ( $pathFile , 'r' ) ; |
|
$stream = fopen ( 's3://' . $bucket . '/' . $staticFile , 'w' ) ; |
|
fwrite ( $stream , fread ( $handle , filesize ( $pathFile ) ) ) ; |
|
fclose ( $stream ) ; |
|
fclose ( $handle ) ; |
|
} catch ( Exception $e ) { |
|
$output -> writeln ( '<error>Error :-(</error>' ) ; |
|
} |
|
} |
|
|
|
} |
|
|
|
} |
Things that are missing in this (still really useful Gist) :
composer.json
:app/config/parameter.yml
:services.yml
:AmazonS3.php
using the new AWS lib credential array andS3Client
class instead ofAws::factory
:AppKernel.php
:Thanks !! And the it should be publishable 👍