From 5e06e179b205758fa3f91013dd91d6cb65ae555e Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Tue, 23 Nov 2021 20:44:35 +0100 Subject: [PATCH 1/3] Adapter trait unification --- .../com_media/src/Model/ApiModel.php | 38 +------ .../com_media/src/Model/MediaModel.php | 19 +--- .../Provider/ProviderManagerHelperTrait.php | 107 +++++++++--------- .../src/Controller/AdaptersController.php | 4 +- .../src/Controller/MediaController.php | 4 +- .../com_media/src/Model/AdapterModel.php | 4 +- .../com_media/src/Model/AdaptersModel.php | 4 +- .../com_media/src/Model/MediaModel.php | 4 +- .../com_media/src/Model/MediumModel.php | 4 +- .../src/View/Adapters/JsonapiView.php | 4 +- .../com_media/src/View/Media/JsonapiView.php | 5 +- 11 files changed, 76 insertions(+), 121 deletions(-) rename api/components/com_media/src/Helper/AdapterTrait.php => administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php (88%) diff --git a/administrator/components/com_media/src/Model/ApiModel.php b/administrator/components/com_media/src/Model/ApiModel.php index a2bd8b647bd42..80e93c2605182 100644 --- a/administrator/components/com_media/src/Model/ApiModel.php +++ b/administrator/components/com_media/src/Model/ApiModel.php @@ -17,12 +17,10 @@ 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\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 @@ -31,13 +29,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. @@ -47,32 +39,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 88% rename from api/components/com_media/src/Helper/AdapterTrait.php rename to administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php index 54155908cef06..003883fc9d4ca 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 __DEPLOY_VERSION__ */ -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 __DEPLOY_VERSION__ */ - 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 __DEPLOY_VERSION__ */ - 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 __DEPLOY_VERSION__ */ - 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 \Exception + * + * @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 __DEPLOY_VERSION__ */ - 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 __DEPLOY_VERSION__ - */ - 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 c8c23df127dc6..0332c08a8eddc 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 379d275f99be7..9d64d3a12943e 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 381306c9bb63f..9d532a2b54996 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 351b79ee9aba0..1978e88440350 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 572ec19e3e16f..be51696a740f2 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 9768526274bf9..d3ed631efe0f8 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 7a2d05b8b2117..e4735f3585f2e 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 84ee67bf32ede..95136e393617b 100644 --- a/api/components/com_media/src/View/Media/JsonapiView.php +++ b/api/components/com_media/src/View/Media/JsonapiView.php @@ -12,8 +12,7 @@ \defined('_JEXEC') or die; use Joomla\CMS\MVC\View\JsonApiView as BaseApiView; -use Joomla\Component\Media\Administrator\Provider\ProviderManager; -use Joomla\Component\Media\Api\Helper\AdapterTrait; +use Joomla\Component\Media\Administrator\Provider\ProviderManagerHelperTrait; /** * Media web service view @@ -22,7 +21,7 @@ */ class JsonapiView extends BaseApiView { - use AdapterTrait; + use ProviderManagerHelperTrait; /** * The fields to render item in the documents From cef9d10cbe35308acd10dad6765d1b2c22cb7006 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Wed, 24 Nov 2021 06:43:28 +0100 Subject: [PATCH 2/3] Update administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php Co-authored-by: Phil E. Taylor --- .../com_media/src/Provider/ProviderManagerHelperTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php b/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php index 003883fc9d4ca..147bfbdf70905 100644 --- a/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php +++ b/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php @@ -109,7 +109,7 @@ protected function resolveAdapterAndPath(String $path): array $parts = explode(':', $path, 2); // If we have 2 parts, we have both an adapter name and a file path - if (\count($parts) == 2) + if (\count($parts) === 2) { $result['adapter'] = $parts[0]; $result['path'] = $parts[1]; From f4afac759b01cfeacb7430880bbb10edc75bfe69 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Wed, 24 Nov 2021 06:44:33 +0100 Subject: [PATCH 3/3] Update administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php Co-authored-by: Phil E. Taylor --- .../com_media/src/Provider/ProviderManagerHelperTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php b/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php index 147bfbdf70905..f5ef1662b9870 100644 --- a/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php +++ b/administrator/components/com_media/src/Provider/ProviderManagerHelperTrait.php @@ -99,7 +99,7 @@ public function getAdapter(String $name): AdapterInterface * * @return array * - * @throws \Exception + * @throws \InvalidArgumentException * * @since __DEPLOY_VERSION__ */