CSS::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 5
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: egorov
5
 * Date: 13.08.2015
6
 * Time: 10:43
7
 */
8
namespace samsonphp\compressor\resource;
9
use samsonphp\compressor\Compressor;
10
use samson\core\Module;
11
12
/**
13
 * Module compression CSS resource management
14
 * @package samsonphp\compressor
15
 */
16
class CSS extends Generic
17
{
18
    /** CSS url() matching pattern */
19
    const PAT_URL = '/url\s*\(\s*(\'|\")*(?<url>[^\'\"\)]+)\s*(\'|\")*\)/i';
20
21
    /** @var Module URL resolving module  */
22
    protected $resolver;
23
24
    /** @var string[] Collection of ignored file extensions */
25
    protected $ignoredExtensions = array('php', 'js', 'md', 'map', 'dbs', 'vphp', 'less', 'gz', 'lock', 'json', 'sql', 'xml', 'yml');
26
27
    /**
28
     * @param Compressor $compressor Pointer to parent compressing object
29
     * @param Module $compressor URL resolving module
30
     * @param string[] $ignoredExtensions Collection of ignored file extensions
31
     * @param string[] $ignoredFiles Collection of ignored files
32
     * @param string[] $ignoredFolders Collection of ignored folders
33
     */
34
    public function __construct(
35
        Compressor & $compressor,
36
        Module $resolver,
37
        $ignoredExtensions = array(),
38
        $ignoredFiles = array(),
39
        $ignoredFolders = array())
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
40
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
41
        $this->resolver = $resolver;
42
43
        parent::__construct($compressor, $ignoredExtensions, $ignoredFiles, $ignoredFolders);
44
    }
45
46
    /**
47
     * Update file resource
48
     * @param string $fromFilePath Source file path
49
     * @param string $toFilePath Destination file path
50
     */
51
    protected function update($fromFilePath, $toFilePath)
52
    {
53
        // Read source file
54
        $text = file_get_contents($fromFilePath);
55
56
        // Найдем ссылки в ресурса
57
        if (preg_match_all(self::PAT_URL, $text, $matches)) {
58
            // Если мы нашли шаблон - переберем все найденные патерны
59
            if (isset($matches['url'])) {
60
                foreach ($matches['url'] as $url) {
61
                    $module = '';
62
                    $path = '';
63
                    // Получим путь к ресурсу используя маршрутизацию
64
                    if ($this->resolver->parseURL($url, $module, $path)) {
65
                        //trace($matches['url'][$i].'-'.url()->base().$module.'/'.$path);
66
                        // Always remove first public path /www/
67
                        $path = ltrim(str_replace(__SAMSON_PUBLIC_PATH, '', $path), '/');
68
                        // Заменим путь в исходном файле
69
                        $text = str_replace($url, url()->base() . ($module == 'local' ? '' : $module . '/www/') . $path, $text);
70
                    }
71
                }
72
            }
73
        }
74
75
        // Write destination file
76
        file_put_contents($toFilePath, $text);
77
    }
78
}
79