Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <colinodell@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
 
14
namespace League\CommonMark\Extension\Embed;
15
 
16
use League\CommonMark\Event\DocumentParsedEvent;
17
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
18
use League\CommonMark\Node\Block\Paragraph;
19
use League\CommonMark\Node\Inline\Text;
20
use League\CommonMark\Node\NodeIterator;
21
 
22
final class EmbedProcessor
23
{
24
    public const FALLBACK_REMOVE = 'remove';
25
    public const FALLBACK_LINK   = 'link';
26
 
27
    private EmbedAdapterInterface $adapter;
28
    private string $fallback;
29
 
30
    public function __construct(EmbedAdapterInterface $adapter, string $fallback = self::FALLBACK_REMOVE)
31
    {
32
        $this->adapter  = $adapter;
33
        $this->fallback = $fallback;
34
    }
35
 
36
    public function __invoke(DocumentParsedEvent $event): void
37
    {
38
        $document = $event->getDocument();
39
        $embeds   = [];
40
        foreach (new NodeIterator($document) as $node) {
41
            if (! ($node instanceof Embed)) {
42
                continue;
43
            }
44
 
45
            if ($node->parent() !== $document) {
46
                $replacement = new Paragraph();
47
                $replacement->appendChild(new Text($node->getUrl()));
48
                $node->replaceWith($replacement);
49
            } else {
50
                $embeds[] = $node;
51
            }
52
        }
53
 
54
        $this->adapter->updateEmbeds($embeds);
55
 
56
        foreach ($embeds as $embed) {
57
            if ($embed->getEmbedCode() !== null) {
58
                continue;
59
            }
60
 
61
            if ($this->fallback === self::FALLBACK_REMOVE) {
62
                $embed->detach();
63
            } elseif ($this->fallback === self::FALLBACK_LINK) {
64
                $paragraph = new Paragraph();
65
                $paragraph->appendChild(new Link($embed->getUrl(), $embed->getUrl()));
66
                $embed->replaceWith($paragraph);
67
            }
68
        }
69
    }
70
}