h4kuna/assets

此包已被废弃,不再维护。作者建议使用 68publishers/asset 包。

支持在开发机上对资产使用mtime。

v1.1.0 2021-08-03 14:38 UTC

This package is auto-updated.

Last update: 2023-02-10 06:39:13 UTC


README

尝试 68publishers/asset

Build Status Downloads this Month Latest stable Coverage Status

如果您需要浏览器自动失效缓存,请使用此扩展。

通过composer安装

$ composer require h4kuna/assets

变更日志

  • 1.1.0 无需要求,更新nette 3.0
  • 1.0.0 支持 PHP 7.1+ (严格类型)
  • 0.1.4 0.1.5 新版本支持 PHP 5.6 及以上版本
  • 0.1.3 支持 PHP 5.3

如何使用

第一步只需注册扩展,其他参数是可选的。您可以使用新的过滤器 asset

extensions:
	assets: h4kuna\Assets\DI\AssetsExtension

assets:
    # required nothing
    
    # optional
    wwwDir: %wwwDir%
    debugMode: %debugMode%
    tempDir: %tempDir%
    wwwTempDir: %wwwDir%/temp # here is place where move assets from 3rd library (from vendor/ etc.)
    externalAssets:
        - %appDir%/../vendor/nette/nette.js # save to %wwwTempDir%/nette.js
        'ext/nette2.4.js': %appDir%/../vendor/nette/nette.js # save to %wwwTempDir%/ext/nette2.4.js
        
        # download from external source, this is experimental!
        - http://example.com/foo.js # save to %wwwTempDir%/foo.js
        'sha256-secure-token': http://example.com/foo.js # check if is right file

优势

  • 无需 basePath
  • 路径相对于您的 wwwDir
  • 当发现新文件或删除 %tempDir%/cache/_assets 时构建缓存
  • 生产环境和开发环境中的行为相同
<link rel="stylesheet" href="{='css/main.css'|asset}">
<script src="{='js/main.js'|asset}"></script>

示例输出: ?file mtime

<link rel="stylesheet" href="/css/main.css?123456">
<script src="/js/main.js?456789"></script>

可以使用双斜杠启用打印模板的绝对路径

<link rel="stylesheet" href="{='//css/main.css'|asset}">

资产

这是一个可以依赖任何东西并收集css和js文件以渲染到模板的对象。

/* @var $assets \h4kuna\Assets\Assets */
$assets->addJs('ext/nette2.4.js', ['async' => TRUE]);
echo (string) $assets->renderJs();

渲染此内容

<script src="/temp/ext/nette2.4.js?456789" async></script>

自定义缓存构建器 - 高级用法

这将在编译时创建缓存。默认情况下,资产缓存是即时构建的

assetsExtension:
	cacheBuilder: \CacheBuilder

使用准备好的接口

class CacheBuilder implements \h4kuna\Assets\DI\ICacheBuilder
{
	public function create(\h4kuna\Assets\CacheAssets $cache, $wwwDir)
	{
		$finder = Nette\Utils\Finder::findFiles('*')->in($wwwDir . '/config');
		foreach ($finder as $file) {
			/* @var $file \SplFileInfo */
			$cache->load(self::replaceSlashOnWindows($file));
		}
	}


	private static function replaceSlashOnWindows(SplFileInfo $file)
	{
		static $isWindows;
		if ($isWindows === NULL) {
			$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
		}

		if ($isWindows) {
			return str_replace('\\', '/', $file->getPathname());
		}
		return $file->getPathname();
	}
}