1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-06-06 22:45:02 +02:00

Merge remote-tracking branch 'github-nicofuma/ticket/11649' into develop

* github-nicofuma/ticket/11649:
  [ticket/11649] Fix coding style
  [ticket/11649] Expose Twig through the container
This commit is contained in:
Nils Adermann 2014-08-09 17:17:57 +02:00
commit f33be87d03
9 changed files with 194 additions and 47 deletions

View File

@ -10,6 +10,7 @@ imports:
- { resource: mimetype_guessers.yml } - { resource: mimetype_guessers.yml }
- { resource: passwords.yml } - { resource: passwords.yml }
- { resource: profilefields.yml } - { resource: profilefields.yml }
- { resource: twig.yml }
services: services:
acl.permissions: acl.permissions:
@ -339,6 +340,9 @@ services:
- @config - @config
- @user - @user
- @template_context - @template_context
- @template.twig.environment
- %core.template.cache_path%
- @template.twig.extensions.collection
- @ext.manager - @ext.manager
template_context: template_context:

36
phpBB/config/twig.yml Normal file
View File

@ -0,0 +1,36 @@
parameters:
core.template.cache_path: %core.root_path%cache/twig/
services:
template.twig.environment:
class: phpbb\template\twig\environment
arguments:
- @config
- @path_helper
- @service_container
- %core.template.cache_path%
- @ext.manager
- @template.twig.loader
template.twig.lexer:
class: phpbb\template\twig\lexer
arguments:
- @template.twig.environment
template.twig.loader:
class: phpbb\template\twig\loader
template.twig.extensions.collection:
class: phpbb\di\service_collection
arguments:
- @service_container
tags:
- { name: service_collection, tag: twig.extension }
template.twig.extensions.phpbb:
class: phpbb\template\twig\extension
arguments:
- @template_context
- @user
tags:
- { name: twig.extension }

View File

@ -13,6 +13,9 @@
namespace phpbb\template\twig; namespace phpbb\template\twig;
use Twig_Lexer;
use Twig_LexerInterface;
class environment extends \Twig_Environment class environment extends \Twig_Environment
{ {
/** @var \phpbb\config\config */ /** @var \phpbb\config\config */
@ -21,6 +24,9 @@ class environment extends \Twig_Environment
/** @var \phpbb\path_helper */ /** @var \phpbb\path_helper */
protected $phpbb_path_helper; protected $phpbb_path_helper;
/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
protected $container;
/** @var \phpbb\extension\manager */ /** @var \phpbb\extension\manager */
protected $extension_manager; protected $extension_manager;
@ -38,23 +44,47 @@ class environment extends \Twig_Environment
* *
* @param \phpbb\config\config $phpbb_config The phpBB configuration * @param \phpbb\config\config $phpbb_config The phpBB configuration
* @param \phpbb\path_helper $path_helper phpBB path helper * @param \phpbb\path_helper $path_helper phpBB path helper
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container The dependency injection container
* @param string $cache_path The path to the cache directory
* @param \phpbb\extension\manager $extension_manager phpBB extension manager * @param \phpbb\extension\manager $extension_manager phpBB extension manager
* @param \Twig_LoaderInterface $loader Twig loader interface * @param \Twig_LoaderInterface $loader Twig loader interface
* @param array $options Array of options to pass to Twig * @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, \phpbb\path_helper $path_helper, \Symfony\Component\DependencyInjection\ContainerInterface $container, $cache_path, \phpbb\extension\manager $extension_manager = null, \Twig_LoaderInterface $loader = null, $options = array())
{ {
$this->phpbb_config = $phpbb_config; $this->phpbb_config = $phpbb_config;
$this->phpbb_path_helper = $path_helper; $this->phpbb_path_helper = $path_helper;
$this->extension_manager = $extension_manager; $this->extension_manager = $extension_manager;
$this->container = $container;
$this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path(); $this->phpbb_root_path = $this->phpbb_path_helper->get_phpbb_root_path();
$this->web_root_path = $this->phpbb_path_helper->get_web_root_path(); $this->web_root_path = $this->phpbb_path_helper->get_web_root_path();
$options = array_merge(array(
'cache' => (defined('IN_INSTALL')) ? false : $cache_path,
'debug' => defined('DEBUG'),
'auto_reload' => (bool) $this->phpbb_config['load_tplcompile'],
'autoescape' => false,
), $options);
return parent::__construct($loader, $options); return parent::__construct($loader, $options);
} }
/**
* {@inheritdoc}
*/
public function getLexer()
{
if (null === $this->lexer)
{
$this->lexer = $this->container->get('template.twig.lexer');
}
return $this->lexer;
}
/** /**
* Get the list of enabled phpBB extensions * Get the list of enabled phpBB extensions
* *

View File

@ -78,9 +78,12 @@ class twig extends \phpbb\template\base
* @param \phpbb\config\config $config * @param \phpbb\config\config $config
* @param \phpbb\user $user * @param \phpbb\user $user
* @param \phpbb\template\context $context template context * @param \phpbb\template\context $context template context
* @param \phpbb\template\twig\environment $twig_environment
* @param string $cache_path
* @param array|\ArrayAccess $extensions
* @param \phpbb\extension\manager $extension_manager extension manager, if null then template events will not be invoked * @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, $user, \phpbb\template\context $context, \phpbb\template\twig\environment $twig_environment, $cache_path, $extensions = array(), \phpbb\extension\manager $extension_manager = null)
{ {
$this->path_helper = $path_helper; $this->path_helper = $path_helper;
$this->phpbb_root_path = $path_helper->get_phpbb_root_path(); $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
@ -89,35 +92,13 @@ class twig extends \phpbb\template\base
$this->user = $user; $this->user = $user;
$this->context = $context; $this->context = $context;
$this->extension_manager = $extension_manager; $this->extension_manager = $extension_manager;
$this->cachepath = $cache_path;
$this->twig = $twig_environment;
$this->cachepath = $this->phpbb_root_path . 'cache/twig/'; foreach ($extensions as $extension)
{
// Initiate the loader, __main__ namespace paths will be setup later in set_style_names() $this->twig->addExtension($extension);
$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
)
);
$lexer = new \phpbb\template\twig\lexer($this->twig);
$this->twig->setLexer($lexer);
// Add admin namespace // 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/')) if ($this->path_helper->get_adm_relative_path() !== null && is_dir($this->phpbb_root_path . $this->path_helper->get_adm_relative_path() . 'style/'))

View File

@ -31,7 +31,28 @@ class phpbb_controller_helper_route_test extends phpbb_test_case
$phpEx $phpEx
); );
$this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0')); $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '0'));
$this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $this->user, new \phpbb\template\context());
$container = new phpbb_mock_container_builder();
$cache_path = $phpbb_root_path . 'cache/twig';
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$twig = new \phpbb\template\twig\environment(
$this->config,
$phpbb_path_helper,
$container,
$cache_path,
null,
$loader,
array(
'cache' => false,
'debug' => false,
'auto_reload' => true,
'autoescape' => false,
)
);
$this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->extension_manager = new phpbb_mock_extension_manager( $this->extension_manager = new phpbb_mock_extension_manager(
dirname(__FILE__) . '/', dirname(__FILE__) . '/',
array( array(

View File

@ -47,20 +47,35 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
$this->user = new \phpbb\user(); $this->user = new \phpbb\user();
$this->table_prefix = 'phpbb_'; $this->table_prefix = 'phpbb_';
$this->template = new \phpbb\template\twig\twig( $container = new phpbb_mock_container_builder();
new \phpbb\path_helper( $cache_path = $this->phpbb_root_path . 'cache/twig';
new \phpbb\symfony_request( $context = new \phpbb\template\context();
new phpbb_mock_request() $loader = new \phpbb\template\twig\loader('');
), $phpbb_path_helper =new \phpbb\path_helper(
new \phpbb\filesystem(), new \phpbb\symfony_request(
$this->getMock('\phpbb\request\request'), new phpbb_mock_request()
$this->phpbb_root_path,
$this->phpEx
), ),
$this->config, new \phpbb\filesystem(),
$this->user, $this->getMock('\phpbb\request\request'),
new \phpbb\template\context() $this->phpbb_root_path,
$this->phpEx
); );
$twig = new \phpbb\template\twig\environment(
$this->config,
$phpbb_path_helper,
$container,
$cache_path,
null,
$loader,
array(
'cache' => false,
'debug' => false,
'auto_reload' => true,
'autoescape' => false,
)
);
$this->template = new phpbb\template\twig\twig($phpbb_path_helper, $this->config, $this->user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->migrator = new \phpbb\db\migrator( $this->migrator = new \phpbb\db\migrator(
$this->config, $this->config,
@ -73,7 +88,6 @@ class phpbb_extension_metadata_manager_test extends phpbb_database_test_case
array(), array(),
new \phpbb\db\migration\helper() new \phpbb\db\migration\helper()
); );
$container = new phpbb_mock_container_builder();
$container->set('migrator', $this->migrator); $container->set('migrator', $this->migrator);
$this->extension_manager = new \phpbb\extension\manager( $this->extension_manager = new \phpbb\extension\manager(

View File

@ -147,7 +147,28 @@ Zeta test event in all',
$phpbb_root_path, $phpbb_root_path,
$phpEx $phpEx
); );
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $user, new \phpbb\template\context, $this->extension_manager);
$container = new phpbb_mock_container_builder();
$cache_path = $phpbb_root_path . 'cache/twig';
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$twig = new \phpbb\template\twig\environment(
$config,
$path_helper,
$container,
$cache_path,
$this->extension_manager,
$loader,
array(
'cache' => false,
'debug' => false,
'auto_reload' => true,
'autoescape' => false,
)
);
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)), $this->extension_manager);
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path)); $this->template->set_custom_style(((!empty($style_names)) ? $style_names : 'silver'), array($this->template_path));
} }
} }

View File

@ -78,7 +78,27 @@ class phpbb_template_template_test_case extends phpbb_test_case
); );
$this->template_path = $this->test_path . '/templates'; $this->template_path = $this->test_path . '/templates';
$this->template = new \phpbb\template\twig\twig($path_helper, $config, $this->user, new \phpbb\template\context());
$container = new phpbb_mock_container_builder();
$cache_path = $phpbb_root_path . 'cache/twig';
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$twig = new \phpbb\template\twig\environment(
$config,
$path_helper,
$container,
$cache_path,
null,
$loader,
array(
'cache' => false,
'debug' => false,
'auto_reload' => true,
'autoescape' => false,
)
);
$this->template = new phpbb\template\twig\twig($path_helper, $config, $user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style('tests', $this->template_path); $this->template->set_custom_style('tests', $this->template_path);
} }

View File

@ -34,7 +34,27 @@ class phpbb_template_template_test_case_with_tree extends phpbb_template_templat
$this->template_path = $this->test_path . '/templates'; $this->template_path = $this->test_path . '/templates';
$this->parent_template_path = $this->test_path . '/parent_templates'; $this->parent_template_path = $this->test_path . '/parent_templates';
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $user, new phpbb\template\context());
$container = new phpbb_mock_container_builder();
$cache_path = $phpbb_root_path . 'cache/twig';
$context = new \phpbb\template\context();
$loader = new \phpbb\template\twig\loader('');
$twig = new \phpbb\template\twig\environment(
$config,
$this->phpbb_path_helper,
$container,
$cache_path,
null,
$loader,
array(
'cache' => false,
'debug' => false,
'auto_reload' => true,
'autoescape' => false,
)
);
$this->template = new phpbb\template\twig\twig($this->phpbb_path_helper, $config, $user, $context, $twig, $cache_path, array(new \phpbb\template\twig\extension($context, $this->user)));
$container->set('template.twig.lexer', new \phpbb\template\twig\lexer($twig));
$this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path)); $this->template->set_custom_style('tests', array($this->template_path, $this->parent_template_path));
} }
} }