Environment::getInlineParserCharacterRegex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark;
16
17
use League\CommonMark\Block\Parser\BlockParserInterface;
18
use League\CommonMark\Block\Renderer\BlockRendererInterface;
19
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection;
20
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
21
use League\CommonMark\Event\AbstractEvent;
22
use League\CommonMark\Extension\CommonMarkCoreExtension;
23
use League\CommonMark\Extension\ExtensionInterface;
24
use League\CommonMark\Inline\Parser\InlineParserInterface;
25
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
26
use League\CommonMark\Util\Configuration;
27
use League\CommonMark\Util\ConfigurationAwareInterface;
28
use League\CommonMark\Util\PrioritizedList;
29
30
final class Environment implements EnvironmentInterface, ConfigurableEnvironmentInterface
31
{
32
    /**
33
     * @var ExtensionInterface[]
34
     */
35
    private $extensions = [];
36
37
    /**
38
     * @var ExtensionInterface[]
39
     */
40
    private $uninitializedExtensions = [];
41
42
    /**
43
     * @var bool
44
     */
45
    private $extensionsInitialized = false;
46
47
    /**
48
     * @var PrioritizedList<BlockParserInterface>
49
     */
50
    private $blockParsers;
51
52
    /**
53
     * @var PrioritizedList<InlineParserInterface>
54
     */
55
    private $inlineParsers;
56
57
    /**
58
     * @var array<string, PrioritizedList<InlineParserInterface>>
59
     */
60
    private $inlineParsersByCharacter = [];
61
62
    /**
63
     * @var DelimiterProcessorCollection
64
     */
65
    private $delimiterProcessors;
66
67
    /**
68
     * @var array<string, PrioritizedList<BlockRendererInterface>>
69
     */
70
    private $blockRenderersByClass = [];
71
72
    /**
73
     * @var array<string, PrioritizedList<InlineRendererInterface>>
74
     */
75
    private $inlineRenderersByClass = [];
76
77
    /**
78
     * @var array<string, PrioritizedList<callable>>
79
     */
80
    private $listeners = [];
81
82
    /**
83
     * @var Configuration
84
     */
85
    private $config;
86
87
    /**
88
     * @var string
89
     */
90
    private $inlineParserCharacterRegex;
91
92 2175
    public function __construct(array $config = [])
93
    {
94 2175
        $this->config = new Configuration($config);
95
96 2175
        $this->blockParsers = new PrioritizedList();
97 2175
        $this->inlineParsers = new PrioritizedList();
98 2175
        $this->delimiterProcessors = new DelimiterProcessorCollection();
99 2175
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 2079
    public function mergeConfig(array $config = [])
105
    {
106 2079
        $this->assertUninitialized('Failed to modify configuration.');
107
108 2076
        $this->config->merge($config);
109 2076
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114 6
    public function setConfig(array $config = [])
115
    {
116 6
        $this->assertUninitialized('Failed to modify configuration.');
117
118 3
        $this->config->replace($config);
119 3
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 2082
    public function getConfig($key = null, $default = null)
125
    {
126 2082
        return $this->config->get($key, $default);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 2076
    public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface
133
    {
134 2076
        $this->assertUninitialized('Failed to add block parser.');
135
136 2073
        $this->blockParsers->add($parser, $priority);
137 2073
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
138
139 2073
        return $this;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 2076
    public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface
146
    {
147 2076
        $this->assertUninitialized('Failed to add inline parser.');
148
149 2073
        $this->inlineParsers->add($parser, $priority);
150 2073
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
151
152 2073
        foreach ($parser->getCharacters() as $character) {
153 2073
            if (!isset($this->inlineParsersByCharacter[$character])) {
154 2073
                $this->inlineParsersByCharacter[$character] = new PrioritizedList();
155
            }
156
157 2073
            $this->inlineParsersByCharacter[$character]->add($parser, $priority);
158
        }
159
160 2073
        return $this;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 2073
    public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface
167
    {
168 2073
        $this->assertUninitialized('Failed to add delimiter processor.');
169 2070
        $this->delimiterProcessors->add($processor);
170 2070
        $this->injectEnvironmentAndConfigurationIfNeeded($processor);
171
172 2070
        return $this;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 2073
    public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface
179
    {
180 2073
        $this->assertUninitialized('Failed to add block renderer.');
181
182 2070
        if (!isset($this->blockRenderersByClass[$blockClass])) {
183 2070
            $this->blockRenderersByClass[$blockClass] = new PrioritizedList();
184
        }
185
186 2070
        $this->blockRenderersByClass[$blockClass]->add($blockRenderer, $priority);
187 2070
        $this->injectEnvironmentAndConfigurationIfNeeded($blockRenderer);
188
189 2070
        return $this;
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195 2073
    public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface
196
    {
197 2073
        $this->assertUninitialized('Failed to add inline renderer.');
198
199 2070
        if (!isset($this->inlineRenderersByClass[$inlineClass])) {
200 2070
            $this->inlineRenderersByClass[$inlineClass] = new PrioritizedList();
201
        }
202
203 2070
        $this->inlineRenderersByClass[$inlineClass]->add($renderer, $priority);
204 2070
        $this->injectEnvironmentAndConfigurationIfNeeded($renderer);
205
206 2070
        return $this;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212 2085
    public function getBlockParsers(): iterable
213
    {
214 2085
        $this->initializeExtensions();
215
216 2085
        return $this->blockParsers->getIterator();
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222 2025
    public function getInlineParsersForCharacter(string $character): iterable
223
    {
224 2025
        $this->initializeExtensions();
225
226 2025
        if (!isset($this->inlineParsersByCharacter[$character])) {
227 1857
            return [];
228
        }
229
230 1203
        return $this->inlineParsersByCharacter[$character]->getIterator();
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 2028
    public function getDelimiterProcessors(): DelimiterProcessorCollection
237
    {
238 2028
        $this->initializeExtensions();
239
240 2028
        return $this->delimiterProcessors;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246 2073
    public function getBlockRenderersForClass(string $blockClass): iterable
247
    {
248 2073
        $this->initializeExtensions();
249
250 2073
        if (!isset($this->blockRenderersByClass[$blockClass])) {
251 6
            return [];
252
        }
253
254 2067
        return $this->blockRenderersByClass[$blockClass]->getIterator();
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260 1788
    public function getInlineRenderersForClass(string $inlineClass): iterable
261
    {
262 1788
        $this->initializeExtensions();
263
264 1788
        if (!isset($this->inlineRenderersByClass[$inlineClass])) {
265 9
            return [];
266
        }
267
268 1779
        return $this->inlineRenderersByClass[$inlineClass]->getIterator();
269
    }
270
271
    /**
272
     * Get all registered extensions
273
     *
274
     * @return ExtensionInterface[]
275
     */
276 12
    public function getExtensions(): iterable
277
    {
278 12
        return $this->extensions;
279
    }
280
281
    /**
282
     * Add a single extension
283
     *
284
     * @param ExtensionInterface $extension
285
     *
286
     * @return $this
287
     */
288 2082
    public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface
289
    {
290 2082
        $this->assertUninitialized('Failed to add extension.');
291
292 2079
        $this->extensions[] = $extension;
293 2079
        $this->uninitializedExtensions[] = $extension;
294
295 2079
        return $this;
296
    }
297
298 2139
    private function initializeExtensions()
299
    {
300
        // Only initialize them once
301 2139
        if ($this->extensionsInitialized) {
302 2064
            return;
303
        }
304
305
        // Ask all extensions to register their components
306 2139
        while (!empty($this->uninitializedExtensions)) {
307 2067
            foreach ($this->uninitializedExtensions as $i => $extension) {
308 2067
                $extension->register($this);
309 2067
                unset($this->uninitializedExtensions[$i]);
310
            }
311
        }
312
313 2139
        $this->extensionsInitialized = true;
314
315
        // Lastly, let's build a regex which matches non-inline characters
316
        // This will enable a huge performance boost with inline parsing
317 2139
        $this->buildInlineParserCharacterRegex();
318 2139
    }
319
320 2100
    private function injectEnvironmentAndConfigurationIfNeeded($object)
321
    {
322 2100
        if ($object instanceof EnvironmentAwareInterface) {
323 2067
            $object->setEnvironment($this);
324
        }
325
326 2100
        if ($object instanceof ConfigurationAwareInterface) {
327 2067
            $object->setConfiguration($this->config);
328
        }
329 2100
    }
330
331
    /**
332
     * @return Environment
333
     */
334 2073
    public static function createCommonMarkEnvironment(): Environment
335
    {
336 2073
        $environment = new static();
337 2073
        $environment->addExtension(new CommonMarkCoreExtension());
338 2073
        $environment->mergeConfig([
339 1382
            'renderer' => [
340 691
                'block_separator' => "\n",
341
                'inner_separator' => "\n",
342
                'soft_break'      => "\n",
343
            ],
344 2073
            'html_input'         => self::HTML_INPUT_ALLOW,
345
            'allow_unsafe_links' => true,
346
            'max_nesting_level'  => \INF,
347
        ]);
348
349 2073
        return $environment;
350
    }
351
352
    /**
353
     * {@inheritdoc}
354
     */
355 1842
    public function getInlineParserCharacterRegex(): string
356
    {
357 1842
        return $this->inlineParserCharacterRegex;
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363 6
    public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface
364
    {
365 6
        $this->assertUninitialized('Failed to add event listener.');
366
367 6
        if (!isset($this->listeners[$eventClass])) {
368 6
            $this->listeners[$eventClass] = new PrioritizedList();
369
        }
370
371 6
        $this->listeners[$eventClass]->add($listener, $priority);
372
373 6
        return $this;
374
    }
375
376
    /**
377
     * {@inheritdoc}
378
     */
379 2067
    public function dispatch(AbstractEvent $event): void
380
    {
381 2067
        $this->initializeExtensions();
382
383 2067
        $type = \get_class($event);
384
385 2067
        foreach ($this->listeners[$type] ?? [] as $listener) {
386 6
            if ($event->isPropagationStopped()) {
387 3
                return;
388
            }
389
390 6
            $listener($event);
391
        }
392 2064
    }
393
394 2139
    private function buildInlineParserCharacterRegex()
395
    {
396 2139
        $chars = \array_unique(\array_merge(
397 2139
            \array_keys($this->inlineParsersByCharacter),
398 2139
            $this->delimiterProcessors->getDelimiterCharacters()
399
        ));
400
401 2139
        if (empty($chars)) {
402
            // If no special inline characters exist then parse the whole line
403 63
            $this->inlineParserCharacterRegex = '/^.+$/u';
404
        } else {
405
            // Match any character which inline parsers are not interested in
406 2076
            $this->inlineParserCharacterRegex = '/^[^' . \preg_quote(\implode('', $chars), '/') . ']+/u';
407
        }
408 2139
    }
409
410
    /**
411
     * @param string $message
412
     *
413
     * @throws \RuntimeException
414
     */
415 2145
    private function assertUninitialized(string $message)
416
    {
417 2145
        if ($this->extensionsInitialized) {
418 24
            throw new \RuntimeException($message . ' Extensions have already been initialized.');
419
        }
420 2121
    }
421
}
422