Skip to content

Commit e6173bd

Browse files
authored
[5.2] Media manager fix default adapter handling (joomla#44313)
1 parent 49f6c13 commit e6173bd

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage com_media
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Component\Media\Administrator\Exception;
12+
13+
// phpcs:disable PSR1.Files.SideEffects
14+
\defined('_JEXEC') or die;
15+
// phpcs:enable PSR1.Files.SideEffects
16+
17+
/**
18+
* Provider Account is empty or not set exception.
19+
*
20+
* @since __DEPLOY_VERSION__
21+
*/
22+
class ProviderAccountIsEmptyException extends \Exception
23+
{
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage com_media
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Component\Media\Administrator\Exception;
12+
13+
// phpcs:disable PSR1.Files.SideEffects
14+
\defined('_JEXEC') or die;
15+
// phpcs:enable PSR1.Files.SideEffects
16+
17+
/**
18+
* Provider Account not found exception.
19+
*
20+
* @since __DEPLOY_VERSION__
21+
*/
22+
class ProviderAccountNotFoundException extends \Exception
23+
{
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Administrator
5+
* @subpackage com_media
6+
*
7+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
namespace Joomla\Component\Media\Administrator\Exception;
12+
13+
// phpcs:disable PSR1.Files.SideEffects
14+
\defined('_JEXEC') or die;
15+
// phpcs:enable PSR1.Files.SideEffects
16+
17+
/**
18+
* Provider not found exception.
19+
*
20+
* @since __DEPLOY_VERSION__
21+
*/
22+
class ProviderNotFoundException extends \Exception
23+
{
24+
}

administrator/components/com_media/src/Provider/ProviderManager.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
use Joomla\CMS\Language\Text;
1414
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
15+
use Joomla\Component\Media\Administrator\Exception\ProviderAccountIsEmptyException;
16+
use Joomla\Component\Media\Administrator\Exception\ProviderAccountNotFoundException;
17+
use Joomla\Component\Media\Administrator\Exception\ProviderNotFoundException;
1518

1619
// phpcs:disable PSR1.Files.SideEffects
1720
\defined('_JEXEC') or die;
@@ -97,7 +100,7 @@ public function unregisterProvider(?ProviderInterface $provider = null): void
97100
public function getProvider($id)
98101
{
99102
if (!isset($this->providers[$id])) {
100-
throw new \Exception(Text::_('COM_MEDIA_ERROR_MEDIA_PROVIDER_NOT_FOUND'));
103+
throw new ProviderNotFoundException(Text::_('COM_MEDIA_ERROR_MEDIA_PROVIDER_NOT_FOUND'));
101104
}
102105

103106
return $this->providers[$id];
@@ -119,13 +122,13 @@ public function getAdapter($name)
119122
list($provider, $account) = array_pad(explode('-', $name, 2), 2, null);
120123

121124
if ($account == null) {
122-
throw new \Exception(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_SET'));
125+
throw new ProviderAccountIsEmptyException(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_SET'));
123126
}
124127

125128
$adapters = $this->getProvider($provider)->getAdapters();
126129

127130
if (!isset($adapters[$account])) {
128-
throw new \Exception(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_FOUND'));
131+
throw new ProviderAccountNotFoundException(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_FOUND'));
129132
}
130133

131134
return $adapters[$account];

administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Joomla\CMS\Plugin\PluginHelper;
1717
use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
1818
use Joomla\Component\Media\Administrator\Event\MediaProviderEvent;
19+
use Joomla\Component\Media\Administrator\Exception\ProviderAccountNotFoundException;
1920

2021
// phpcs:disable PSR1.Files.SideEffects
2122
\defined('_JEXEC') or die;
@@ -148,14 +149,19 @@ protected function getDefaultAdapterName(): ?string
148149
return $this->defaultAdapterName;
149150
}
150151

151-
$defaultAdapter = $this->getAdapter('local-' . ComponentHelper::getParams('com_media')->get('file_path', 'images'));
152+
try {
153+
// Check for default path in Media config, and whether associated account exists, and use it as default adapter.
154+
$defaultFilePath = ComponentHelper::getParams('com_media')->get('file_path', 'images');
155+
$defaultAdapter = $this->getAdapter('local-' . $defaultFilePath);
156+
// @TODO: Need a proper configuration for default adapter.
157+
} catch (ProviderAccountNotFoundException $e) {
158+
$defaultAdapter = null;
159+
}
152160

153-
if (
154-
!$defaultAdapter
155-
&& $this->getProviderManager()->getProvider('local')
156-
&& $this->getProviderManager()->getProvider('local')->getAdapters()
157-
) {
158-
$defaultAdapter = $this->getProviderManager()->getProvider('local')->getAdapters()[0];
161+
if (!$defaultAdapter) {
162+
// Default adapter were not found, pick the first available local adapter
163+
$localAdapters = $this->getProviderManager()->getProvider('local')->getAdapters();
164+
$defaultAdapter = $localAdapters ? reset($localAdapters) : null;
159165
}
160166

161167
if (!$defaultAdapter) {

0 commit comments

Comments
 (0)