Completed
Push — master ( 890a94...88aecf )
by Nikita
03:20
created

Resource::compress()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 40
Code Lines 20

Duplication

Lines 16
Ratio 40 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 16
loc 40
rs 4.909
cc 9
eloc 20
nc 12
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: nikita
5
 * Date: 15.08.16
6
 * Time: 22:14
7
 */
8
9
namespace samsonphp\compressor;
10
11
12
use samsonframework\filemanager\FileManagerInterface;
13
14
class Resource
15
{
16
    /** @var FileManagerInterface File system manager */
17
    protected $fileManager;
18
19
    public function __construct(FileManagerInterface $fileManager)
20
    {
21
        $this->fileManager = $fileManager;
22
    }
23
24
    public function compress(array $urls, $type, $output)
25
    {
26
        $content = '';
27
        $fileName ='';
28
        foreach ($urls as $url)
29
        {
30
            if ($this->fileManager->exists($url)) {
31
                $fileName .= $url . $this->fileManager->lastModified($url);
32
                $content .= $this->fileManager->read($url);
33
            }
34
        }
35
        $fileName = md5($fileName);
36
37
        $fileName = $fileName.'.'.$type;
38
39
        if ($type == 'css'){
40 View Code Duplication
            if (preg_match_all('/url\s*\(\s*(\'|\")*(?<url>[^\'\"\)]+)\s*(\'|\")*\)/i', $content, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
                // Если мы нашли шаблон - переберем все найденные патерны
42
                if (isset($matches['url'])) {
43
                    foreach ($matches['url'] as $url) {
44
                        $module = '';
45
                        $path = '';
46
                        // Получим путь к ресурсу используя маршрутизацию
47
                        if ($this->parseURL($url, $module, $path)) {
48
                            // Always remove first public path /www/
49
                            $path = ltrim(str_replace(__SAMSON_PUBLIC_PATH, '', $path), '/');
50
                            // Заменим путь в исходном файле
51
                            $content = str_replace($url, url()->base() . ($module == 'local' ? '' : $module . '/www/') . $path, $content);
52
                        }
53
                    }
54
                }
55
            }
56
        }
57
58
59
60
        $this->fileManager->write($output.$fileName, $content);
61
62
        return $fileName;
63
    }
64
65
    public  function parseURL($url, & $module = null, &$path = null)
0 ignored issues
show
Coding Style introduced by
Scope keyword "public" must be followed by a single space
Loading history...
66
    {
67
        // If we have URL to resource router
68
        if (preg_match('/'.STATIC_RESOURCE_HANDLER.'\/\?p=(((\/src\/|\/vendor\/samson[^\/]+\/)(?<module>[^\/]+)(?<path>.+))|((?<local>.+)))/ui', $url, $matches)) {
69
            if (array_key_exists('local', $matches)) {
70
                $module = 'local';
71
                $path = $matches['local'];
72
            } else {
73
                $module = $matches['module'];
74
                $path = $matches['path'];
75
            }
76
            return true;
77
        } else {
78
            return false;
79
        }
80
    }
81
}