Skip to content

Commit f04e004

Browse files
committed
Allow to register more detectors
1 parent 27919d3 commit f04e004

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,34 @@ $factory->removeAdapter('pinterest.com');
250250
$factory->setDefault(CustomExtractor::class);
251251
```
252252

253+
### Detectors
254+
255+
Embed comes with several predefined detectors, but you may want to change or add more. Just create a class extending `Embed\Detectors\Detector` class and register it in the extractor factory. For example:
256+
257+
```php
258+
use Embed\Embed;
259+
use Embed\Detectors\Detector;
260+
261+
class Robots extends Detector
262+
{
263+
public function detect(): ?string
264+
{
265+
$response = $this->extractor->getResponse();
266+
$document = $this->extractor->getDocument();
267+
268+
return $response->getHeaderLine('x-robots-tag'),
269+
?: $document->meta('robots');
270+
}
271+
}
272+
273+
//Register the detector
274+
$embed = new Embed();
275+
$embed->getExtractorFactory()->addDetector('robots', Robots::class);
276+
277+
//Use it
278+
$info = $embed->get('http://example.com');
279+
$robots = $info->robots;
280+
```
253281
---
254282

255283
If this library is useful for you, say thanks [buying me a beer :beer:](https://www.paypal.me/oscarotero)!

src/Extractor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class Extractor
4343
private OEmbed $oembed;
4444
private LinkedData $linkedData;
4545

46+
private array $customDetectors = [];
47+
4648
protected AuthorName $authorName;
4749
protected AuthorUrl $authorUrl;
4850
protected Cms $cms;
@@ -99,7 +101,7 @@ public function __construct(UriInterface $uri, RequestInterface $request, Respon
99101

100102
public function __get(string $name)
101103
{
102-
$detector = $this->$name ?? null;
104+
$detector = $this->customDetectors[$name] ?? $this->$name ?? null;
103105

104106
if (!$detector || !($detector instanceof Detector)) {
105107
throw new DomainException(sprintf('Invalid key "%s". No detector found for this value', $name));
@@ -108,6 +110,11 @@ public function __get(string $name)
108110
return $detector->get();
109111
}
110112

113+
public function addDetector(string $name, Detector $detector): void
114+
{
115+
$this->customDetectors[$name] = $detector;
116+
}
117+
111118
public function getDocument(): Document
112119
{
113120
return $this->document;

src/ExtractorFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ExtractorFactory
3030
'imagizer.imageshack.com' => Adapters\ImageShack\Extractor::class,
3131
'youtube.com' => Adapters\Youtube\Extractor::class,
3232
];
33+
private array $customDetectors = [];
3334

3435
public function createExtractor(UriInterface $uri, RequestInterface $request, ResponseInterface $response, Crawler $crawler): Extractor
3536
{
@@ -38,14 +39,25 @@ public function createExtractor(UriInterface $uri, RequestInterface $request, Re
3839

3940
$class = $this->adapters[$host] ?? $this->default;
4041

41-
return new $class($uri, $request, $response, $crawler);
42+
$extractor = new $class($uri, $request, $response, $crawler);
43+
44+
foreach ($this->customDetectors as $name => $detector) {
45+
$extractor->addDetector($name, new $detector($extractor));
46+
}
47+
48+
return $extractor;
4249
}
4350

4451
public function addAdapter(string $pattern, string $class): void
4552
{
4653
$this->adapters[$pattern] = $class;
4754
}
4855

56+
public function addDetector(string $name, string $class): void
57+
{
58+
$this->customDetectors[$name] = $class;
59+
}
60+
4961
public function removeAdapter(string $pattern): void
5062
{
5163
unset($this->adapters[$pattern]);

0 commit comments

Comments
 (0)