Skip to content

Instantly share code, notes, and snippets.

@Remiii
Created July 24, 2015 09:55
Show Gist options
  • Save Remiii/83ecbd9639f150c53429 to your computer and use it in GitHub Desktop.
Save Remiii/83ecbd9639f150c53429 to your computer and use it in GitHub Desktop.

Revisions

  1. Remiii created this gist Jul 24, 2015.
    73 changes: 73 additions & 0 deletions AssetsUpdateStaticFilesCommand.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?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>' ) ;
    }
    }

    }

    }
    18 changes: 18 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    # Assetic + S3 = ❤️

    Some info about my Assetic configuration with S3.

    Marks:

    - All my Assets vendor are downloaded with `Bower` in `web/vendor`
    - Assets not used with Assetic (need to be in a plublic part) in `web/assets`
    - Assets used with Assetic in `.../public/...` (default conf)

    ## Base config

    In order to hook Assetic to S3 you need to update `app/config/config_prod.yml` file. That's it!

    ## PIMP config

    But all file not loaded by Assetic need to but push to S3... That's why I use the following Symfony Cmd `src/remiii/UtilsBundle/Command/Assets/AssetsUpdateStaticFilesCommand.php` with list of assets in the conf file `src/remiii/UtilsBundle/Resources/config/staticFiles.yml`
    .
    2 changes: 2 additions & 0 deletions config_prod.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    assetic:
    write_to: "s3://%aws_assets_bucket_name%"
    15 changes: 15 additions & 0 deletions staticFiles.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    staticFiles:
    # Bootstrap Fonts
    - vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.eot
    - vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.svg
    - vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf
    - vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.woff
    - vendor/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2
    # FontAwesome
    - vendor/font-awesome/fonts/fontawesome-webfont.eot
    - vendor/font-awesome/fonts/fontawesome-webfont.svg
    - vendor/font-awesome/fonts/fontawesome-webfont.ttf
    - vendor/font-awesome/fonts/fontawesome-webfont.woff
    - vendor/font-awesome/fonts/fontawesome-webfont.woff2
    # Assets not used with Assetic
    - assets/images/background-image-jumbo.jpg