diff options
author | Marc Alexander <admin@m-a-styles.de> | 2019-09-28 12:57:06 +0200 |
---|---|---|
committer | Marc Alexander <admin@m-a-styles.de> | 2019-09-28 12:57:06 +0200 |
commit | a85bb1332d2d4660688fe51b9d4622c1eb2c9df5 (patch) | |
tree | 28133b882be947eadb3701a7cdae58cc3faedd1c | |
parent | 36614e6f81bf4022694e3886862d184ebf9dee28 (diff) | |
parent | f05022941de0c8ec647cc7bcf67be997d2370b77 (diff) | |
download | forums-a85bb1332d2d4660688fe51b9d4622c1eb2c9df5.tar forums-a85bb1332d2d4660688fe51b9d4622c1eb2c9df5.tar.gz forums-a85bb1332d2d4660688fe51b9d4622c1eb2c9df5.tar.bz2 forums-a85bb1332d2d4660688fe51b9d4622c1eb2c9df5.tar.xz forums-a85bb1332d2d4660688fe51b9d4622c1eb2c9df5.zip |
Merge pull request #5480 from mrgoldy/ticket/15905
[ticket/15905] Create multiple twig extensions
-rw-r--r-- | phpBB/config/default/container/services_twig.yml | 17 | ||||
-rw-r--r-- | phpBB/phpbb/template/twig/extension.php | 10 | ||||
-rw-r--r-- | phpBB/phpbb/template/twig/extension/avatar.php | 80 | ||||
-rw-r--r-- | phpBB/phpbb/template/twig/extension/config.php | 64 | ||||
-rw-r--r-- | phpBB/phpbb/template/twig/extension/username.php | 84 | ||||
-rw-r--r-- | tests/template/extension_test.php | 258 | ||||
-rw-r--r-- | tests/template/templates/avatar_user.html | 1 | ||||
-rw-r--r-- | tests/template/templates/extension_config_test.html | 1 | ||||
-rw-r--r-- | tests/template/templates/extension_username_test.html | 1 |
9 files changed, 511 insertions, 5 deletions
diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index ce76a6a5ea..6f70155295 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -43,6 +43,18 @@ services: tags: - { name: twig.extension } + template.twig.extensions.avatar: + class: phpbb\template\twig\extension\avatar + tags: + - { name: twig.extension } + + template.twig.extensions.config: + class: phpbb\template\twig\extension\config + arguments: + - '@config' + tags: + - { name: twig.extension } + template.twig.extensions.routing: class: phpbb\template\twig\extension\routing arguments: @@ -50,6 +62,11 @@ services: tags: - { name: twig.extension } + template.twig.extensions.username: + class: phpbb\template\twig\extension\username + tags: + - { name: twig.extension } + template.twig.extensions.debug: class: Twig_Extension_Debug diff --git a/phpBB/phpbb/template/twig/extension.php b/phpBB/phpbb/template/twig/extension.php index c5b3db1aaf..1131a7f3aa 100644 --- a/phpBB/phpbb/template/twig/extension.php +++ b/phpBB/phpbb/template/twig/extension.php @@ -30,7 +30,6 @@ class extension extends \Twig_Extension * @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, \phpbb\template\twig\environment $environment, $language) { @@ -91,6 +90,7 @@ class extension extends \Twig_Extension return array( new \Twig_SimpleFunction('lang', array($this, 'lang')), new \Twig_SimpleFunction('lang_defined', array($this, 'lang_defined')), + new \Twig_SimpleFunction('get_class', 'get_class'), ); } @@ -190,10 +190,10 @@ class extension extends \Twig_Extension } /** - * Check if a language variable exists - * - * @return bool - */ + * Check if a language variable exists + * + * @return bool + */ public function lang_defined($key) { return call_user_func_array([$this->language, 'is_set'], [$key]); diff --git a/phpBB/phpbb/template/twig/extension/avatar.php b/phpBB/phpbb/template/twig/extension/avatar.php new file mode 100644 index 0000000000..7a17fd4b42 --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/avatar.php @@ -0,0 +1,80 @@ +<?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; + +class avatar extends \Twig_Extension +{ + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'avatar'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('avatar', array($this, 'get_avatar')), + ); + } + + /** + * Get avatar for placing into templates. + * + * How to use in a template: + * - {{ avatar('mode', row, alt, ignore_config, lazy) }} + * + * The mode and row (group_row or user_row) are required. + * The other fields (alt|ignore_config|lazy) are optional. + * + * @uses \phpbb_get_group_avatar() + * @uses \phpbb_get_user_avatar() + * + * @return string The avatar HTML for the specified mode + */ + public function get_avatar() + { + $args = func_get_args(); + + $mode = (string) $args[0]; + $row = (array) $args[1]; + $alt = isset($args[2]) ? (string) $args[2] : false; + $ignore_config = isset($args[3]) ? (bool) $args[3] : false; + $lazy = isset($args[4]) ? (bool) $args[4] : false; + + // To prevent having to redefine alt attribute ('USER_AVATAR'|'GROUP_AVATAR'), we check if an alternative has been provided + switch ($mode) + { + case 'group': + return $alt ? phpbb_get_group_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_group_avatar($row); + break; + + case 'user': + return $alt ? phpbb_get_user_avatar($row, $alt, $ignore_config, $lazy) : phpbb_get_user_avatar($row); + break; + + default: + return ''; + break; + } + } +} diff --git a/phpBB/phpbb/template/twig/extension/config.php b/phpBB/phpbb/template/twig/extension/config.php new file mode 100644 index 0000000000..cbf6e505c5 --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/config.php @@ -0,0 +1,64 @@ +<?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; + +class config extends \Twig_Extension +{ + /** @var \phpbb\config\config */ + protected $config; + + /** + * Constructor. + * + * @param \phpbb\config\config $config Configuration object + */ + public function __construct(\phpbb\config\config $config) + { + $this->config = $config; + } + + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'config'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('config', array($this, 'get_config')), + ); + } + + /** + * Retrieves a configuration value for use in templates. + * + * @return string The configuration value + */ + public function get_config() + { + $args = func_get_args(); + + return $this->config->offsetGet($args[0]); + } +} diff --git a/phpBB/phpbb/template/twig/extension/username.php b/phpBB/phpbb/template/twig/extension/username.php new file mode 100644 index 0000000000..ef149693a0 --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/username.php @@ -0,0 +1,84 @@ +<?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; + +class username extends \Twig_Extension +{ + /** + * Get the name of this extension + * + * @return string + */ + public function getName() + { + return 'username'; + } + + /** + * Returns a list of global functions to add to the existing list. + * + * @return array An array of global functions + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('username', array($this, 'get_username')), + ); + } + + /** + * Get username details for placing into templates. + * + * How to use in a template: + * - {{ username('mode', user_id, username, user_colour, guest_username, custom_profile_url) }} + * - {{ username('mode', user_row, guest_username, custom_profile_url) }} + * It's possible to provide the user identifier, name and colour separately, + * or provide the entire user row at once as an array. + * + * The mode, user_id and username are required (separately or through a user row). + * The other fields (user_colour|guest_username|custom_profile_url) are optional. + * + * @uses \get_username_string() + * + * @return string A string based on what is wanted depending on $mode + */ + public function get_username() + { + $args = func_get_args(); + + $mode = $args[0]; + $user = $args[1]; + + // If the entire user row is provided + if (is_array($user)) + { + $user_id = isset($user['user_id']) ? $user['user_id'] : ''; + $username = isset($user['username']) ? $user['username'] : ''; + $user_colour = isset($user['user_colour']) ? $user['user_colour'] : ''; + $guest_username = isset($args[2]) ? $args[2] : false; + $custom_profile_url = isset($args[3]) ? $args[3] : false; + } + else + { + // Options are provided separately + $user_id = $user; + $username = $args[2]; + $user_colour = isset($args[3]) ? $args[3] : ''; + $guest_username = isset($args[4]) ? $args[4] : false; + $custom_profile_url = isset($args[5]) ? $args[5] : false; + } + + return get_username_string($mode, $user_id, $username, $user_colour, $guest_username, $custom_profile_url); + } +} diff --git a/tests/template/extension_test.php b/tests/template/extension_test.php new file mode 100644 index 0000000000..d633001060 --- /dev/null +++ b/tests/template/extension_test.php @@ -0,0 +1,258 @@ +<?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. + * + */ + +require_once dirname(__FILE__) . '/template_test_case.php'; + +class phpbb_template_extension_test extends phpbb_template_template_test_case +{ + protected function setup_engine(array $new_config = array()) + { + global $config, $phpbb_container, $phpbb_dispatcher, $phpbb_root_path, $phpEx; + + $defaults = $this->config_defaults(); + $defaults = array_merge($defaults, [ + 'allow_avatar' => true, + 'allow_avatar_upload' => true, + ]); + $config = new \phpbb\config\config(array_merge($defaults, $new_config)); + $lang_loader = new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx); + $this->lang = $lang = new \phpbb\language\language($lang_loader); + $this->user = new \phpbb\user($lang, '\phpbb\datetime'); + + global $auth, $request, $symfony_request, $user; + $user = new phpbb_mock_user(); + $user->optionset('user_id', 2); + $auth = $this->getMockBuilder('phpbb\auth\auth') + ->disableOriginalConstructor() + ->setMethods(['acl_get']) + ->getMock(); + $auth->method('acl_get') + ->willReturn(true); + + $filesystem = new \phpbb\filesystem\filesystem(); + $request = new phpbb_mock_request; + $symfony_request = new \phpbb\symfony_request( + $request + ); + $phpbb_path_helper = new \phpbb\path_helper( + $symfony_request, + $filesystem, + $request, + $phpbb_root_path, + $phpEx + ); + + $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); + $phpbb_container = new phpbb_mock_container_builder(); + $files = new phpbb\files\factory($phpbb_container); + $upload_avatar_driver = new phpbb\avatar\driver\upload($config, $phpbb_root_path, $phpEx, $filesystem, $phpbb_path_helper, $phpbb_dispatcher, $files); + $upload_avatar_driver->set_name('avatar.driver.upload'); + $phpbb_container->set('avatar.manager', new \phpbb\avatar\manager($config, $phpbb_dispatcher, [ + $upload_avatar_driver, + ])); + $phpbb_container->set('path_helper', $phpbb_path_helper); + + $class = new ReflectionClass('\phpbb\avatar\manager'); + $enabled_drivers = $class->getProperty('enabled_drivers'); + $enabled_drivers->setAccessible(true); + $enabled_drivers->setValue(false); + + $this->template_path = $this->test_path . '/templates'; + + $cache_path = $phpbb_root_path . 'cache/twig'; + $context = new \phpbb\template\context(); + $loader = new \phpbb\template\twig\loader($filesystem); + $twig = new \phpbb\template\twig\environment( + $config, + $filesystem, + $phpbb_path_helper, + $cache_path, + null, + $loader, + new \phpbb\event\dispatcher($phpbb_container), + array( + 'cache' => false, + 'debug' => false, + 'auto_reload' => true, + 'autoescape' => false, + ) + ); + $this->template = new phpbb\template\twig\twig( + $phpbb_path_helper, + $config, + $context, + $twig, + $cache_path, + $this->user, + [ + new \phpbb\template\twig\extension($context, $twig, $this->lang), + new \phpbb\template\twig\extension\avatar(), + new \phpbb\template\twig\extension\config($config), + new \phpbb\template\twig\extension\username(), + ] + ); + $twig->setLexer(new \phpbb\template\twig\lexer($twig)); + $this->template->set_custom_style('tests', $this->template_path); + } + + public function data_template_extensions() + { + return [ + [ + 'avatar_user.html', + [ + 'row' => [ + 'user_avatar' => 'great_avatar.png', + 'user_avatar_type' => 'avatar.driver.upload', + 'user_avatar_width' => 90, + 'user_avatar_height' => 90, + ], + 'alt' => 'foo' + ], + [], + [], + '<img class="avatar" src="phpBB/download/file.php?avatar=great_avatar.png" width="90" height="90" alt="foo" />', + [] + ], + [ + 'avatar_user.html', + [ + 'row' => [ + 'user_avatar' => 'great_avatar.png', + 'user_avatar_type' => 'avatar.driver.upload', + 'user_avatar_width' => 90, + 'user_avatar_height' => 90, + ], + 'alt' => 'foo', + 'ignore_config' => true, + 'lazy' => true, + ], + [], + [], + '<img class="avatar" src="phpBB/styles//theme/images/no_avatar.gif" data-src="phpBB/download/file.php?avatar=great_avatar.png" width="90" height="90" alt="foo" />', + [] + ], + [ + 'avatar_user.html', + [ + 'row' => [ + 'user_avatar' => 'foo@bar.com', + 'user_avatar_type' => 'avatar.driver.gravatar', + 'user_avatar_width' => 90, + 'user_avatar_height' => 90, + ], + 'alt' => 'foo' + ], + [], + [], + '', + [] + ], + [ + 'extension_username_test.html', + [ + 'mode' => 'profile', + 'user_id' => 2, + 'username' => 'admin', + 'user_colour' => 'abcdef', + 'guest_username' => 'lol', + ], + [], + [], + 'phpBB/memberlist.php?mode=viewprofile&u=2', + [] + ], + [ + 'extension_username_test.html', + [ + 'mode' => 'profile', + 'user_id' => 2, + 'username' => 'admin', + 'user_colour' => 'abcdef', + 'guest_username' => 'lol', + 'custom_profile_url' => 'http://lol.bar', + ], + [], + [], + 'http://lol.bar&u=2', + [] + ], + [ + 'extension_username_test.html', + [ + 'mode' => 'full', + 'user_id' => 2, + 'username' => 'admin', + 'user_colour' => 'abcdef', + 'guest_username' => 'lol', + ], + [], + [], + '<a href="phpBB/memberlist.php?mode=viewprofile&u=2" style="color: #abcdef;" class="username-coloured">admin</a>', + [] + ], + [ + 'extension_username_test.html', + [ + 'mode' => 'no_profile', + 'user_id' => 2, + 'username' => 'admin', + 'user_colour' => 'abcdef', + 'guest_username' => 'lol', + ], + [], + [], + '<span style="color: #abcdef;" class="username-coloured">admin</span>', + [] + ], + [ + 'extension_config_test.html', + [ + 'config_name' => 'allow_avatar', + ], + [], + [], + '1', + [] + ], + [ + 'extension_config_test.html', + [ + 'config_name' => 'does not exist', + ], + [], + [], + '', + [] + ], + [ + 'extension_config_test.html', + [ + 'config_name' => 'tpl_allow_php', + ], + [], + [], + '', + [] + ], + ]; + } + + /** + * @dataProvider data_template_extensions + */ + public function test_get_user_avatar($file, $vars, $block_vars, $destroy_array, $expected, $lang_vars = []) + { + $this->run_template($file, $vars, $block_vars, $destroy_array, $expected, $lang_vars); + } +} diff --git a/tests/template/templates/avatar_user.html b/tests/template/templates/avatar_user.html new file mode 100644 index 0000000000..bd0b960611 --- /dev/null +++ b/tests/template/templates/avatar_user.html @@ -0,0 +1 @@ +{{ avatar('user', row, alt, ignore_config, lazy) }} diff --git a/tests/template/templates/extension_config_test.html b/tests/template/templates/extension_config_test.html new file mode 100644 index 0000000000..0f12ab2672 --- /dev/null +++ b/tests/template/templates/extension_config_test.html @@ -0,0 +1 @@ +{{ config(config_name) }} diff --git a/tests/template/templates/extension_username_test.html b/tests/template/templates/extension_username_test.html new file mode 100644 index 0000000000..a78bf0df62 --- /dev/null +++ b/tests/template/templates/extension_username_test.html @@ -0,0 +1 @@ +{{ username(mode, user_id, username, user_colour, guest_username, custom_profile_url) }} |