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)) { |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
} |
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.