Skip to content

Instantly share code, notes, and snippets.

@lologhi
Forked from Remiii/AssetsUpdateStaticFilesCommand.php
Last active September 10, 2015 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lologhi/e022bd15eb705ac8d50c to your computer and use it in GitHub Desktop.
Save lologhi/e022bd15eb705ac8d50c to your computer and use it in GitHub Desktop.
Assetic + S3 = ❤️

Assetic + S3 = ❤️

Some info about my Assetic configuration with S3.

Remarks:

  • All my Assets vendor are downloaded with Bower in web/vendor
  • 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 command AssetsUpdateStaticFilesCommand.php with list of assets in the conf file app/config/staticFiles.yml .

<?php
namespace Bundle\Services;
use Aws\S3\S3Client;
class AmazonS3
{
protected $s3;
public function __construct($key, $secret, $region)
{
$aws = array(
'credentials' => array(
'key' => $key,
'secret' => $secret,
),
'region' => $region,
'version'=> 'latest'
);
$this->s3 = new S3Client($aws);
}
public function registerStreamWrapper()
{
$this->s3->registerStreamWrapper();
}
}
<?php
class AppKernel extends Kernel
{
// ...
public function boot()
{
parent::boot();
$s3client = $this->container->get('amazon_s3');
$s3client->registerStreamWrapper();
}
}
<?php
namespace Bundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;
class AssetsUpdateStaticFilesCommand extends ContainerAwareCommand
{
protected $filename;
protected function configure()
{
$this
->setname('utils:assets:update-static-files')
->setDescription('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($this->getContainer()->get('kernel')->getRootDir() . '/config/staticFiles.yml'));
$staticFiles = $datas['staticFiles'];
$bucket = $this->getContainer()->getParameter('aws_assets_bucket_name');
$s3client = $this->getContainer()->get('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>');
}
}
}
}
{
...
"require": {
...
"aws/aws-sdk-php": "^3.3"
},
...
}
assetic:
write_to: "s3://%aws_assets_bucket_name%"
parameters:
aws_key: blabla
aws_secret: blablalbla
aws_region: 'ap-southeast-1'
aws_assets_bucket_name: villafinder-assets.s3.amazonaws.com
services:
amazon.s3:
class: Bundle\Services\AmazonS3
arguments: [%aws_key%, %aws_secret%, %aws_region%]
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/fontawesome/fonts/FontAwesome.otf
- vendor/fontawesome/fonts/fontawesome-webfont.eot
- vendor/fontawesome/fonts/fontawesome-webfont.svg
- vendor/fontawesome/fonts/fontawesome-webfont.ttf
- vendor/fontawesome/fonts/fontawesome-webfont.woff
- vendor/fontawesome/fonts/fontawesome-webfont.woff2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment