1 <?php
2
3 namespace Docolight\Agno\Traits;
4
5 use Yii;
6
7 /**
8 * A trait to manage your asset url. This one is tested only in Module. You can implement this trait to your module.
9 *
10 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
11 */
12 trait HasAssetsUrl
13 {
14 public $assetPath = 'asset';
15
16 /**
17 * A javascript API url for assets
18 *
19 * @var string
20 */
21 protected $assetsUrl;
22
23 /**
24 * Get absolute path of published assets
25 *
26 * @param string $pathToFile Path to asset file you want to return.
27 * If no argument was supplied, then it will return the root path of asset.
28 * @param boolean $forceCopy Whether we should copy the asset file or directory even if it is already
29 *
30 * @return string
31 */
32 public function asset($pathToFile = '', $forceCopy = false)
33 {
34 if (($this->assetsUrl === null) or $forceCopy) {
35 $assetManager = Yii::app()
36 ->getAssetManager();
37
38 $oldForceCopy = $assetManager->forceCopy;
39
40 $assetManager->forceCopy = $forceCopy;
41
42 $this->assetsUrl = $assetManager
43 ->publish(Yii::getPathOfAlias($this->getId().'.'.$this->assetPath), false, -1, $forceCopy);
44
45 // Revert state
46 $assetManager->forceCopy = $oldForceCopy;
47 }
48
49 return $pathToFile ? $this->assetsUrl.'/'.ltrim($pathToFile, '/') : $this->assetsUrl;
50 }
51 }
52