natsukiss/assetic

PHP 的资产管理框架

1.5.0 2021-03-22 10:04 UTC

README

Assetic 是一个 PHP 的资产管理框架。

项目状态

该项目不再维护。开发已转移到 https://github.com/assetic-php/assetic,包名为 assetic/framework

示例

<?php

use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;

$js = new AssetCollection(array(
    new GlobAsset('/path/to/js/*'),
    new FileAsset('/path/to/another.js'),
));

// the code is merged when the asset is dumped
echo $js->dump();

资源

Assetic 的资源是一种可过滤内容,可以加载和转储的对象。资源还包括元数据,其中一些可以操作,一些则不可变。

"目标路径"属性表示资源(或资源集合)应该转储的位置。

过滤器

可以对资源应用过滤器来操作它们。

<?php

use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;
use Assetic\Filter\LessFilter;
use Assetic\Filter\Yui;

$css = new AssetCollection(array(
    new FileAsset('/path/to/src/styles.less', array(new LessFilter())),
    new GlobAsset('/path/to/css/*'),
), array(
    new Yui\CssCompressorFilter('/path/to/yuicompressor.jar'),
));

// this will echo CSS compiled by LESS and compressed by YUI
echo $css->dump();

如果遍历集合,则应用到集合上的过滤器将级联到每个资源叶子上。

<?php

foreach ($css as $leaf) {
    // each leaf is compressed by YUI
    echo $leaf->dump();
}

核心在 Assetic\Filter 命名空间中提供了以下过滤器

  • AutoprefixerFilter:使用 autoprefixer 解析和更新供应商特定的属性
  • CoffeeScriptFilter:将 CoffeeScript 编译成 JavaScript
  • CompassFilter:Compass CSS 开发框架
  • CssEmbedFilter:在样式表中嵌入图像数据
  • CssImportFilter:内联导入的样式表
  • CssMinFilter:压缩 CSS
  • CleanCssFilter:压缩 CSS
  • CssRewriteFilter:在移动到新 URL 时修复 CSS 资产中的相对 URL
  • DartFilter:使用 dart2js 编译 JavaScript
  • EmberPrecompileFilter:预编译 Handlebars 模板到 JavaScript 以供 Ember.js 框架使用
  • GoogleClosure\CompilerApiFilter:使用 Google Closure Compiler API 编译 JavaScript
  • GoogleClosure\CompilerJarFilter:使用 Google Closure Compiler JAR 编译 JavaScript
  • GssFilter:使用 Google Closure Stylesheets Compiler 编译 CSS
  • HandlebarsFilter:将 Handlebars 模板编译成 JavaScript
  • JpegoptimFilter:优化你的 JPEGs
  • JpegtranFilter:优化你的 JPEGs
  • JSMinFilter:压缩 JavaScript
  • JSMinPlusFilter:压缩 JavaScript
  • JSqueezeFilter:压缩 JavaScript
  • LessFilter:使用 less.js 和 node.js 将 LESS 解析成 CSS
  • LessphpFilter:使用 lessphp 将 LESS 解析成 CSS
  • OptiPngFilter:优化你的 PNGs
  • PackagerFilter:解析包含打包标签的 JavaScript
  • PackerFilter:使用 Dean Edwards 的 Packer 压缩 JavaScript
  • PhpCssEmbedFilter:在样式表中嵌入图像数据
  • PngoutFilter:优化你的 PNGs
  • ReactJsxFilter:将 React JSX 编译成 JavaScript
  • Sass\SassFilter:将 SASS 解析成 CSS
  • Sass\ScssFilter:将 SCSS 解析成 CSS
  • SassphpFilter:使用 Libsass 的 sassphp 绑定将 Sass 解析成 CSS
  • ScssphpFilter:使用 scssphp 解析 SCSS
  • SeparatorFilter:在资源之间插入分隔符以防止合并失败
  • SprocketsFilter:Sprockets JavaScript 依赖关系管理
  • StylusFilter:将 STYL 解析成 CSS
  • TypeScriptFilter:将 TypeScript 解析成 JavaScript
  • UglifyCssFilter:压缩 CSS
  • UglifyJs2Filter:压缩 JavaScript
  • UglifyJsFilter:压缩 JavaScript
  • Yui\CssCompressorFilter:使用 YUI 压缩器压缩 CSS
  • Yui\JsCompressorFilter:使用 YUI 压缩器压缩 JavaScript

资源管理器

提供资源管理器以组织资源。

<?php

use Assetic\AssetManager;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;

$am = new AssetManager();
$am->set('jquery', new FileAsset('/path/to/jquery.js'));
$am->set('base_css', new GlobAsset('/path/to/css/*'));

资源管理器也可以用来引用资源以避免重复。

<?php

use Assetic\Asset\AssetCollection;
use Assetic\Asset\AssetReference;
use Assetic\Asset\FileAsset;

$am->set('my_plugin', new AssetCollection(array(
    new AssetReference($am, 'jquery'),
    new FileAsset('/path/to/jquery.plugin.js'),
)));

过滤器管理器

还提供了一个过滤器管理器来组织过滤器。

<?php

use Assetic\FilterManager;
use Assetic\Filter\Sass\SassFilter;
use Assetic\Filter\Yui;

$fm = new FilterManager();
$fm->set('sass', new SassFilter('/path/to/parser/sass'));
$fm->set('yui_css', new Yui\CssCompressorFilter('/path/to/yuicompressor.jar'));

资产工厂

如果您不想手动创建所有这些对象,可以使用资产工厂,它会为您做大部分工作。

<?php

use Assetic\Factory\AssetFactory;

$factory = new AssetFactory('/path/to/asset/directory/');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
$factory->setDebug(true);

$css = $factory->createAsset(array(
    '@reset',         // load the asset manager's "reset" asset
    'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/"
), array(
    'scss',           // filter through the filter manager's "scss" filter
    '?yui_css',       // don't use this filter in debug mode
));

echo $css->dump();

AssetFactory 使用根目录作为相对资产路径的基本目录进行构造。

在过滤器名称前加上问号,如这里的 yui_css,当工厂处于调试模式时,将导致该过滤器被省略。

您还可以在工厂上注册 Workers,所有创建的资产都将传递到工作者的 process() 方法,然后再返回。请参见下方的 缓存失效 示例。

将资产导出到静态文件

您可以将 AssetManager 持有的所有资产导出到目录中的文件。这通常位于您的 web 服务器文档根目录下,以便可以静态地提供文件。

<?php

use Assetic\AssetWriter;

$writer = new AssetWriter('/path/to/web');
$writer->writeManagerAssets($am);

这将使用资产的目标路径。

缓存失效

如果您像刚才描述的那样从静态文件中提供资产,您可以使用 CacheBustingWorker 重新编写资产的目标路径。它将在文件扩展名前插入一个特定版本资产的唯一标识符。

此标识符基于资产的最后修改时间,如果应用过滤器支持它,还将考虑依赖的资产。

<?php

use Assetic\Factory\AssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;

$factory = new AssetFactory('/path/to/asset/directory/');
$factory->setAssetManager($am);
$factory->setFilterManager($fm);
$factory->setDebug(true);
$factory->addWorker(new CacheBustingWorker());

$css = $factory->createAsset(array(
    '@reset',         // load the asset manager's "reset" asset
    'css/src/*.scss', // load every scss files from "/path/to/asset/directory/css/src/"
), array(
    'scss',           // filter through the filter manager's "scss" filter
    '?yui_css',       // don't use this filter in debug mode
));

echo $css->dump();

内部缓存

提供了一个简单的缓存机制来避免不必要的操作。

<?php

use Assetic\Asset\AssetCache;
use Assetic\Asset\FileAsset;
use Assetic\Cache\FilesystemCache;
use Assetic\Filter\Yui;

$yui = new Yui\JsCompressorFilter('/path/to/yuicompressor.jar');
$js = new AssetCache(
    new FileAsset('/path/to/some.js', array($yui)),
    new FilesystemCache('/path/to/cache')
);

// the YUI compressor will only run on the first call
$js->dump();
$js->dump();
$js->dump();

Twig

要使用 Assetic 的 Twig 扩展,您必须将其注册到 Twig 环境中

<?php

$twig->addExtension(new AsseticExtension($factory));

放置就绪后,该扩展将公开具有与资产工厂类似语法的样式表和 JavaScript 标签

{% stylesheets '/path/to/sass/main.sass' filter='sass,?yui_css' output='css/all.css' %}
    <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
{% endstylesheets %}

此示例将在页面上渲染一个 link 元素,其中包含可以找到已过滤资产的 URL。

当扩展处于调试模式时,此相同标签将渲染多个 link 元素,每个元素对应于 css/src/*.sass 全局中引用的每个资产。指定的过滤器仍将被应用,除非使用 ? 前缀标记为可选。

通过在标签上设置 debug 属性也可以触发此行为

{% stylesheets 'css/*' debug=true %} ... {% stylesheets %}

这些资产需要写入到 web 目录,这样这些 URL 就不会返回 404 错误。

<?php

use Assetic\AssetWriter;
use Assetic\Extension\Twig\TwigFormulaLoader;
use Assetic\Extension\Twig\TwigResource;
use Assetic\Factory\LazyAssetManager;

$am = new LazyAssetManager($factory);

// enable loading assets from twig templates
$am->setLoader('twig', new TwigFormulaLoader($twig));

// loop through all your templates
foreach ($templates as $template) {
    $resource = new TwigResource($twigLoader, $template);
    $am->addResource($resource, 'twig');
}

$writer = new AssetWriter('/path/to/web');
$writer->writeManagerAssets($am);

Assetic 基于 Python 的 webassets 库(可在 GitHub 上找到)。