-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathExtractorFactory.php
94 lines (79 loc) · 3.06 KB
/
ExtractorFactory.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
<?php
declare(strict_types = 1);
namespace Embed;
use Embed\Http\Crawler;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
class ExtractorFactory
{
private string $default = Extractor::class;
private array $adapters = [
'slides.com' => Adapters\Slides\Extractor::class,
'pinterest.com' => Adapters\Pinterest\Extractor::class,
'flickr.com' => Adapters\Flickr\Extractor::class,
'snipplr.com' => Adapters\Snipplr\Extractor::class,
'play.cadenaser.com' => Adapters\CadenaSer\Extractor::class,
'ideone.com' => Adapters\Ideone\Extractor::class,
'gist.github.com' => Adapters\Gist\Extractor::class,
'github.com' => Adapters\Github\Extractor::class,
'wikipedia.org' => Adapters\Wikipedia\Extractor::class,
'archive.org' => Adapters\Archive\Extractor::class,
'sassmeister.com' => Adapters\Sassmeister\Extractor::class,
'facebook.com' => Adapters\Facebook\Extractor::class,
'instagram.com' => Adapters\Instagram\Extractor::class,
'imageshack.com' => Adapters\ImageShack\Extractor::class,
'youtube.com' => Adapters\Youtube\Extractor::class,
'twitch.tv' => Adapters\Twitch\Extractor::class,
'bandcamp.com' => Adapters\Bandcamp\Extractor::class,
'twitter.com' => Adapters\Twitter\Extractor::class,
'x.com' => Adapters\Twitter\Extractor::class,
];
private array $customDetectors = [];
private array $settings;
public function __construct(?array $settings = [])
{
$this->settings = $settings ?? [];
}
public function createExtractor(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler): Extractor
{
$host = $uri->getHost();
$class = $this->default;
foreach ($this->adapters as $adapterHost => $adapter) {
if (substr($host, -strlen($adapterHost)) === $adapterHost) {
$class = $adapter;
break;
}
}
/** @var Extractor $extractor */
$extractor = new $class($uri, $request, $response, $crawler);
$extractor->setSettings($this->settings);
foreach ($this->customDetectors as $name => $detector) {
$extractor->addDetector($name, new $detector($extractor));
}
foreach ($extractor->createCustomDetectors() as $name => $detector) {
$extractor->addDetector($name, $detector);
}
return $extractor;
}
public function addAdapter(string $pattern, string $class): void
{
$this->adapters[$pattern] = $class;
}
public function addDetector(string $name, string $class): void
{
$this->customDetectors[$name] = $class;
}
public function removeAdapter(string $pattern): void
{
unset($this->adapters[$pattern]);
}
public function setDefault(string $class): void
{
$this->default = $class;
}
public function setSettings(array $settings): void
{
$this->settings = $settings;
}
}