Skip to content

Commit

Permalink
[4.2] Change task plugin to service provider (#37954)
Browse files Browse the repository at this point in the history
* Change task plugin to service provider

* typo

* tabs

* Update plugins/task/requests/src/Extension/Requests.php

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

* version

* Lookup form without reflection

Co-authored-by: heelc29 <66922325+heelc29@users.noreply.github.com>
  • Loading branch information
laoneo and heelc29 authored Jun 8, 2022
1 parent c297caa commit d56377b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status;
use Joomla\Event\EventInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Utilities\ArrayHelper;

/**
Expand Down Expand Up @@ -146,7 +145,7 @@ public function enhanceTaskItemForm($context, $data = null): bool
}

// We expect the form XML in "{PLUGIN_PATH}/forms/{FORM_NAME}.xml"
$path = \dirname((new \ReflectionClass(static::class))->getFileName());
$path = JPATH_PLUGINS . '/' . $this->_type . '/' . $this->_name;
$enhancementFormFile = $path . '/forms/' . $enhancementFormName . '.xml';

try
Expand Down
5 changes: 3 additions & 2 deletions plugins/task/requests/requests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.1</version>
<description>PLG_TASK_REQUESTS_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Task\Requests</namespace>
<files>
<filename plugin="requests">requests.php</filename>
<folder>language</folder>
<folder plugin="requests">services</folder>
<folder>src</folder>
<folder>forms</folder>
</files>
<languages>
Expand Down
47 changes: 47 additions & 0 deletions plugins/task/requests/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Task.requests
*
* @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\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Http\HttpFactory;
use Joomla\Plugin\Task\Requests\Extension\Requests;

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)
{
$plugin = new Requests(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('task', 'requests'),
new HttpFactory
);

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\Task\Requests\Extension;

// Restrict direct access
defined('_JEXEC') or die;

use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Http\HttpFactory;
use Joomla\CMS\Language\Text;
use Exception;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status as TaskStatus;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Event\DispatcherInterface;
use Joomla\Event\SubscriberInterface;
use Joomla\Registry\Registry;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\Http\HttpFactory;

/**
* Task plugin with routines to make HTTP requests.
* At the moment, offers a single routine for GET requests.
*
* @since 4.1.0
*/
class PlgTaskRequests extends CMSPlugin implements SubscriberInterface
class Requests extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;

Expand All @@ -44,13 +46,7 @@ class PlgTaskRequests extends CMSPlugin implements SubscriberInterface
];

/**
* @var boolean
* @since 4.1.0
*/
protected $autoloadLanguage = true;

/**
* @inheritDoc
* Returns an array of events this subscriber will listen to.
*
* @return string[]
*
Expand All @@ -65,6 +61,44 @@ public static function getSubscribedEvents(): array
];
}

/**
* @var boolean
* @since 4.1.0
*/
protected $autoloadLanguage = true;

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

/**
* The http factory
*
* @var HttpFactory
* @since __DEPLOY_VERSION__
*/
private $httpFactory;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param HttpFactory $httpFactory The http factory
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DispatcherInterface $dispatcher, array $config, HttpFactory $httpFactory)
{
parent::__construct($dispatcher, $config);

$this->httpFactory = $httpFactory;
}

/**
* Standard routine method for the get request routine.
*
Expand Down Expand Up @@ -92,15 +126,13 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
$headers = [$authType => $authKey];
}

$options = new Registry;

try
{
$response = HttpFactory::getHttp($options)->get($url, $headers, $timeout);
$response = $this->httpFactory->getHttp([])->get($url, $headers, $timeout);
}
catch (Exception $e)
{
$this->logTask(Text::sprintf('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT'));
$this->logTask($this->app->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_TIMEOUT'));

return TaskStatus::TIMEOUT;
}
Expand All @@ -118,7 +150,7 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
}
else
{
$this->logTask('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT', 'error');
$this->logTask($this->app->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_UNWRITEABLE_OUTPUT'), 'error');
$responseStatus = 'NOT_SAVED';
}

Expand All @@ -129,7 +161,7 @@ protected function makeGetRequest(ExecuteTaskEvent $event): int
> Response: $responseStatus
EOF;

$this->logTask(Text::sprintf('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE', $responseCode));
$this->logTask(sprintf($this->app->getLanguage()->_('PLG_TASK_REQUESTS_TASK_GET_REQUEST_LOG_RESPONSE'), $responseCode));

if ($response->code !== 200)
{
Expand Down

0 comments on commit d56377b

Please sign in to comment.