Skip to content

Commit 40e61e4

Browse files
François-Xavier de Guillebonmarc1706
authored andcommitted
[ticket/15508] Remove call to getEnvironment() from parser
PHPBB3-15508
1 parent ff35f65 commit 40e61e4

17 files changed

Lines changed: 78 additions & 39 deletions

phpBB/config/default/container/services_twig.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ services:
3838
class: phpbb\template\twig\extension
3939
arguments:
4040
- '@template_context'
41+
- '@template.twig.environment'
4142
- '@language'
4243
tags:
4344
- { name: twig.extension }

phpBB/phpbb/template/twig/extension.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ class extension extends \Twig_Extension
1818
/** @var \phpbb\template\context */
1919
protected $context;
2020

21+
/** @var \phpbb\template\twig\environment */
22+
protected $environment;
23+
2124
/** @var \phpbb\language\language */
2225
protected $language;
2326

2427
/**
2528
* Constructor
2629
*
2730
* @param \phpbb\template\context $context
31+
* @param \phpbb\template\twig\environment $environment
2832
* @param \phpbb\language\language $language
2933
* @return \phpbb\template\twig\extension
3034
*/
31-
public function __construct(\phpbb\template\context $context, $language)
35+
public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language)
3236
{
3337
$this->context = $context;
38+
$this->environment = $environment;
3439
$this->language = $language;
3540
}
3641

@@ -56,9 +61,9 @@ public function getTokenParsers()
5661
new \phpbb\template\twig\tokenparser\includeparser,
5762
new \phpbb\template\twig\tokenparser\includejs,
5863
new \phpbb\template\twig\tokenparser\includecss,
59-
new \phpbb\template\twig\tokenparser\event,
60-
new \phpbb\template\twig\tokenparser\includephp,
61-
new \phpbb\template\twig\tokenparser\php,
64+
new \phpbb\template\twig\tokenparser\event($this->environment),
65+
new \phpbb\template\twig\tokenparser\includephp($this->environment),
66+
new \phpbb\template\twig\tokenparser\php($this->environment),
6267
);
6368
}
6469

phpBB/phpbb/template/twig/node/includeasset.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@
1515

1616
abstract class includeasset extends \Twig_Node
1717
{
18-
/** @var \Twig_Environment */
19-
protected $environment;
20-
21-
public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null)
18+
public function __construct(\Twig_Node_Expression $expr, $lineno, $tag = null)
2219
{
23-
$this->environment = $environment;
24-
2520
parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
2621
}
22+
2723
/**
2824
* Compiles the node to PHP.
2925
*
@@ -33,20 +29,18 @@ public function compile(\Twig_Compiler $compiler)
3329
{
3430
$compiler->addDebugInfo($this);
3531

36-
$config = $this->environment->get_phpbb_config();
37-
3832
$compiler
3933
->write("\$asset_file = ")
4034
->subcompile($this->getNode('expr'))
4135
->raw(";\n")
42-
->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper(), \$this->getEnvironment()->get_filesystem());\n")
36+
->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->env->get_path_helper(), \$this->env->get_filesystem());\n")
4337
->write("if (substr(\$asset_file, 0, 2) !== './' && \$asset->is_relative()) {\n")
4438
->indent()
4539
->write("\$asset_path = \$asset->get_path();")
46-
->write("\$local_file = \$this->getEnvironment()->get_phpbb_root_path() . \$asset_path;\n")
40+
->write("\$local_file = \$this->env->get_phpbb_root_path() . \$asset_path;\n")
4741
->write("if (!file_exists(\$local_file)) {\n")
4842
->indent()
49-
->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")
43+
->write("\$local_file = \$this->env->findTemplate(\$asset_path);\n")
5044
->write("\$asset->set_path(\$local_file, true);\n")
5145
->outdent()
5246
->write("}\n")
@@ -55,10 +49,10 @@ public function compile(\Twig_Compiler $compiler)
5549
->write("\n")
5650
->write("if (\$asset->is_relative()) {\n")
5751
->indent()
58-
->write("\$asset->add_assets_version('{$config['assets_version']}');\n")
52+
->write("\$asset->add_assets_version(\$this->env->get_phpbb_config()['assets_version']);\n")
5953
->outdent()
6054
->write("}\n")
61-
->write("\$this->getEnvironment()->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")
55+
->write("\$this->env->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")
6256
;
6357
}
6458

phpBB/phpbb/template/twig/node/includephp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ public function compile(\Twig_Compiler $compiler)
6363
// Absolute path specified
6464
->write("require(\$location);\n")
6565
->outdent()
66-
->write("} else if (file_exists(\$this->getEnvironment()->get_phpbb_root_path() . \$location)) {\n")
66+
->write("} else if (file_exists(\$this->env->get_phpbb_root_path() . \$location)) {\n")
6767
->indent()
6868
// PHP file relative to phpbb_root_path
69-
->write("require(\$this->getEnvironment()->get_phpbb_root_path() . \$location);\n")
69+
->write("require(\$this->env->get_phpbb_root_path() . \$location);\n")
7070
->outdent()
7171
->write("} else {\n")
7272
->indent()
7373
// Local path (behaves like INCLUDE)
74-
->write("require(\$this->getEnvironment()->getLoader()->getCacheKey(\$location));\n")
74+
->write("require(\$this->env->getLoader()->getCacheKey(\$location));\n")
7575
->outdent()
7676
->write("}\n")
7777
;

phpBB/phpbb/template/twig/tokenparser/event.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515

1616
class event extends \Twig_TokenParser
1717
{
18+
/** @var \phpbb\template\twig\environment */
19+
protected $environment;
20+
21+
/**
22+
* Constructor
23+
*
24+
* @param \phpbb\template\twig\environment $environment
25+
*/
26+
public function __construct(\phpbb\template\twig\environment $environment)
27+
{
28+
$this->environment = $environment;
29+
}
30+
1831
/**
1932
* Parses a token and returns a node.
2033
*
@@ -29,7 +42,7 @@ public function parse(\Twig_Token $token)
2942
$stream = $this->parser->getStream();
3043
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
3144

32-
return new \phpbb\template\twig\node\event($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
45+
return new \phpbb\template\twig\node\event($expr, $this->environment, $token->getLine(), $this->getTag());
3346
}
3447

3548
/**

phpBB/phpbb/template/twig/tokenparser/includecss.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function parse(\Twig_Token $token)
2929
$stream = $this->parser->getStream();
3030
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
3131

32-
return new \phpbb\template\twig\node\includecss($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
32+
return new \phpbb\template\twig\node\includecss($expr, $token->getLine(), $this->getTag());
3333
}
3434

3535
/**

phpBB/phpbb/template/twig/tokenparser/includejs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function parse(\Twig_Token $token)
2929
$stream = $this->parser->getStream();
3030
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
3131

32-
return new \phpbb\template\twig\node\includejs($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
32+
return new \phpbb\template\twig\node\includejs($expr, $token->getLine(), $this->getTag());
3333
}
3434

3535
/**

phpBB/phpbb/template/twig/tokenparser/includephp.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616

1717
class includephp extends \Twig_TokenParser
1818
{
19+
/** @var \phpbb\template\twig\environment */
20+
protected $environment;
21+
22+
/**
23+
* Constructor
24+
*
25+
* @param \phpbb\template\twig\environment $environment
26+
*/
27+
public function __construct(\phpbb\template\twig\environment $environment)
28+
{
29+
$this->environment = $environment;
30+
}
31+
1932
/**
2033
* Parses a token and returns a node.
2134
*
@@ -40,7 +53,7 @@ public function parse(\Twig_Token $token)
4053

4154
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
4255

43-
return new \phpbb\template\twig\node\includephp($expr, $this->parser->getEnvironment(), $token->getLine(), $ignoreMissing, $this->getTag());
56+
return new \phpbb\template\twig\node\includephp($expr, $this->environment, $token->getLine(), $ignoreMissing, $this->getTag());
4457
}
4558

4659
/**

phpBB/phpbb/template/twig/tokenparser/php.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515

1616
class php extends \Twig_TokenParser
1717
{
18+
/** @var \phpbb\template\twig\environment */
19+
protected $environment;
20+
21+
/**
22+
* Constructor
23+
*
24+
* @param \phpbb\template\twig\environment $environment
25+
*/
26+
public function __construct(\phpbb\template\twig\environment $environment)
27+
{
28+
$this->environment = $environment;
29+
}
30+
1831
/**
1932
* Parses a token and returns a node.
2033
*
@@ -32,7 +45,7 @@ public function parse(\Twig_Token $token)
3245

3346
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
3447

35-
return new \phpbb\template\twig\node\php($body, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
48+
return new \phpbb\template\twig\node\php($body, $this->environment, $token->getLine(), $this->getTag());
3649
}
3750

3851
public function decideEnd(\Twig_Token $token)

tests/controller/common_helper_route.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function generate_route_objects()
122122
'autoescape' => false,
123123
)
124124
);
125-
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
125+
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)));
126126
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
127127

128128
$this->extension_manager = new phpbb_mock_extension_manager(

0 commit comments

Comments
 (0)