资产 - 无法写资产

时间:2013-01-26 15:23:46

标签: php assetic

这是我的目录结构(这只是一个测试项目):

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

这是我应该生成资产的简单脚本('run'文件):

#!/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'的文件?有帮助吗?

P.S。决议:

确定。洙。显然我必须跑

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

然后它起作用了......

1 个答案:

答案 0 :(得分:2)

文档中并不完全清楚,但在Dumping Assets to static files部分确实提到了

$writer->writeManagerAssets($am);

“将利用资产的目标路径。”所以是的,正如您建议在编写资产之前设置目标路径将解决问题:

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