diff options
Diffstat (limited to 'phpBB/phpbb/template')
| -rw-r--r-- | phpBB/phpbb/template/asset.php | 15 | ||||
| -rw-r--r-- | phpBB/phpbb/template/assets_bag.php | 95 | ||||
| -rw-r--r-- | phpBB/phpbb/template/exception/user_object_not_available.php | 22 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/environment.php | 104 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/extension.php | 15 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/extension/routing.php | 43 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/lexer.php | 11 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/loader.php | 22 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/node/event.php | 6 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/node/includeasset.php | 27 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/node/includecss.php | 16 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/node/includejs.php | 18 | ||||
| -rw-r--r-- | phpBB/phpbb/template/twig/twig.php | 63 | 
13 files changed, 346 insertions, 111 deletions
| diff --git a/phpBB/phpbb/template/asset.php b/phpBB/phpbb/template/asset.php index ff9366af4a..cb00f16549 100644 --- a/phpBB/phpbb/template/asset.php +++ b/phpBB/phpbb/template/asset.php @@ -20,15 +20,20 @@ class asset  	/** @var \phpbb\path_helper **/  	protected $path_helper; +	/** @var \phpbb\filesystem\filesystem */ +	protected $filesystem; +  	/**  	* Constructor  	*  	* @param string $url URL  	* @param \phpbb\path_helper $path_helper Path helper object +	* @param \phpbb\filesystem\filesystem $filesystem  	*/ -	public function __construct($url, \phpbb\path_helper $path_helper) +	public function __construct($url, \phpbb\path_helper $path_helper, \phpbb\filesystem\filesystem $filesystem)  	{  		$this->path_helper = $path_helper; +		$this->filesystem = $filesystem;  		$this->set_url($url);  	} @@ -152,18 +157,18 @@ class asset  	*/  	public function set_path($path, $urlencode = false)  	{ -		// Since 1.7.0 Twig returns the real path of the file. We need it to be relative to the working directory. -		$real_root_path = realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR; +		// Since 1.7.0 Twig returns the real path of the file. We need it to be relative. +		$real_root_path = $this->filesystem->realpath($this->path_helper->get_phpbb_root_path()) . DIRECTORY_SEPARATOR;  		// If the asset is under the phpBB root path we need to remove its path and then prepend $phpbb_root_path -		if (substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path) +		if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)  		{  			$path = $this->path_helper->get_phpbb_root_path() . str_replace('\\', '/', substr($path, strlen($real_root_path)));  		}  		else  		{  			// Else we make the path relative to the current working directory -			$real_root_path = realpath('.') . DIRECTORY_SEPARATOR; +			$real_root_path = $this->filesystem->realpath('.') . DIRECTORY_SEPARATOR;  			if ($real_root_path && substr($path . DIRECTORY_SEPARATOR, 0, strlen($real_root_path)) === $real_root_path)  			{  				$path = str_replace('\\', '/', substr($path, strlen($real_root_path))); diff --git a/phpBB/phpbb/template/assets_bag.php b/phpBB/phpbb/template/assets_bag.php new file mode 100644 index 0000000000..9013061b96 --- /dev/null +++ b/phpBB/phpbb/template/assets_bag.php @@ -0,0 +1,95 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\template; + +class assets_bag +{ +	/** @var asset[] */ +	protected $stylesheets = []; + +	/** @var asset[] */ +	protected $scripts = []; + +	/** +	 * Add a css asset to the bag +	 * +	 * @param asset $asset +	 */ +	public function add_stylesheet(asset $asset) +	{ +		$this->stylesheets[] = $asset; +	} + +	/** +	 * Add a js script asset to the bag +	 * +	 * @param asset $asset +	 */ +	public function add_script(asset $asset) +	{ +		$this->scripts[] = $asset; +	} + +	/** +	 * Returns all css assets +	 * +	 * @return asset[] +	 */ +	public function get_stylesheets() +	{ +		return $this->stylesheets; +	} + +	/** +	 * Returns all js assets +	 * +	 * @return asset[] +	 */ +	public function get_scripts() +	{ +		return $this->scripts; +	} + +	/** +	 * Returns the HTML code to includes all css assets +	 * +	 * @return string +	 */ +	public function get_stylesheets_content() +	{ +		$output = ''; +		foreach ($this->stylesheets as $stylesheet) +		{ +			$output .= "<link href=\"{$stylesheet->get_url()}\" rel=\"stylesheet\" type=\"text/css\" media=\"screen\" />\n"; +		} + +		return $output; +	} + +	/** +	 * Returns the HTML code to includes all js assets +	 * +	 * @return string +	 */ +	public function get_scripts_content() +	{ +		$output = ''; +		foreach ($this->scripts as $script) +		{ +			$output .= "<script type=\"text/javascript\" src=\"{$script->get_url()}\"></script>\n"; +		} + +		return $output; +	} +} diff --git a/phpBB/phpbb/template/exception/user_object_not_available.php b/phpBB/phpbb/template/exception/user_object_not_available.php new file mode 100644 index 0000000000..62fd2743c1 --- /dev/null +++ b/phpBB/phpbb/template/exception/user_object_not_available.php @@ -0,0 +1,22 @@ +<?php +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\exception; + +/** + * This exception is thrown when the user object was not set but it is required by the called method + */ +class user_object_not_available extends \phpbb\exception\runtime_exception +{ + +} diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index 476ffd935e..179412a2e3 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -13,14 +13,22 @@  namespace phpbb\template\twig; +use phpbb\template\assets_bag; +  class environment extends \Twig_Environment  {  	/** @var \phpbb\config\config */  	protected $phpbb_config; +	/** @var \phpbb\filesystem\filesystem */ +	protected $filesystem; +  	/** @var \phpbb\path_helper */  	protected $phpbb_path_helper; +	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */ +	protected $container; +  	/** @var \phpbb\extension\manager */  	protected $extension_manager; @@ -33,26 +41,41 @@ class environment extends \Twig_Environment  	/** @var array **/  	protected $namespace_look_up_order = array('__main__'); +	/** @var assets_bag */ +	protected $assets_bag; +  	/**  	* Constructor  	*  	* @param \phpbb\config\config $phpbb_config The phpBB configuration +	* @param \phpbb\filesystem\filesystem $filesystem  	* @param \phpbb\path_helper $path_helper phpBB path helper +	* @param string $cache_path The path to the cache directory  	* @param \phpbb\extension\manager $extension_manager phpBB extension manager  	* @param \Twig_LoaderInterface $loader Twig loader interface  	* @param array $options Array of options to pass to Twig  	*/ -	public function __construct($phpbb_config, \phpbb\path_helper $path_helper, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array()) +	public function __construct(\phpbb\config\config $phpbb_config, \phpbb\filesystem\filesystem $filesystem, \phpbb\path_helper $path_helper, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array())  	{  		$this->phpbb_config = $phpbb_config; +		$this->filesystem = $filesystem;  		$this->phpbb_path_helper = $path_helper;  		$this->extension_manager = $extension_manager;  		$this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path();  		$this->web_root_path = $this->phpbb_path_helper->get_web_root_path(); -		return parent::__construct($loader, $options); +		$this->assets_bag = new assets_bag(); + +		$options = array_merge(array( +			'cache'			=> (defined('IN_INSTALL')) ? false : $cache_path, +			'debug'			=> false, +			'auto_reload'	=> (bool) $this->phpbb_config['load_tplcompile'], +			'autoescape'	=> false, +		), $options); + +		parent::__construct($loader, $options);  	}  	/** @@ -78,16 +101,26 @@ class environment extends \Twig_Environment  	}  	/** -	* Get the phpBB root path -	* -	* @return string -	*/ +	 * Get the phpBB root path +	 * +	 * @return string +	 */  	public function get_phpbb_root_path()  	{  		return $this->phpbb_root_path;  	}  	/** +	* Get the filesystem object +	* +	* @return \phpbb\filesystem\filesystem +	*/ +	public function get_filesystem() +	{ +		return $this->filesystem; +	} + +	/**  	* Get the web root path  	*  	* @return string @@ -108,6 +141,16 @@ class environment extends \Twig_Environment  	}  	/** +	 * Gets the assets bag +	 * +	 * @return assets_bag +	 */ +	public function get_assets_bag() +	{ +		return $this->assets_bag; +	} + +	/**  	* Get the namespace look up order  	*  	* @return array @@ -131,6 +174,55 @@ class environment extends \Twig_Environment  	}  	/** +	 * {@inheritdoc} +	 */ +	public function render($name, array $context = []) +	{ +		return $this->display_with_assets($name, $context); +	} + +	/** +	 * {@inheritdoc} +	 */ +	public function display($name, array $context = []) +	{ +		echo $this->display_with_assets($name, $context); +	} + +	/** +	 * {@inheritdoc} +	 */ +	private function display_with_assets($name, array $context = []) +	{ +		$placeholder_salt = unique_id(); + +		if (array_key_exists('definition', $context)) +		{ +			$context['definition']->set('SCRIPTS', '__SCRIPTS_' . $placeholder_salt . '__'); +			$context['definition']->set('STYLESHEETS', '__STYLESHEETS_' . $placeholder_salt . '__'); +		} + +		$output = parent::render($name, $context); + +		return $this->inject_assets($output, $placeholder_salt); +	} + +	/** +	 * Injects the assets (from INCLUDECSS/JS) in the output. +	 * +	 * @param string $output +	 * +	 * @return string +	 */ +	private function inject_assets($output, $placeholder_salt) +	{ +		$output = str_replace('__STYLESHEETS_' . $placeholder_salt . '__', $this->assets_bag->get_stylesheets_content(), $output); +		$output = str_replace('__SCRIPTS_' . $placeholder_salt . '__', $this->assets_bag->get_scripts_content(), $output); + +		return $output; +	} + +	/**  	* Loads a template by name.  	*  	* @param string  $name  The template name diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index 3a983491b9..92f87a0331 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -18,20 +18,20 @@ class extension extends \Twig_Extension  	/** @var \phpbb\template\context */  	protected $context; -	/** @var \phpbb\user */ -	protected $user; +	/** @var \phpbb\language\language */ +	protected $language;  	/**  	* Constructor  	*  	* @param \phpbb\template\context $context -	* @param \phpbb\user $user +	* @param \phpbb\language\language $language  	* @return \phpbb\template\twig\extension  	*/ -	public function __construct(\phpbb\template\context $context, $user) +	public function __construct(\phpbb\template\context $context, $language)  	{  		$this->context = $context; -		$this->user = $user; +		$this->language = $language;  	}  	/** @@ -71,6 +71,7 @@ class extension extends \Twig_Extension  	{  		return array(  			new \Twig_SimpleFilter('subset', array($this, 'loop_subset'), array('needs_environment' => true)), +			// @deprecated 3.2.0 Uses twig's JS escape method instead of addslashes  			new \Twig_SimpleFilter('addslashes', 'addslashes'),  		);  	} @@ -177,9 +178,9 @@ class extension extends \Twig_Extension  			return $context_vars['L_' . $key];  		} -		// LA_ is transformed into lang(\'$1\')|addslashes, so we should not +		// LA_ is transformed into lang(\'$1\')|escape('js'), so we should not  		// need to check for it -		return call_user_func_array(array($this->user, 'lang'), $args); +		return call_user_func_array(array($this->language, 'lang'), $args);  	}  } diff --git a/phpBB/phpbb/template/twig/extension/routing.php b/phpBB/phpbb/template/twig/extension/routing.php new file mode 100644 index 0000000000..829ce738eb --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/routing.php @@ -0,0 +1,43 @@ +<?php +/** +* +* This file is part of the phpBB Forum Software package. +* +* @copyright (c) phpBB Limited <https://www.phpbb.com> +* @license GNU General Public License, version 2 (GPL-2.0) +* +* For full copyright and license information, please see +* the docs/CREDITS.txt file. +* +*/ + +namespace phpbb\template\twig\extension; + +use Symfony\Bridge\Twig\Extension\RoutingExtension; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; + +class routing extends RoutingExtension +{ +	/** @var \phpbb\controller\helper */ +	protected $helper; + +	/** +	* Constructor +	* +	* @param \phpbb\routing\helper $helper +	*/ +	public function __construct(\phpbb\routing\helper $helper) +	{ +		$this->helper = $helper; +	} + +	public function getPath($name, $parameters = array(), $relative = false) +	{ +		return $this->helper->route($name, $parameters, true, false, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH); +	} + +	public function getUrl($name, $parameters = array(), $schemeRelative = false) +	{ +		return $this->helper->route($name, $parameters, true, false, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL); +	} +} diff --git a/phpBB/phpbb/template/twig/lexer.php b/phpBB/phpbb/template/twig/lexer.php index c5dc7273ba..f1542109a4 100644 --- a/phpBB/phpbb/template/twig/lexer.php +++ b/phpBB/phpbb/template/twig/lexer.php @@ -15,6 +15,11 @@ namespace phpbb\template\twig;  class lexer extends \Twig_Lexer  { +	public function set_environment(\Twig_Environment $env) +	{ +		$this->env = $env; +	} +  	public function tokenize($code, $filename = null)  	{  		// Our phpBB tags @@ -112,9 +117,9 @@ class lexer extends \Twig_Lexer  		// Appends any filters after lang()  		$code = preg_replace('#{L_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2 }}', $code); -		// Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|addslashes }} -		// Appends any filters after lang(), but before addslashes -		$code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2|addslashes }}', $code); +		// Replace all of our escaped language variables, {LA_VARNAME}, with Twig style, {{ lang('NAME')|escape('js') }} +		// Appends any filters after lang(), but before escape('js') +		$code = preg_replace('#{LA_([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ lang(\'$1\')$2|escape(\'js\') }}', $code);  		// Replace all of our variables, {VARNAME}, with Twig style, {{ VARNAME }}  		// Appends any filters diff --git a/phpBB/phpbb/template/twig/loader.php b/phpBB/phpbb/template/twig/loader.php index 139a413b70..8b12188a77 100644 --- a/phpBB/phpbb/template/twig/loader.php +++ b/phpBB/phpbb/template/twig/loader.php @@ -21,6 +21,24 @@ class loader extends \Twig_Loader_Filesystem  	protected $safe_directories = array();  	/** +	 * @var \phpbb\filesystem\filesystem_interface +	 */ +	protected $filesystem; + +	/** +	 * Constructor +	 * +	 * @param \phpbb\filesystem\filesystem_interface $filesystem +	 * @param string|array	$paths +	 */ +	public function __construct(\phpbb\filesystem\filesystem_interface $filesystem, $paths = array()) +	{ +		$this->filesystem = $filesystem; + +		parent::__construct($paths); +	} + +	/**  	* Set safe directories  	*  	* @param array $directories Array of directories that are safe (empty to clear) @@ -49,7 +67,7 @@ class loader extends \Twig_Loader_Filesystem  	*/  	public function addSafeDirectory($directory)  	{ -		$directory = phpbb_realpath($directory); +		$directory = $this->filesystem->realpath($directory);  		if ($directory !== false)  		{ @@ -119,7 +137,7 @@ class loader extends \Twig_Loader_Filesystem  				//	can now check if we're within a "safe" directory  				// Find the real path of the directory the file is in -				$directory = phpbb_realpath(dirname($file)); +				$directory = $this->filesystem->realpath(dirname($file));  				if ($directory === false)  				{ diff --git a/phpBB/phpbb/template/twig/node/event.php b/phpBB/phpbb/template/twig/node/event.php index b765bde98d..11fdb75247 100644 --- a/phpBB/phpbb/template/twig/node/event.php +++ b/phpBB/phpbb/template/twig/node/event.php @@ -46,7 +46,7 @@ class event extends \Twig_Node  		{  			$ext_namespace = str_replace('/', '_', $ext_namespace); -			if (defined('DEBUG')) +			if ($this->environment->isDebug())  			{  				// If debug mode is enabled, lets check for new/removed EVENT  				//  templates on page load rather than at compile. This is @@ -58,7 +58,7 @@ class event extends \Twig_Node  				;  			} -			if (defined('DEBUG') || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html')) +			if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html'))  			{  				$compiler  					->write("\$previous_look_up_order = \$this->env->getNamespaceLookUpOrder();\n") @@ -70,7 +70,7 @@ class event extends \Twig_Node  				;  			} -			if (defined('DEBUG')) +			if ($this->environment->isDebug())  			{  				$compiler  					->outdent() diff --git a/phpBB/phpbb/template/twig/node/includeasset.php b/phpBB/phpbb/template/twig/node/includeasset.php index 15195a226b..6d50eafc9d 100644 --- a/phpBB/phpbb/template/twig/node/includeasset.php +++ b/phpBB/phpbb/template/twig/node/includeasset.php @@ -39,7 +39,7 @@ abstract class includeasset extends \Twig_Node  			->write("\$asset_file = ")  			->subcompile($this->getNode('expr'))  			->raw(";\n") -			->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper());\n") +			->write("\$asset = new \phpbb\\template\\asset(\$asset_file, \$this->getEnvironment()->get_path_helper(), \$this->getEnvironment()->get_filesystem());\n")  			->write("if (substr(\$asset_file, 0, 2) !== './' && \$asset->is_relative()) {\n")  			->indent()  				->write("\$asset_path = \$asset->get_path();") @@ -49,33 +49,18 @@ abstract class includeasset extends \Twig_Node  					->write("\$local_file = \$this->getEnvironment()->findTemplate(\$asset_path);\n")  					->write("\$asset->set_path(\$local_file, true);\n")  				->outdent() -				->write("\$asset->add_assets_version('{$config['assets_version']}');\n") -				->write("\$asset_file = \$asset->get_url();\n")  				->write("}\n") +				->write("\$asset->add_assets_version('{$config['assets_version']}');\n")  			->outdent()  			->write("}\n") -			->write("\$context['definition']->append('{$this->get_definition_name()}', '") -		; - -		$this->append_asset($compiler); - -		$compiler -			->raw("\n');\n") +			->write("\$this->getEnvironment()->get_assets_bag()->add_{$this->get_setters_name()}(\$asset);")  		;  	}  	/** -	* Get the definition name +	* Get the name of the assets bag setter  	* -	* @return string (e.g. 'SCRIPTS') -	*/ -	abstract public function get_definition_name(); - -	/** -	* Append the output code for the asset -	* -	* @param \Twig_Compiler A Twig_Compiler instance -	* @return null +	* @return string (e.g. 'script')  	*/ -	abstract protected function append_asset(\Twig_Compiler $compiler); +	abstract public function get_setters_name();  } diff --git a/phpBB/phpbb/template/twig/node/includecss.php b/phpBB/phpbb/template/twig/node/includecss.php index 2dac154036..2e97d4972d 100644 --- a/phpBB/phpbb/template/twig/node/includecss.php +++ b/phpBB/phpbb/template/twig/node/includecss.php @@ -18,20 +18,8 @@ class includecss extends \phpbb\template\twig\node\includeasset  	/**  	* {@inheritdoc}  	*/ -	public function get_definition_name() +	public function get_setters_name()  	{ -		return 'STYLESHEETS'; -	} - -	/** -	* {@inheritdoc} -	*/ -	public function append_asset(\Twig_Compiler $compiler) -	{ -		$compiler -			->raw("<link href=\"' . ") -			->raw("\$asset_file . '\"") -			->raw(' rel="stylesheet" type="text/css" media="screen" />') -		; +		return 'stylesheet';  	}  } diff --git a/phpBB/phpbb/template/twig/node/includejs.php b/phpBB/phpbb/template/twig/node/includejs.php index 0f67f9ff60..505b49757b 100644 --- a/phpBB/phpbb/template/twig/node/includejs.php +++ b/phpBB/phpbb/template/twig/node/includejs.php @@ -18,22 +18,8 @@ class includejs extends \phpbb\template\twig\node\includeasset  	/**  	* {@inheritdoc}  	*/ -	public function get_definition_name() +	public function get_setters_name()  	{ -		return 'SCRIPTS'; -	} - -	/** -	* {@inheritdoc} -	*/ -	protected function append_asset(\Twig_Compiler $compiler) -	{ -		$config = $this->environment->get_phpbb_config(); - -		$compiler -			->raw("<script type=\"text/javascript\" src=\"' . ") -			->raw("\$asset_file") -			->raw(". '\"></script>\n") -		; +		return 'script';  	}  } diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index d1bbb2b55a..f322778eda 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -13,6 +13,8 @@  namespace phpbb\template\twig; +use phpbb\template\exception\user_object_not_available; +  /**  * Twig Template class.  */ @@ -76,11 +78,14 @@ class twig extends \phpbb\template\base  	*  	* @param \phpbb\path_helper $path_helper  	* @param \phpbb\config\config $config -	* @param \phpbb\user $user  	* @param \phpbb\template\context $context template context +	* @param \phpbb\template\twig\environment $twig_environment +	* @param string $cache_path +	* @param \phpbb\user|null $user +	* @param array|\ArrayAccess $extensions  	* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked  	*/ -	public function __construct(\phpbb\path_helper $path_helper, $config, $user, \phpbb\template\context $context, \phpbb\extension\manager $extension_manager = null) +	public function __construct(\phpbb\path_helper $path_helper, $config, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, \phpbb\user $user = null, $extensions = array(), \phpbb\extension\manager $extension_manager = null)  	{  		$this->path_helper = $path_helper;  		$this->phpbb_root_path = $path_helper->get_phpbb_root_path(); @@ -89,41 +94,14 @@ class twig extends \phpbb\template\base  		$this->user = $user;  		$this->context = $context;  		$this->extension_manager = $extension_manager; +		$this->cachepath = $cache_path; +		$this->twig = $twig_environment; -		$this->cachepath = $this->phpbb_root_path . 'cache/twig/'; - -		// Initiate the loader, __main__ namespace paths will be setup later in set_style_names() -		$loader = new \phpbb\template\twig\loader(''); - -		$this->twig = new \phpbb\template\twig\environment( -			$this->config, -			$this->path_helper, -			$this->extension_manager, -			$loader, -			array( -				'cache'			=> (defined('IN_INSTALL')) ? false : $this->cachepath, -				'debug'			=> defined('DEBUG'), -				'auto_reload'	=> (bool) $this->config['load_tplcompile'], -				'autoescape'	=> false, -			) -		); - -		$this->twig->addExtension( -			new \phpbb\template\twig\extension( -				$this->context, -				$this->user -			) -		); - -		if (defined('DEBUG')) +		foreach ($extensions as $extension)  		{ -			$this->twig->addExtension(new \Twig_Extension_Debug()); +			$this->twig->addExtension($extension);  		} -		$lexer = new \phpbb\template\twig\lexer($this->twig); - -		$this->twig->setLexer($lexer); -  		// Add admin namespace  		if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))  		{ @@ -150,9 +128,16 @@ class twig extends \phpbb\template\base  	* Get the style tree of the style preferred by the current user  	*  	* @return array Style tree, most specific first +	* +	* @throws \phpbb\template\exception\user_object_not_available	When user service was not set  	*/  	public function get_user_style()  	{ +		if ($this->user === null) +		{ +			throw new user_object_not_available(); +		} +  		$style_list = array(  			$this->user->style['style_path'],  		); @@ -368,14 +353,24 @@ class twig extends \phpbb\template\base  			$context_vars['.'][0], // To get normal vars  			array(  				'definition'	=> new \phpbb\template\twig\definition(), -				'user'			=> $this->user,  				'loops'			=> $context_vars, // To get loops  			)  		); +		if ($this->user instanceof \phpbb\user) +		{ +			$vars['user'] = $this->user; +		} +  		// cleanup  		unset($vars['loops']['.']); +		// Inject in the main context the value added by assign_block_vars() to be able to use directly the Twig loops. +		foreach ($vars['loops'] as $key => &$value) +		{ +			$vars[$key] = $value; +		} +  		return $vars;  	} | 
