Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
parserchain.php
1<?php
2
4
7
9{
11 protected static $metadataParsers = array(
12 'Bitrix\Main\UrlPreview\Parser\OpenGraph',
13 'Bitrix\Main\UrlPreview\Parser\SchemaOrg',
14 'Bitrix\Main\UrlPreview\Parser\Oembed',
15 'Bitrix\Main\UrlPreview\Parser\Common'
16 );
17
21 protected static $metadataParsersByHost = [
22 'vk.com' => 'Bitrix\Main\UrlPreview\Parser\Vk',
23 'www.facebook.com' => 'Bitrix\Main\UrlPreview\Parser\Facebook',
24 'www.instagram.com' => 'Bitrix\Main\UrlPreview\Parser\Instagram',
25 'maps.apple.com' => 'Bitrix\Main\UrlPreview\Parser\AppleMaps',
26 'rutube.ru' => 'Bitrix\Main\UrlPreview\Parser\RuTube',
27 ];
28
33 protected static function getParserChain(Uri $uri)
34 {
35 $result = array();
36 if(isset(static::$metadataParsersByHost[$uri->getHost()]))
37 {
38 $result[] = static::$metadataParsersByHost[$uri->getHost()];
39 }
40
41 $result = array_merge($result, static::$metadataParsers);
42
43 return $result;
44 }
45
51 public static function extractMetadata(HtmlDocument $document)
52 {
53 foreach(static::getParserChain($document->getUri()) as $parserClassName)
54 {
56 if(class_exists($parserClassName))
57 {
58 $parser = new $parserClassName();
59 if ($parser instanceof Parser)
60 {
61 $parser->handle($document);
62 }
63 }
64 if($document->checkMetadata())
65 {
66 break;
67 }
68 }
69 }
70
78 public static function registerMetadataParser($host, $parserClassName)
79 {
80 if(!class_exists($parserClassName) || !is_subclass_of($parserClassName, '\Bitrix\Main\UrlPreview\Parser'))
81 {
82 throw new ArgumentException('Parser class must extend \Bitrix\Main\UrlPreview\Parser', 'parserClassName');
83 }
84
85 static::$metadataParsersByHost[$host] = $parserClassName;
86 }
87}
static registerMetadataParser($host, $parserClassName)