mirror of
https://github.com/phpbb/phpbb.git
synced 2025-08-08 17:56:52 +02:00
Merge pull request #6770 from rxu/ticket/15214
[ticket/15214] Add event & functionality for assigning template event priority
This commit is contained in:
@@ -161,6 +161,16 @@ class environment extends \Twig\Environment
|
||||
return $this->assets_bag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the event dispatcher instance
|
||||
*
|
||||
* @return dispatcher_interface
|
||||
*/
|
||||
public function get_phpbb_dispatcher()
|
||||
{
|
||||
return $this->phpbb_dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the namespace look up order
|
||||
*
|
||||
|
@@ -24,9 +24,13 @@ class event extends \Twig\Node\Node
|
||||
/** @var \phpbb\template\twig\environment */
|
||||
protected $environment;
|
||||
|
||||
public function __construct(\Twig\Node\Expression\AbstractExpression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null)
|
||||
/** @var array */
|
||||
protected $template_event_priority_array;
|
||||
|
||||
public function __construct(\Twig\Node\Expression\AbstractExpression $expr, \phpbb\template\twig\environment $environment, $lineno, $tag = null, $template_event_priority_array = [])
|
||||
{
|
||||
$this->environment = $environment;
|
||||
$this->template_event_priority_array = $template_event_priority_array;
|
||||
|
||||
parent::__construct(array('expr' => $expr), array(), $lineno, $tag);
|
||||
}
|
||||
@@ -42,10 +46,20 @@ class event extends \Twig\Node\Node
|
||||
|
||||
$location = $this->listener_directory . $this->getNode('expr')->getAttribute('name');
|
||||
|
||||
$template_event_listeners = [];
|
||||
|
||||
// Group and sort extension template events in according to their priority (0 by default if not set)
|
||||
foreach ($this->environment->get_phpbb_extensions() as $ext_namespace => $ext_path)
|
||||
{
|
||||
$ext_namespace = str_replace('/', '_', $ext_namespace);
|
||||
$priority_key = intval($this->template_event_priority_array[$ext_namespace][$location] ?? 0);
|
||||
$template_event_listeners[$priority_key][] = $ext_namespace;
|
||||
}
|
||||
krsort($template_event_listeners);
|
||||
|
||||
$template_event_listeners = array_merge(...$template_event_listeners);
|
||||
foreach ($template_event_listeners as $ext_namespace)
|
||||
{
|
||||
if ($this->environment->isDebug())
|
||||
{
|
||||
// If debug mode is enabled, lets check for new/removed EVENT
|
||||
@@ -54,8 +68,7 @@ class event extends \Twig\Node\Node
|
||||
// purge the cache when a new event template file is added)
|
||||
$compiler
|
||||
->write("if (\$this->env->getLoader()->exists('@{$ext_namespace}/{$location}.html')) {\n")
|
||||
->indent()
|
||||
;
|
||||
->indent();
|
||||
}
|
||||
|
||||
if ($this->environment->isDebug() || $this->environment->getLoader()->exists('@' . $ext_namespace . '/' . $location . '.html'))
|
||||
@@ -66,16 +79,14 @@ class event extends \Twig\Node\Node
|
||||
// We set the namespace lookup order to be this extension first, then the main path
|
||||
->write("\$this->env->setNamespaceLookUpOrder(array('{$ext_namespace}', '__main__'));\n")
|
||||
->write("\$this->env->loadTemplate(\$this->env->getTemplateClass('@{$ext_namespace}/{$location}.html'), '@{$ext_namespace}/{$location}.html')->display(\$context);\n")
|
||||
->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n")
|
||||
;
|
||||
->write("\$this->env->setNamespaceLookUpOrder(\$previous_look_up_order);\n");
|
||||
}
|
||||
|
||||
if ($this->environment->isDebug())
|
||||
{
|
||||
$compiler
|
||||
->outdent()
|
||||
->write("}\n\n")
|
||||
;
|
||||
->write("}\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,9 @@ class event extends \Twig\TokenParser\AbstractTokenParser
|
||||
/** @var \phpbb\template\twig\environment */
|
||||
protected $environment;
|
||||
|
||||
/** @var array */
|
||||
protected $template_event_priority_array;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -26,6 +29,25 @@ class event extends \Twig\TokenParser\AbstractTokenParser
|
||||
public function __construct(\phpbb\template\twig\environment $environment)
|
||||
{
|
||||
$this->environment = $environment;
|
||||
$phpbb_dispatcher = $this->environment->get_phpbb_dispatcher();
|
||||
|
||||
$template_event_priority_array = [];
|
||||
/**
|
||||
* Allows assigning priority to template event listeners
|
||||
*
|
||||
* @event core.twig_event_tokenparser_constructor
|
||||
* @var array template_event_priority_array Array with template event priority assignments per extension namespace
|
||||
*
|
||||
* @since 4.0.0-a1
|
||||
*/
|
||||
if ($phpbb_dispatcher)
|
||||
{
|
||||
$vars = ['template_event_priority_array'];
|
||||
extract($phpbb_dispatcher->trigger_event('core.twig_event_tokenparser_constructor', compact($vars)));
|
||||
}
|
||||
|
||||
$this->template_event_priority_array = $template_event_priority_array;
|
||||
unset($template_event_priority_array);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,7 +64,7 @@ class event extends \Twig\TokenParser\AbstractTokenParser
|
||||
$stream = $this->parser->getStream();
|
||||
$stream->expect(\Twig\Token::BLOCK_END_TYPE);
|
||||
|
||||
return new \phpbb\template\twig\node\event($expr, $this->environment, $token->getLine(), $this->getTag());
|
||||
return new \phpbb\template\twig\node\event($expr, $this->environment, $token->getLine(), $this->getTag(), $this->template_event_priority_array);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user