Skip to content

Commit e0f56ab

Browse files
committed
merged branch bschussek/improve-name-parser (PR #7812)
This PR was merged into the master branch. Discussion ---------- [FrameworkBundle] Improved TemplateNameParser performance | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Performance test in symfony-standard: ```php <?php use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser; require __DIR__.'/vendor/autoload.php'; require __DIR__.'/app/AppKernel.php'; $kernel = new AppKernel('dev', true); $kernel->boot(); $parser = new TemplateNameParser($kernel); $time = microtime(true); for ($i = 0; $i < 50; ++$i) { $parser->parse("AcmeDemoBundle:Foo:bar$i.html.twig"); } echo "Time: " . (microtime(true) - $time)*1000 . "ms\n"; echo "Memory: " . memory_get_peak_usage(true)/(1024*1024) . "MB\n"; ``` Before: ``` Time: 3.80706787109ms Memory: 1.5MB ``` After: ``` Time: 3.13401222229ms Memory: 1.5MB ``` Commits ------- f092c7f [FrameworkBundle] Improved TemplateNameParser performance
2 parents 4c5cf45 + 95f5ddc commit e0f56ab

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

Templating/TemplateNameParser.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,11 @@ public function parse($name)
5656
throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
5757
}
5858

59-
$parts = explode(':', $name);
60-
if (3 !== count($parts)) {
59+
if (!preg_match('/^([^:]*):([^:]*):(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches)) {
6160
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
6261
}
6362

64-
$elements = explode('.', $parts[2]);
65-
if (3 > count($elements)) {
66-
throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name));
67-
}
68-
$engine = array_pop($elements);
69-
$format = array_pop($elements);
70-
71-
$template = new TemplateReference($parts[0], $parts[1], implode('.', $elements), $format, $engine);
63+
$template = new TemplateReference($matches[1], $matches[2], $matches[3], $matches[4], $matches[5]);
7264

7365
if ($template->get('bundle')) {
7466
try {

0 commit comments

Comments
 (0)