assetic - unable to write assets
Asked Answered
W

1

10

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...

Welldressed answered 26/1, 2013 at 15:23 Comment(2)
Please post your complete solution as an answer to your own question to help other people.Perichondrium
Post your solution as an answer and mark it as accepted for the benefit of other people looking for a similar solution.Stamina
P
2

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');   
Precipitancy answered 9/2, 2014 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.