1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-03-14 12:40:13 +01:00

[ticket/15508] Remove call to getEnvironment() from parser

PHPBB3-15508
This commit is contained in:
François-Xavier de Guillebon 2018-01-16 12:46:48 +01:00 committed by Marc Alexander
parent ff35f65064
commit 40e61e4d1e
No known key found for this signature in database
GPG Key ID: 50E0D2423696F995
17 changed files with 78 additions and 39 deletions

View File

@ -38,6 +38,7 @@ services:
class: phpbb\template\twig\extension
arguments:
- '@template_context'
- '@template.twig.environment'
- '@language'
tags:
- { name: twig.extension }

View File

@ -18,6 +18,9 @@ class extension extends \Twig_Extension
/** @var \phpbb\template\context */
protected $context;
/** @var \phpbb\template\twig\environment */
protected $environment;
/** @var \phpbb\language\language */
protected $language;
@ -25,12 +28,14 @@ class extension extends \Twig_Extension
* Constructor
*
* @param \phpbb\template\context $context
* @param \phpbb\template\twig\environment $environment
* @param \phpbb\language\language $language
* @return \phpbb\template\twig\extension
*/
public function __construct(\phpbb\template\context $context, $language)
public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language)
{
$this->context = $context;
$this->environment = $environment;
$this->language = $language;
}
@ -56,9 +61,9 @@ class extension extends \Twig_Extension
new \phpbb\template\twig\tokenparser\includeparser,
new \phpbb\template\twig\tokenparser\includejs,
new \phpbb\template\twig\tokenparser\includecss,
new \phpbb\template\twig\tokenparser\event,
new \phpbb\template\twig\tokenparser\includephp,
new \phpbb\template\twig\tokenparser\php,
new \phpbb\template\twig\tokenparser\event($this->environment),
new \phpbb\template\twig\tokenparser\includephp($this->environment),
new \phpbb\template\twig\tokenparser\php($this->environment),
);
}

View File

@ -15,15 +15,11 @@ namespace phpbb\template\twig\node;
abstract class includeasset extends \Twig_Node
{
/** @var \Twig_Environment */
protected $environment;
public function __construct(\Twig_Node_Expression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null)
public function __construct(\Twig_Node_Expression $expr, $lineno, $tag = null)
{
$this->environment = $environment;
parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
}
/**
* Compiles the node to PHP.
*
@ -33,20 +29,18 @@ abstract class includeasset extends \Twig_Node
{
$compiler->addDebugInfo($this);
$config = $this->environment->get_phpbb_config();
$compiler
->write("\$asset_file = ")
->subcompile($this->getNode('expr'))
->raw(";\n")
->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper(), \$this->getEnvironment()->get_filesystem());\n")
->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->env->get_path_helper(), \$this->env->get_filesystem());\n")
->write("if (substr(\$asset_file, 0, 2) !== './' && \$asset->is_relative()) {\n")
->indent()
->write("\$asset_path = \$asset->get_path();")
->write("\$local_file = \$this->getEnvironment()->get_phpbb_root_path() . \$asset_path;\n")
->write("\$local_file = \$this->env->get_phpbb_root_path() . \$asset_path;\n")
->write("if (!file_exists(\$local_file)) {\n")
->indent()
->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")
->write("\$local_file = \$this->env->findTemplate(\$asset_path);\n")
->write("\$asset->set_path(\$local_file, true);\n")
->outdent()
->write("}\n")
@ -55,10 +49,10 @@ abstract class includeasset extends \Twig_Node
->write("\n")
->write("if (\$asset->is_relative()) {\n")
->indent()
->write("\$asset->add_assets_version('{$config['assets_version']}');\n")
->write("\$asset->add_assets_version(\$this->env->get_phpbb_config()['assets_version']);\n")
->outdent()
->write("}\n")
->write("\$this->getEnvironment()->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")
->write("\$this->env->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")
;
}

View File

@ -63,15 +63,15 @@ class includephp extends \Twig_Node
// Absolute path specified
->write("require(\$location);\n")
->outdent()
->write("} else if (file_exists(\$this->getEnvironment()->get_phpbb_root_path() . \$location)) {\n")
->write("} else if (file_exists(\$this->env->get_phpbb_root_path() . \$location)) {\n")
->indent()
// PHP file relative to phpbb_root_path
->write("require(\$this->getEnvironment()->get_phpbb_root_path() . \$location);\n")
->write("require(\$this->env->get_phpbb_root_path() . \$location);\n")
->outdent()
->write("} else {\n")
->indent()
// Local path (behaves like INCLUDE)
->write("require(\$this->getEnvironment()->getLoader()->getCacheKey(\$location));\n")
->write("require(\$this->env->getLoader()->getCacheKey(\$location));\n")
->outdent()
->write("}\n")
;

View File

@ -15,6 +15,19 @@ namespace phpbb\template\twig\tokenparser;
class event extends \Twig_TokenParser
{
/** @var \phpbb\template\twig\environment */
protected $environment;
/**
* Constructor
*
* @param \phpbb\template\twig\environment $environment
*/
public function __construct(\phpbb\template\twig\environment $environment)
{
$this->environment = $environment;
}
/**
* Parses a token and returns a node.
*
@ -29,7 +42,7 @@ class event extends \Twig_TokenParser
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\event($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
return new \phpbb\template\twig\node\event($expr, $this->environment, $token->getLine(), $this->getTag());
}
/**

View File

@ -29,7 +29,7 @@ class includecss extends \Twig_TokenParser
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\includecss($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
return new \phpbb\template\twig\node\includecss($expr, $token->getLine(), $this->getTag());
}
/**

View File

@ -29,7 +29,7 @@ class includejs extends \Twig_TokenParser
$stream = $this->parser->getStream();
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\includejs($expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
return new \phpbb\template\twig\node\includejs($expr, $token->getLine(), $this->getTag());
}
/**

View File

@ -16,6 +16,19 @@ namespace phpbb\template\twig\tokenparser;
class includephp extends \Twig_TokenParser
{
/** @var \phpbb\template\twig\environment */
protected $environment;
/**
* Constructor
*
* @param \phpbb\template\twig\environment $environment
*/
public function __construct(\phpbb\template\twig\environment $environment)
{
$this->environment = $environment;
}
/**
* Parses a token and returns a node.
*
@ -40,7 +53,7 @@ class includephp extends \Twig_TokenParser
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\includephp($expr, $this->parser->getEnvironment(), $token->getLine(), $ignoreMissing, $this->getTag());
return new \phpbb\template\twig\node\includephp($expr, $this->environment, $token->getLine(), $ignoreMissing, $this->getTag());
}
/**

View File

@ -15,6 +15,19 @@ namespace phpbb\template\twig\tokenparser;
class php extends \Twig_TokenParser
{
/** @var \phpbb\template\twig\environment */
protected $environment;
/**
* Constructor
*
* @param \phpbb\template\twig\environment $environment
*/
public function __construct(\phpbb\template\twig\environment $environment)
{
$this->environment = $environment;
}
/**
* Parses a token and returns a node.
*
@ -32,7 +45,7 @@ class php extends \Twig_TokenParser
$stream->expect(\Twig_Token::BLOCK_END_TYPE);
return new \phpbb\template\twig\node\php($body, $this->parser->getEnvironment(), $token->getLine(), $this->getTag());
return new \phpbb\template\twig\node\php($body, $this->environment, $token->getLine(), $this->getTag());
}
public function decideEnd(\Twig_Token $token)

View File

@ -122,7 +122,7 @@ abstract class phpbb_controller_common_helper_route extends phpbb_test_case
'autoescape' => false,
)
);
$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)));
$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)));
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
$this->extension_manager = new phpbb_mock_extension_manager(

View File

@ -67,13 +67,6 @@ class phpbb_email_parsing_test extends phpbb_test_case
$phpbb_container->set('ext.manager', $extension_manager);
$context = new \phpbb\template\context();
$twig_extension = new \phpbb\template\twig\extension($context, $lang);
$phpbb_container->set('template.twig.extensions.phpbb', $twig_extension);
$twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
$twig_extensions_collection->add('template.twig.extensions.phpbb');
$phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection);
$twig = new \phpbb\template\twig\environment(
$config,
$filesystem,
@ -89,6 +82,13 @@ class phpbb_email_parsing_test extends phpbb_test_case
'autoescape' => false,
)
);
$twig_extension = new \phpbb\template\twig\extension($context, $twig, $lang);
$phpbb_container->set('template.twig.extensions.phpbb', $twig_extension);
$twig_extensions_collection = new \phpbb\di\service_collection($phpbb_container);
$twig_extensions_collection->add('template.twig.extensions.phpbb');
$phpbb_container->set('template.twig.extensions.collection', $twig_extensions_collection);
$twig->addExtension($twig_extension);
$phpbb_container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));

View File

@ -111,7 +111,7 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
$lang = new \phpbb\language\language($lang_loader);
$this->user = new \phpbb\user($lang, '\phpbb\datetime');
$this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)));
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
}

View File

@ -75,7 +75,7 @@ class phpbb_template_allfolder_test extends phpbb_template_template_test_case
'autoescape' => false,
)
);
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager);
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)), $this->extension_manager);
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
$this->template_path = $this->test_path . '/templates';

View File

@ -169,7 +169,7 @@ Zeta test event in all',
'autoescape' => false,
)
);
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager);
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)), $this->extension_manager);
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path));

View File

@ -68,7 +68,7 @@ class phpbb_template_template_includecss_test extends phpbb_template_template_te
$twig,
$cache_path,
$this->user,
array(new \phpbb\template\twig\extension($context, $this->user)),
array(new \phpbb\template\twig\extension($context, $twig, $this->user)),
new phpbb_mock_extension_manager(
dirname(__FILE__) . '/',
array(

View File

@ -113,7 +113,7 @@ class phpbb_template_template_test_case extends phpbb_test_case
'autoescape' => false,
)
);
$this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$this->template = new phpbb\template\twig\twig($path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)));
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style('tests', $this->template_path);
}

View File

@ -56,7 +56,7 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat
'autoescape' => false,
)
);
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $this->user)));
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $context, $twig, $cache_path, $this->user, array(new \phpbb\template\twig\extension($context, $twig, $this->user)));
$twig->setLexer(new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path));
}