Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e508ecc

Browse files
committed
merged branch jfsimon/issue-6747 (PR #7281)
This PR was squashed before being merged into the 2.1 branch (closes #7281). Commits ------- e3547c6 [TwigBridge] fixes Discussion ---------- [TwigBridge] fixes `TranslationDefaultDomainNodeVisitor` ... by adding scope management. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6747 --------------------------------------------------------------------------- by vicb at 2013-03-06T15:14:57Z Are `open` and `close` good names ? I would prefer `addChild` and `getParent` as there is nothing that is really opened or closed ? Edit: by looking at the code a second time, I think open & close make sense . --------------------------------------------------------------------------- by jfsimon at 2013-03-07T13:46:13Z @vicb I know this is not consistent with the rest of the framework. I dont think @fabpot will like them. --------------------------------------------------------------------------- by stof at 2013-03-07T13:53:14Z what about enter and leave ? It would be consistent with the naming used for scopes in the DI component --------------------------------------------------------------------------- by vicb at 2013-03-07T13:59:35Z @stof I like your proposal. My main concern being "leave" (former "close"), @jfsimon could you throw when a scope is used after it has been left ? --------------------------------------------------------------------------- by jfsimon at 2013-03-07T14:01:56Z go for enter/leave, but why throwing an exception on using a leaved scope? --------------------------------------------------------------------------- by vicb at 2013-03-07T14:05:22Z because if you have a ref to a left scope, you can still use it which is bad
2 parents a27f7d8 + e3547c6 commit e508ecc

File tree

2 files changed

+151
-7
lines changed

2 files changed

+151
-7
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
namespace Symfony\Bridge\Twig\NodeVisitor;
4+
5+
/**
6+
* @author Jean-François Simon <[email protected]>
7+
*/
8+
class Scope
9+
{
10+
/**
11+
* @var Scope|null
12+
*/
13+
private $parent;
14+
15+
/**
16+
* @var Scope[]
17+
*/
18+
private $children;
19+
20+
/**
21+
* @var array
22+
*/
23+
private $data;
24+
25+
/**
26+
* @var boolean
27+
*/
28+
private $left;
29+
30+
/**
31+
* @param Scope $parent
32+
*/
33+
public function __construct(Scope $parent = null)
34+
{
35+
$this->parent = $parent;
36+
$this->left = false;
37+
}
38+
39+
/**
40+
* Opens a new child scope.
41+
*
42+
* @return Scope
43+
*/
44+
public function enter()
45+
{
46+
$child = new self($this);
47+
$this->children[] = $child;
48+
49+
return $child;
50+
}
51+
52+
/**
53+
* Closes current scope and returns parent one.
54+
*
55+
* @return Scope|null
56+
*/
57+
public function leave()
58+
{
59+
$this->left = true;
60+
61+
return $this->parent;
62+
}
63+
64+
/**
65+
* Stores data into current scope.
66+
*
67+
* @param string $key
68+
* @param mixed $value
69+
*
70+
* @throws \LogicException
71+
*
72+
* @return Scope Current scope
73+
*/
74+
public function set($key, $value)
75+
{
76+
if ($this->left) {
77+
throw new \LogicException('Left scope is not mutable.');
78+
}
79+
80+
$this->data[$key] = $value;
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* Tests if a data is visible from current scope.
87+
*
88+
* @param string $key
89+
*
90+
* @return boolean
91+
*/
92+
public function has($key)
93+
{
94+
if (array_key_exists($key, $this->data)) {
95+
return true;
96+
}
97+
98+
if (null === $this->parent) {
99+
return false;
100+
}
101+
102+
return $this->parent->has($key);
103+
}
104+
105+
/**
106+
* Returns data visible from current scope.
107+
*
108+
* @param string $key
109+
* @param mixed $default
110+
*
111+
* @return mixed
112+
*/
113+
public function get($key, $default = null)
114+
{
115+
if (array_key_exists($key, $this->data)) {
116+
return $this->data[$key];
117+
}
118+
119+
if (null === $this->parent) {
120+
return $default;
121+
}
122+
123+
return $this->parent->get($key, $default);
124+
}
125+
}

src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,47 @@
2121
*/
2222
class TranslationDefaultDomainNodeVisitor implements \Twig_NodeVisitorInterface
2323
{
24-
private $domain;
24+
/**
25+
* @var Scope
26+
*/
27+
private $scope;
28+
29+
/**
30+
* Constructor.
31+
*/
32+
public function __construct()
33+
{
34+
$this->scope = new Scope();
35+
}
2536

2637
/**
2738
* {@inheritdoc}
2839
*/
2940
public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
3041
{
42+
if ($node instanceof \Twig_Node_Block) {
43+
$this->scope = $this->scope->enter();
44+
}
45+
3146
if ($node instanceof \Twig_Node_Module) {
32-
$this->domain = null;
47+
$this->scope->set('domain', null);
3348
}
3449

3550
if ($node instanceof TransDefaultDomainNode) {
3651
if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) {
37-
$this->domain = $node->getNode('expr');
52+
$this->scope->set('domain', $node->getNode('expr'));
3853

3954
return $node;
4055
} else {
4156
$var = $env->getParser()->getVarName();
4257
$name = new \Twig_Node_Expression_AssignName($var, $node->getLine());
43-
$this->domain = new \Twig_Node_Expression_Name($var, $node->getLine());
58+
$this->scope->set('domain', new \Twig_Node_Expression_Name($var, $node->getLine()));
4459

4560
return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine());
4661
}
4762
}
4863

49-
if (null === $this->domain) {
64+
if (!$this->scope->has('domain')) {
5065
return $node;
5166
}
5267

@@ -58,11 +73,11 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
5873
$arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
5974
}
6075

61-
$arguments->setNode($ind, $this->domain);
76+
$arguments->setNode($ind, $this->scope->get('domain'));
6277
}
6378
} elseif ($node instanceof TransNode) {
6479
if (null === $node->getNode('domain')) {
65-
$node->setNode('domain', $this->domain);
80+
$node->setNode('domain', $this->scope->get('domain'));
6681
}
6782
}
6883

@@ -78,6 +93,10 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
7893
return false;
7994
}
8095

96+
if ($node instanceof \Twig_Node_Block) {
97+
$this->scope = $this->scope->leave();
98+
}
99+
81100
return $node;
82101
}
83102

0 commit comments

Comments
 (0)