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