Skip to content

Commit

Permalink
Add onBeforeExecute event, move languagefilter plugin detection to th…
Browse files Browse the repository at this point in the history
…is event
  • Loading branch information
Michael Babker committed Sep 23, 2016
1 parent 3feb5c2 commit 23e35ce
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 16 deletions.
29 changes: 29 additions & 0 deletions installation/application/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,35 @@ protected function doExecute()
$this->dispatch();
}

/**
* Execute the application.
*
* @return void
*
* @since 4.0
*/
public function execute()
{
// Perform application routines.
$this->doExecute();

// If we have an application document object, render it.
if ($this->document instanceof JDocument)
{
// Render the application output.
$this->render();
}

// If gzip compression is enabled in configuration and the server is compliant, compress the output.
if ($this->get('gzip') && !ini_get('zlib.output_compression') && (ini_get('output_handler') != 'ob_gzhandler'))
{
$this->compress();
}

// Send the application response.
$this->respond();
}

/**
* Method to load a PHP configuration class file based on convention and return the instantiated data object. You
* will extend this method in child classes to provide configuration data from whatever data source is relevant
Expand Down
16 changes: 16 additions & 0 deletions libraries/cms/application/cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('JPATH_PLATFORM') or die;

use Joomla\Cms\Event\AbstractEvent;
use Joomla\Cms\Event\BeforeExecuteEvent;
use Joomla\DI\Container;
use Joomla\DI\ContainerAwareInterface;
use Joomla\DI\ContainerAwareTrait;
Expand Down Expand Up @@ -300,6 +302,20 @@ public function enqueueMessage($msg, $type = 'message')
*/
public function execute()
{
JPluginHelper::importPlugin('system');

// Trigger the onBeforeExecute event.
$this->triggerEvent(
'onBeforeExecute',
AbstractEvent::create(
'onBeforeExecute',
[
'subject' => $this,
'eventClass' => BeforeExecuteEvent::class,
]
)
);

// Perform application routines.
$this->doExecute();

Expand Down
16 changes: 0 additions & 16 deletions libraries/cms/application/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,22 +575,6 @@ protected function initialiseApp($options = array())
$user->groups = array($guestUsergroup);
}

/*
* If a language was specified it has priority, otherwise use user or default language settings
* Check this only if the languagefilter plugin is enabled
*
* @TODO - Remove the hardcoded dependency to the languagefilter plugin
*/
if (JPluginHelper::isEnabled('system', 'languagefilter'))
{
$plugin = JPluginHelper::getPlugin('system', 'languagefilter');

$pluginParams = new Registry($plugin->params);

$this->setLanguageFilter(true);
$this->setDetectBrowser($pluginParams->get('detect_browser', '1') == '1');
}

if (empty($options['language']))
{
// Detect the specified language
Expand Down
33 changes: 33 additions & 0 deletions libraries/src/Cms/Event/BeforeExecuteEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Cms\Event;

use Joomla\Application\AbstractApplication;

defined('JPATH_PLATFORM') or die;

/**
* Event class for representing the application's `onBeforeExecute` event
*
* @since __DEPLOY_VERSION__
*/
class BeforeExecuteEvent extends AbstractImmutableEvent
{
/**
* Get the event's application object
*
* @return AbstractApplication
*
* @since __DEPLOY_VERSION__
*/
public function getApplication()
{
return $this->getArgument('subject');
}
}
25 changes: 25 additions & 0 deletions plugins/system/languagefilter/languagefilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

defined('_JEXEC') or die;

use Joomla\Cms\Event\BeforeExecuteEvent;
use Joomla\Registry\Registry;

JLoader::register('MenusHelper', JPATH_ADMINISTRATOR . '/components/com_menus/helpers/menus.php');
Expand Down Expand Up @@ -163,6 +164,30 @@ public function onAfterRoute()
}
}

/**
* Listener for the onBeforeExecute event
*
* @param BeforeExecuteEvent $event
*
* @return void
*
* @since 4.0
*/
public function onBeforeExecute(BeforeExecuteEvent $event)
{
/** @var JApplicationCms $app */
$app = $event->getApplication();

if (!$app->isSite())
{
return;
}

// If a language was specified it has priority, otherwise use user or default language settings
$app->setLanguageFilter(true);
$app->setDetectBrowser($this->params->get('detect_browser', '1') == '1');
}

/**
* Add build preprocess rule to router.
*
Expand Down

0 comments on commit 23e35ce

Please sign in to comment.