1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jclyons52\PagePreview; |
4
|
|
|
|
5
|
|
|
use Jclyons52\PagePreview\Cache\Cache; |
6
|
|
|
use Jclyons52\PHPQuery\Document; |
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
8
|
|
|
|
9
|
|
|
class PreviewManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Crawler |
13
|
|
|
*/ |
14
|
|
|
private $crawler; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Cache |
18
|
|
|
*/ |
19
|
|
|
private $cache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Url object |
23
|
|
|
* @var Url |
24
|
|
|
*/ |
25
|
|
|
private $url; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var HttpInterface |
29
|
|
|
*/ |
30
|
|
|
protected $http; |
31
|
|
|
|
32
|
|
|
|
33
|
90 |
|
public function __construct(HttpInterface $http, CacheItemPoolInterface $cache = null) |
34
|
|
|
{ |
35
|
90 |
|
$this->http = $http; |
36
|
90 |
|
if ($cache !== null) { |
37
|
3 |
|
$this->cache = new Cache($cache); |
38
|
3 |
|
} |
39
|
90 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Instantiate class with dependencies |
43
|
|
|
* @param CacheItemPoolInterface $cache |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
6 |
|
public static function create(CacheItemPoolInterface $cache = null) |
47
|
|
|
{ |
48
|
6 |
|
$http = new Http(); |
49
|
6 |
|
return new PreviewManager($http, $cache); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $url |
54
|
|
|
* @return Preview |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
51 |
|
public function fetch($url = null) |
58
|
|
|
{ |
59
|
51 |
|
if ($url instanceof Url) { |
60
|
|
|
$this->url = $url; |
61
|
|
|
} |
62
|
51 |
|
if (is_string($url)) { |
63
|
48 |
|
$this->url = new Url($url); |
64
|
45 |
|
} |
65
|
48 |
|
if ($this->url === null) { |
66
|
|
|
throw new \Exception('failed to get url'); |
67
|
|
|
} |
68
|
|
|
|
69
|
48 |
|
$body = $this->http->get($this->url->original); |
70
|
|
|
|
71
|
48 |
|
if ($body === false) { |
72
|
3 |
|
throw new \Exception('failed to load page'); |
73
|
|
|
} |
74
|
|
|
|
75
|
45 |
|
$document = new Document($body); |
76
|
|
|
|
77
|
45 |
|
$this->crawler = new Crawler($document); |
78
|
|
|
|
79
|
45 |
|
return $this->getPreview(); |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
public function findUrl($text) |
83
|
|
|
{ |
84
|
3 |
|
$this->url = Url::findFirst($text); |
85
|
|
|
|
86
|
3 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
public function findOrFetch($url) |
90
|
|
|
{ |
91
|
6 |
|
if ($this->cache === null) { |
92
|
3 |
|
return $this->fetch($url); |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
return $this->cache->get($url); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
public function cache(Preview $preview) |
99
|
|
|
{ |
100
|
3 |
|
$this->cache->set($preview); |
101
|
3 |
|
} |
102
|
|
|
/** |
103
|
|
|
* returns an instance of Preview |
104
|
|
|
* @return Preview |
105
|
|
|
*/ |
106
|
45 |
|
private function getPreview() |
107
|
|
|
{ |
108
|
45 |
|
$title = $this->crawler->title(); |
109
|
|
|
|
110
|
45 |
|
$images = $this->images(); |
111
|
|
|
|
112
|
45 |
|
$description = $this->crawler->meta('description'); |
113
|
|
|
|
114
|
45 |
|
$meta = $this->crawler->meta(); |
115
|
|
|
|
116
|
45 |
|
$keywords = $this->crawler->metaKeywords(); |
117
|
|
|
|
118
|
45 |
|
if ($keywords !== []) { |
119
|
39 |
|
$meta['keywords'] = $keywords; |
120
|
39 |
|
} |
121
|
|
|
|
122
|
45 |
|
$media = new Media($this->http); |
123
|
|
|
|
124
|
45 |
|
return new Preview($media, [ |
125
|
45 |
|
'title' => $title, |
126
|
45 |
|
'images' => $images, |
127
|
45 |
|
'description' => $description, |
128
|
45 |
|
'url' => $this->url->original, |
129
|
45 |
|
'meta' => $meta, |
130
|
45 |
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
45 |
|
private function images() |
134
|
|
|
{ |
135
|
45 |
|
$urls = $this->crawler->images(); |
136
|
45 |
|
$result = []; |
137
|
45 |
|
foreach ($urls as $url) { |
138
|
42 |
|
$result[] = $this->url->formatRelativeToAbsolute($url); |
139
|
45 |
|
} |
140
|
45 |
|
return $result; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|