mbrowniebytes/yii2-clean-assets

清理yii2 web/assets/目录

安装次数: 89 662

依赖关系: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 3

开放问题: 1

类型:yii2-extension

0.1.0 2017-08-07 20:01 UTC

This package is not auto-updated.

Last update: 2024-09-23 12:44:00 UTC


README

在开发应用程序并使用Yii2资源时,当您对js和css文件进行更改,web/assets/目录将会充满缓存资源。

此扩展将删除Yii的web/assets/目录中的旧缓存。

安装

安装此扩展的首选方法是通过 composer

运行以下命令之一:

composer require mbrowniebytes/yii2-clean-assets:0.1.0

或添加

"mbrowniebytes/yii2-clean-assets": "0.1.0"

到您的composer.json的require部分。

使用方法

要使用此扩展,请将以下代码添加到您的应用程序配置中

  • 基本模板:config/console.php
  • 高级模板:common/config/main-local.php
'controllerMap' => [
    'clean-assets' => [
        'class' => 'mbrowniebytes\yii2cleanassets\CleanAssetsController',
    ],
],

然后从命令行调用扩展

/path/yii/web/assets> ls -l
08/2/2017  05:07 PM    <DIR>          1069cc51
08/2/2017  05:07 PM    <DIR>          324e82f6
08/4/2017  09:29 AM    <DIR>          8443a260
08/4/2017  10:39 AM    <DIR>          a66e14a3
08/4/2017  09:28 AM    <DIR>          a7594ccf

/path/yii> php yii clean-assets dry-run verbose keep=2
Checking web/assets/ to remove old caches ..
would have removed web/assets/1069cc51, last modified Aug 2, 2017 05:07:21 PM
would have removed web/assets/324e82f6, last modified Aug 2, 2017 05:07:22 PM
would have removed web/assets/a7594ccf, last modified Aug 4, 2017 09:28:26 PM
Done. Removed 0 web/assets/ caches

由于提供了keep=2,因此将保留最新的2个缓存。

您还可以在常见事件中调用扩展以实现资源的“自动”清理

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\commands\CleanAssetsController as CleanAssets;

class BaseController extends Controller
{
	private function cleanAssets()
	{
		// keep dev/test env clean; remove old web/assets/ caches
		if (YII_ENV != 'prod' && rand(0, 100) < 30) {
			$clean_assets = new CleanAssets('CleanAssetsController', 'command');
			$clean_assets->keep = 4;
			$clean_assets->silent = true;
			$nbr_cleanded = $clean_assets->cleanAssetDirs();
		}
	}

	public function afterAction($action, $result)
	{
		$this->cleanAssets();
		
		return parent::afterAction($action, $result);
	}
}

<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;

class MyController extends BaseController
{
    public function actionIndex()
    {
    }
}

其他参数

dry-run     	show what would happen; do not delete anything
verbose     	show more info; echo rules being run, dir/file being deleted
silent		do not echo anything
keep=#		nbr asset dirs to keep; might be one or more for app, one for toolbar, etc (default 5)
structure=a 	based on yii2 structures, set asset dirs to clean; advanced, basic, auto (default)
dirs=a,b    	list of custom asset dirs to clean, comma seperated

变更日志

2017年8月8日 标签 0.1.0

  • 问题 #1 支持高级模板
  • 公开了新的cleanAssetDirs()
  • cleanAssetDir($dir)现在需要一个参数
  • 选项现在没有减号,例如verbose用于yii 2.0.13

2015年8月25日 标签 0.0.1

  • 公开了cleanAssetDir()
  • 选项现在带有减号,例如-verbose用于更早的yii 2.0.?