Skip to content

Commit 16604e3

Browse files
committed
Use a factory to create Processor instances
The Processor uses a somewhat surprising pattern of returning new instances from with*() methods to assure instance immutability. Thus, we cannot easily configure it as a service in the Symfony DIC but need to resort to writing a factory for it.
1 parent e11f686 commit 16604e3

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

DependencyInjection/WebfactoryShortcodeExtension.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
77
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
88
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9-
use Thunder\Shortcode\Processor\Processor;
109

1110
/**
1211
* Loads the bundle configuration.
@@ -23,8 +22,7 @@ public function load(array $configs, ContainerBuilder $container)
2322

2423
$container->setAlias('webfactory_shortcode.parser', 'webfactory_shortcode.'.$config['parser'].'_parser');
2524

26-
$container->getDefinition(Processor::class)
27-
->addMethodCall('withRecursionDepth', [$config['recursion_depth']])
28-
->addMethodCall('withMaxIterations', [$config['max_iterations']]);
25+
$container->setParameter('webfactory_shortcode.recursion_depth', $config['recursion_depth']);
26+
$container->setParameter('webfactory_shortcode.max_iterations', $config['max_iterations']);
2927
}
3028
}

Factory/ProcessorFactory.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Webfactory\ShortcodeBundle\Factory;
4+
5+
use Thunder\Shortcode\EventContainer\EventContainer;
6+
use Thunder\Shortcode\HandlerContainer\HandlerContainer;
7+
use Thunder\Shortcode\Parser\ParserInterface;
8+
use Thunder\Shortcode\Processor\Processor;
9+
10+
class ProcessorFactory
11+
{
12+
/**
13+
* @var ParserInterface
14+
*/
15+
private $parserInterface;
16+
17+
/**
18+
* @var HandlerContainer
19+
*/
20+
private $handlerContainer;
21+
22+
/**
23+
* @var EventContainer
24+
*/
25+
private $eventContainer;
26+
27+
/**
28+
* @var int
29+
*/
30+
private $recursionDepth;
31+
32+
/**
33+
* @var int
34+
*/
35+
private $maxIterations;
36+
37+
public function __construct(ParserInterface $parserInterface, HandlerContainer $handlerContainer, EventContainer $eventContainer, ?int $recursionDepth, ?int $maxIterations)
38+
{
39+
$this->parserInterface = $parserInterface;
40+
$this->handlerContainer = $handlerContainer;
41+
$this->eventContainer = $eventContainer;
42+
$this->recursionDepth = $recursionDepth;
43+
$this->maxIterations = $maxIterations;
44+
}
45+
46+
public function create(): Processor
47+
{
48+
$processor = new Processor($this->parserInterface, $this->handlerContainer);
49+
50+
return $processor->withEventContainer($this->eventContainer)->withRecursionDepth($this->recursionDepth)->withMaxIterations($this->maxIterations);
51+
}
52+
}

Resources/config/shortcodes.xml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
<service id="webfactory_shortcode.regular_parser" class="Thunder\Shortcode\Parser\RegularParser"/>
1212
<service id="webfactory_shortcode.regex_parser" class="Thunder\Shortcode\Parser\RegexParser"/>
1313

14-
<!-- Definition of a Shortcode Processor instance. -->
15-
<service id="Thunder\Shortcode\Processor\Processor">
14+
<service id="Webfactory\ShortcodeBundle\Factory\ProcessorFactory">
1615
<argument type="service" id="webfactory_shortcode.parser" />
1716
<argument type="service" id="Thunder\Shortcode\HandlerContainer\HandlerContainer" />
18-
<call method="withEventContainer">
19-
<argument type="service" id="Thunder\Shortcode\EventContainer\EventContainer" />
20-
</call>
17+
<argument type="service" id="Thunder\Shortcode\EventContainer\EventContainer" />
18+
<argument>%webfactory_shortcode.recursion_depth%</argument>
19+
<argument>%webfactory_shortcode.max_iterations%</argument>
20+
</service>
21+
22+
<!-- Definition of a Shortcode Processor instance. -->
23+
<service id="Thunder\Shortcode\Processor\Processor">
24+
<factory service="Webfactory\ShortcodeBundle\Factory\ProcessorFactory" method="create" />
2125
</service>
2226

2327
<!-- Definition of a Shortcode EventContainer instance. -->

0 commit comments

Comments
 (0)