Skip to content

Commit

Permalink
minor #24863 [Bridge\Twig] Lazy-load deps (nicolas-grekas)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[Bridge\Twig] Lazy-load deps

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Spotted while benching a HelloWorld and comparing v3.3 to v4.0.
At this micro-bench level, even registering one more twig extension makes a difference (here, it's `TranslationExtension`, after #24358)

Commits
-------

3fc766f [Bridge\Twig] Lazy-load deps
  • Loading branch information
fabpot committed Nov 7, 2017
2 parents 2dbe17b + 3fc766f commit 850bb2d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Expand Up @@ -32,7 +32,7 @@ class DumpExtension extends AbstractExtension
public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
{
$this->cloner = $cloner;
$this->dumper = $dumper ?: new HtmlDumper();
$this->dumper = $dumper;
}

public function getFunctions()
Expand Down Expand Up @@ -73,6 +73,7 @@ public function dump(Environment $env, $context)
}

$dump = fopen('php://memory', 'r+b');
$this->dumper = $this->dumper ?: new HtmlDumper();
$this->dumper->setCharset($env->getCharset());

foreach ($vars as $value) {
Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Bridge/Twig/Extension/TranslationExtension.php
Expand Up @@ -34,10 +34,6 @@ class TranslationExtension extends AbstractExtension

public function __construct(TranslatorInterface $translator = null, NodeVisitorInterface $translationNodeVisitor = null)
{
if (!$translationNodeVisitor) {
$translationNodeVisitor = new TranslationNodeVisitor();
}

$this->translator = $translator;
$this->translationNodeVisitor = $translationNodeVisitor;
}
Expand Down Expand Up @@ -84,12 +80,12 @@ public function getTokenParsers()
*/
public function getNodeVisitors()
{
return array($this->translationNodeVisitor, new TranslationDefaultDomainNodeVisitor());
return array($this->getTranslationNodeVisitor(), new TranslationDefaultDomainNodeVisitor());
}

public function getTranslationNodeVisitor()
{
return $this->translationNodeVisitor;
return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor();
}

public function trans($message, array $arguments = array(), $domain = null, $locale = null)
Expand Down

0 comments on commit 850bb2d

Please sign in to comment.