Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.1] Adapter trait unification #36097

Merged
merged 15 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 2 additions & 36 deletions administrator/components/com_media/src/Model/ApiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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().
Expand Down
19 changes: 5 additions & 14 deletions administrator/components/com_media/src/Model/MediaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +21,8 @@
*/
class MediaModel extends BaseDatabaseModel
{
use ProviderManagerHelperTrait;

/**
* Obtain list of supported providers
*
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,7 +24,7 @@
*
* @since __DEPLOY_VERSION__
*/
trait AdapterTrait
trait ProviderManagerHelperTrait
{
/**
* Holds the available media file adapters.
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
Expand All @@ -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
laoneo marked this conversation as resolved.
Show resolved Hide resolved
*
* @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)
laoneo marked this conversation as resolved.
Show resolved Hide resolved
{
$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.
*
Expand All @@ -118,7 +140,7 @@ private function getAdapter(String $name): AdapterInterface
*
* @since __DEPLOY_VERSION__
*/
private function getDefaultAdapterName(): ?string
protected function getDefaultAdapterName(): ?string
{
if ($this->defaultAdapterName)
{
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,7 +22,7 @@
*/
class AdaptersController extends ApiController
{
use AdapterTrait;
use ProviderManagerHelperTrait;

/**
* The content type of the item.
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_media/src/Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +30,7 @@
*/
class MediaController extends ApiController
{
use AdapterTrait;
use ProviderManagerHelperTrait;

/**
* The content type of the item.
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_media/src/Model/AdapterModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,7 +21,7 @@
*/
class AdapterModel extends BaseModel
{
use AdapterTrait;
use ProviderManagerHelperTrait;

/**
* Method to get a single adapter.
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_media/src/Model/AdaptersModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_media/src/Model/MediaModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,7 +27,7 @@
*/
class MediaModel extends BaseModel implements ListModelInterface
{
use AdapterTrait;
use ProviderManagerHelperTrait;

/**
* Instance of com_media's ApiModel
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_media/src/Model/MediumModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -28,7 +28,7 @@
*/
class MediumModel extends BaseModel
{
use AdapterTrait;
use ProviderManagerHelperTrait;

/**
* Instance of com_media's ApiModel
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_media/src/View/Adapters/JsonapiView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +21,7 @@
*/
class JsonapiView extends BaseApiView
{
use AdapterTrait;
use ProviderManagerHelperTrait;

/**
* The fields to render item in the documents
Expand Down
Loading