Skip to content

Commit

Permalink
[imp] get events working in J4
Browse files Browse the repository at this point in the history
  • Loading branch information
phproberto committed Nov 20, 2017
1 parent f7bbe07 commit 6d8be16
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
31 changes: 27 additions & 4 deletions extensions/libraries/twig/src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

defined('_JEXEC') || die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Twig\Loader\LoaderInterface;
use Twig\Environment as BaseEnvironment;
Expand All @@ -21,6 +23,14 @@
*/
final class Environment extends BaseEnvironment
{
/**
* Application where enviroment is loaded.
*
* @var CMSApplication
* @since __DEPLOY_VERSION__
*/
private $app;

/**
* Plugins connected to the events triggered by this class.
*
Expand All @@ -35,16 +45,31 @@ final class Environment extends BaseEnvironment
*
* @param LoaderInterface $loader Loader instance
* @param array $options An array of options
* @param CMSApplication $app CMSApplication | null active application
*/
public function __construct(LoaderInterface $loader, $options = [])
public function __construct(LoaderInterface $loader, $options = [], CMSApplication $app = null)
{
$this->app = $app ?: $this->activeApplication();

$this->trigger('onTwigBeforeLoad', [&$loader, &$options]);

parent::__construct($loader, $options);

$this->trigger('onTwigAfterLoad', [$options]);
}

/**
* Get the active Joomla application.
*
* @return CMSApplication
*
* @since __DEPLOY_VERSION__
*/
private function activeApplication()
{
return Factory::getApplication();
}

/**
* Import available plugins.
*
Expand All @@ -68,13 +93,11 @@ private function importPlugins()
*/
public function trigger($event, $params = [])
{
$dispatcher = \JEventDispatcher::getInstance();

$this->importPlugins();

// Always send enviroment as first param
array_unshift($params, $this);

return $dispatcher->trigger($event, $params);
return $this->app->triggerEvent($event, $params);
}
}
13 changes: 10 additions & 3 deletions tests/Unit/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Twig\Loader\LoaderInterface;
use Phproberto\Joomla\Twig\Environment;
use Joomla\CMS\Application\CMSApplication;

/**
* Environment tests.
Expand Down Expand Up @@ -45,10 +46,16 @@ protected function setUp()
{
$this->saveFactoryState();

\JFactory::$session = $this->getMockSession();
\JFactory::$config = $this->getMockConfig();
\JFactory::$session = $this->getMockSession();

$this->dispatcher = new \JEventDispatcher;
\TestReflection::setValue($this->dispatcher, 'instance', $this->dispatcher);

$app = $this->getMockForAbstractClass(CMSApplication::class);
$app->loadDispatcher($this->dispatcher);

\JFactory::$application = $app;
}

/**
Expand Down Expand Up @@ -77,8 +84,8 @@ public function testConstructorTriggersEvents()
{
$this->calledEvents = [];

$this->dispatcher->register('onTwigBeforeLoad', [$this, 'onTwigBeforeLoad']);
$this->dispatcher->register('onTwigAfterLoad', [$this, 'onTwigAfterLoad']);
\JFactory::$application->registerEvent('onTwigBeforeLoad', [$this, 'onTwigBeforeLoad']);
\JFactory::$application->registerEvent('onTwigAfterLoad', [$this, 'onTwigAfterLoad']);

$loader = new \Twig_Loader_Array;
$options = ['sample' => 'option'];
Expand Down

0 comments on commit 6d8be16

Please sign in to comment.