Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2][Events] Use event classes for Sampledata plugins #43636

Open
wants to merge 4 commits into
base: 5.2-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
SampleData Event
  • Loading branch information
Fedik committed Jun 8, 2024
commit 653b67c609a7155dcf8048d08b5df3cc11806bdb
2 changes: 2 additions & 0 deletions libraries/src/Event/CoreEventAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ trait CoreEventAware
'onPageCacheSetCaching' => PageCache\SetCachingEvent::class,
'onPageCacheGetKey' => PageCache\GetKeyEvent::class,
'onPageCacheIsExcluded' => PageCache\IsExcludedEvent::class,
// Sample Data
'onSampledataGetOverview' => SampleData\GetOverviewEvent::class,
];

/**
Expand Down
29 changes: 29 additions & 0 deletions libraries/src/Event/SampleData/GetOverviewEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Event\SampleData;

use Joomla\CMS\Event\Result\ResultAware;
use Joomla\CMS\Event\Result\ResultAwareInterface;
use Joomla\CMS\Event\Result\ResultTypeObjectAware;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Cass for Sample data events
*
* @since __DEPLOY_VERSION__
*/
class GetOverviewEvent extends SampleDataEvent implements ResultAwareInterface
{
use ResultAware;
use ResultTypeObjectAware;
}
43 changes: 43 additions & 0 deletions libraries/src/Event/SampleData/SampleDataEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Event\SampleData;

use Joomla\CMS\Event\AbstractImmutableEvent;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Base class for Sample data events
*
* @since __DEPLOY_VERSION__
*/
abstract class SampleDataEvent extends AbstractImmutableEvent
{
/**
* Constructor.
*
* @param string $name The event name.
* @param array $arguments The event arguments.
*
* @throws \BadMethodCallException
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, array $arguments = [])
{
parent::__construct($name, $arguments);

if (!\array_key_exists('subject', $this->arguments)) {
throw new \BadMethodCallException("Argument 'subject' of event {$name} is required but has not been provided");
}
}
}
38 changes: 20 additions & 18 deletions plugins/sampledata/testing/src/Extension/Testing.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\Plugin\AjaxEvent;
use Joomla\CMS\Event\SampleData\GetOverviewEvent;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Categories\Administrator\Model\CategoryModel;
Expand All @@ -30,15 +32,6 @@ final class Testing extends CMSPlugin
{
use DatabaseAwareTrait;

/**
* Affects constructor behavior. If true, language files will be loaded automatically.
*
* @var boolean
*
* @since 3.8.0
*/
protected $autoloadLanguage = true;

/**
* Holds the category model
*
Expand All @@ -62,30 +55,36 @@ final class Testing extends CMSPlugin
/**
* Get an overview of the proposed sampledata.
*
* @return object Object containing the name, title, description, icon and steps.
* @param GetOverviewEvent $event Event instance
*
* @return void
*
* @since 3.8.0
*/
public function onSampledataGetOverview()
public function onSampledataGetOverview(GetOverviewEvent $event): void
{
$this->loadLanguage();

$data = new \stdClass();
$data->name = $this->_name;
$data->title = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_TESTING_OVERVIEW_TITLE');
$data->description = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_TESTING_OVERVIEW_DESC');
$data->icon = 'bolt';
$data->steps = 9;

return $data;
$event->addResult($data);
}

/**
* First step to enter the sampledata. Tags
*
* @return array|void Will be converted into the JSON response to the module.
* @param AjaxEvent $event Event instance
*
* @return void
*
* @since 3.8.0
*/
public function onAjaxSampledataApplyStep1()
public function onAjaxSampledataApplyStep1(AjaxEvent $event): void
{
if ($this->getApplication()->getInput()->get('type') !== $this->_name) {
return;
Expand All @@ -96,7 +95,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = true;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_TESTING_STEP_SKIPPED', 1, 'com_tags');

return $response;
$event->addResult($response);
return;
}

/** @var \Joomla\Component\Tags\Administrator\Model\TagModel $model */
Expand Down Expand Up @@ -130,7 +130,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_TESTING_STEP_FAILED', 1, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$tagIds[] = $model->getState('tag.id');
Expand Down Expand Up @@ -160,7 +161,8 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = false;
$response['message'] = Text::sprintf('PLG_SAMPLEDATA_TESTING_STEP_FAILED', 1, $e->getMessage());

return $response;
$event->addResult($response);
return;
}

$tagIds[] = $model->getState('tag.id');
Expand All @@ -172,7 +174,7 @@ public function onAjaxSampledataApplyStep1()
$response['success'] = true;
$response['message'] = $this->getApplication()->getLanguage()->_('PLG_SAMPLEDATA_TESTING_STEP1_SUCCESS');

return $response;
$event->addResult($response);
}

/**
Expand Down