[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/administrator/components/com_media/src/Provider/ -> ProviderManager.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Administrator
   5   * @subpackage  com_media
   6   *
   7   * @copyright   (C) 2017 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\Provider;
  12  
  13  use Joomla\CMS\Language\Text;
  14  use Joomla\Component\Media\Administrator\Adapter\AdapterInterface;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('_JEXEC') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Media Adapter Manager
  22   *
  23   * @since  4.0.0
  24   */
  25  class ProviderManager
  26  {
  27      /**
  28       * The array of providers
  29       *
  30       * @var  ProviderInterface[]
  31       *
  32       * @since  4.0.0
  33       */
  34      private $providers = [];
  35  
  36      /**
  37       * Returns an associative array of adapters with provider name as the key
  38       *
  39       * @return  ProviderInterface[]
  40       *
  41       * @since  4.0.0
  42       */
  43      public function getProviders()
  44      {
  45          return $this->providers;
  46      }
  47  
  48      /**
  49       * Register a provider into the ProviderManager
  50       *
  51       * @param   ProviderInterface  $provider  The provider to be registered
  52       *
  53       * @return  void
  54       *
  55       * @since  4.0.0
  56       */
  57      public function registerProvider(ProviderInterface $provider)
  58      {
  59          $this->providers[$provider->getID()] = $provider;
  60      }
  61  
  62      /**
  63       * Unregister a provider from the ProviderManager.
  64       * When no provider, or null is passed in, then all providers are cleared.
  65       *
  66       * @param   ProviderInterface|null  $provider  The provider to be unregistered
  67       *
  68       * @return  void
  69       *
  70       * @since   4.0.6
  71       */
  72      public function unregisterProvider(ProviderInterface $provider = null): void
  73      {
  74          if ($provider === null) {
  75              $this->providers = [];
  76              return;
  77          }
  78  
  79          if (!array_key_exists($provider->getID(), $this->providers)) {
  80              return;
  81          }
  82  
  83          unset($this->providers[$provider->getID()]);
  84      }
  85  
  86      /**
  87       * Returns the provider for a particular ID
  88       *
  89       * @param   string  $id  The ID for the provider
  90       *
  91       * @return  ProviderInterface
  92       *
  93       * @throws \Exception
  94       *
  95       * @since  4.0.0
  96       */
  97      public function getProvider($id)
  98      {
  99          if (!isset($this->providers[$id])) {
 100              throw new \Exception(Text::_('COM_MEDIA_ERROR_MEDIA_PROVIDER_NOT_FOUND'));
 101          }
 102  
 103          return $this->providers[$id];
 104      }
 105  
 106      /**
 107       * Returns an adapter for an account
 108       *
 109       * @param   string  $name  The name of an adapter
 110       *
 111       * @return  AdapterInterface
 112       *
 113       * @throws \Exception
 114       *
 115       * @since  4.0.0
 116       */
 117      public function getAdapter($name)
 118      {
 119          list($provider, $account) = array_pad(explode('-', $name, 2), 2, null);
 120  
 121          if ($account == null) {
 122              throw new \Exception(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_SET'));
 123          }
 124  
 125          $adapters = $this->getProvider($provider)->getAdapters();
 126  
 127          if (!isset($adapters[$account])) {
 128              throw new \Exception(Text::_('COM_MEDIA_ERROR_ACCOUNT_NOT_FOUND'));
 129          }
 130  
 131          return $adapters[$account];
 132      }
 133  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer