这是我的目录结构(这只是一个测试项目):
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...
这是我应该生成资产的简单脚本(“运行”文件):
#!/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);
所以 - 我希望来自 assets/css 的 css 文件将被合并、缩小并转储到名为“basecss.css”的公共目录中。这是我得到的例外:
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
所以 - 似乎资产试图转储名为“public”而不是“public/basecss.css”的文件?有什么帮助吗?
PS解决方案:
好的。苏。显然我必须跑
$css->setTargetPath('css_file_name.css');
然后它起作用了......