Skip to content

Commit

Permalink
Convert workflow plugins to service provider (joomla#40205)
Browse files Browse the repository at this point in the history
* Convert workflow plugins to service provider

* cs

* Update plugins/workflow/notification/services/provider.php

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>

* use aware trait

* correct dispatcher

---------
  • Loading branch information
laoneo authored May 23, 2023
1 parent 073d4af commit 83ef3c2
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 113 deletions.
11 changes: 8 additions & 3 deletions libraries/src/Workflow/WorkflowPluginTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ protected function enhanceWorkflowTransitionForm(Form $form, $data)
// Load XML file from "parent" plugin
$path = dirname((new ReflectionClass(static::class))->getFileName());

if (!is_file($path . '/forms/action.xml')) {
$path = JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name;
}

if (is_file($path . '/forms/action.xml')) {
$form->loadFile($path . '/forms/action.xml');
}
Expand All @@ -65,13 +69,14 @@ protected function enhanceWorkflowTransitionForm(Form $form, $data)
*/
protected function getWorkflow(int $workflowId = null)
{
$workflowId = !empty($workflowId) ? $workflowId : $this->app->getInput()->getInt('workflow_id');
$app = $this->getApplication() ?? $this->app;
$workflowId = !empty($workflowId) ? $workflowId : $app->getInput()->getInt('workflow_id');

if (is_array($workflowId)) {
return false;
}

return $this->app->bootComponent('com_workflow')
return $app->bootComponent('com_workflow')
->getMVCFactory()
->createModel('Workflow', 'Administrator', ['ignore_request' => true])
->getItem($workflowId);
Expand Down Expand Up @@ -134,7 +139,7 @@ protected function checkExtensionSupport($context, $functionality)
{
$parts = explode('.', $context);

$component = $this->app->bootComponent($parts[0]);
$component = ($this->getApplication() ?? $this->app)->bootComponent($parts[0]);

if (
!$component instanceof WorkflowServiceInterface
Expand Down
4 changes: 3 additions & 1 deletion plugins/workflow/featuring/featuring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_WORKFLOW_FEATURING_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Workflow\Featuring</namespace>
<files>
<filename plugin="featuring">featuring.php</filename>
<folder>forms</folder>
<folder plugin="featuring">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_workflow_featuring.ini</language>
Expand Down
47 changes: 47 additions & 0 deletions plugins/workflow/featuring/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Workflow.featuring
*
* @copyright (C) 2023 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\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Workflow\Featuring\Extension\Featuring;

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): void
{
$container->set(
PluginInterface::class,
function (Container $container) {
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new Featuring(
$dispatcher,
(array) PluginHelper::getPlugin('workflow', 'featuring')
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

/**
* @package Joomla.Plugin
* @subpackage Workflow.Featuring
* @subpackage Workflow.featuring
*
* @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

use Joomla\CMS\Application\CMSWebApplicationInterface;
namespace Joomla\Plugin\Workflow\Featuring\Extension;

use Joomla\CMS\Event\AbstractEvent;
use Joomla\CMS\Event\Table\BeforeStoreEvent;
use Joomla\CMS\Event\View\DisplayEvent;
Expand Down Expand Up @@ -39,7 +38,7 @@
*
* @since 4.0.0
*/
class PlgWorkflowFeaturing extends CMSPlugin implements SubscriberInterface
final class Featuring extends CMSPlugin implements SubscriberInterface
{
use WorkflowPluginTrait;

Expand All @@ -51,14 +50,6 @@ class PlgWorkflowFeaturing extends CMSPlugin implements SubscriberInterface
*/
protected $autoloadLanguage = true;

/**
* Loads the CMS Application for direct access
*
* @var CMSWebApplicationInterface
* @since 4.0.0
*/
protected $app;

/**
* The name of the supported functionality to check against
*
Expand Down Expand Up @@ -134,11 +125,11 @@ protected function enhanceItemForm(Form $form, $data)

$parts = explode('.', $context);

$component = $this->app->bootComponent($parts[0]);
$component = $this->getApplication()->bootComponent($parts[0]);

$modelName = $component->getModelName($context);

$table = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), ['ignore_request' => true])
$table = $component->getMVCFactory()->createModel($modelName, $this->getApplication()->getName(), ['ignore_request' => true])
->getTable();

$fieldname = $table->getColumnAlias('featured');
Expand Down Expand Up @@ -192,7 +183,7 @@ protected function enhanceItemForm(Form $form, $data)
*/
public function onAfterDisplay(DisplayEvent $event)
{
if (!$this->app->isClient('administrator')) {
if (!$this->getApplication()->isClient('administrator')) {
return;
}

Expand Down Expand Up @@ -234,7 +225,7 @@ public function onAfterDisplay(DisplayEvent $event)
});
";

$this->app->getDocument()->addScriptDeclaration($js);
$this->getApplication()->getDocument()->addScriptDeclaration($js);

return true;
}
Expand Down Expand Up @@ -269,10 +260,10 @@ public function onWorkflowBeforeTransition(WorkflowTransitionEvent $event)
* Execute the normal "onContentBeforeChangeFeatured" plugins. But they could cancel the execution,
* So we have to precheck and cancel the whole transition stuff if not allowed.
*/
$this->app->set('plgWorkflowFeaturing.' . $context, $pks);
$this->getApplication()->set('plgWorkflowFeaturing.' . $context, $pks);

// Trigger the change state event.
$eventResult = $this->app->getDispatcher()->dispatch(
$eventResult = $this->getApplication()->getDispatcher()->dispatch(
'onContentBeforeChangeFeatured',
AbstractEvent::create(
'onContentBeforeChangeFeatured',
Expand All @@ -289,7 +280,7 @@ public function onWorkflowBeforeTransition(WorkflowTransitionEvent $event)
);

// Release allowed pks, the job is done
$this->app->set('plgWorkflowFeaturing.' . $context, []);
$this->getApplication()->set('plgWorkflowFeaturing.' . $context, []);

if ($eventResult->getArgument('abort')) {
$event->setStopTransition();
Expand Down Expand Up @@ -320,7 +311,7 @@ public function onWorkflowAfterTransition(WorkflowTransitionEvent $event): void
return;
}

$component = $this->app->bootComponent($extensionName);
$component = $this->getApplication()->bootComponent($extensionName);

$value = $transition->options->get('featuring');

Expand All @@ -336,7 +327,7 @@ public function onWorkflowAfterTransition(WorkflowTransitionEvent $event): void

$modelName = $component->getModelName($context);

$model = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), $options);
$model = $component->getMVCFactory()->createModel($modelName, $this->getApplication()->getName(), $options);

$model->featured($pks, $value);
}
Expand All @@ -362,7 +353,7 @@ public function onContentBeforeChangeFeatured(FeatureEvent $event)

// We have allowed the pks, so we're the one who triggered
// With onWorkflowBeforeTransition => free pass
if ($this->app->get('plgWorkflowFeaturing.' . $extension) === $pks) {
if ($this->getApplication()->get('plgWorkflowFeaturing.' . $extension) === $pks) {
return true;
}

Expand Down Expand Up @@ -403,7 +394,10 @@ public function onContentBeforeSave(EventInterface $event)
* As we're setting the field to disabled, no value should be there at all
*/
if (isset($data[$keyName])) {
$this->app->enqueueMessage(Text::_('PLG_WORKFLOW_FEATURING_CHANGE_STATE_NOT_ALLOWED'), 'error');
$this->getApplication()->enqueueMessage(
$this->getApplication()->getLanguage()->_('PLG_WORKFLOW_FEATURING_CHANGE_STATE_NOT_ALLOWED'),
'error'
);

return false;
}
Expand Down Expand Up @@ -431,11 +425,11 @@ public function onContentVersioningPrepareTable(EventInterface $event)

$parts = explode('.', $context);

$component = $this->app->bootComponent($parts[0]);
$component = $this->getApplication()->bootComponent($parts[0]);

$modelName = $component->getModelName($context);

$model = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), ['ignore_request' => true]);
$model = $component->getMVCFactory()->createModel($modelName, $this->getApplication()->getName(), ['ignore_request' => true]);

$table = $model->getTable();

Expand Down Expand Up @@ -467,11 +461,11 @@ public function onTableBeforeStore(BeforeStoreEvent $event)
return;
}

$component = $this->app->bootComponent($parts[0]);
$component = $this->getApplication()->bootComponent($parts[0]);

$modelName = $component->getModelName($typeAlias);

$model = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), ['ignore_request' => true]);
$model = $component->getMVCFactory()->createModel($modelName, $this->getApplication()->getName(), ['ignore_request' => true]);

$table = $model->getTable();

Expand Down Expand Up @@ -506,7 +500,7 @@ protected function isSupported($context)
return false;
}

$component = $this->app->bootComponent($parts[0]);
$component = $this->getApplication()->bootComponent($parts[0]);

if (
!$component instanceof WorkflowServiceInterface
Expand All @@ -518,7 +512,7 @@ protected function isSupported($context)

$modelName = $component->getModelName($context);

$model = $component->getMVCFactory()->createModel($modelName, $this->app->getName(), ['ignore_request' => true]);
$model = $component->getMVCFactory()->createModel($modelName, $this->getApplication()->getName(), ['ignore_request' => true]);

if (!$model instanceof DatabaseModelInterface || !method_exists($model, 'featured')) {
return false;
Expand Down
4 changes: 3 additions & 1 deletion plugins/workflow/notification/notification.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_WORKFLOW_NOTIFICATION_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Workflow\Notification</namespace>
<files>
<filename plugin="notification">notification.php</filename>
<folder>forms</folder>
<folder plugin="notification">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_workflow_notification.ini</language>
Expand Down
52 changes: 52 additions & 0 deletions plugins/workflow/notification/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Workflow.notification
*
* @copyright (C) 2023 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\Factory;
use Joomla\CMS\Language\LanguageFactoryInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\User\UserFactoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Workflow\Notification\Extension\Notification;

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): void
{
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new Notification(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('workflow', 'notification'),
$container->get(LanguageFactoryInterface::class)
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));
$plugin->setUserFactory($container->get(UserFactoryInterface::class));

return $plugin;
}
);
}
};
Loading

0 comments on commit 83ef3c2

Please sign in to comment.