[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Http/ -> HttpFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Http;
  11  
  12  use Joomla\CMS\Version;
  13  use Joomla\Http\TransportInterface;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * HTTP factory class.
  21   *
  22   * @since  3.0.0
  23   */
  24  class HttpFactory
  25  {
  26      /**
  27       * Method to create a JHttp instance.
  28       *
  29       * @param   array|\ArrayAccess  $options   Client options array.
  30       * @param   array|string        $adapters  Adapter (string) or queue of adapters (array) to use for communication.
  31       *
  32       * @return  Http
  33       *
  34       * @since   3.0.0
  35       * @throws  \RuntimeException
  36       */
  37      public static function getHttp($options = [], $adapters = null)
  38      {
  39          if (!\is_array($options) && !($options instanceof \ArrayAccess)) {
  40              throw new \InvalidArgumentException(
  41                  'The options param must be an array or implement the ArrayAccess interface.'
  42              );
  43          }
  44  
  45          // Set default userAgent if nothing else is set
  46          if (!isset($options['userAgent'])) {
  47              $version = new Version();
  48              $options['userAgent'] = $version->getUserAgent('Joomla', true, false);
  49          }
  50  
  51          if (!$driver = static::getAvailableDriver($options, $adapters)) {
  52              throw new \RuntimeException('No transport driver available.');
  53          }
  54  
  55          return new Http($options, $driver);
  56      }
  57  
  58      /**
  59       * Finds an available http transport object for communication
  60       *
  61       * @param   array|\ArrayAccess  $options  Options for creating TransportInterface object
  62       * @param   array|string        $default  Adapter (string) or queue of adapters (array) to use
  63       *
  64       * @return  TransportInterface|boolean  Interface sub-class or boolean false if no adapters are available
  65       *
  66       * @since   3.0.0
  67       */
  68      public static function getAvailableDriver($options = [], $default = null)
  69      {
  70          if (\is_null($default)) {
  71              $availableAdapters = static::getHttpTransports();
  72          } else {
  73              settype($default, 'array');
  74              $availableAdapters = $default;
  75          }
  76  
  77          // Check if there is at least one available http transport adapter
  78          if (!\count($availableAdapters)) {
  79              return false;
  80          }
  81  
  82          foreach ($availableAdapters as $adapter) {
  83              /** @var $class TransportInterface */
  84              $class = __NAMESPACE__ . '\\Transport\\' . ucfirst($adapter) . 'Transport';
  85  
  86              if (!class_exists($class)) {
  87                  $class = 'JHttpTransport' . ucfirst($adapter);
  88              }
  89  
  90              if (class_exists($class) && $class::isSupported()) {
  91                  return new $class($options);
  92              }
  93          }
  94  
  95          return false;
  96      }
  97  
  98      /**
  99       * Get the http transport handlers
 100       *
 101       * @return  array  An array of available transport handlers
 102       *
 103       * @since   3.0.0
 104       */
 105      public static function getHttpTransports()
 106      {
 107          $names = array();
 108          $iterator = new \DirectoryIterator(__DIR__ . '/Transport');
 109  
 110          /** @type  $file  \DirectoryIterator */
 111          foreach ($iterator as $file) {
 112              $fileName = $file->getFilename();
 113  
 114              // Only load for php files.
 115              if ($file->isFile() && $file->getExtension() === 'php') {
 116                  $names[] = substr($fileName, 0, strrpos($fileName, 'Transport.'));
 117              }
 118          }
 119  
 120          // Keep alphabetical order across all environments
 121          sort($names);
 122  
 123          // If curl is available set it to the first position
 124          if ($key = array_search('Curl', $names)) {
 125              unset($names[$key]);
 126              array_unshift($names, 'Curl');
 127          }
 128  
 129          return $names;
 130      }
 131  }


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