拘留 / 资产
PHP的资产管理从kriswall/smith/asset更新
Requires
- php: >=5.3.1
- symfony/process: ~2.1|~3.0|~4.0
Requires (Dev)
- leafo/lessphp: ^0.3.7
- leafo/scssphp: ~0.1
- meenie/javascript-packer: ^1.1
- mrclay/minify: <2.3
- natxet/cssmin: 3.0.4
- patchwork/jsqueeze: ~1.0|~2.0
- phpunit/phpunit: ~4.8 || ^5.6
- psr/log: ~1.0
- ptachoire/cssembed: ~1.0
- symfony/phpunit-bridge: ~2.7|~3.0
- twig/twig: ~1.23|~2.0
- yfix/packager: dev-master
Suggests
- leafo/lessphp: Assetic provides the integration with the lessphp LESS compiler
- leafo/scssphp: Assetic provides the integration with the scssphp SCSS compiler
- leafo/scssphp-compass: Assetic provides the integration with the SCSS compass plugin
- patchwork/jsqueeze: Assetic provides the integration with the JSqueeze JavaScript compressor
- ptachoire/cssembed: Assetic provides the integration with phpcssembed to embed data uris
- twig/twig: Assetic provides the integration with the Twig templating engine
Conflicts
- twig/twig: <1.27
- v1.5.0
- dev-master / 1.4.x-dev
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.1
- v1.2.0
- v1.2.0-alpha1
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.1.0-beta1
- v1.1.0-alpha4
- v1.1.0-alpha3
- v1.1.0-alpha2
- v1.1.0-alpha1
- v1.0.5
- v1.0.4
- v1.0.3
- dev-whitesource/configure
- dev-node-visitor
- dev-experimental/tree
- dev-pipeline-asset-locator
- dev-asset-locator
- dev-output-collisions
This package is auto-updated.
Last update: 2024-08-29 04:48:27 UTC
README
Assetic是PHP的资产管理框架。
项目状态
该项目不再维护。开发已转移至https://github.com/assetic-php/assetic,包名为asset/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编译成JavaScriptCompassFilter
:Compass CSS创作框架CssEmbedFilter
:在样式表中嵌入图像数据CssImportFilter
:内联导入的样式表CssMinFilter
:最小化CSSCleanCssFilter
:最小化CSSCssRewriteFilter
:在移动到新URL时修复CSS资产中的相对URLDartFilter
:使用dart2js编译JavaScriptEmberPrecompileFilter
:预编译Handlebars模板为JavaScript以供Ember.js框架使用GoogleClosure\CompilerApiFilter
:使用Google Closure Compiler API编译JavaScriptGoogleClosure\CompilerJarFilter
:使用Google Closure Compiler JAR编译JavaScriptGssFilter
:使用Google Closure Stylesheets Compiler编译CSSHandlebarsFilter
:将Handlebars模板编译成JavaScriptJpegoptimFilter
:优化你的JPEGJpegtranFilter
:优化你的JPEGJSMinFilter
:最小化JavaScriptJSMinPlusFilter
:最小化JavaScriptJSqueezeFilter
:压缩JavaScriptLessFilter
:将LESS解析成CSS(使用less.js与node.js)LessphpFilter
:将LESS解析成CSS(使用lessphp)OptiPngFilter
:优化你的PNGPackagerFilter
:解析JavaScript中的打包标签PackerFilter
:使用Dean Edwards的Packer压缩JavaScriptPhpCssEmbedFilter
:在样式表中嵌入图像数据PngoutFilter
:优化你的PNGReactJsxFilter
:将React JSX编译成JavaScriptSass\SassFilter
:将SASS解析成CSSSass\ScssFilter
:将SCSS解析成CSSSassphpFilter
:使用Libsass的sassphp绑定将Sass解析成CSSScssphpFilter
:使用scssphp解析SCSSSeparatorFilter
:在资产之间插入分隔符以防止合并失败SprocketsFilter
:Sprockets JavaScript依赖管理StylusFilter
:将STYL解析成CSSTypeScriptFilter
:将TypeScript解析成JavaScriptUglifyCssFilter
:最小化CSSUglifyJs2Filter
:最小化JavaScriptUglifyJsFilter
:最小化JavaScriptYui\CssCompressorFilter
:使用YUI压缩器压缩CSSYui\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
,当工厂处于调试模式时,将忽略该过滤器。
您还可以在工厂上注册工作者,它所创建的所有资产在返回之前都将传递给工作者的 process()
方法。下面是关于 缓存失效 的示例。
将资产导出到静态文件
您可以将资产管理器持有的所有资产导出到目录中的文件。这通常在您的 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));
一旦设置好,该扩展将公开样式表和类似 asset 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);