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...
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With