Completed
Push — master ( d3fffa...285358 )
by Colin
33:06 queued 55s
created

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