tripomatic / nette-assetic
Nette 框架的 Assetic
Requires
- php: >=5.4.0
- kriswallsmith/assetic: ^1.2
- latte/latte: ^2.3
- nette/application: ^2.3
- nette/di: ^2.3
- nette/http: ^2.3
- nette/php-generator: ^2.3
Suggests
- symfony/console: Allows using Symfony\Console commands
This package is not auto-updated.
Last update: 2024-09-28 17:49:03 UTC
README
一个轻量级的 Assetic 集成方案,用于 Nette 框架。
Tripomatic\NetteApi 以最小化方式将 Assetic 集成到 Nette 框架中,允许无限制地使用 Assetic 功能。配置与官方 Assetic 包完全兼容 - 没有引入任何额外的非必要约定。Tripomatic\NetteApi 还集成了开发环境中的资源路由,并在生产部署期间提供资源导出命令。
安装
使用 Composer 安装 Tripomatic\NetteAssetic
$ composer require tripomatic/nette-assetic
快速入门
在您的 NEON 配置 中添加 NetteAssetic 扩展
extensions: assetic: Tripomatic\NetteAssetic\DI\AsseticExtension
在相应的扩展部分配置资源
assetic: assets: cssLibs: files: - %appDir%/../vendor/bower-assets/bootstrap/dist/css/bootstrap.min.css - %appDir%/../vendor/bower-assets/bootstrap-select/dist/css/bootstrap-select.min.css cssDefault: files: - %appDir%/../document_root/css/main.css jsLibs: files: - %appDir%/../document_root/bower-assets/jquery/dist/jquery.js - %appDir%/../document_root/bower-assets/bootstrap/dist/js/bootstrap.js jsDefault: files: - %appDir%/../document_root/js/main.js
在您的 Latte 模板中包含资源
<link rel="stylesheet" href="{asset cssLibs}"> <link rel="stylesheet" href="{asset cssDefault}"> <script src="{asset jsLibs}"></script> <script src="{asset jsDefault}"></script>
配置
Tripomatic\NetteAssetic 提供简单明了的配置,与 Assetic 的设置兼容。
调试模式
调试模式将根据您的应用程序的 debugMode 自动设置。此行为可以被覆盖。
assetic: debug: TRUE # or FALSE to disable debug mode
资源路由
在调试模式下,资源将自动通过 AssetRoute 编译并服务于输出。此行为可以被覆盖。
assetic: route: TRUE # or FALSE to disable asset routing
过滤器
Assetic 过滤器 完全支持。首先,过滤器必须以类似于常用服务的方式注册。
assetic: filters: lessPhp: Assetic\Filter\LessphpFilter yuiCss: Assetic\Filter\Yui\CssCompressorFilter('/path/to/yuicompressor.jar')
然后,它们可以被分配给资源
assetic: assets: assetName: files: - %appDir%/../document_root/css/front.less - %appDir%/../document_root/css/admin.less filters: - lessPhp - ?yuiCss
在过滤器名称前加上 ? 将导致在调试模式下忽略该过滤器。
工作进程
Assetic 工作进程 完全支持。所有资源都将传递到工作进程的 process() 方法。一个典型的例子是 CacheBustingWorker
assetic: workers: - Assetic\Factory\Worker\CacheBustingWorker
缓存
资源编译和提供可能非常昂贵。因此,资源被缓存,只有当资源改变时才重新编译。默认情况下,资源仅使用 Assetic\Cache\ArrayCache 在内存中缓存。使用可以保持两个请求之间数据持久化的持久缓存会更好。
assetic: cache: Assetic\Cache\FilesystemCache(%tempDir%/assetic)
有关缓存实现的完整列表,请参阅 https://github.com/kriswallsmith/assetic/tree/master/src/Assetic/Cache。
AssetRoute 还会自动通过使用正确的缓存控制头处理 HTTP 缓存。如果资源未修改,客户端将收到 HTTP/1.1 304 Not Modified。
资源导出
尽管内容和 HTTP 缓存机制可以显著提高资源提供的速度,但每次运行应用程序都会产生开销。因此,在生产环境中,在部署期间应将资源导出到文件系统或 CDN,并由 HTTP 服务器或 CDN 网络直接提供。
将所有资源导出到目标位置的最简单方法是通过提供的 Symfony\Console 命令。NetteAssetic 会自动将命令注册到依赖注入容器中,因此您只需将命令注册到您的控制台应用程序实例中即可。
$commands = $container->findByType('Symfony\Component\Console\Command\Command'); foreach ($commands as $commandName) { $consoleApplication->add($container->getService($commandName)); }
然后可以使用以下命令将资源导出(假设您从 console.php 运行 Symfony\Console 应用程序)
$ php console.php assetic:dump
别忘了将上述行添加到您的部署脚本中。
配置选项列表
以下列出了所有可能的配置选项以及一些示例值
assetic: root: %wwwDir% output: assetic/* debug: %debugMode% route: TRUE cache: Assetic\Cache\FilesystemCache(%tempDir%/assetic) filters: lessPhp: Assetic\Filter\LessphpFilter yuiCss: Assetic\Filter\Yui\CssCompressorFilter('/path/to/yuicompressor.jar') workers: - Assetic\Factory\Worker\CacheBustingWorker assets: assetName: files: - %appDir%/../document_root/css/front.less - %appDir%/../document_root/css/admin.less filters: - lessPhp - ?yuiCss output: assetic/* root: %wwwDir% debug: TRUE variables: []
常见问题解答
如何包含 Bootstrap 字体或图片?
目前,您必须将它们列为具有固定输出的资源(Assetic 本身对于此没有更好的选项)
assetic: assets: bootstrap_glyphicons_eot: [ files: %appDir%/../vendor/bower-assets/bootstrap/dist/fonts/glyphicons-halflings-regular.eot, output: fonts/glyphicons-halflings-regular.eot ] bootstrap_glyphicons_svg: [ files: %appDir%/../vendor/bower-assets/bootstrap/dist/fonts/glyphicons-halflings-regular.svg, output: fonts/glyphicons-halflings-regular.svg ] bootstrap_glyphicons_ttf: [ files: %appDir%/../vendor/bower-assets/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf, output: fonts/glyphicons-halflings-regular.ttf ] bootstrap_glyphicons_woff: [ files: %appDir%/../vendor/bower-assets/bootstrap/dist/fonts/glyphicons-halflings-regular.woff, output: fonts/glyphicons-halflings-regular.woff ] bootstrap_glyphicons_woff2: [ files: %appDir%/../vendor/bower-assets/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2, output: fonts/glyphicons-halflings-regular.woff2 ]
许可证
Tripomatic\NetteAssetic 在 MIT 许可下发布。