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

CssCompressor::_getFilePath()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 4
nop 1
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\Assets\Exception;
21
use JBZoo\Assets\FileAsset;
22
23
/**
24
 * Class CssCompressor
25
 *
26
 * @package JBZoo\Assets\Filter
27
 */
28
class CssCompressor extends CompressorAbstract
29
{
30
31
    /**
32
     * Filter process.
33
     *
34
     * @return null|string
35
     * @throws Exception
36
     */
37
    public function process()
38
    {
39
        if (($this->_asset instanceof FileAsset)) {
40
            /** @var FileAsset $file */
41
            $file = $this->_asset;
42
            $full = $file->getFullPath();
43
44
            if (!FS::isFile($full)) {
45
                throw new Exception(sprintf('Asset file "%s" not found', $full));
46
            }
47
48
            return $this->_getFilePath($full);
49
        }
50
51
        return null;
52
    }
53
54
    /**
55
     * @param string $path
56
     * @return string
57
     */
58
    protected function _getFilePath($path)
59
    {
60
        /** @var FileAsset $file */
61
        $file   = $this->_asset;
62
        $ext    = $file->getExt();
63
        $params = $file->getParams();
64
65
        if ($ext === 'css') {
66
            $newFile = $this->_getResultFile();
67
            if ($params->get('minify_css', false) === true) {
68
69
                if ($this->_isExpired()) {
70
                    $styles = $file->getContent($path);
71
                    $styles = implode(PHP_EOL, [$this->_fileHead(), $this->_compress($styles)]);
72
                    file_put_contents($newFile, $styles);
73
                }
74
75
                return $newFile;
76
            }
77
        }
78
79
        return $path;
80
    }
81
82
    /**
83
     * CSS compressing.
84
     *
85
     * @param string $code
86
     * @return mixed|string
87
     */
88
    protected function _compress($code)
89
    {
90
        $code = (string) $code;
91
92
        // remove comments
93
        $code = preg_replace('#/\*[^*]*\*+([^/][^*]*\*+)*/#ius', '', $code);
94
        $code = str_replace(
95
            array("\r\n", "\r", "\n", "\t", '  ', '    ', ' {', '{ ', ' }', '; ', ';;', ';;;', ';;;;', ';}'),
96
            array('', '', '', '', '', '', '{', '{', '}', ';', ';', ';', ';', '}'),
97
            $code
98
        ); // remove tabs, spaces, newlines, etc.
99
100
        // remove spaces after and before colons
101
        $code = preg_replace('#([a-z\-])(:\s*|\s*:\s*|\s*:)#ius', '$1:', $code);
102
103
        // spaces before "!important"
104
        $code = preg_replace('#(\s*\!important)#ius', '!important', $code);
105
106
        // trim
107
        $code = Str::trim($code);
108
109
        return $code;
110
    }
111
}
112