Passed
Push — 2.0 ( 040f68...c13575 )
by Colin
02:06
created

Environment::getInlineParsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Environment;
18
19
use League\CommonMark\Delimiter\DelimiterParser;
20
use League\CommonMark\Delimiter\Processor\DelimiterProcessorCollection;
21
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
22
use League\CommonMark\Event\DocumentParsedEvent;
23
use League\CommonMark\Event\ListenerData;
24
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
25
use League\CommonMark\Extension\ConfigurableExtensionInterface;
26
use League\CommonMark\Extension\ExtensionInterface;
27
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;
28
use League\CommonMark\Normalizer\SlugNormalizer;
29
use League\CommonMark\Normalizer\TextNormalizerInterface;
30
use League\CommonMark\Normalizer\UniqueSlugNormalizer;
31
use League\CommonMark\Normalizer\UniqueSlugNormalizerInterface;
32
use League\CommonMark\Parser\Block\BlockStartParserInterface;
33
use League\CommonMark\Parser\Block\SkipLinesStartingWithLettersParser;
34
use League\CommonMark\Parser\Inline\InlineParserInterface;
35
use League\CommonMark\Renderer\NodeRendererInterface;
36
use League\CommonMark\Util\HtmlFilter;
37
use League\CommonMark\Util\PrioritizedList;
38
use League\Config\Configuration;
39
use League\Config\ConfigurationAwareInterface;
40
use League\Config\ConfigurationInterface;
41
use Nette\Schema\Expect;
42
use Psr\EventDispatcher\EventDispatcherInterface;
43
use Psr\EventDispatcher\ListenerProviderInterface;
44
use Psr\EventDispatcher\StoppableEventInterface;
45
46
final class Environment implements EnvironmentInterface, EnvironmentBuilderInterface, ListenerProviderInterface
47
{
48
    /**
49
     * @var ExtensionInterface[]
50
     *
51
     * @psalm-readonly-allow-private-mutation
52
     */
53
    private array $extensions = [];
54
55
    /**
56
     * @var ExtensionInterface[]
57
     *
58
     * @psalm-readonly-allow-private-mutation
59
     */
60
    private array $uninitializedExtensions = [];
61
62
    /** @psalm-readonly-allow-private-mutation */
63
    private bool $extensionsInitialized = false;
64
65
    /**
66
     * @var PrioritizedList<BlockStartParserInterface>
67
     *
68
     * @psalm-readonly
69
     */
70
    private PrioritizedList $blockStartParsers;
71
72
    /**
73
     * @var PrioritizedList<InlineParserInterface>
74
     *
75
     * @psalm-readonly
76
     */
77
    private PrioritizedList $inlineParsers;
78
79
    /** @psalm-readonly */
80
    private DelimiterProcessorCollection $delimiterProcessors;
81
82
    /**
83
     * @var array<string, PrioritizedList<NodeRendererInterface>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, Prioritize...NodeRendererInterface>> at position 4 could not be parsed: Expected '>' at position 4, but found 'PrioritizedList'.
Loading history...
84
     *
85
     * @psalm-readonly-allow-private-mutation
86
     */
87
    private array $renderersByClass = [];
88
89
    /**
90
     * @var PrioritizedList<ListenerData>
91
     *
92
     * @psalm-readonly-allow-private-mutation
93
     */
94
    private PrioritizedList $listenerData;
95
96
    private ?EventDispatcherInterface $eventDispatcher = null;
97
98
    /** @psalm-readonly */
99
    private Configuration $config;
100
101
    private ?TextNormalizerInterface $slugNormalizer = null;
102
103
    /**
104
     * @param array<string, mixed> $config
105
     */
106 2256
    public function __construct(array $config = [])
107
    {
108 2256
        $this->config = self::createDefaultConfiguration();
109 2256
        $this->config->merge($config);
110
111 2256
        $this->blockStartParsers   = new PrioritizedList();
112 2256
        $this->inlineParsers       = new PrioritizedList();
113 2256
        $this->listenerData        = new PrioritizedList();
114 2256
        $this->delimiterProcessors = new DelimiterProcessorCollection();
115
116
        // Performance optimization: always include a block "parser" that aborts parsing if a line starts with a letter
117
        // and is therefore unlikely to match any lines as a block start.
118 2256
        $this->addBlockStartParser(new SkipLinesStartingWithLettersParser(), 249);
119 2256
    }
120
121 2180
    public function getConfiguration(): ConfigurationInterface
122
    {
123 2180
        return $this->config->reader();
124
    }
125
126
    /**
127
     * @deprecated Environment::mergeConfig() is deprecated since league/commonmark v2.0 and will be removed in v3.0. Configuration should be set when instantiating the environment instead.
128
     *
129
     * @param array<string, mixed> $config
130
     */
131 4
    public function mergeConfig(array $config): void
132
    {
133 4
        @\trigger_error('Environment::mergeConfig() is deprecated since league/commonmark v2.0 and will be removed in v3.0. Configuration should be set when instantiating the environment instead.', \E_USER_DEPRECATED);
134
135 4
        $this->assertUninitialized('Failed to modify configuration.');
136
137 2
        $this->config->merge($config);
138 2
    }
139
140 2260
    public function addBlockStartParser(BlockStartParserInterface $parser, int $priority = 0): EnvironmentBuilderInterface
141
    {
142 2260
        $this->assertUninitialized('Failed to add block start parser.');
143
144 2260
        $this->blockStartParsers->add($parser, $priority);
145 2260
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
146
147 2260
        return $this;
148
    }
149
150 2152
    public function addInlineParser(InlineParserInterface $parser, int $priority = 0): EnvironmentBuilderInterface
151
    {
152 2152
        $this->assertUninitialized('Failed to add inline parser.');
153
154 2150
        $this->inlineParsers->add($parser, $priority);
155 2150
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
156
157 2150
        return $this;
158
    }
159
160 2150
    public function addDelimiterProcessor(DelimiterProcessorInterface $processor): EnvironmentBuilderInterface
161
    {
162 2150
        $this->assertUninitialized('Failed to add delimiter processor.');
163 2148
        $this->delimiterProcessors->add($processor);
164 2148
        $this->injectEnvironmentAndConfigurationIfNeeded($processor);
165
166 2148
        return $this;
167
    }
168
169 2162
    public function addRenderer(string $nodeClass, NodeRendererInterface $renderer, int $priority = 0): EnvironmentBuilderInterface
170
    {
171 2162
        $this->assertUninitialized('Failed to add renderer.');
172
173 2160
        if (! isset($this->renderersByClass[$nodeClass])) {
174 2160
            $this->renderersByClass[$nodeClass] = new PrioritizedList();
175
        }
176
177 2160
        $this->renderersByClass[$nodeClass]->add($renderer, $priority);
178 2160
        $this->injectEnvironmentAndConfigurationIfNeeded($renderer);
179
180 2160
        return $this;
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186 2158
    public function getBlockStartParsers(): iterable
187
    {
188 2158
        if (! $this->extensionsInitialized) {
189 22
            $this->initializeExtensions();
190
        }
191
192 2158
        return $this->blockStartParsers->getIterator();
193
    }
194
195 1938
    public function getDelimiterProcessors(): DelimiterProcessorCollection
196
    {
197 1938
        if (! $this->extensionsInitialized) {
198 4
            $this->initializeExtensions();
199
        }
200
201 1938
        return $this->delimiterProcessors;
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     */
207 2156
    public function getRenderersForClass(string $nodeClass): iterable
208
    {
209 2156
        if (! $this->extensionsInitialized) {
210 18
            $this->initializeExtensions();
211
        }
212
213
        // If renderers are defined for this specific class, return them immediately
214 2156
        if (isset($this->renderersByClass[$nodeClass])) {
215 2150
            return $this->renderersByClass[$nodeClass];
216
        }
217
218
        /** @psalm-suppress TypeDoesNotContainType -- Bug: https://github.com/vimeo/psalm/issues/3332 */
219 26
        while (\class_exists($parent ??= $nodeClass) && $parent = \get_parent_class($parent)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parent does not seem to be defined for all execution paths leading up to this point.
Loading history...
220 22
            if (! isset($this->renderersByClass[$parent])) {
221 4
                continue;
222
            }
223
224
            // "Cache" this result to avoid future loops
225 20
            return $this->renderersByClass[$nodeClass] = $this->renderersByClass[$parent];
226
        }
227
228 6
        return [];
229
    }
230
231
    /**
232
     * {@inheritDoc}
233
     */
234 14
    public function getExtensions(): iterable
235
    {
236 14
        return $this->extensions;
237
    }
238
239
    /**
240
     * Add a single extension
241
     *
242
     * @return $this
243
     */
244 2186
    public function addExtension(ExtensionInterface $extension): EnvironmentBuilderInterface
245
    {
246 2186
        $this->assertUninitialized('Failed to add extension.');
247
248 2184
        $this->extensions[]              = $extension;
249 2184
        $this->uninitializedExtensions[] = $extension;
250
251 2184
        if ($extension instanceof ConfigurableExtensionInterface) {
252 2180
            $extension->configureSchema($this->config);
253
        }
254
255 2184
        return $this;
256
    }
257
258 2206
    private function initializeExtensions(): void
259
    {
260
        // Initialize the slug normalizer
261 2206
        $this->getSlugNormalizer();
262
263
        // Ask all extensions to register their components
264 2206
        while (\count($this->uninitializedExtensions) > 0) {
265 2144
            foreach ($this->uninitializedExtensions as $i => $extension) {
266 2144
                $extension->register($this);
267 2144
                unset($this->uninitializedExtensions[$i]);
268
            }
269
        }
270
271 2206
        $this->extensionsInitialized = true;
272
273
        // Create the special delimiter parser if any processors were registered
274 2206
        if ($this->delimiterProcessors->count() > 0) {
275 2146
            $this->inlineParsers->add(new DelimiterParser($this->delimiterProcessors), PHP_INT_MIN);
276
        }
277 2206
    }
278
279 2260
    private function injectEnvironmentAndConfigurationIfNeeded(object $object): void
280
    {
281 2260
        if ($object instanceof EnvironmentAwareInterface) {
282 2152
            $object->setEnvironment($this);
283
        }
284
285 2260
        if ($object instanceof ConfigurationAwareInterface) {
286 2214
            $object->setConfiguration($this->config->reader());
287
        }
288 2260
    }
289
290
    /**
291
     * @deprecated Instantiate the environment and add the extension yourself
292
     *
293
     * @param array<string, mixed> $config
294
     */
295 2
    public static function createCommonMarkEnvironment(array $config = []): Environment
296
    {
297 2
        $environment = new self($config);
298 2
        $environment->addExtension(new CommonMarkCoreExtension());
299
300 2
        return $environment;
301
    }
302
303
    /**
304
     * @deprecated Instantiate the environment and add the extension yourself
305
     *
306
     * @param array<string, mixed> $config
307
     */
308 2
    public static function createGFMEnvironment(array $config = []): Environment
309
    {
310 2
        $environment = new self($config);
311 2
        $environment->addExtension(new CommonMarkCoreExtension());
312 2
        $environment->addExtension(new GithubFlavoredMarkdownExtension());
313
314 2
        return $environment;
315
    }
316
317 2214
    public function addEventListener(string $eventClass, callable $listener, int $priority = 0): EnvironmentBuilderInterface
318
    {
319 2214
        $this->assertUninitialized('Failed to add event listener.');
320
321 2214
        $this->listenerData->add(new ListenerData($eventClass, $listener), $priority);
322
323 2214
        if (\is_object($listener)) {
324 312
            $this->injectEnvironmentAndConfigurationIfNeeded($listener);
325 2214
        } elseif (\is_array($listener) && \is_object($listener[0])) {
326 2214
            $this->injectEnvironmentAndConfigurationIfNeeded($listener[0]);
327
        }
328
329 2214
        return $this;
330
    }
331
332
    /**
333
     * {@inheritDoc}
334
     */
335 2154
    public function dispatch(object $event)
336
    {
337 2154
        if (! $this->extensionsInitialized) {
338 2154
            $this->initializeExtensions();
339
        }
340
341 2154
        if ($this->eventDispatcher !== null) {
342 2
            return $this->eventDispatcher->dispatch($event);
343
        }
344
345 2152
        foreach ($this->getListenersForEvent($event) as $listener) {
346 2146
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
347 2
                return $event;
348
            }
349
350 2146
            $listener($event);
351
        }
352
353 2148
        return $event;
354
    }
355
356 2
    public function setEventDispatcher(EventDispatcherInterface $dispatcher): void
357
    {
358 2
        $this->eventDispatcher = $dispatcher;
359 2
    }
360
361
    /**
362
     * {@inheritDoc}
363
     *
364
     * @return iterable<callable>
365
     */
366 2152
    public function getListenersForEvent(object $event): iterable
367
    {
368 2152
        foreach ($this->listenerData as $listenerData) {
369
            \assert($listenerData instanceof ListenerData);
370
371 2150
            if (! \is_a($event, $listenerData->getEvent())) {
372 2144
                continue;
373
            }
374
375 2146
            yield function (object $event) use ($listenerData) {
376 2146
                if (! $this->extensionsInitialized) {
377
                    $this->initializeExtensions();
378
                }
379
380 2146
                return \call_user_func($listenerData->getListener(), $event);
381 2146
            };
382
        }
383 2148
    }
384
385
    /**
386
     * @return iterable<InlineParserInterface>
387
     */
388 2144
    public function getInlineParsers(): iterable
389
    {
390 2144
        if (! $this->extensionsInitialized) {
391 8
            $this->initializeExtensions();
392
        }
393
394 2144
        return $this->inlineParsers->getIterator();
395
    }
396
397 2220
    public function getSlugNormalizer(): TextNormalizerInterface
398
    {
399 2220
        if ($this->slugNormalizer === null) {
400 2220
            $normalizer = $this->config->get('slug_normalizer/instance');
401
            \assert($normalizer instanceof TextNormalizerInterface);
402 2218
            $this->injectEnvironmentAndConfigurationIfNeeded($normalizer);
403
404 2218
            if ($this->config->get('slug_normalizer/unique') !== UniqueSlugNormalizerInterface::DISABLED && ! $normalizer instanceof UniqueSlugNormalizer) {
405 2216
                $normalizer = new UniqueSlugNormalizer($normalizer);
406
            }
407
408 2218
            if ($normalizer instanceof UniqueSlugNormalizer) {
409 2216
                if ($this->config->get('slug_normalizer/unique') === UniqueSlugNormalizerInterface::PER_DOCUMENT) {
410 2214
                    $this->addEventListener(DocumentParsedEvent::class, [$normalizer, 'clearHistory'], -1000);
411
                }
412
            }
413
414 2218
            $this->slugNormalizer = $normalizer;
415
        }
416
417 2218
        return $this->slugNormalizer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->slugNormalizer could return the type null which is incompatible with the type-hinted return League\CommonMark\Normal...TextNormalizerInterface. Consider adding an additional type-check to rule them out.
Loading history...
418
    }
419
420
    /**
421
     * @throws \RuntimeException
422
     */
423 2260
    private function assertUninitialized(string $message): void
424
    {
425 2260
        if ($this->extensionsInitialized) {
426 14
            throw new \RuntimeException($message . ' Extensions have already been initialized.');
427
        }
428 2260
    }
429
430 2404
    public static function createDefaultConfiguration(): Configuration
431
    {
432 2404
        return new Configuration([
433 2404
            'html_input' => Expect::anyOf(HtmlFilter::STRIP, HtmlFilter::ALLOW, HtmlFilter::ESCAPE)->default(HtmlFilter::ALLOW),
434 2404
            'allow_unsafe_links' => Expect::bool(true),
435 2404
            'max_nesting_level' => Expect::type('int')->default(PHP_INT_MAX),
436 2404
            'renderer' => Expect::structure([
437 2404
                'block_separator' => Expect::string("\n"),
438 2404
                'inner_separator' => Expect::string("\n"),
439 2404
                'soft_break' => Expect::string("\n"),
440
            ]),
441 2404
            'slug_normalizer' => Expect::structure([
442 2404
                'instance' => Expect::type(TextNormalizerInterface::class)->default(new SlugNormalizer()),
443 2404
                'max_length' => Expect::int()->min(0)->default(255),
444 2404
                'unique' => Expect::anyOf(UniqueSlugNormalizerInterface::DISABLED, UniqueSlugNormalizerInterface::PER_ENVIRONMENT, UniqueSlugNormalizerInterface::PER_DOCUMENT)->default(UniqueSlugNormalizerInterface::PER_DOCUMENT),
445
            ]),
446
        ]);
447
    }
448
}
449