__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This is part of the webuni/front-matter package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\FrontMatter\Markdown;
14
15
use League\CommonMark\ConfigurableEnvironmentInterface;
16
use League\CommonMark\Event\DocumentPreParsedEvent;
17
use League\CommonMark\Extension\ExtensionInterface;
18
use League\CommonMark\Input\MarkdownInput;
19
use Webuni\FrontMatter\FrontMatterInterface;
20
21
class FrontMatterLeagueCommonMarkExtension implements ExtensionInterface
22
{
23
    /** @var FrontMatterInterface */
24
    private $frontMatter;
25
26
    public function __construct(FrontMatterInterface $frontMatter)
27
    {
28
        $this->frontMatter = $frontMatter;
29
    }
30
31
    public function register(ConfigurableEnvironmentInterface $environment): void
32
    {
33
        $environment->addEventListener(DocumentPreParsedEvent::class, [$this, 'parse']);
34
    }
35
36
    public function parse(DocumentPreParsedEvent $event): void
37
    {
38
        $content = $event->getMarkdown()->getContent();
39
        $document = $this->frontMatter->parse($content);
40
        $data = $event->getDocument()->data;
41
42
        $event->getDocument()->data = array_merge($document->getData(), $data);
43
        $event->replaceMarkdown(new MarkdownInput($document->getContent()));
44
    }
45
}
46