29
0
mirror of https://github.com/joomla/joomla-cms.git synced 2024-07-03 02:23:53 +00:00

Converts local filesystem plugin to service providers (#39642)

This commit is contained in:
Allon Moritz 2023-02-17 18:25:54 +01:00 committed by GitHub
parent 3a1114d736
commit 9d73496bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 227 additions and 26 deletions

View File

@ -11,7 +11,7 @@
<description>PLG_FILESYSTEM_LOCAL_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Filesystem\Local</namespace>
<files>
<filename plugin="local">local.php</filename>
<folder plugin="local">services</folder>
<folder>src</folder>
</files>

View File

@ -0,0 +1,47 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Filesystem.local
*
* @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\Filesystem\Local\Extension\Local;
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 Local(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('filesystem', 'local'),
JPATH_ROOT
);
$plugin->setApplication(Factory::getApplication());
return $plugin;
}
);
}
};

View File

@ -2,7 +2,7 @@
/**
* @package Joomla.Plugin
* @subpackage Filesystem.Local
* @subpackage Filesystem.local
*
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt

View File

@ -2,18 +2,19 @@
/**
* @package Joomla.Plugin
* @subpackage FileSystem.Local
* @subpackage FileSystem.local
*
* @copyright (C) 2017 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\Language\Text;
namespace Joomla\Plugin\Filesystem\Local\Extension;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Media\Administrator\Event\MediaProviderEvent;
use Joomla\Component\Media\Administrator\Provider\ProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
@ -26,7 +27,7 @@ use Joomla\Component\Media\Administrator\Provider\ProviderInterface;
*
* @since 4.0.0
*/
class PlgFileSystemLocal extends CMSPlugin implements ProviderInterface
final class Local extends CMSPlugin implements ProviderInterface
{
/**
* Affects constructor behavior. If true, language files will be loaded automatically.
@ -35,6 +36,29 @@ class PlgFileSystemLocal extends CMSPlugin implements ProviderInterface
* @since 4.0.0
*/
protected $autoloadLanguage = true;
/**
* The root directory path
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $rootDirectory;
/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param string $rootDirectory The root directory to look for images
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DispatcherInterface $dispatcher, array $config, string $rootDirectory)
{
parent::__construct($dispatcher, $config);
$this->rootDirectory = $rootDirectory;
}
/**
* Setup Providers for Local Adapter
@ -71,7 +95,7 @@ class PlgFileSystemLocal extends CMSPlugin implements ProviderInterface
*/
public function getDisplayName()
{
return Text::_('PLG_FILESYSTEM_LOCAL_DEFAULT_NAME');
return $this->getApplication()->getLanguage()->_('PLG_FILESYSTEM_LOCAL_DEFAULT_NAME');
}
/**
@ -86,30 +110,31 @@ class PlgFileSystemLocal extends CMSPlugin implements ProviderInterface
$adapters = [];
$directories = $this->params->get('directories', '[{"directory": "images", "thumbs": 0}]');
// Do a check if default settings are not saved by user
// If not initialize them manually
// Do a check if default settings are not saved by user, if not initialize them manually
if (is_string($directories)) {
$directories = json_decode($directories);
}
foreach ($directories as $directoryEntity) {
if ($directoryEntity->directory) {
$directoryPath = JPATH_ROOT . '/' . $directoryEntity->directory;
$directoryPath = rtrim($directoryPath) . '/';
if (!isset($directoryEntity->thumbs)) {
$directoryEntity->thumbs = 0;
}
$adapter = new \Joomla\Plugin\Filesystem\Local\Adapter\LocalAdapter(
$directoryPath,
$directoryEntity->directory,
$directoryEntity->thumbs,
[200, 200]
);
$adapters[$adapter->getAdapterName()] = $adapter;
if (!$directoryEntity->directory) {
continue;
}
$directoryPath = $this->rootDirectory . '/' . $directoryEntity->directory;
$directoryPath = rtrim($directoryPath) . '/';
if (!isset($directoryEntity->thumbs)) {
$directoryEntity->thumbs = 0;
}
$adapter = new LocalAdapter(
$directoryPath,
$directoryEntity->directory,
$directoryEntity->thumbs,
[200, 200]
);
$adapters[$adapter->getAdapterName()] = $adapter;
}
return $adapters;

View File

@ -0,0 +1,129 @@
<?php
/**
* @package Joomla.UnitTest
* @subpackage Extension
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Tests\Unit\Plugin\Filesystem\Local\Extension;
use InvalidArgumentException;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Language\Language;
use Joomla\Component\Media\Administrator\Event\MediaProviderEvent;
use Joomla\Component\Media\Administrator\Provider\ProviderManager;
use Joomla\Event\Dispatcher;
use Joomla\Plugin\Filesystem\Local\Extension\Local;
use Joomla\Tests\Unit\UnitTestCase;
/**
* Test class for Local plugin
*
* @package Joomla.UnitTest
* @subpackage Local
*
* @testdox The Local plugin
*
* @since __DEPLOY_VERSION__
*/
class LocalPluginTest extends UnitTestCase
{
/**
* @testdox has the correct id
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testID()
{
$dispatcher = new Dispatcher();
$plugin = new Local($dispatcher, ['name' => 'test'], __DIR__);
$this->assertEquals('test', $plugin->getID());
}
/**
* @testdox has the correct display name
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testDisplayName()
{
$dispatcher = new Dispatcher();
$language = $this->createStub(Language::class);
$language->method('_')->willReturn('test');
$app = $this->createStub(CMSApplicationInterface::class);
$app->method('getLanguage')->willReturn($language);
$plugin = new Local($dispatcher, [], __DIR__);
$plugin->setApplication($app);
$this->assertEquals('test', $plugin->getDisplayName());
}
/**
* @testdox can setup providers
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testSetupProviders()
{
$dispatcher = new Dispatcher();
$manager = new ProviderManager();
$event = new MediaProviderEvent('test');
$event->setProviderManager($manager);
$plugin = new Local($dispatcher, ['name' => 'test'], __DIR__);
$plugin->onSetupProviders($event);
$this->assertEquals(['test' => $plugin], $manager->getProviders());
$this->assertEquals($plugin, $manager->getProvider('test'));
}
/**
* @testdox can deliver adapters
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testAdapters()
{
$dispatcher = new Dispatcher();
$plugin = new Local($dispatcher, ['params' => ['directories' => '[{"directory": "tests"}]']], JPATH_ROOT);
$adapters = $plugin->getAdapters();
$this->assertCount(1, $adapters);
$this->assertEquals('tests', $adapters['tests']->getAdapterName());
}
/**
* @testdox throws an Exception when an invalid directory
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function testAdaptersInvalidDirectoy()
{
$this->expectException(InvalidArgumentException::class);
$dispatcher = new Dispatcher();
$plugin = new Local($dispatcher, ['params' => ['directories' => '[{"directory": "invalid"}]']], __DIR__);
$plugin->getAdapters();
}
}