|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\MFinante\Scrapers; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\MFinante\Clients\ClientFactory; |
|
6
|
|
|
use ByTIC\MFinante\Parsers\AbstractParser; |
|
7
|
|
|
use Goutte\Client; |
|
8
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AbstractScraper |
|
12
|
|
|
* @package ByTIC\MFinante\Scrapers |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class AbstractScraper |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Crawler |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $crawler = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Client |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $client = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return AbstractParser |
|
29
|
|
|
*/ |
|
30
|
|
|
public function execute() |
|
31
|
|
|
{ |
|
32
|
|
|
$crawler = $this->getCrawler(); |
|
33
|
|
|
$parser = $this->getNewParser(); |
|
34
|
|
|
$parser->setCrawler($crawler); |
|
35
|
|
|
|
|
36
|
|
|
return $parser; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return Crawler |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function getCrawler() |
|
43
|
|
|
{ |
|
44
|
2 |
|
if (!$this->crawler) { |
|
45
|
2 |
|
$this->initCrawler(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
return $this->crawler; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
protected function initCrawler() |
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->crawler = $this->generateCrawler(); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Crawler |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected function generateCrawler(); |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Client |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function getClient() |
|
65
|
|
|
{ |
|
66
|
2 |
|
if ($this->client == null) { |
|
67
|
2 |
|
$this->initClient(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return $this->client; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
protected function initClient() |
|
74
|
|
|
{ |
|
75
|
2 |
|
$client = $this->generateClient(); |
|
76
|
2 |
|
$this->setClient($client); |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param Client $client |
|
81
|
|
|
*/ |
|
82
|
2 |
|
public function setClient($client) |
|
83
|
|
|
{ |
|
84
|
2 |
|
$this->client = $client; |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return Client |
|
89
|
|
|
*/ |
|
90
|
2 |
|
protected function generateClient() |
|
91
|
|
|
{ |
|
92
|
2 |
|
return ClientFactory::getPhantomJsClient(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return AbstractParser |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function getNewParser() |
|
99
|
|
|
{ |
|
100
|
|
|
$class = $this->getParserName(); |
|
101
|
|
|
/** @var AbstractParser $parser */ |
|
102
|
|
|
$parser = new $class(); |
|
103
|
|
|
$parser->setScraper($this); |
|
104
|
|
|
return $parser; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function getParserName() |
|
111
|
|
|
{ |
|
112
|
|
|
$fullClassName = get_class($this); |
|
113
|
|
|
$partsClassName = explode('\\', $fullClassName); |
|
114
|
|
|
$classFirstName = array_pop($partsClassName); |
|
115
|
|
|
$classNamespacePath = implode('\\', $partsClassName); |
|
116
|
|
|
$classNamespacePath = str_replace('\Scrapers', '', $classNamespacePath); |
|
117
|
|
|
|
|
118
|
|
|
return $classNamespacePath.'\Parsers\\'.$classFirstName; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|