1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector\Content; |
4
|
|
|
|
5
|
|
|
use Enjoys\AssetsCollector\Asset; |
6
|
|
|
use Enjoys\AssetsCollector\Content\Minify\MinifyFactory; |
7
|
|
|
use Enjoys\AssetsCollector\Environment; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
use Psr\Log\NullLogger; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Reader |
16
|
|
|
* @package Enjoys\AssetsCollector\Content |
17
|
|
|
*/ |
18
|
|
|
class Reader |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Asset |
22
|
|
|
*/ |
23
|
|
|
private Asset $asset; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var false|string |
27
|
|
|
*/ |
28
|
|
|
private $content; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array{css: array<mixed>, js: array<mixed>} |
32
|
|
|
*/ |
33
|
|
|
private array $minifyOptions; |
34
|
|
|
private Environment $environment; |
35
|
|
|
/** |
36
|
|
|
* @var LoggerInterface|NullLogger |
37
|
|
|
*/ |
38
|
|
|
private LoggerInterface $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Reader constructor. |
42
|
|
|
* @param Asset $asset |
43
|
|
|
* @param array{css: array<mixed>, js: array<mixed>} $minifyOptions |
44
|
|
|
* @param LoggerInterface|null $logger |
45
|
|
|
*/ |
46
|
6 |
|
public function __construct( |
47
|
|
|
Asset $asset, |
48
|
|
|
array $minifyOptions, |
49
|
|
|
Environment $environment, |
50
|
|
|
LoggerInterface $logger = null |
51
|
|
|
) { |
52
|
6 |
|
$this->environment = $environment; |
53
|
6 |
|
$this->asset = $asset; |
54
|
6 |
|
$this->logger = $logger ?? new NullLogger(); |
55
|
|
|
|
56
|
6 |
|
$this->content = $this->getContent(); |
57
|
6 |
|
$this->minifyOptions = $minifyOptions; |
58
|
6 |
|
} |
59
|
|
|
|
60
|
6 |
|
public function getContents(): string |
61
|
|
|
{ |
62
|
6 |
|
$path = $this->asset->getPath(); |
63
|
|
|
|
64
|
6 |
|
if ($this->content === false || $path === false) { |
65
|
2 |
|
$this->logger->notice(sprintf('Nothing return: path is `%s`', (string)$path)); |
66
|
2 |
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
if ($this->asset->isUrl()) { |
70
|
|
|
$replaceRelativeUrls = new ReplaceRelativeUrls($this->content, $path); |
|
|
|
|
71
|
|
|
$replaceRelativeUrls->setLogger($this->logger); |
72
|
|
|
$this->content = $replaceRelativeUrls->getContent(); |
73
|
|
|
} else { |
74
|
4 |
|
$replaceRelativePath = new ReplaceRelativePaths( |
75
|
4 |
|
$this->content, $path, $this->environment |
|
|
|
|
76
|
|
|
); |
77
|
4 |
|
$replaceRelativePath->setLogger($this->logger); |
78
|
4 |
|
$this->content = $replaceRelativePath->getContent(); |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
if ($this->asset->isMinify()) { |
82
|
2 |
|
$this->content = MinifyFactory::minify( |
83
|
2 |
|
$this->content, |
84
|
2 |
|
$this->asset->getType(), |
85
|
2 |
|
$this->minifyOptions |
86
|
2 |
|
)->getContent() . "\n"; |
87
|
2 |
|
$this->logger->info(sprintf('Minify: %s', $path)); |
88
|
|
|
} |
89
|
4 |
|
return $this->content; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return false|string |
95
|
|
|
*/ |
96
|
6 |
|
private function getContent() |
97
|
|
|
{ |
98
|
6 |
|
if (false !== $path = $this->asset->getPath()) { |
|
|
|
|
99
|
5 |
|
if ($this->asset->isUrl()) { |
100
|
|
|
return $this->readUrl($path); |
101
|
|
|
} |
102
|
5 |
|
return $this->readFile($path); |
103
|
|
|
} |
104
|
1 |
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $url |
109
|
|
|
* @return false|string |
110
|
|
|
*/ |
111
|
|
|
private function readUrl(string $url) |
112
|
|
|
{ |
113
|
|
|
try { |
114
|
|
|
$client = new Client( |
115
|
|
|
[ |
116
|
|
|
'verify' => false, |
117
|
|
|
'allow_redirects' => true, |
118
|
|
|
'headers' => [ |
119
|
|
|
'User-Agent' => |
120
|
|
|
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36', |
121
|
|
|
] |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
$response = $client->get($url); |
125
|
|
|
$this->logger->info(sprintf('Read: %s', $url)); |
126
|
|
|
return $response->getBody()->getContents(); |
127
|
|
|
} catch (ClientException | GuzzleException $e) { |
128
|
|
|
$this->logger->notice($e->getMessage()); |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $filename |
135
|
|
|
* @return false|string |
136
|
|
|
*/ |
137
|
5 |
|
private function readFile(string $filename) |
138
|
|
|
{ |
139
|
|
|
//Clear the most recent error |
140
|
5 |
|
error_clear_last(); |
141
|
|
|
|
142
|
5 |
|
if (!file_exists($filename)) { |
143
|
|
|
$this->logger->notice(sprintf("Файла по указанному пути нет: %s", $filename)); |
144
|
|
|
return false; |
145
|
|
|
} |
146
|
5 |
|
$content = @file_get_contents($filename); |
147
|
|
|
|
148
|
|
|
/** @var null|string[] $error */ |
149
|
5 |
|
$error = error_get_last(); |
150
|
5 |
|
if ($error !== null) { |
151
|
1 |
|
$this->logger->notice(sprintf("Ошибка чтения содержимого файла: %s", $error['message'])); |
152
|
1 |
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
4 |
|
$this->logger->info(sprintf('Read: %s', $filename)); |
156
|
4 |
|
return $content; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param LoggerInterface|NullLogger $logger |
161
|
|
|
*/ |
162
|
|
|
public function setLogger($logger): void |
163
|
|
|
{ |
164
|
|
|
$this->logger = $logger; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|