Completed
Push — master ( 18ef26...83739a )
by Colin
32:49
created

Environment::mergeConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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