-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRepository.php
235 lines (212 loc) · 4.46 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* slince template collector library
* @author Tao <[email protected]>
*/
namespace Slince\Collector;
class Repository
{
/**
* 当前链接
* @var Url
*/
protected $url;
/**
* 内容类型
* @var int
*/
protected $contentType;
/**
* 当前连接下载的内容
* @var string
*/
protected $content;
/**
* 页面链接
* @var array
*/
protected $pageUrls = [];
/**
* 图片链接
* @var array
*/
protected $imageUrls = [];
/**
* 样式链接
* @var array
*/
protected $cssUrls = [];
/**
* 脚本链接
* @var array
*/
protected $scriptUrls = [];
function __construct(
Url $url,
$content,
array $pageUrls = [],
array $imageUrls = [],
array $cssUrls = [],
array $scriptUrls = []
) {
$this->url = $url;
$this->content = $content;
$this->pageUrls = $pageUrls;
$this->imageUrls = $imageUrls;
$this->cssUrls = $cssUrls;
$this->scriptUrls = $scriptUrls;
}
/**
* 设置内容
* @param $content
*/
function setContent($content)
{
$this->content = $content;
}
/**
* 获取内容
* @return string
*/
function getContent()
{
return $this->content;
}
/**
* 设置页面链接
* @param array $pageUrls
*/
function setPageUrls(array $pageUrls)
{
$this->pageUrls = $this->handleRawUrls($pageUrls);
}
/**
* 获取页面链接
* @return array
*/
function getPageUrls()
{
return $this->pageUrls;
}
/**
* 设置图片链接
* @param array $imageUrls
*/
function setImageUrls(array $imageUrls)
{
$this->imageUrls = $this->handleRawUrls($imageUrls);
}
/**
* 获取图片链接
* @return array
*/
function getImageUrls()
{
return $this->imageUrls;
}
/**
* 设置样式链接
* @param array $cssUrls
*/
function setCssUrls(array $cssUrls)
{
$this->cssUrls = $this->handleRawUrls($cssUrls);
}
/**
* 获取样式链接
* @return array
*/
function getCssUrls()
{
return $this->cssUrls;
}
/**
* 设置脚本链接
* @param array $scriptUrls
*/
function setScriptUrls(array $scriptUrls)
{
$this->scriptUrls = $this->handleRawUrls($scriptUrls);
}
/**
* 获取脚本链接
* @return array
*/
function getScriptUrls()
{
return $this->scriptUrls;
}
/**
* 设置当前连接
* @param mixed $url
*/
public function setUrl(Url $url)
{
$this->url = $url;
}
/**
* 获取当前链接
* @return Url
*/
public function getUrl()
{
return $this->url;
}
/**
* @param int $contentType
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
}
/**
* @return int
*/
public function getContentType()
{
return $this->contentType;
}
/**
* 批量处理原生url
* @param $rawUrls
* @return array
*/
protected function handleRawUrls($rawUrls)
{
$rawUrls = array_unique($rawUrls);
$urls = [];
foreach ($rawUrls as $rawUrl) {
if (!empty($rawUrl)) {
$urls[] = $this->handleRawUrl($rawUrl);
}
}
return $urls;
}
/**
* 处理原生url
* @param $rawUrl
* @return Url
*/
protected function handleRawUrl($rawUrl)
{
if (strpos($rawUrl, 'http') !== false || substr($rawUrl, 0, 2) == '//') {
$newRawUrl = $rawUrl;
} else {
if ($rawUrl{0} !== '/') {
if ($this->url->getParameter('extension') == '') {
$pathname = rtrim($this->url->getPath(), '/') . '/' . $rawUrl;
} else {
$pathname = dirname($this->url->getPath()) . '/' . $rawUrl;
}
} else {
$pathname = $rawUrl;
}
$newRawUrl = $this->url->getOrigin() . $pathname;;
}
$url = Url::createFromUrl($newRawUrl);
$url->setRawUrl($rawUrl);
//将链接所属的repository记录下来
$url->setParameter('repository', $this);
return $url;
}
}