Assetic

PHP资产管理。Asset Management for PHP

  • Owner: kriswallsmith/assetic
  • Platform: Linux, Mac, Windows
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

Assetic

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

<?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();

Assets

Assetic 资产是可以加载和转储的可过滤内容的东西。 资产还包括元数据,其中一些可以被操纵,一些是不可变的。

Property Accessor Mutator
content getContent setContent
mtime getLastModified n/a
source root getSourceRoot n/a
source path getSourcePath n/a
target path getTargetPath setTargetPath

过滤器

可以应用过滤器来操纵资产。

<?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:优化您的JPEG
  • JpegtranFilter:优化你的JPEG
  • JSMinFilter:缩小Javascript
  • JSMinPlusFilter:缩小Javascript
  • JSqueezeFilter:压缩Javascript
  • LessFilter:将LESS解析为CSS(使用带有node.js的less.js)
  • LessphpFilter:将LESS解析为CSS(使用lessphp)
  • OptiPngFilter:优化您的PNG
  • PackagerFilter:解析包装程序标签的Javascript
  • PackerFilter:使用Dean Edwards的Packer压缩Javascript
  • PhpCssEmbedFilter:将图像数据嵌入样式表中
  • PngoutFilter:优化您的PNG
  • 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

Asset Manager

提供 Asset Manager(资产管理器)用于组织资产。

资产管理器还可用于引用资产以避免重复。

资产工厂

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

AssetFactory 使用根目录构建,该目录用作相对资产路径的基目录。

使用问号前缀过滤器名称(如 yui_css 所示)将导致在工厂处于调试模式时省略该过滤器。

您还可以在工厂中注册 Workers,并且在返回之前,它们创建的所有资产都将传递给 worker 的 process() 方法。有关示例,请参阅下面的缓存清除。

将资产转储到静态文件

您可以将 AssetManager 保存的所有资产转储到目录中的文件。这可能会低于您的网络服务器的文档根目录,因此可以静态地提供文件。

缓存爆破

如果您如上所述从静态文件提供资源,则可以使用 CacheBustingWorker 重写资产的目标路径。它将在文件扩展名之前插入一个标识符,该标识符对于特定版本的资产是唯一的。

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

内部缓存

提供简单的缓存机制以避免不必要的工作。

Twig

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

Assetic 基于 Python webassets 库(可在 GitHub 上获得)。

Main metrics

Overview
Name With Ownerkriswallsmith/assetic
Primary LanguagePHP
Program languageRuby (Language Count: 9)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2011-01-10 17:47:13
Pushed At2022-02-03 13:00:27
Last Commit At2020-10-13 15:24:01
Release Count26
Last Release Namev1.4.0 (Posted on )
First Release Namev1.0.0alpha1 (Posted on )
用户参与
Stargazers Count3.7k
Watchers Count119
Fork Count553
Commits Count1.2k
Has Issues Enabled
Issues Count439
Issue Open Count217
Pull Requests Count175
Pull Requests Open Count77
Pull Requests Close Count211
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Assetic Build Status project status

Assetic is an asset management framework for PHP.

<?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();

Assets

An Assetic asset is something with filterable content that can be loaded and
dumped. An asset also includes metadata, some of which can be manipulated and
some of which is immutable., Property, Accessor, Mutator, --------------, -----------------, ---------------, content, getContent, setContent, mtime, getLastModified, n/a, source root, getSourceRoot, n/a, source path, getSourcePath, n/a, target path, getTargetPath, setTargetPath, The "target path" property denotes where an asset (or an collection of assets) should be dumped.

Filters

Filters can be applied to manipulate assets.

<?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();

The filters applied to the collection will cascade to each asset leaf if you
iterate over it.

<?php

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

The core provides the following filters in the Assetic\Filter namespace:

  • AutoprefixerFilter: Parse and update vendor-specific properties using autoprefixer
  • CoffeeScriptFilter: compiles CoffeeScript into Javascript
  • CompassFilter: Compass CSS authoring framework
  • CssEmbedFilter: embeds image data in your stylesheets
  • CssImportFilter: inlines imported stylesheets
  • CssMinFilter: minifies CSS
  • CleanCssFilter: minifies CSS
  • CssRewriteFilter: fixes relative URLs in CSS assets when moving to a new URL
  • DartFilter: compiles Javascript using dart2js
  • EmberPrecompileFilter: precompiles Handlebars templates into Javascript for use in the Ember.js framework
  • GoogleClosure\CompilerApiFilter: compiles Javascript using the Google Closure Compiler API
  • GoogleClosure\CompilerJarFilter: compiles Javascript using the Google Closure Compiler JAR
  • GssFilter: compliles CSS using the Google Closure Stylesheets Compiler
  • HandlebarsFilter: compiles Handlebars templates into Javascript
  • JpegoptimFilter: optimize your JPEGs
  • JpegtranFilter: optimize your JPEGs
  • JSMinFilter: minifies Javascript
  • JSMinPlusFilter: minifies Javascript
  • JSqueezeFilter: compresses Javascript
  • LessFilter: parses LESS into CSS (using less.js with node.js)
  • LessphpFilter: parses LESS into CSS (using lessphp)
  • OptiPngFilter: optimize your PNGs
  • PackagerFilter: parses Javascript for packager tags
  • PackerFilter: compresses Javascript using Dean Edwards's Packer
  • PhpCssEmbedFilter: embeds image data in your stylesheet
  • PngoutFilter: optimize your PNGs
  • ReactJsxFilter: compiles React JSX into JavaScript
  • Sass\SassFilter: parses SASS into CSS
  • Sass\ScssFilter: parses SCSS into CSS
  • SassphpFilter: parses Sass into CSS using the sassphp bindings for Libsass
  • ScssphpFilter: parses SCSS using scssphp
  • SeparatorFilter: inserts a separator between assets to prevent merge failures
  • SprocketsFilter: Sprockets Javascript dependency management
  • StylusFilter: parses STYL into CSS
  • TypeScriptFilter: parses TypeScript into Javascript
  • UglifyCssFilter: minifies CSS
  • UglifyJs2Filter: minifies Javascript
  • UglifyJsFilter: minifies Javascript
  • Yui\CssCompressorFilter: compresses CSS using the YUI compressor
  • Yui\JsCompressorFilter: compresses Javascript using the YUI compressor

Asset Manager

An asset manager is provided for organizing assets.

<?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/*'));

The asset manager can also be used to reference assets to avoid duplication.

<?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'),
)));

Filter Manager

A filter manager is also provided for organizing filters.

<?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'));

Asset Factory

If you'd rather not create all these objects by hand, you can use the asset
factory, which will do most of the work for you.

<?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();

The AssetFactory is constructed with a root directory which is used as the base directory for relative asset paths.

Prefixing a filter name with a question mark, as yui_css is here, will cause
that filter to be omitted when the factory is in debug mode.

You can also register Workers on the factory and all assets created
by it will be passed to the worker's process() method before being returned. See Cache Busting below for an example.

Dumping Assets to static files

You can dump all the assets an AssetManager holds to files in a directory. This will probably be below your webserver's document root
so the files can be served statically.

<?php

use Assetic\AssetWriter;

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

This will make use of the assets' target path.

Cache Busting

If you serve your assets from static files as just described, you can use the CacheBustingWorker to rewrite the target
paths for assets. It will insert an identifier before the filename extension that is unique for a particular version
of the asset.

This identifier is based on the modification time of the asset and will also take depended-on assets into
consideration if the applied filters support it.

<?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();

Internal caching

A simple caching mechanism is provided to avoid unnecessary work.

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

To use the Assetic Twig extension you must register it to your Twig
environment:

<?php

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

Once in place, the extension exposes a stylesheets and a javascripts tag with a syntax similar
to what the asset factory uses:

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

This example will render one link element on the page that includes a URL
where the filtered asset can be found.

When the extension is in debug mode, this same tag will render multiple link
elements, one for each asset referenced by the css/src/*.sass glob. The
specified filters will still be applied, unless they are marked as optional
using the ? prefix.

This behavior can also be triggered by setting a debug attribute on the tag:

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

These assets need to be written to the web directory so these URLs don't
return 404 errors.

<?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 is based on the Python webassets library (available on
GitHub).