1
|
|
|
<?php |
2
|
|
|
namespace app\modules\prototype\assets; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @link http://www.diemeisterei.de/ |
6
|
|
|
* |
7
|
|
|
* @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use app\modules\prototype\models\Less; |
14
|
|
|
use yii\caching\FileDependency; |
15
|
|
|
use yii\helpers\FileHelper; |
16
|
|
|
use yii\helpers\Json; |
17
|
|
|
use yii\web\AssetBundle; |
18
|
|
|
|
19
|
|
|
class DbAsset extends AssetBundle |
20
|
|
|
{ |
21
|
|
|
const CACHE_ID = 'app\assets\SettingsAsset'; |
22
|
|
|
const SETTINGS_KEY = 'app.less'; |
23
|
|
|
const MAIN_LESS_FILE = 'main.less'; |
24
|
|
|
|
25
|
|
|
public $sourcePath = '@runtime/settings-asset'; |
26
|
|
|
|
27
|
|
|
public $css = [ |
28
|
|
|
self::MAIN_LESS_FILE, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
public $depends = [ |
32
|
|
|
// if a full Bootstrap CSS is compiled, it's recommended to disable the asset in assetManager configuration |
33
|
|
|
'yii\bootstrap\BootstrapAsset', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function init() |
37
|
|
|
{ |
38
|
|
|
parent::init(); |
39
|
|
|
|
40
|
|
|
$sourcePath = \Yii::getAlias($this->sourcePath); |
41
|
|
|
@mkdir($sourcePath); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$models = Less::find()->all(); |
44
|
|
|
$hash = sha1(Json::encode($models)); |
45
|
|
|
|
46
|
|
|
if ($hash !== \Yii::$app->cache->get(self::CACHE_ID)) { |
47
|
|
|
$lessFiles = FileHelper::findFiles($sourcePath, ['only' => ['*.less']]); |
|
|
|
|
48
|
|
|
foreach ($lessFiles as $file) { |
49
|
|
|
unlink($file); |
50
|
|
|
} |
51
|
|
|
foreach ($models as $model) { |
52
|
|
|
file_put_contents("$sourcePath/{$model->key}.less", $model->value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$dependency = new FileDependency(); |
56
|
|
|
$dependency->fileName = __FILE__; |
57
|
|
|
\Yii::$app->cache->set(self::CACHE_ID, $hash, 0, $dependency); |
58
|
|
|
@touch($sourcePath); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: