Skip to content

Commit 465965a

Browse files
committed
merged branch gnutix/2.3 (PR symfony#8339)
This PR was submitted for the 2.3 branch but it was merged into the master branch instead (closes symfony#8339). Discussion ---------- [Routing] Add an extension-point for DI into the Matcher/Generator Dumpers | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - The Routing's MatcherDumper and GeneratorDumper classes are instantiated very deeply into the `Router::getGenerator()` and `Router::getMatcher()` methods. This pull requests aims to : 1. separate the instances creation 2. ease overriding (lesser risks of changes at framework updates) 3. ease dependencies injection for these classes (as we can't inject dependencies in another way, be it service definition, compiler pass or whatever I'm aware of). An example of usage would be the following : https://gist.github.com/gnutix/5844630 It's a real case I'm having in my company's current project. Commits ------- 5a03a7f [Routing] Add an extension-point for DI into the Matcher/Generator Dumpers
2 parents 31ac13b + 40b598c commit 465965a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/Symfony/Component/Routing/Router.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Psr\Log\LoggerInterface;
1717
use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
1818
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19+
use Symfony\Component\Routing\Generator\Dumper\GeneratorDumperInterface;
1920
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
21+
use Symfony\Component\Routing\Matcher\Dumper\MatcherDumperInterface;
2022

2123
/**
2224
* The Router class is an example of the integration of all pieces of the
@@ -233,7 +235,7 @@ public function getMatcher()
233235
$class = $this->options['matcher_cache_class'];
234236
$cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
235237
if (!$cache->isFresh($class)) {
236-
$dumper = new $this->options['matcher_dumper_class']($this->getRouteCollection());
238+
$dumper = $this->getMatcherDumperInstance();
237239

238240
$options = array(
239241
'class' => $class,
@@ -265,7 +267,7 @@ public function getGenerator()
265267
$class = $this->options['generator_cache_class'];
266268
$cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
267269
if (!$cache->isFresh($class)) {
268-
$dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());
270+
$dumper = $this->getGeneratorDumperInstance();
269271

270272
$options = array(
271273
'class' => $class,
@@ -286,4 +288,20 @@ public function getGenerator()
286288

287289
return $this->generator;
288290
}
291+
292+
/**
293+
* @return GeneratorDumperInterface
294+
*/
295+
protected function getGeneratorDumperInstance()
296+
{
297+
return new $this->options['generator_dumper_class']($this->getRouteCollection());
298+
}
299+
300+
/**
301+
* @return MatcherDumperInterface
302+
*/
303+
protected function getMatcherDumperInstance()
304+
{
305+
return new $this->options['matcher_dumper_class']($this->getRouteCollection());
306+
}
289307
}

0 commit comments

Comments
 (0)