|
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
|
|
|
* @var false|string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $path; |
|
33
|
|
|
|
|
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 Environment $environment |
|
44
|
|
|
* @param LoggerInterface|null $logger |
|
45
|
|
|
*/ |
|
46
|
7 |
|
public function __construct( |
|
47
|
|
|
Asset $asset, |
|
48
|
|
|
Environment $environment, |
|
49
|
|
|
LoggerInterface $logger = null |
|
50
|
|
|
) { |
|
51
|
7 |
|
$this->environment = $environment; |
|
52
|
7 |
|
$this->asset = $asset; |
|
53
|
7 |
|
$this->logger = $logger ?? new NullLogger(); |
|
54
|
7 |
|
$this->path = $this->asset->getPath(); |
|
55
|
7 |
|
$this->content = $this->getContent(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
7 |
|
public function getContents(): string |
|
62
|
|
|
{ |
|
63
|
7 |
|
if ($this->content === false || $this->path === false) { |
|
64
|
2 |
|
$this->logger->notice(sprintf('Nothing return: path is `%s`', $this->asset->getOrigPath())); |
|
65
|
2 |
|
return ''; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
if ($this->asset->isReplaceRelativeUrls()) { |
|
69
|
4 |
|
$replaceRelativeUrls = new ReplaceRelative($this->content, $this->path, $this->asset, $this->environment); |
|
|
|
|
|
|
70
|
4 |
|
$replaceRelativeUrls->setLogger($this->logger); |
|
71
|
4 |
|
$this->content = $replaceRelativeUrls->getContent(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
if ($this->asset->isMinify()) { |
|
75
|
2 |
|
$this->content = MinifyFactory::minify( |
|
76
|
2 |
|
$this->content, |
|
|
|
|
|
|
77
|
2 |
|
$this->asset->getType(), |
|
78
|
2 |
|
$this->environment |
|
79
|
2 |
|
)->getContent() . "\n"; |
|
80
|
2 |
|
$this->logger->info(sprintf('Minify: %s', $this->path)); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
5 |
|
return $this->content; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return false|string |
|
88
|
|
|
*/ |
|
89
|
7 |
|
private function getContent() |
|
90
|
|
|
{ |
|
91
|
7 |
|
if ($this->path === false) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
if ($this->asset->isUrl()) { |
|
96
|
|
|
return $this->readUrl($this->path); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
6 |
|
return $this->readFile($this->path); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $url |
|
103
|
|
|
* @return false|string |
|
104
|
|
|
*/ |
|
105
|
|
|
private function readUrl(string $url) |
|
106
|
|
|
{ |
|
107
|
|
|
try { |
|
108
|
|
|
$client = new Client( |
|
109
|
|
|
[ |
|
110
|
|
|
'verify' => false, |
|
111
|
|
|
'allow_redirects' => true, |
|
112
|
|
|
'headers' => [ |
|
113
|
|
|
'User-Agent' => |
|
114
|
|
|
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36', |
|
115
|
|
|
] |
|
116
|
|
|
] |
|
117
|
|
|
); |
|
118
|
|
|
$response = $client->get($url); |
|
119
|
|
|
$this->logger->info(sprintf('Read: %s', $url)); |
|
120
|
|
|
return $response->getBody()->getContents(); |
|
121
|
|
|
} catch (ClientException | GuzzleException $e) { |
|
122
|
|
|
$this->logger->notice($e->getMessage()); |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $filename |
|
129
|
|
|
* @return false|string |
|
130
|
|
|
*/ |
|
131
|
6 |
|
private function readFile(string $filename) |
|
132
|
|
|
{ |
|
133
|
|
|
//Clear the most recent error |
|
134
|
6 |
|
error_clear_last(); |
|
135
|
|
|
|
|
136
|
6 |
|
if (!file_exists($filename)) { |
|
137
|
|
|
$this->logger->notice(sprintf("Файла по указанному пути нет: %s", $filename)); |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
6 |
|
$content = @file_get_contents($filename); |
|
141
|
|
|
|
|
142
|
|
|
/** @var null|string[] $error */ |
|
143
|
6 |
|
$error = error_get_last(); |
|
144
|
6 |
|
if ($error !== null) { |
|
145
|
1 |
|
$this->logger->notice(sprintf("Ошибка чтения содержимого файла: %s", $error['message'])); |
|
146
|
1 |
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
5 |
|
$this->logger->info(sprintf('Read: %s', $filename)); |
|
150
|
5 |
|
return $content; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param LoggerInterface|NullLogger $logger |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setLogger($logger): void |
|
157
|
|
|
{ |
|
158
|
|
|
$this->logger = $logger; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|