forked from php-embed/Embed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractorFactory.php
More file actions
119 lines (103 loc) · 3.77 KB
/
ExtractorFactory.php
File metadata and controls
119 lines (103 loc) · 3.77 KB
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
<?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
{
/** @var class-string<Extractor> */
private string $default = Extractor::class;
/** @var array<string, class-string<Extractor>> */
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,
'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,
];
/** @var array<string, class-string<Detectors\Detector<Extractor>>> */
private array $customDetectors = [];
/** @var array<string, mixed> */
private array $settings;
/**
* @param array<string, mixed>|null $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) {
// Check if $host is the same domain as $adapterHost.
if ($host === $adapterHost) {
$class = $adapter;
break;
}
// Check if $host is a subdomain of $adapterHost.
if (substr($host, -strlen($adapterHost) - 1) === ".{$adapterHost}") {
$class = $adapter;
break;
}
}
$extractor = new $class($uri, $request, $response, $crawler);
$extractor->setSettings($this->settings);
foreach ($this->customDetectors as $name => $detectorClass) {
$detector = new $detectorClass($extractor);
$extractor->addDetector($name, $detector);
}
foreach ($extractor->createCustomDetectors() as $name => $detector) {
$extractor->addDetector($name, $detector);
}
return $extractor;
}
/**
* @param class-string<Extractor> $class
*/
public function addAdapter(string $pattern, string $class): void
{
$this->adapters[$pattern] = $class;
}
/**
* @param class-string<Detectors\Detector<Extractor>> $class
*/
public function addDetector(string $name, string $class): void
{
$this->customDetectors[$name] = $class;
}
public function removeAdapter(string $pattern): void
{
unset($this->adapters[$pattern]);
}
/**
* @param class-string<Extractor> $class
*/
public function setDefault(string $class): void
{
$this->default = $class;
}
/**
* @param array<string, mixed> $settings
*/
public function setSettings(array $settings): void
{
$this->settings = $settings;
}
}