1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file was originally part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
11
|
|
|
* - (c) John MacFarlane |
12
|
|
|
* |
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
14
|
|
|
* file that was distributed with this source code. |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace UnicornFail\Emoji\Renderer; |
18
|
|
|
|
19
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
20
|
|
|
use UnicornFail\Emoji\Environment\EnvironmentAwareInterface; |
21
|
|
|
use UnicornFail\Emoji\Environment\EnvironmentInterface; |
22
|
|
|
use UnicornFail\Emoji\Event\DocumentRenderedEvent; |
23
|
|
|
use UnicornFail\Emoji\Node\Document; |
24
|
|
|
use UnicornFail\Emoji\Node\Node; |
25
|
|
|
use UnicornFail\Emoji\Output\RenderedContent; |
26
|
|
|
use UnicornFail\Emoji\Output\RenderedContentInterface; |
27
|
|
|
|
28
|
|
|
final class DocumentRenderer implements DocumentRendererInterface |
29
|
|
|
{ |
30
|
|
|
/** @var EnvironmentInterface */ |
31
|
|
|
private $environment; |
32
|
|
|
|
33
|
|
|
public function __construct(EnvironmentInterface $environment) |
34
|
|
|
{ |
35
|
|
|
$this->environment = $environment; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function renderDocument(Document $document): RenderedContentInterface |
39
|
|
|
{ |
40
|
|
|
$output = new RenderedContent($document, (string) $this->renderNodes($document->children())); |
41
|
|
|
|
42
|
|
|
$event = new DocumentRenderedEvent($output); |
43
|
|
|
$this->environment->dispatch($event); |
44
|
|
|
|
45
|
|
|
return $event->getContent(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param Node[] $nodes |
50
|
|
|
*/ |
51
|
|
|
protected function renderNodes(iterable $nodes): string |
52
|
|
|
{ |
53
|
|
|
$output = ''; |
54
|
|
|
|
55
|
|
|
foreach ($nodes as $node) { |
56
|
|
|
$output .= $this->renderNode($node); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $output; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \Stringable|string |
64
|
|
|
* |
65
|
|
|
* @throws \RuntimeException |
66
|
|
|
*/ |
67
|
|
|
private function renderNode(Node $node) |
68
|
|
|
{ |
69
|
|
|
$renderers = $this->environment->getRenderersForClass(\get_class($node)); |
70
|
|
|
|
71
|
|
|
/** @var NodeRendererInterface $renderer */ |
72
|
|
|
foreach ($renderers as $renderer) { |
73
|
|
|
if ($renderer instanceof ConfigurationAwareInterface) { |
74
|
|
|
$renderer->setConfiguration($this->environment->getConfiguration()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($renderer instanceof EnvironmentAwareInterface) { |
78
|
|
|
$renderer->setEnvironment($this->environment); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (($result = $renderer->render($node)) !== null) { |
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new \RuntimeException('Unable to find corresponding renderer for node type ' . \get_class($node)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|