Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assetic - unable to write assets

Tags:

php

assetic

Here's my directory structure (this is just a test project):

stan@mypc:/generate_assets$ sudo tree -L 3 -p
.
├── [drwxr-xr-x]  assets
│   └── [drwxr-xr-x]  css
│       ├── [-rw-r--r--]  test1.css
│       └── [-rw-r--r--]  test2.css
├── [-rw-r--r--]  composer.json
├── [-rw-r--r--]  composer.lock
├── [drwxrwxrwx]  public
├── [-rwxr-xr-x]  run
└── [drwxr-xr-x]  vendor
     ....skipping...

Here's my simple script ('run' file) that supposed to generate assets:

#!/usr/bin/env php

<?php
    //loading composer's autoload
    chdir(__DIR__);
    if (file_exists('vendor/autoload.php')) {
        include 'vendor/autoload.php';
    }

    use Assetic\Asset\AssetCollection;
    use Assetic\Factory\AssetFactory;
    use Assetic\Asset\FileAsset;
    use Assetic\Asset\GlobAsset;
    use Assetic\Filter\LessFilter;
    use Assetic\Filter\CssMinFilter;
    use Assetic\AssetWriter;
    use Assetic\AssetManager;

    //Asset manager
    $am = new AssetManager();

    //Add all css files
    $css = new GlobAsset('assets/css/*.css', array(new CssMinFilter()));
    $am->set('basecss', $css);

    // Dumping assets into public
    $writer = new AssetWriter('public');
    $writer->writeManagerAssets($am);

So - I expected that css files from assets/css would be combined, minified and dumped into public directory under name 'basecss.css'. Here 's the exception I get:

PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Unable to write file public/' in /generate_assets/vendor/kriswallsmith/assetic/src/Assetic/AssetWriter.php:106
Stack trace:
#0 /generate_assets/vendor/kriswallsmith/assetic/src/Assetic/AssetWriter.php(65): Assetic\AssetWriter::write('public/', '*{color:green}?...')
#1 /generate_assets/vendor/kriswallsmith/assetic/src/Assetic/AssetWriter.php(54): Assetic\AssetWriter->writeAsset(Object(Assetic\Asset\GlobAsset))
#2 /generate_assets/run(31): Assetic\AssetWriter->writeManagerAssets(Object(Assetic\AssetManager))
#3 {main}
  thrown in /generate_assets/vendor/kriswallsmith/assetic/src/Assetic/AssetWriter.php on line 106

So - It seems like assetic tries to dump file named as 'public' instead of 'public/basecss.css'? Any help?

P.S. RESOLUTION:

Ok. soo. Apparently I must run

$css->setTargetPath('css_file_name.css');   

And then it worked...

like image 487
Randy Marsh Avatar asked Jan 26 '13 15:01

Randy Marsh


1 Answers

Not entirely clear from the documentation, but it does mention in the Dumping Assets to static files section that

$writer->writeManagerAssets($am);

"will make use of the assets' target path." So yes, as you suggested setting the target path before writing the asset will fix the problem:

$css->setTargetPath('css_file_name.css');   
like image 175
Todd Chaffee Avatar answered Oct 11 '22 07:10

Todd Chaffee