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\Exception\InvalidOptionException; |
16
|
|
|
use League\CommonMark\Extension\ExtensionInterface; |
17
|
|
|
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface; |
18
|
|
|
|
19
|
|
|
final class MentionExtension implements ExtensionInterface |
20
|
|
|
{ |
21
|
21 |
|
public function register(ConfigurableEnvironmentInterface $environment) |
22
|
|
|
{ |
23
|
21 |
|
$mentions = $environment->getConfig('mentions', []); |
24
|
21 |
|
foreach ($mentions as $name => $mention) { |
25
|
18 |
|
if (\array_key_exists('symbol', $mention)) { |
26
|
12 |
|
@\trigger_error('The "mentions/*/symbol" configuration option is deprecated in league/commonmark 1.6; rename "symbol" to "prefix" for compatibility with 2.0', \E_USER_DEPRECATED); |
27
|
12 |
|
$mention['prefix'] = $mention['symbol']; |
28
|
|
|
} |
29
|
|
|
|
30
|
18 |
|
if (\array_key_exists('pattern', $mention)) { |
31
|
|
|
// v2.0 does not allow full regex patterns passeed into the configuration |
32
|
6 |
|
if (!self::isAValidPartialRegex($mention['pattern'])) { |
33
|
3 |
|
throw new InvalidOptionException(\sprintf('Option "mentions/%s/pattern" must not include starting/ending delimiters (like "/")', $name)); |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
$mention['pattern'] = '/' . $mention['pattern'] . '/i'; |
37
|
8 |
|
} elseif (\array_key_exists('regex', $mention)) { |
38
|
12 |
|
@\trigger_error('The "mentions/*/regex" configuration option is deprecated in league/commonmark 1.6; rename "regex" to "pattern" for compatibility with 2.0', \E_USER_DEPRECATED); |
39
|
12 |
|
$mention['pattern'] = $mention['regex']; |
40
|
|
|
} |
41
|
|
|
|
42
|
15 |
|
foreach (['prefix', 'pattern', 'generator'] as $key) { |
43
|
15 |
|
if (empty($mention[$key])) { |
44
|
5 |
|
throw new \RuntimeException("Missing \"$key\" from MentionParser configuration"); |
45
|
|
|
} |
46
|
|
|
} |
47
|
15 |
|
if ($mention['generator'] instanceof MentionGeneratorInterface) { |
48
|
3 |
|
$environment->addInlineParser(new MentionParser($mention['prefix'], $mention['pattern'], $mention['generator'])); |
49
|
12 |
|
} elseif (is_string($mention['generator'])) { |
50
|
6 |
|
$environment->addInlineParser(MentionParser::createWithStringTemplate($mention['prefix'], $mention['pattern'], $mention['generator'])); |
51
|
6 |
|
} elseif (is_callable($mention['generator'])) { |
52
|
3 |
|
$environment->addInlineParser(MentionParser::createWithCallback($mention['prefix'], $mention['pattern'], $mention['generator'])); |
53
|
|
|
} else { |
54
|
7 |
|
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)); |
55
|
|
|
} |
56
|
|
|
} |
57
|
15 |
|
} |
58
|
|
|
|
59
|
6 |
|
private static function isAValidPartialRegex(string $regex): bool |
60
|
|
|
{ |
61
|
6 |
|
$regex = '/' . $regex . '/i'; |
62
|
|
|
|
63
|
6 |
|
return @\preg_match($regex, '') !== false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|