Skip to content

Commit

Permalink
Merge pull request #42 from phproberto/joomla-4
Browse files Browse the repository at this point in the history
Joomla 4 compatibility
  • Loading branch information
phproberto authored Nov 20, 2017
2 parents 94facac + 6d8be16 commit e6dd368
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
6 changes: 4 additions & 2 deletions extensions/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('_JEXEC') || die;

use Joomla\CMS\Factory;

/**
* Package installer
*
Expand Down Expand Up @@ -56,7 +58,7 @@ private function enablePlugins($parent)
$extName = (string) $node->attributes()->id;
$extGroup = (string) $node->attributes()->group;

$db = JFactory::getDbo();
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->update($db->quoteName("#__extensions"));
$query->set("enabled=1");
Expand Down Expand Up @@ -111,7 +113,7 @@ protected function loadManifest($parent)
// Package manifest found
if (file_exists($manifestFile))
{
$this->manifest = JFactory::getXML($manifestFile);
$this->manifest = simplexml_load_file($manifestFile);

return;
}
Expand Down
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
26 changes: 26 additions & 0 deletions tests/Unit/Plugin/BasePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
*/
class BasePluginTest extends \TestCase
{
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @return void
*/
protected function setUp()
{
$this->saveFactoryState();

\JFactory::$config = $this->getMockConfig();
\JFactory::$application = $this->getMockCmsApp();
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @return void
*/
protected function tearDown()
{
$this->restoreFactoryState();
parent::tearDown();
}

/**
* pluginPath returns correct path.
*
Expand Down

0 comments on commit e6dd368

Please sign in to comment.