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 <[email protected]> |
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\Mention; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Environment\ConfigurableEnvironmentInterface; |
17
|
|
|
use League\CommonMark\Extension\ExtensionInterface; |
18
|
|
|
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface; |
19
|
|
|
|
20
|
|
|
final class MentionExtension implements ExtensionInterface |
21
|
|
|
{ |
22
|
15 |
|
public function register(ConfigurableEnvironmentInterface $environment): void |
23
|
|
|
{ |
24
|
15 |
|
$mentions = $environment->getConfig('mentions', []); |
25
|
15 |
|
foreach ($mentions as $name => $mention) { |
26
|
12 |
|
foreach (['symbol', 'regex', 'generator'] as $key) { |
27
|
12 |
|
if (! \array_key_exists($key, $mention)) { |
28
|
|
|
throw new \RuntimeException(\sprintf('Missing "%s" from MentionParser configuration', $key)); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
12 |
|
if ($mention['generator'] instanceof MentionGeneratorInterface) { |
33
|
3 |
|
$environment->addInlineParser(new MentionParser($mention['symbol'], $mention['regex'], $mention['generator'])); |
34
|
9 |
|
} elseif (\is_string($mention['generator'])) { |
35
|
3 |
|
$environment->addInlineParser(MentionParser::createWithStringTemplate($mention['symbol'], $mention['regex'], $mention['generator'])); |
36
|
6 |
|
} elseif (\is_callable($mention['generator'])) { |
37
|
3 |
|
$environment->addInlineParser(MentionParser::createWithCallback($mention['symbol'], $mention['regex'], $mention['generator'])); |
38
|
|
|
} else { |
39
|
3 |
|
throw new \RuntimeException(\sprintf('The "generator" provided for the MentionParser configuration must be a string template, callable, or an object that implements %s.', MentionGeneratorInterface::class)); |
40
|
|
|
} |
41
|
|
|
} |
42
|
12 |
|
} |
43
|
|
|
} |
44
|
|
|
|