From cede9f6d5f3f1489283fa3a27e9d5933278ac14a Mon Sep 17 00:00:00 2001 From: "Nicholas K. Dionysopoulos" Date: Wed, 22 May 2024 14:25:45 +0300 Subject: [PATCH] Bad formatting of concrete event handling example Close gh-33 --- sections/plugins.xml | 116 +++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 71 deletions(-) diff --git a/sections/plugins.xml b/sections/plugins.xml index fb12e6d..02d229b 100644 --- a/sections/plugins.xml +++ b/sections/plugins.xml @@ -1817,6 +1817,7 @@ namespace Acme\Plugin\System\Example; defined('_JEXEC') || die; +use Joomla\CMS\Event\Result\ResultAwareInterface; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Component\Content\Administrator\Table\ArticleTable; use Joomla\Event\Event; @@ -1824,84 +1825,57 @@ use Joomla\Event\SubscriberInterface; class ExamplePlugin extends CMSPlugin implements SubscriberInterface { - public static function getSubscribedEvents(): array - { - return [ - 'onExampleFoobar' => 'handleFoobar', - ]; - } - - public function handleFoobar(Event $event) - { - // If using a concrete event, do it the simple way - if ($event instanceof \Acme\Component\Example\Administrator\Event\Foobar) { - /** @var ArticleTable $article */ - $article = $event->getArgument('article'); - /** @var integer $count */ - $count = $event->getArgument('count', 10);<?php -namespace Acme\Plugin\System\Example; + public static function getSubscribedEvents(): array + { + return [ + 'onExampleFoobar' => 'handleFoobar', + ]; + } -defined('_JEXEC') || die; + public function handleFoobar(Event $event) + { + // If using a concrete event, do it the simple way + if ($event instanceof \Acme\Component\Example\Administrator\Event\Foobar) { + /** @var ArticleTable $article */ + $article = $event->getArgument('article'); + /** @var integer $count */ + $count = $event->getArgument('count', 10); + // If using a generic event, do it the hard way + } else { + [$article, $count] = $event->getArguments(); + + if (!$article instanceof ArticleTable) { + throw new \InvalidArgumentException('The article argument to the onExampleFoobar event must be an ArticleTable'); + } -use Joomla\CMS\Event\Result\ResultAwareInterface; -use Joomla\CMS\Plugin\CMSPlugin; -use Joomla\Component\Content\Administrator\Table\ArticleTable; -use Joomla\Event\Event; -use Joomla\Event\SubscriberInterface; + if (!is_int($count) || $count <= 0) { + throw new \InvalidArgumentException('The count argument to the onExampleFoobar event must be a positive integer'); + } + } -class ExamplePlugin extends CMSPlugin implements SubscriberInterface -{ - public static function getSubscribedEvents(): array - { - return [ - 'onExampleFoobar' => 'handleFoobar', - ]; - } + // Do something... + $myResult = [ + 'foobar' => $article->title, + 'foobaz' => $count + ]; - public function handleFoobar(Event $event) - { - // If using a concrete event, do it the simple way - if ($event instanceof \Acme\Component\Example\Administrator\Event\Foobar) { - /** @var ArticleTable $article */ - $article = $event->getArgument('article'); - /** @var integer $count */ - $count = $event->getArgument('count', 10); - // If using a generic event, do it the hard way - } else { - [$article, $count] = $event->getArguments(); - - if (!$article instanceof ArticleTable) { - throw new \InvalidArgumentException('The article argument to the onExampleFoobar event must be an ArticleTable'); - } - - if (!is_int($count) || $count <= 0) { - throw new \InvalidArgumentException('The count argument to the onExampleFoobar event must be a positive integer'); - } - } + // Return the result + $this->setResult($myResult); + } - // Do something... - $myResult = [ - 'foobar' => $article->title, - 'foobaz' => $count - ]; + private function setResult(Event $event, $value): void + { + if ($event instanceof ResultAwareInterface) { + $event->addResult($value); - // Return the result - $this->setResult($myResult); - } - - private function setResult(Event $event, $value): void - { - if ($event instanceof ResultAwareInterface) { - $event->addResult($value); - - return; - } + return; + } - $result = $event->getArgument('result', []) ?: []; - $result = is_array($result) ? $result : []; - $result[] = $value; - $event->setArgument('result', $result); - } + $result = $event->getArgument('result', []) ?: []; + $result = is_array($result) ? $result : []; + $result[] = $value; + $event->setArgument('result', $result); + } } In the example code above the setResult method can be reused by all of your event handlers, at any return