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

[4.2] Convert versionable plugin to services #37929

Merged
merged 8 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 50 additions & 0 deletions plugins/behaviour/versionable/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Behaviour.versionable
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Helper\CMSHelper;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Filter\InputFilter;
use Joomla\Plugin\Behaviour\Versionable\Extension\Versionable;

return new class implements ServiceProviderInterface
{
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container)
{
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new Versionable(
$dispatcher,
(array) PluginHelper::getPlugin('behaviour', 'versionable'),
new InputFilter,
new CMSHelper
);

return $plugin;
}
);
}
};
169 changes: 169 additions & 0 deletions plugins/behaviour/versionable/src/Extension/Versionable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Behaviour.versionable
*
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\Behaviour\Versionable\Extension;

defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Event\Table\AfterStoreEvent;
use Joomla\CMS\Event\Table\BeforeDeleteEvent;
use Joomla\CMS\Helper\CMSHelper;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Versioning\VersionableTableInterface;
use Joomla\CMS\Versioning\Versioning;
use Joomla\Event\DispatcherInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Filter\InputFilter;

/**
* Implements the Versionable behaviour which allows extensions to automatically support content history for their content items.
*
* This plugin supersedes JTableObserverContenthistory.
*
* @since 4.0.0
*/
final class Versionable extends CMSPlugin implements SubscriberInterface
{
/**
* Returns an array of events this subscriber will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onTableAfterStore' => 'onTableAfterStore',
'onTableBeforeDelete' => 'onTableBeforeDelete',
];
}

/**
* The application object
*
* @var CMSApplicationInterface
* @since __DEPLOY_VERSION__
*/
protected $app;

/**
* The input filter
*
* @var InputFilter
* @since __DEPLOY_VERSION__
*/
private $filter;

/**
* The CMS helper
*
* @var CMSHelper
* @since __DEPLOY_VERSION__
*/
private $helper;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param InputFilter $filter The input filter
* @param CMSHelper $helper The CMS helper
*
* @since 4.0.0
*/
public function __construct(DispatcherInterface $dispatcher, array $config, InputFilter $filter, CMSHelper $helper)
{
parent::__construct($dispatcher, $config);

$this->filter = $filter;
$this->helper = $helper;
}

/**
* Post-processor for $table->store($updateNulls)
*
* @param AfterStoreEvent $event The event to handle
*
* @return void
*
* @since 4.0.0
*/
public function onTableAfterStore(AfterStoreEvent $event)
{
// Extract arguments
/** @var VersionableTableInterface $table */
$table = $event['subject'];
$result = $event['result'];

if (!$result)
{
return;
}

if (!(is_object($table) && $table instanceof VersionableTableInterface))
laoneo marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

// Get the Tags helper and assign the parsed alias
$typeAlias = $table->getTypeAlias();
$aliasParts = explode('.', $typeAlias);

if ($aliasParts[0] === '' || !ComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
{
return;
}

$id = $table->getId();
$data = $this->helper->getDataObject($table);
$input = $this->app->input;
$jform = $input->get('jform', array(), 'array');
$versionNote = '';

if (isset($jform['version_note']))
{
$versionNote = $this->filter->clean($jform['version_note'], 'string');
}

Versioning::store($typeAlias, $id, $data, $versionNote);
}

/**
* Pre-processor for $table->delete($pk)
*
* @param BeforeDeleteEvent $event The event to handle
*
* @return void
*
* @since 4.0.0
*/
public function onTableBeforeDelete(BeforeDeleteEvent $event)
{
// Extract arguments
/** @var VersionableTableInterface $table */
$table = $event['subject'];

if (!(is_object($table) && $table instanceof VersionableTableInterface))
roland-d marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

$typeAlias = $table->getTypeAlias();
$aliasParts = explode('.', $typeAlias);

if ($aliasParts[0] && ComponentHelper::getParams($aliasParts[0])->get('save_history', 0))
{
Versioning::delete($typeAlias, $table->getId());
}
}
}
126 changes: 0 additions & 126 deletions plugins/behaviour/versionable/versionable.php

This file was deleted.

8 changes: 7 additions & 1 deletion plugins/behaviour/versionable/versionable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
<copyright>(C) 2016 Open Source Matters, Inc.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<description>PLG_BEHAVIOUR_VERSIONABLE_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Behaviour\Versionable</namespace>
<files>
<filename plugin="versionable">versionable.php</filename>
<folder plugin="versionable">services</folder>
<folder>src</folder>
</files>
laoneo marked this conversation as resolved.
Show resolved Hide resolved
<languages>
<language tag="en-GB">language/en-GB/plg_behaviour_versionable.ini</language>
<language tag="en-GB">language/en-GB/plg_behaviour_versionable.sys.ini</language>
</languages>
<config />
laoneo marked this conversation as resolved.
Show resolved Hide resolved
roland-d marked this conversation as resolved.
Show resolved Hide resolved
</extension>