Completed
Push — master ( 422878...4f5158 )
by Nikolay
19s
created

Component::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Intersvyaz\AssetManager;
3
4
use Yii;
5
use yii\caching\Cache;
6
use yii\caching\CacheInterface;
7
use yii\di\Instance;
8
9
/**
10
 * Расширенный AssetManager, позволяющий рассчитывать хэш публикации ассетов по содержимому директории.
11
 * Необходимо для того, чтобы на всех нодах проекта ассеты находилить в одинаковых директориях.
12
 */
13
class Component extends \yii\web\AssetManager
14
{
15
    /**
16
     * Включение расчета хэша ассетов по содержимому директории.
17
     * @var bool
18
     */
19
    public $hashByContent = false;
20
21
    /**
22
     * Класс, реализующий возможность вычисления хэша по ФС.
23
     * @var FileSystemHashInterface
24
     */
25
    public $fileSystemHash = '\Intersvyaz\AssetManager\FileSystemHash';
26
27
    /**
28
     * Имя компонента, используемого для кеширования.
29
     * @var CacheInterface|array|string
30
     */
31
    public $cacheComponent = 'cache';
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function init()
37
    {
38
        parent::init();
39
        $this->fileSystemHash = Instance::ensure(
40
            $this->fileSystemHash,
41
            'Intersvyaz\AssetManager\FileSystemHashInterface'
42
        );
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    protected function hash($path)
49
    {
50
        if (!$this->hashByContent) {
51
            return parent::hash($path);
52
        }
53
54
        if (is_callable($this->hashCallback)) {
55
            return call_user_func($this->hashCallback, $path);
56
        }
57
58
        $path = (is_file($path) ? dirname($path) : $path);
59
        return $this->hashByContent($path);
60
    }
61
62
    /**
63
     * Расчет хэша ассетов по содержимому директории.
64
     * @param string $path
65
     * @return string
66
     */
67
    protected function hashByContent($path)
68
    {
69
        $filemtime = @filemtime($path);
70
        $key = md5(__CLASS__ . $path . $filemtime);
71
72
        /** @var Cache $cacheComponent */
73
        $cacheComponent = Yii::$app->{$this->cacheComponent};
74
75
        $hash = $cacheComponent->getOrSet($key, function () use ($path) {
76
            return $this->fileSystemHash->hashPath($path);
77
        });
78
79
        return sprintf('%x', crc32(
80
                $this->fileSystemHash->hashPathName($path) . '|' .
81
                $hash . Yii::getVersion() . '|' .
0 ignored issues
show
Bug introduced by
Are you sure $hash of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

81
                /** @scrutinizer ignore-type */ $hash . Yii::getVersion() . '|' .
Loading history...
82
                $this->linkAssets
83
            )
84
        );
85
    }
86
}
87