Completed
Push — master ( 72e87e...d3fffa )
by Colin
17s queued 11s
created

Environment::addDocumentProcessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 2
cts 2
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 PrioritizedList<DocumentProcessorInterface>
64
     */
65
    private $documentProcessors;
66
67
    /**
68
     * @var DelimiterProcessorCollection
69
     */
70
    private $delimiterProcessors;
71
72
    /**
73
     * @var array<string, PrioritizedList<BlockRendererInterface>>
74
     */
75
    private $blockRenderersByClass = [];
76
77
    /**
78
     * @var array<string, PrioritizedList<InlineRendererInterface>>
79
     */
80
    private $inlineRenderersByClass = [];
81
82
    /**
83
     * @var array<string, PrioritizedList<callable>>
84
     */
85
    private $listeners = [];
86
87
    /**
88
     * @var Configuration
89
     */
90
    private $config;
91 2175
92
    /**
93 2175
     * @var string
94
     */
95 2175
    private $inlineParserCharacterRegex;
96 2175
97 2175
    public function __construct(array $config = [])
98 2175
    {
99 2175
        $this->config = new Configuration($config);
100
101
        $this->blockParsers = new PrioritizedList();
102
        $this->inlineParsers = new PrioritizedList();
103
        $this->documentProcessors = new PrioritizedList();
104 2076
        $this->delimiterProcessors = new DelimiterProcessorCollection();
105
    }
106 2076
107
    /**
108 2073
     * {@inheritdoc}
109 2073
     */
110
    public function mergeConfig(array $config = [])
111
    {
112
        $this->assertUninitialized('Failed to modify configuration.');
113
114 6
        $this->config->merge($config);
115
    }
116 6
117
    /**
118 3
     * {@inheritdoc}
119 3
     */
120
    public function setConfig(array $config = [])
121
    {
122
        $this->assertUninitialized('Failed to modify configuration.');
123
124 2079
        $this->config->replace($config);
125
    }
126 2079
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getConfig($key = null, $default = null)
131
    {
132 2073
        return $this->config->get($key, $default);
133
    }
134 2073
135
    /**
136 2070
     * {@inheritdoc}
137 2070
     */
138
    public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface
139 2070
    {
140
        $this->assertUninitialized('Failed to add block parser.');
141
142
        $this->blockParsers->add($parser, $priority);
143
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
144
145 2073
        return $this;
146
    }
147 2073
148
    /**
149 2070
     * {@inheritdoc}
150 2070
     */
151
    public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface
152 2070
    {
153 2070
        $this->assertUninitialized('Failed to add inline parser.');
154 2070
155
        $this->inlineParsers->add($parser, $priority);
156
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
157 2070
158
        foreach ($parser->getCharacters() as $character) {
159
            if (!isset($this->inlineParsersByCharacter[$character])) {
160 2070
                $this->inlineParsersByCharacter[$character] = new PrioritizedList();
161
            }
162
163
            $this->inlineParsersByCharacter[$character]->add($parser, $priority);
164
        }
165
166 2070
        return $this;
167
    }
168 2070
169 2067
    /**
170 2067
     * {@inheritdoc}
171
     */
172 2067
    public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface
173
    {
174
        $this->assertUninitialized('Failed to add delimiter processor.');
175
        $this->delimiterProcessors->add($processor);
176
        $this->injectEnvironmentAndConfigurationIfNeeded($processor);
177
178 9
        return $this;
179
    }
180 9
181
    /**
182 6
     * {@inheritdoc}
183 6
     */
184
    public function addDocumentProcessor(DocumentProcessorInterface $processor, int $priority = 0): ConfigurableEnvironmentInterface
185 6
    {
186
        $this->assertUninitialized('Failed to add document processor.');
187
188
        $this->documentProcessors->add($processor, $priority);
189
        $this->injectEnvironmentAndConfigurationIfNeeded($processor);
190
191 2070
        return $this;
192
    }
193 2070
194
    /**
195 2067
     * {@inheritdoc}
196 2067
     */
197
    public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface
198
    {
199 2067
        $this->assertUninitialized('Failed to add block renderer.');
200 2067
201
        if (!isset($this->blockRenderersByClass[$blockClass])) {
202 2067
            $this->blockRenderersByClass[$blockClass] = new PrioritizedList();
203
        }
204
205
        $this->blockRenderersByClass[$blockClass]->add($blockRenderer, $priority);
206
        $this->injectEnvironmentAndConfigurationIfNeeded($blockRenderer);
207
208 2070
        return $this;
209
    }
210 2070
211
    /**
212 2067
     * {@inheritdoc}
213 2067
     */
214
    public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface
215
    {
216 2067
        $this->assertUninitialized('Failed to add inline renderer.');
217 2067
218
        if (!isset($this->inlineRenderersByClass[$inlineClass])) {
219 2067
            $this->inlineRenderersByClass[$inlineClass] = new PrioritizedList();
220
        }
221
222
        $this->inlineRenderersByClass[$inlineClass]->add($renderer, $priority);
223
        $this->injectEnvironmentAndConfigurationIfNeeded($renderer);
224
225 2082
        return $this;
226
    }
227 2082
228
    /**
229 2082
     * {@inheritdoc}
230
     */
231
    public function getBlockParsers(): iterable
232
    {
233
        $this->initializeExtensions();
234
235 2022
        return $this->blockParsers->getIterator();
236
    }
237 2022
238
    /**
239 2022
     * {@inheritdoc}
240 1854
     */
241
    public function getInlineParsersForCharacter(string $character): iterable
242
    {
243 1203
        $this->initializeExtensions();
244
245
        if (!isset($this->inlineParsersByCharacter[$character])) {
246
            return [];
247
        }
248
249 2025
        return $this->inlineParsersByCharacter[$character]->getIterator();
250
    }
251 2025
252
    /**
253 2025
     * {@inheritdoc}
254
     */
255
    public function getDelimiterProcessors(): DelimiterProcessorCollection
256
    {
257
        $this->initializeExtensions();
258
259 2070
        return $this->delimiterProcessors;
260
    }
261 2070
262
    /**
263 2070
     * {@inheritdoc}
264
     */
265
    public function getDocumentProcessors(): iterable
266
    {
267
        $this->initializeExtensions();
268
269 2073
        return $this->documentProcessors->getIterator();
270
    }
271 2073
272
    /**
273 2073
     * {@inheritdoc}
274 6
     */
275
    public function getBlockRenderersForClass(string $blockClass): iterable
276
    {
277 2067
        $this->initializeExtensions();
278
279
        if (!isset($this->blockRenderersByClass[$blockClass])) {
280
            return [];
281
        }
282
283 1788
        return $this->blockRenderersByClass[$blockClass]->getIterator();
284
    }
285 1788
286
    /**
287 1788
     * {@inheritdoc}
288 9
     */
289
    public function getInlineRenderersForClass(string $inlineClass): iterable
290
    {
291 1779
        $this->initializeExtensions();
292
293
        if (!isset($this->inlineRenderersByClass[$inlineClass])) {
294
            return [];
295
        }
296
297
        return $this->inlineRenderersByClass[$inlineClass]->getIterator();
298
    }
299 12
300
    /**
301 12
     * Get all registered extensions
302
     *
303
     * @return ExtensionInterface[]
304
     */
305
    public function getExtensions(): iterable
306
    {
307
        return $this->extensions;
308
    }
309
310
    /**
311 2079
     * Add a single extension
312
     *
313 2079
     * @param ExtensionInterface $extension
314
     *
315 2076
     * @return $this
316 2076
     */
317
    public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface
318 2076
    {
319
        $this->assertUninitialized('Failed to add extension.');
320
321 2142
        $this->extensions[] = $extension;
322
        $this->uninitializedExtensions[] = $extension;
323
324 2142
        return $this;
325 2061
    }
326
327
    private function initializeExtensions()
328
    {
329 2142
        // Only initialize them once
330 2064
        if ($this->extensionsInitialized) {
331 2064
            return;
332 2064
        }
333
334
        // Ask all extensions to register their components
335
        while (!empty($this->uninitializedExtensions)) {
336 2142
            foreach ($this->uninitializedExtensions as $i => $extension) {
337
                $extension->register($this);
338
                unset($this->uninitializedExtensions[$i]);
339
            }
340 2142
        }
341 2142
342
        $this->extensionsInitialized = true;
343 2103
344
        // Lastly, let's build a regex which matches non-inline characters
345 2103
        // This will enable a huge performance boost with inline parsing
346 2064
        $this->buildInlineParserCharacterRegex();
347
    }
348
349 2103
    private function injectEnvironmentAndConfigurationIfNeeded($object)
350 2064
    {
351
        if ($object instanceof EnvironmentAwareInterface) {
352 2103
            $object->setEnvironment($this);
353
        }
354
355
        if ($object instanceof ConfigurationAwareInterface) {
356
            $object->setConfiguration($this->config);
357 2070
        }
358
    }
359 2070
360 2070
    /**
361 2070
     * @return Environment
362 1380
     */
363 690
    public static function createCommonMarkEnvironment(): Environment
364
    {
365
        $environment = new static();
366
        $environment->addExtension(new CommonMarkCoreExtension());
367 2070
        $environment->mergeConfig([
368
            'renderer' => [
369 2070
                'block_separator' => "\n",
370
                'inner_separator' => "\n",
371
                'soft_break'      => "\n",
372 2070
            ],
373
            'html_input'         => self::HTML_INPUT_ALLOW,
374
            'allow_unsafe_links' => true,
375
            'max_nesting_level'  => INF,
376
        ]);
377
378 1839
        return $environment;
379
    }
380 1839
381
    /**
382
     * {@inheritdoc}
383 2142
     */
384
    public function getInlineParserCharacterRegex(): string
385 2142
    {
386 2142
        return $this->inlineParserCharacterRegex;
387 2142
    }
388
389
    /**
390 2142
     * {@inheritdoc}
391
     */
392 69
    public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface
393
    {
394
        $this->assertUninitialized('Failed to add event listener.');
395 2073
396
        if (!isset($this->listeners[$eventClass])) {
397 2142
            $this->listeners[$eventClass] = new PrioritizedList();
398
        }
399
400
        $this->listeners[$eventClass]->add($listener, $priority);
401
402
        return $this;
403
    }
404 2148
405
    /**
406 2148
     * {@inheritdoc}
407 27
     */
408
    public function dispatch(AbstractEvent $event): void
409 2121
    {
410
        $this->initializeExtensions();
411
412
        $type = get_class($event);
413
414
        foreach ($this->listeners[$type] ?? [] as $listener) {
415
            if ($event->isPropagationStopped()) {
416
                return;
417
            }
418
419
            $listener($event);
420
        }
421
    }
422
423
    private function buildInlineParserCharacterRegex()
424
    {
425
        $chars = \array_unique(\array_merge(
426
            \array_keys($this->inlineParsersByCharacter),
427
            $this->delimiterProcessors->getDelimiterCharacters()
428
        ));
429
430
        if (empty($chars)) {
431
            // If no special inline characters exist then parse the whole line
432
            $this->inlineParserCharacterRegex = '/^.+$/u';
433
        } else {
434
            // Match any character which inline parsers are not interested in
435
            $this->inlineParserCharacterRegex = '/^[^' . \preg_quote(\implode('', $chars), '/') . ']+/u';
436
        }
437
    }
438
439
    /**
440
     * @param string $message
441
     *
442
     * @throws \RuntimeException
443
     */
444
    private function assertUninitialized(string $message)
445
    {
446
        if ($this->extensionsInitialized) {
447
            throw new \RuntimeException($message . ' Extensions have already been initialized.');
448
        }
449
    }
450
}
451