|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace League\CommonMark\Extension\Mention; |
|
13
|
|
|
|
|
14
|
|
|
use League\CommonMark\ConfigurableEnvironmentInterface; |
|
15
|
|
|
use League\CommonMark\Extension\ExtensionInterface; |
|
16
|
|
|
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class MentionExtension implements ExtensionInterface |
|
19
|
|
|
{ |
|
20
|
15 |
|
public function register(ConfigurableEnvironmentInterface $environment) |
|
21
|
|
|
{ |
|
22
|
15 |
|
$mentions = $environment->getConfig('mentions', []); |
|
23
|
15 |
|
foreach ($mentions as $name => $mention) { |
|
24
|
12 |
|
foreach (['symbol', 'regex', 'generator'] as $key) { |
|
25
|
12 |
|
if (empty($mention[$key])) { |
|
26
|
4 |
|
throw new \RuntimeException("Missing \"$key\" from MentionParser configuration"); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
12 |
|
if ($mention['generator'] instanceof MentionGeneratorInterface) { |
|
30
|
3 |
|
$environment->addInlineParser(new MentionParser($mention['symbol'], $mention['regex'], $mention['generator'])); |
|
31
|
9 |
|
} elseif (is_string($mention['generator'])) { |
|
32
|
3 |
|
$environment->addInlineParser(MentionParser::createWithStringTemplate($mention['symbol'], $mention['regex'], $mention['generator'])); |
|
33
|
6 |
|
} elseif (is_callable($mention['generator'])) { |
|
34
|
3 |
|
$environment->addInlineParser(MentionParser::createWithCallback($mention['symbol'], $mention['regex'], $mention['generator'])); |
|
35
|
|
|
} else { |
|
36
|
6 |
|
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)); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
12 |
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|