Skip to content

Commit c7e43cc

Browse files
mpdudeMalteWunsch
andauthored
Make the parser type, iteration count and recursion limit configurable (#6)
* Make the parser type, iteration count and recursion limit configurable * Additional documentation of configuration options Co-authored-by: Malte Wunsch <mw@webfactory.de>
1 parent 98cdeb6 commit c7e43cc

5 files changed

Lines changed: 71 additions & 11 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Webfactory\ShortcodeBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
class Configuration implements ConfigurationInterface
9+
{
10+
public function getConfigTreeBuilder()
11+
{
12+
$treeBuilder = new TreeBuilder('webfactory_shortcode');
13+
14+
// For details on these configuration options, see https://github.com/thunderer/Shortcode#parsing and
15+
// https://github.com/thunderer/Shortcode#configuration .
16+
$treeBuilder->getRootNode()
17+
->children()
18+
->enumNode('parser')
19+
->info('Which parser type to use, choose "regular" or "regex".')
20+
->values(['regular', 'regex'])
21+
->defaultValue('regular')
22+
->end()
23+
->integerNode('recursion_depth')
24+
->info('Controls how many levels of shortcodes to process')
25+
->defaultValue(null)
26+
->end()
27+
->integerNode('max_iterations')
28+
->info('Limit the number of iterations when resolving shortcodes')
29+
->defaultValue(null)
30+
->end()
31+
;
32+
33+
return $treeBuilder;
34+
}
35+
}

DependencyInjection/WebfactoryShortcodeExtension.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Webfactory\ShortcodeBundle\DependencyInjection;
44

5+
use Symfony\Component\Config\FileLocator;
56
use Symfony\Component\DependencyInjection\ContainerBuilder;
6-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
77
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
8-
use Symfony\Component\Config\FileLocator;
8+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9+
use Thunder\Shortcode\Processor\Processor;
910

1011
/**
1112
* Loads the bundle configuration.
@@ -16,5 +17,14 @@ public function load(array $configs, ContainerBuilder $container)
1617
{
1718
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
1819
$loader->load('shortcodes.xml');
20+
21+
$configuration = new Configuration();
22+
$config = $this->processConfiguration($configuration, $configs);
23+
24+
$container->setAlias('webfactory_shortcode.parser', 'webfactory_shortcode.'.$config['parser'].'_parser');
25+
26+
$container->getDefinition(Processor::class)
27+
->addMethodCall('withRecursionDepth', [$config['recursion_depth']])
28+
->addMethodCall('withMaxIterations', [$config['max_iterations']]);
1929
}
2030
}

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ With the route prefix defined as above, call ```/shortcodes/``` to get the list
216216
detail pages.
217217

218218

219+
### Configuration
220+
221+
In most cases, the default values should work fine. But you might want to configure something else, e.g. if the default
222+
parser needs too much memory for a large snippet. See thunderer's documentation on [parsing](https://github.com/thunderer/Shortcode#parsing)
223+
and [configuration](https://github.com/thunderer/Shortcode#configuration) so you understand the advantages,
224+
disadvantages and limitations:
225+
226+
```yaml
227+
// config.yml
228+
229+
webfactory_shortcode:
230+
parser: 'regex' # default: regular
231+
recursion_depth: 2 # default: null
232+
max_iterations: 2 # default: null
233+
```
234+
219235
### Automated Tests for your Shortcodes
220236
221237
With the shortcode guide enabled (remember: you may enable it just in your test environment), you can easily write

Resources/config/shortcodes.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
<!-- Definition of a Shortcode HandlerContainer instance. -->
99
<service id="Thunder\Shortcode\HandlerContainer\HandlerContainer"/>
1010

11-
<!-- Definition of a Shortcode RegularParser instance. -->
12-
<service id="Thunder\Shortcode\Parser\RegularParser"/>
11+
<service id="webfactory_shortcode.regular_parser" class="Thunder\Shortcode\Parser\RegularParser"/>
12+
<service id="webfactory_shortcode.regex_parser" class="Thunder\Shortcode\Parser\RegexParser"/>
1313

1414
<!-- Definition of a Shortcode Processor instance. -->
1515
<service id="Thunder\Shortcode\Processor\Processor">
16-
<argument type="service" id="Thunder\Shortcode\Parser\RegularParser" />
16+
<argument type="service" id="webfactory_shortcode.parser" />
1717
<argument type="service" id="Thunder\Shortcode\HandlerContainer\HandlerContainer" />
18+
<call method="withEventContainer">
19+
<argument type="service" id="Thunder\Shortcode\EventContainer\EventContainer" />
20+
</call>
1821
</service>
1922

2023
<!-- Definition of a Shortcode EventContainer instance. -->
@@ -55,7 +58,6 @@
5558
<!-- Twig extension providing the |shortcodes filter. The content will be passed to the Shortcode Processor. -->
5659
<service id="Webfactory\ShortcodeBundle\Twig\ShortcodeExtension" class="Webfactory\ShortcodeBundle\Twig\ShortcodeExtension">
5760
<argument type="service" id="Thunder\Shortcode\Processor\Processor"/>
58-
<argument type="service" id="Thunder\Shortcode\EventContainer\EventContainer"/>
5961
<tag name="twig.extension"/>
6062
</service>
6163

Twig/ShortcodeExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ final class ShortcodeExtension extends AbstractExtension
1515
/** @var Processor */
1616
private $processor;
1717

18-
/**
19-
* @param Processor $processor
20-
*/
21-
public function __construct(Processor $processor, EventContainer $eventContainer)
18+
public function __construct(Processor $processor)
2219
{
23-
$this->processor = $processor->withMaxIterations(null)->withEventContainer($eventContainer);
20+
$this->processor = $processor;
2421
}
2522

2623
public function getFilters()

0 commit comments

Comments
 (0)