diff --git a/administrator/components/com_media/src/Model/ApiModel.php b/administrator/components/com_media/src/Model/ApiModel.php index f0808f81f76c4..473b543754cdd 100644 --- a/administrator/components/com_media/src/Model/ApiModel.php +++ b/administrator/components/com_media/src/Model/ApiModel.php @@ -17,15 +17,13 @@ use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Plugin\PluginHelper; -use Joomla\Component\Media\Administrator\Adapter\AdapterInterface; use Joomla\Component\Media\Administrator\Event\FetchMediaItemEvent; use Joomla\Component\Media\Administrator\Event\FetchMediaItemsEvent; use Joomla\Component\Media\Administrator\Event\FetchMediaItemUrlEvent; -use Joomla\Component\Media\Administrator\Event\MediaProviderEvent; use Joomla\Component\Media\Administrator\Exception\FileExistsException; use Joomla\Component\Media\Administrator\Exception\FileNotFoundException; use Joomla\Component\Media\Administrator\Exception\InvalidPathException; -use Joomla\Component\Media\Administrator\Provider\ProviderManager; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Api Model @@ -34,13 +32,7 @@ */ class ApiModel extends BaseDatabaseModel { - /** - * Holds the available media file adapters. - * - * @var ProviderManager - * @since 4.0.0 - */ - private $providerManager = null; + use ProviderManagerHelperTrait; /** * The available extensions. @@ -50,32 +42,6 @@ class ApiModel extends BaseDatabaseModel */ private $allowedExtensions = null; - /** - * Return the requested adapter - * - * @param string $name Name of the provider - * - * @since 4.0.0 - * @return AdapterInterface - * - * @throws \Exception - */ - private function getAdapter($name) - { - if ($this->providerManager == null) - { - $this->providerManager = new ProviderManager; - - // Fire the event to get the results - $eventParameters = ['context' => 'AdapterManager', 'providerManager' => $this->providerManager]; - $event = new MediaProviderEvent('onSetupProviders', $eventParameters); - PluginHelper::importPlugin('filesystem'); - Factory::getApplication()->triggerEvent('onSetupProviders', $event); - } - - return $this->providerManager->getAdapter($name); - } - /** * Returns the requested file or folder information. More information * can be found in AdapterInterface::getFile(). diff --git a/administrator/components/com_media/src/Model/MediaModel.php b/administrator/components/com_media/src/Model/MediaModel.php index 01a49be87fd10..257113bc153fe 100644 --- a/administrator/components/com_media/src/Model/MediaModel.php +++ b/administrator/components/com_media/src/Model/MediaModel.php @@ -11,11 +11,8 @@ \defined('_JEXEC') or die; -use Joomla\CMS\Factory; use Joomla\CMS\MVC\Model\BaseDatabaseModel; -use Joomla\CMS\Plugin\PluginHelper; -use Joomla\Component\Media\Administrator\Event\MediaProviderEvent; -use Joomla\Component\Media\Administrator\Provider\ProviderManager; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media View Model @@ -24,6 +21,8 @@ */ class MediaModel extends BaseDatabaseModel { + use ProviderManagerHelperTrait; + /** * Obtain list of supported providers * @@ -33,17 +32,9 @@ class MediaModel extends BaseDatabaseModel */ public function getProviders() { - // Setup provider manager and event parameters - $providerManager = new ProviderManager; - $eventParameters = ['context' => 'AdapterManager', 'providerManager' => $providerManager]; - $event = new MediaProviderEvent('onSetupProviders', $eventParameters); - $results = []; - - // Import plugin group and fire the event - PluginHelper::importPlugin('filesystem'); - Factory::getApplication()->triggerEvent('onSetupProviders', $event); + $results = []; - foreach ($providerManager->getProviders() as $provider) + foreach ($this->getProviderManager()->getProviders() as $provider) { $result = new \stdClass; $result->name = $provider->getID(); diff --git a/api/components/com_media/src/Helper/AdapterTrait.php b/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php similarity index 85% rename from api/components/com_media/src/Helper/AdapterTrait.php rename to administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php index 6c1297a25f3d7..87bb64afebd92 100644 --- a/api/components/com_media/src/Helper/AdapterTrait.php +++ b/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php @@ -7,7 +7,7 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ -namespace Joomla\Component\Media\Api\Helper; +namespace Joomla\Component\Media\Administrator\Provider; \defined('_JEXEC') or die; @@ -24,7 +24,7 @@ * * @since 4.1.0 */ -trait AdapterTrait +trait ProviderManagerHelperTrait { /** * Holds the available media file adapters. @@ -45,40 +45,25 @@ trait AdapterTrait private $defaultAdapterName = null; /** - * Returns an array with the adapter name as key and the path of the file. - * - * @return array + * Return a provider manager. * - * @throws \Exception + * @return ProviderManager * * @since 4.1.0 */ - private function resolveAdapterAndPath(String $path): array + public function getProviderManager(): ProviderManager { - $result = []; - $parts = explode(':', $path, 2); - - // If we have 2 parts, we have both an adapter name and a file path - if (\count($parts) == 2) - { - $result['adapter'] = $parts[0]; - $result['path'] = $parts[1]; - - return $result; - } - - if (!$this->getDefaultAdapterName()) + if (!$this->providerManager) { - throw new \InvalidArgumentException('No adapter found'); + // Fire the event to get the results + $eventParameters = ['context' => 'AdapterManager', 'providerManager' => new ProviderManager]; + $event = new MediaProviderEvent('onSetupProviders', $eventParameters); + PluginHelper::importPlugin('filesystem'); + Factory::getApplication()->triggerEvent('onSetupProviders', $event); + $this->providerManager = $event->getProviderManager(); } - // If we have less than 2 parts, we return a default adapter name - $result['adapter'] = $this->getDefaultAdapterName(); - - // If we have 1 part, we return it as the path. Otherwise we return a default path - $result['path'] = \count($parts) ? $parts[0] : '/'; - - return $result; + return $this->providerManager; } /** @@ -90,7 +75,7 @@ private function resolveAdapterAndPath(String $path): array * * @since 4.1.0 */ - private function getProvider(String $id): ProviderInterface + public function getProvider(String $id): ProviderInterface { return $this->getProviderManager()->getProvider($id); } @@ -104,11 +89,48 @@ private function getProvider(String $id): ProviderInterface * * @since 4.1.0 */ - private function getAdapter(String $name): AdapterInterface + public function getAdapter(String $name): AdapterInterface { return $this->getProviderManager()->getAdapter($name); } + /** + * Returns an array with the adapter name as key and the path of the file. + * + * @return array + * + * @throws \InvalidArgumentException + * + * @since __DEPLOY_VERSION__ + */ + protected function resolveAdapterAndPath(String $path): array + { + $result = []; + $parts = explode(':', $path, 2); + + // If we have 2 parts, we have both an adapter name and a file path + if (\count($parts) === 2) + { + $result['adapter'] = $parts[0]; + $result['path'] = $parts[1]; + + return $result; + } + + if (!$this->getDefaultAdapterName()) + { + throw new \InvalidArgumentException('No adapter found'); + } + + // If we have less than 2 parts, we return a default adapter name + $result['adapter'] = $this->getDefaultAdapterName(); + + // If we have 1 part, we return it as the path. Otherwise we return a default path + $result['path'] = \count($parts) ? $parts[0] : '/'; + + return $result; + } + /** * Returns the default adapter name. * @@ -118,7 +140,7 @@ private function getAdapter(String $name): AdapterInterface * * @since 4.1.0 */ - private function getDefaultAdapterName(): ?string + protected function getDefaultAdapterName(): ?string { if ($this->defaultAdapterName) { @@ -143,27 +165,4 @@ private function getDefaultAdapterName(): ?string return $this->defaultAdapterName; } - - /** - * Return a provider manager. - * - * @return ProviderManager - * - * @since 4.1.0 - */ - private function getProviderManager(): ProviderManager - { - if (!$this->providerManager) - { - $this->providerManager = new ProviderManager; - - // Fire the event to get the results - $eventParameters = ['context' => 'AdapterManager', 'providerManager' => $this->providerManager]; - $event = new MediaProviderEvent('onSetupProviders', $eventParameters); - PluginHelper::importPlugin('filesystem'); - Factory::getApplication()->triggerEvent('onSetupProviders', $event); - } - - return $this->providerManager; - } } diff --git a/api/components/com_media/src/Controller/AdaptersController.php b/api/components/com_media/src/Controller/AdaptersController.php index 1831173a1ce72..a48950a03070d 100644 --- a/api/components/com_media/src/Controller/AdaptersController.php +++ b/api/components/com_media/src/Controller/AdaptersController.php @@ -13,7 +13,7 @@ use Joomla\CMS\MVC\Controller\ApiController; use Joomla\Component\Media\Administrator\Exception\InvalidPathException; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service controller. @@ -22,7 +22,7 @@ */ class AdaptersController extends ApiController { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * The content type of the item. diff --git a/api/components/com_media/src/Controller/MediaController.php b/api/components/com_media/src/Controller/MediaController.php index a94ade2e02e03..e12c4c46db3a4 100644 --- a/api/components/com_media/src/Controller/MediaController.php +++ b/api/components/com_media/src/Controller/MediaController.php @@ -18,7 +18,7 @@ use Joomla\CMS\MVC\Controller\ApiController; use Joomla\Component\Media\Administrator\Exception\FileExistsException; use Joomla\Component\Media\Administrator\Exception\InvalidPathException; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; use Joomla\Component\Media\Api\Model\MediumModel; use Joomla\String\Inflector; use Tobscure\JsonApi\Exception\InvalidParameterException; @@ -30,7 +30,7 @@ */ class MediaController extends ApiController { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * The content type of the item. diff --git a/api/components/com_media/src/Model/AdapterModel.php b/api/components/com_media/src/Model/AdapterModel.php index 4141a6c3cdccc..2ddd9743ec657 100644 --- a/api/components/com_media/src/Model/AdapterModel.php +++ b/api/components/com_media/src/Model/AdapterModel.php @@ -12,7 +12,7 @@ \defined('_JEXEC') or die; use Joomla\CMS\MVC\Model\BaseModel; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service model supporting a single adapter item. @@ -21,7 +21,7 @@ */ class AdapterModel extends BaseModel { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * Method to get a single adapter. diff --git a/api/components/com_media/src/Model/AdaptersModel.php b/api/components/com_media/src/Model/AdaptersModel.php index db3f885057cfe..c102c175ce168 100644 --- a/api/components/com_media/src/Model/AdaptersModel.php +++ b/api/components/com_media/src/Model/AdaptersModel.php @@ -14,7 +14,7 @@ use Joomla\CMS\MVC\Model\BaseModel; use Joomla\CMS\MVC\Model\ListModelInterface; use Joomla\CMS\Pagination\Pagination; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service model supporting lists of media adapters. @@ -23,7 +23,7 @@ */ class AdaptersModel extends BaseModel implements ListModelInterface { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * A hacky way to enable the standard jsonapiView::displayList() to create a Pagination object, diff --git a/api/components/com_media/src/Model/MediaModel.php b/api/components/com_media/src/Model/MediaModel.php index 50d37fa8e9855..ef0612191072b 100644 --- a/api/components/com_media/src/Model/MediaModel.php +++ b/api/components/com_media/src/Model/MediaModel.php @@ -18,7 +18,7 @@ use Joomla\CMS\Pagination\Pagination; use Joomla\Component\Media\Administrator\Exception\FileNotFoundException; use Joomla\Component\Media\Administrator\Model\ApiModel; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service model supporting lists of media items. @@ -27,7 +27,7 @@ */ class MediaModel extends BaseModel implements ListModelInterface { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * Instance of com_media's ApiModel diff --git a/api/components/com_media/src/Model/MediumModel.php b/api/components/com_media/src/Model/MediumModel.php index 1ba7da34c36cc..d2a9e651848e9 100644 --- a/api/components/com_media/src/Model/MediumModel.php +++ b/api/components/com_media/src/Model/MediumModel.php @@ -19,7 +19,7 @@ use Joomla\Component\Media\Administrator\Exception\FileNotFoundException; use Joomla\Component\Media\Administrator\Exception\InvalidPathException; use Joomla\Component\Media\Administrator\Model\ApiModel; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service model supporting a single media item. @@ -28,7 +28,7 @@ */ class MediumModel extends BaseModel { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * Instance of com_media's ApiModel diff --git a/api/components/com_media/src/View/Adapters/JsonapiView.php b/api/components/com_media/src/View/Adapters/JsonapiView.php index 7f4e7a3de084c..7c059674a9aa9 100644 --- a/api/components/com_media/src/View/Adapters/JsonapiView.php +++ b/api/components/com_media/src/View/Adapters/JsonapiView.php @@ -12,7 +12,7 @@ \defined('_JEXEC') or die; use Joomla\CMS\MVC\View\JsonApiView as BaseApiView; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service view @@ -21,7 +21,7 @@ */ class JsonapiView extends BaseApiView { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * The fields to render item in the documents diff --git a/api/components/com_media/src/View/Media/JsonapiView.php b/api/components/com_media/src/View/Media/JsonapiView.php index 69169719fae24..bc8d0db67ccec 100644 --- a/api/components/com_media/src/View/Media/JsonapiView.php +++ b/api/components/com_media/src/View/Media/JsonapiView.php @@ -12,7 +12,7 @@ \defined('_JEXEC') or die; use Joomla\CMS\MVC\View\JsonApiView as BaseApiView; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service view @@ -21,7 +21,7 @@ */ class JsonapiView extends BaseApiView { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * The fields to render item in the documents