Last active
January 6, 2021 01:46
-
-
Save mdmunir/581d51e20d35d2bad41fa2a0eb6d2c85 to your computer and use it in GitHub Desktop.
Publish asset tanpa hash folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\classes; | |
use Yii; | |
use yii\base\Behavior; | |
use yii\web\AssetManager; | |
/** | |
* Description of AssetPublishBehavior | |
* | |
* @author Misbahul D Munir <[email protected]> | |
* @since 1.0 | |
*/ | |
class AssetPublishBehavior extends Behavior | |
{ | |
/** | |
* sesuaikan kebutuhan | |
*/ | |
public $maps = [ | |
'@node_modules' => 'nm', | |
'@vendor' => 'v', | |
'@bower' => 'b', | |
'@npm' => 'nm', | |
'@app' => '', | |
]; | |
protected $mapDirs = []; | |
public function init() | |
{ | |
foreach ($this->maps as $path => $to) { | |
$path = rtrim(Yii::getAlias($path), '/') . '/'; | |
$this->mapDirs[$path] = trim($to, '/'); | |
} | |
krsort($this->mapDirs); | |
} | |
/** | |
* | |
* @param AssetManager $owner | |
*/ | |
public function attach($owner) | |
{ | |
parent::attach($owner); | |
$owner->hashCallback = [$this, 'hash']; | |
} | |
public function hash($path) | |
{ | |
foreach ($this->mapDirs as $key => $to) { | |
if (strpos($path, $key) === 0) { | |
$subpath = substr($path, strlen($key)); | |
return $to ? "$to/$subpath" : $subpath; | |
} | |
} | |
return ltrim($path, '/'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return [ | |
... | |
'components' => [ | |
... | |
'assetManager' => [ | |
'as publish' => 'app\classes\AssetPublishBehavior' | |
] | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment