Skip to content

Commit e4e6b98

Browse files
author
Dominik Jansen
committed
feat: add support of settings to ExtractorFactory
1 parent 921d9d9 commit e4e6b98

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,22 @@ $client->setSettings([
324324
$embed = new Embed(new Crawler($client));
325325
```
326326

327-
If you need to pass settings to your detectors, you can use the `setSettings` method:
327+
If you need to pass settings to your detectors, you can add settings to the `ExtractorFactory`:
328328

329329
```php
330-
//Create the extractor
331-
$info = $embed->get($url);
330+
use Embed\Embed;
331+
use Embed\ExtractorFactory;
332332

333-
$info->setSettings([
333+
$embed = new Embed();
334+
$extractorFactory = new ExtractorFactory([
334335
'oembed:query_parameters' => [], //Extra parameters send to oembed
335336
'twitch:parent' => 'example.com', //Required to embed twitch videos as iframe
336337
'facebook:token' => '1234|5678', //Required to embed content from Facebook
337338
'instagram:token' => '1234|5678', //Required to embed content from Instagram
338339
]);
340+
$embed->setExtractorFactory($extractorFactory);
341+
342+
$info = $embed->get($url);
339343
```
340344

341345
Note: The built-in detectors does not require settings. This feature is only for convenience if you create a specific detector that requires settings.

demo/index.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66

77
function getUrl(): ?string
88
{
9-
$url = $_GET['url'] ?? null;
9+
$skipParams = ['url', 'instagram_token', 'facebook_token'];
10+
$url = getParam('url');
1011

11-
if (empty($url)) {
12+
if (!$url) {
1213
return null;
1314
}
1415

1516
//fix for unescaped urls
1617
foreach ($_GET as $name => $value) {
17-
if ($name === 'url') {
18+
if (in_array($name, $skipParams, true)) {
1819
continue;
1920
}
2021

@@ -24,6 +25,10 @@ function getUrl(): ?string
2425
return $url;
2526
}
2627

28+
function getParam(string $paramName): ?string {
29+
return $_GET[$paramName] ?? null;
30+
}
31+
2732
function getEscapedUrl(): ?string
2833
{
2934
$url = getUrl();
@@ -120,7 +125,6 @@ function printCode(?string $code, bool $asHtml = true): void
120125
'cms' => 'printText',
121126
'language' => 'printText',
122127
'languages' => 'printArray',
123-
'cms' => 'printText',
124128
];
125129
?>
126130

@@ -173,6 +177,14 @@ function printCode(?string $code, bool $asHtml = true): void
173177
<span>Url to test:</span>
174178
<input type="url" name="url" autofocus placeholder="http://" value="<?php echo getEscapedUrl(); ?>">
175179
</label>
180+
<label>
181+
<span>Instagram Token:</span>
182+
<input type="text" name="instagram_token" placeholder="1234|5678" value="<?php echo getParam('instagram_token'); ?>">
183+
</label>
184+
<label>
185+
<span>Facebook Token:</span>
186+
<input type="text" name="facebook_token" placeholder="1234|5678" value="<?php echo getParam('facebook_token') ?>">
187+
</label>
176188
</fieldset>
177189

178190
<fieldset class="action">
@@ -193,10 +205,16 @@ function printCode(?string $code, bool $asHtml = true): void
193205
'Accept-Language' => 'en-US,en;q=0.2',
194206
'Cache-Control' => 'max-age=0,no-cache',
195207
]);
208+
209+
$extractorFactory = new \Embed\ExtractorFactory(
210+
[
211+
'twitch:parent' => $_SERVER['SERVER_NAME'] === 'localhost' ? null : $_SERVER['SERVER_NAME'],
212+
'instagram:token' => getParam('instagram_token'),
213+
'facebook:token' => getParam('facebook_token'),
214+
]
215+
);
216+
$embed->setExtractorFactory($extractorFactory);
196217
$info = $embed->get(getUrl());
197-
$info->setSettings([
198-
'twitch:parent' => $_SERVER['SERVER_NAME'] === 'localhost' ? null : $_SERVER['SERVER_NAME'],
199-
]);
200218
} catch (Exception $exception) {
201219
echo '<pre>';
202220
echo $exception;

src/Embed.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ private function mustRedirect(Extractor $extractor): bool
8181

8282
return $extractor->redirect !== null;
8383
}
84+
85+
public function setExtractorFactory(ExtractorFactory $extractorFactory): void
86+
{
87+
$this->extractorFactory = $extractorFactory;
88+
}
8489
}

src/ExtractorFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ class ExtractorFactory
3333
'twitch.tv' => Adapters\Twitch\Extractor::class,
3434
];
3535
private array $customDetectors = [];
36+
private array $settings;
37+
38+
public function __construct(?array $settings = [])
39+
{
40+
$this->settings = $settings ?? [];
41+
}
3642

3743
public function createExtractor(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler): Extractor
3844
{
@@ -41,7 +47,9 @@ public function createExtractor(UriInterface $uri, RequestInterface $request, Re
4147

4248
$class = $this->adapters[$host] ?? $this->default;
4349

50+
/** @var Extractor $extractor */
4451
$extractor = new $class($uri, $request, $response, $crawler);
52+
$extractor->setSettings($this->settings);
4553

4654
foreach ($this->customDetectors as $name => $detector) {
4755
$extractor->addDetector($name, new $detector($extractor));

0 commit comments

Comments
 (0)