Completed
Push — master ( 08cbf3...6ef5d2 )
by Cheren
04:26
created

CompressorAbstract::_isExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * JBZoo Assets
4
 *
5
 * This file is part of the JBZoo CCK package.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package   Assets
10
 * @license   MIT
11
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
12
 * @link      https://github.com/JBZoo/Assets
13
 * @author    Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace JBZoo\Assets\Filter;
17
18
use JBZoo\Utils\FS;
19
use JBZoo\Utils\Str;
20
use JBZoo\Utils\Slug;
21
use JBZoo\Assets\FileAsset;
22
23
/**
24
 * Class CompressorAbstract
25
 *
26
 * @package JBZoo\Assets\Filter
27
 */
28
abstract class CompressorAbstract extends FilterAbstract
29
{
30
31
    /**
32
     * Code compressing.
33
     *
34
     * @param $code
35
     * @return string
36
     */
37
    abstract protected function _compress($code);
38
39
    /**
40
     * Asset file head comment.
41
     *
42
     * @return string
43
     */
44
    protected function _fileHead()
45
    {
46
        $relPath = Slug::filter($this->_asset->getSource(), '_');
47
        return implode(PHP_EOL, [
48
            '/* cacheid:' . $this->_getHash() . ' */',
49
            '/* resource:' . $relPath . ' */',
50
        ]);
51
    }
52
53
    /**
54
     * Actual asset hash.
55
     *
56
     * @return string
57
     */
58
    protected function _getHash()
59
    {
60
        /** @var FileAsset $asset */
61
        $asset  = $this->_asset;
62
        $params = $asset->getParams()->getArrayCopy();
63
64
        $hashed = [
65
            'params'       => $params,
66
            'name'         => $asset->getName(),
67
            'source'       => $asset->getSource(),
68
            'options'      => $asset->getOptions(),
69
            'dependencies' => $asset->getDependencies(),
70
            'file_md5'     => md5_file($asset->getFullPath()),
71
        ];
72
73
        $hashed = serialize($hashed);
74
        $hash   = md5($hashed);
75
76
        return $hash;
77
    }
78
79
    /**
80
     * Get new file path.
81
     *
82
     * @return string
83
     */
84
    protected function _getResultFile()
85
    {
86
        $params = $this->_asset->getParams();
87
        $path = FS::clean($params->get('cache_path') . '/' . $this->_newName(), '/');
88
89
        return $path;
90
    }
91
92
    /**
93
     * Check is current cache is expired.
94
     *
95
     * @return bool
96
     */
97
    protected function _isExpired()
98
    {
99
        $newFile    = $this->_getResultFile();
100
        $actualHash = Str::clean(FS::firstLine($newFile));
101
102
        list($expectedHash) = explode(PHP_EOL, $this->_fileHead());
103
104
        return $expectedHash !== $actualHash;
105
    }
106
107
    /**
108
     * New name by hash.
109
     *
110
     * @return string
111
     */
112
    protected function _newName()
113
    {
114
        return $this->_getHash() . '.css';
115
    }
116
}
117