Passed
Push — main ( ff6d77...f1b7a1 )
by Colin
05:05 queued 02:19
created

Environment::getExtensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
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
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 2266
    public function __construct(array $config = [])
107
    {
108 2266
        $this->config = self::createDefaultConfiguration();
109 2266
        $this->config->merge($config);
110
111 2266
        $this->blockStartParsers   = new PrioritizedList();
112 2266
        $this->inlineParsers       = new PrioritizedList();
113 2266
        $this->listenerData        = new PrioritizedList();
114 2266
        $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 2266
        $this->addBlockStartParser(new SkipLinesStartingWithLettersParser(), 249);
119 2266
    }
120
121 2190
    public function getConfiguration(): ConfigurationInterface
122
    {
123 2190
        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 2270
    public function addBlockStartParser(BlockStartParserInterface $parser, int $priority = 0): EnvironmentBuilderInterface
141
    {
142 2270
        $this->assertUninitialized('Failed to add block start parser.');
143
144 2270
        $this->blockStartParsers->add($parser, $priority);
145 2270
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
146
147 2270
        return $this;
148
    }
149
150 2162
    public function addInlineParser(InlineParserInterface $parser, int $priority = 0): EnvironmentBuilderInterface
151
    {
152 2162
        $this->assertUninitialized('Failed to add inline parser.');
153
154 2160
        $this->inlineParsers->add($parser, $priority);
155 2160
        $this->injectEnvironmentAndConfigurationIfNeeded($parser);
156
157 2160
        return $this;
158
    }
159
160 2160
    public function addDelimiterProcessor(DelimiterProcessorInterface $processor): EnvironmentBuilderInterface
161
    {
162 2160
        $this->assertUninitialized('Failed to add delimiter processor.');
163 2158
        $this->delimiterProcessors->add($processor);
164 2158
        $this->injectEnvironmentAndConfigurationIfNeeded($processor);
165
166 2158
        return $this;
167
    }
168
169 2172
    public function addRenderer(string $nodeClass, NodeRendererInterface $renderer, int $priority = 0): EnvironmentBuilderInterface
170
    {
171 2172
        $this->assertUninitialized('Failed to add renderer.');
172
173 2170
        if (! isset($this->renderersByClass[$nodeClass])) {
174 2170
            $this->renderersByClass[$nodeClass] = new PrioritizedList();
175
        }
176
177 2170
        $this->renderersByClass[$nodeClass]->add($renderer, $priority);
178 2170
        $this->injectEnvironmentAndConfigurationIfNeeded($renderer);
179
180 2170
        return $this;
181
    }
182
183
    /**
184
     * {@inheritDoc}
185
     */
186 2166
    public function getBlockStartParsers(): iterable
187
    {
188 2166
        if (! $this->extensionsInitialized) {
189 22
            $this->initializeExtensions();
190
        }
191
192 2166
        return $this->blockStartParsers->getIterator();
193
    }
194
195 1946
    public function getDelimiterProcessors(): DelimiterProcessorCollection
196
    {
197 1946
        if (! $this->extensionsInitialized) {
198 4
            $this->initializeExtensions();
199
        }
200
201 1946
        return $this->delimiterProcessors;
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     */
207 2164
    public function getRenderersForClass(string $nodeClass): iterable
208
    {
209 2164
        if (! $this->extensionsInitialized) {
210 18
            $this->initializeExtensions();
211
        }
212
213
        // If renderers are defined for this specific class, return them immediately
214 2164
        if (isset($this->renderersByClass[$nodeClass])) {
215 2158
            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 2196
    public function addExtension(ExtensionInterface $extension): EnvironmentBuilderInterface
245
    {
246 2196
        $this->assertUninitialized('Failed to add extension.');
247
248 2194
        $this->extensions[]              = $extension;
249 2194
        $this->uninitializedExtensions[] = $extension;
250
251 2194
        if ($extension instanceof ConfigurableExtensionInterface) {
252 2190
            $extension->configureSchema($this->config);
253
        }
254
255 2194
        return $this;
256
    }
257
258 2216
    private function initializeExtensions(): void
259
    {
260
        // Initialize the slug normalizer
261 2216
        $this->getSlugNormalizer();
262
263
        // Ask all extensions to register their components
264 2216
        while (\count($this->uninitializedExtensions) > 0) {
265 2154
            foreach ($this->uninitializedExtensions as $i => $extension) {
266 2154
                $extension->register($this);
267 2154
                unset($this->uninitializedExtensions[$i]);
268
            }
269
        }
270
271 2216
        $this->extensionsInitialized = true;
272
273
        // Create the special delimiter parser if any processors were registered
274 2216
        if ($this->delimiterProcessors->count() > 0) {
275 2156
            $this->inlineParsers->add(new DelimiterParser($this->delimiterProcessors), PHP_INT_MIN);
276
        }
277 2216
    }
278
279 2270
    private function injectEnvironmentAndConfigurationIfNeeded(object $object): void
280
    {
281 2270
        if ($object instanceof EnvironmentAwareInterface) {
282 2162
            $object->setEnvironment($this);
283
        }
284
285 2270
        if ($object instanceof ConfigurationAwareInterface) {
286 2224
            $object->setConfiguration($this->config->reader());
287
        }
288 2270
    }
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 2224
    public function addEventListener(string $eventClass, callable $listener, int $priority = 0): EnvironmentBuilderInterface
318
    {
319 2224
        $this->assertUninitialized('Failed to add event listener.');
320
321 2224
        $this->listenerData->add(new ListenerData($eventClass, $listener), $priority);
322
323 2224
        if (\is_object($listener)) {
324 322
            $this->injectEnvironmentAndConfigurationIfNeeded($listener);
325 2224
        } elseif (\is_array($listener) && \is_object($listener[0])) {
326 2224
            $this->injectEnvironmentAndConfigurationIfNeeded($listener[0]);
327
        }
328
329 2224
        return $this;
330
    }
331
332
    /**
333
     * {@inheritDoc}
334
     */
335 2164
    public function dispatch(object $event)
336
    {
337 2164
        if (! $this->extensionsInitialized) {
338 2164
            $this->initializeExtensions();
339
        }
340
341 2164
        if ($this->eventDispatcher !== null) {
342 2
            return $this->eventDispatcher->dispatch($event);
343
        }
344
345 2162
        foreach ($this->getListenersForEvent($event) as $listener) {
346 2156
            if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
347 2
                return $event;
348
            }
349
350 2156
            $listener($event);
351
        }
352
353 2156
        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 2162
    public function getListenersForEvent(object $event): iterable
367
    {
368 2162
        foreach ($this->listenerData as $listenerData) {
369
            \assert($listenerData instanceof ListenerData);
370
371
            /** @psalm-suppress ArgumentTypeCoercion */
372 2160
            if (! \is_a($event, $listenerData->getEvent())) {
373 2152
                continue;
374
            }
375
376 2156
            yield function (object $event) use ($listenerData) {
377
                if (! $this->extensionsInitialized) {
378
                    $this->initializeExtensions();
379
                }
380
381
                return \call_user_func($listenerData->getListener(), $event);
382 2156
            };
383
        }
384 2156
    }
385
386
    /**
387
     * @return iterable<InlineParserInterface>
388
     */
389 2152
    public function getInlineParsers(): iterable
390
    {
391 2152
        if (! $this->extensionsInitialized) {
392 8
            $this->initializeExtensions();
393
        }
394
395 2152
        return $this->inlineParsers->getIterator();
396
    }
397
398 2230
    public function getSlugNormalizer(): TextNormalizerInterface
399
    {
400 2230
        if ($this->slugNormalizer === null) {
401 2230
            $normalizer = $this->config->get('slug_normalizer/instance');
402
            \assert($normalizer instanceof TextNormalizerInterface);
403 2228
            $this->injectEnvironmentAndConfigurationIfNeeded($normalizer);
404
405 2228
            if ($this->config->get('slug_normalizer/unique') !== UniqueSlugNormalizerInterface::DISABLED && ! $normalizer instanceof UniqueSlugNormalizer) {
406 2226
                $normalizer = new UniqueSlugNormalizer($normalizer);
407
            }
408
409 2228
            if ($normalizer instanceof UniqueSlugNormalizer) {
410 2226
                if ($this->config->get('slug_normalizer/unique') === UniqueSlugNormalizerInterface::PER_DOCUMENT) {
411 2224
                    $this->addEventListener(DocumentParsedEvent::class, [$normalizer, 'clearHistory'], -1000);
412
                }
413
            }
414
415 2228
            $this->slugNormalizer = $normalizer;
416
        }
417
418 2228
        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...
419
    }
420
421
    /**
422
     * @throws \RuntimeException
423
     */
424 2270
    private function assertUninitialized(string $message): void
425
    {
426 2270
        if ($this->extensionsInitialized) {
427 14
            throw new \RuntimeException($message . ' Extensions have already been initialized.');
428
        }
429 2270
    }
430
431 2414
    public static function createDefaultConfiguration(): Configuration
432
    {
433 2414
        return new Configuration([
434 2414
            'html_input' => Expect::anyOf(HtmlFilter::STRIP, HtmlFilter::ALLOW, HtmlFilter::ESCAPE)->default(HtmlFilter::ALLOW),
435 2414
            'allow_unsafe_links' => Expect::bool(true),
436 2414
            'max_nesting_level' => Expect::type('int')->default(PHP_INT_MAX),
437 2414
            'renderer' => Expect::structure([
438 2414
                'block_separator' => Expect::string("\n"),
439 2414
                'inner_separator' => Expect::string("\n"),
440 2414
                'soft_break' => Expect::string("\n"),
441
            ]),
442 2414
            'slug_normalizer' => Expect::structure([
443 2414
                'instance' => Expect::type(TextNormalizerInterface::class)->default(new SlugNormalizer()),
444 2414
                'max_length' => Expect::int()->min(0)->default(255),
445 2414
                'unique' => Expect::anyOf(UniqueSlugNormalizerInterface::DISABLED, UniqueSlugNormalizerInterface::PER_ENVIRONMENT, UniqueSlugNormalizerInterface::PER_DOCUMENT)->default(UniqueSlugNormalizerInterface::PER_DOCUMENT),
446
            ]),
447
        ]);
448
    }
449
}
450