Skip to content

Commit

Permalink
Events backward compatibility handling (#41357)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Aug 14, 2023
1 parent 60faae8 commit fcc7209
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
34 changes: 34 additions & 0 deletions libraries/src/Event/AbstractEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,23 @@ public function __construct(string $name, array $arguments = [])
*/
public function getArgument($name, $default = null)
{
// B/C check for numeric access to named argument, eg $event->getArgument('0').
if (is_numeric($name)) {
if (key($this->arguments) != 0) {
$argNames = \array_keys($this->arguments);
$name = $argNames[$name] ?? '';
}

@trigger_error(
sprintf(
'Numeric access to named event arguments is deprecated, and will not work in Joomla 6. Event %s argument %s',
\get_class($this),
$name
),
E_USER_DEPRECATED
);
}

$methodName = 'get' . ucfirst($name);

$value = parent::getArgument($name, $default);
Expand Down Expand Up @@ -170,6 +187,23 @@ public function getArgument($name, $default = null)
*/
public function setArgument($name, $value)
{
// B/C check for numeric access to named argument, eg $event->setArgument('0', $value).
if (is_numeric($name)) {
if (key($this->arguments) != 0) {
$argNames = \array_keys($this->arguments);
$name = $argNames[$name] ?? '';
}

@trigger_error(
sprintf(
'Numeric access to named event arguments is deprecated, and will not work in Joomla 6. Event %s argument %s',
\get_class($this),
$name
),
E_USER_DEPRECATED
);
}

$methodName = 'set' . ucfirst($name);

if (method_exists($this, $methodName)) {
Expand Down
12 changes: 9 additions & 3 deletions libraries/src/Plugin/CMSPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace Joomla\CMS\Plugin;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Event\AbstractImmutableEvent;
use Joomla\CMS\Event\Result\ResultAwareInterface;
use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\LanguageAwareInterface;
Expand Down Expand Up @@ -291,9 +293,13 @@ function (AbstractEvent $event) use ($methodName) {
return;
}

// Restore the old results and add the new result from our method call
$allResults[] = $result;
$event['result'] = $allResults;
if ($event instanceof ResultAwareInterface) {
$event->addResult($result);
} elseif (!$event instanceof AbstractImmutableEvent) {
// Restore the old results and add the new result from our method call
$allResults[] = $result;
$event['result'] = $allResults;
}
}
);
}
Expand Down

0 comments on commit fcc7209

Please sign in to comment.