1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Facebook Instant Articles Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2016 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\FacebookInstantArticlesBundle\Parser; |
18
|
|
|
|
19
|
|
|
use Facebook\InstantArticles\Elements\InstantArticle; |
20
|
|
|
use Symfony\Component\Templating\EngineInterface; |
21
|
|
|
use Facebook\InstantArticles\Transformer\Transformer; |
22
|
|
|
|
23
|
|
|
final class TemplateParser implements TemplateParserInterface |
24
|
|
|
{ |
25
|
|
|
const FBIA_TEMPLATE_NAME = 'facebook_instant_article.html.twig'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EngineInterface |
29
|
|
|
*/ |
30
|
|
|
protected $templating; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Transformer |
34
|
|
|
*/ |
35
|
|
|
protected $transformer; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* TemplateParser constructor. |
39
|
|
|
* |
40
|
|
|
* @param EngineInterface $templating |
41
|
|
|
*/ |
42
|
11 |
|
public function __construct(EngineInterface $templating) |
43
|
|
|
{ |
44
|
11 |
|
$this->templating = $templating; |
45
|
11 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function parse(string $html = null): InstantArticle |
51
|
|
|
{ |
52
|
1 |
|
if (null === $html) { |
53
|
1 |
|
$html = $this->renderTemplate(); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$logger = \Logger::getLogger('facebook-instantarticles-transformer'); |
57
|
1 |
|
$logger->getParent()->removeAllAppenders(); |
58
|
|
|
|
59
|
1 |
|
libxml_use_internal_errors(true); |
60
|
1 |
|
$document = new \DOMDocument(); |
61
|
1 |
|
$document->loadHTML($html); |
62
|
1 |
|
libxml_use_internal_errors(false); |
63
|
|
|
|
64
|
1 |
|
$instantArticle = InstantArticle::create(); |
65
|
1 |
|
$transformer = $this->getTransformer(); |
66
|
1 |
|
$transformer->transform($instantArticle, $document); |
67
|
|
|
|
68
|
1 |
|
return $instantArticle; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function renderTemplate(): string |
75
|
|
|
{ |
76
|
1 |
|
return $this->getTemplating()->render('platforms/'.self::FBIA_TEMPLATE_NAME); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
1 |
|
public function getTransformer(): Transformer |
83
|
|
|
{ |
84
|
1 |
|
if (null !== $this->transformer) { |
85
|
|
|
return $this->transformer; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$json_file = file_get_contents(__DIR__.'/../Resources/instant-articles-rules.json'); |
89
|
1 |
|
$this->transformer = new Transformer(); |
90
|
1 |
|
$this->transformer->loadRules($json_file); |
91
|
|
|
|
92
|
1 |
|
return $this->transformer; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function getTemplating() |
99
|
|
|
{ |
100
|
1 |
|
return $this->templating; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|