[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/http/src/ -> HttpFactory.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Http Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Http;
  10  
  11  /**
  12   * HTTP factory class.
  13   *
  14   * @since  1.0
  15   */
  16  class HttpFactory
  17  {
  18      /**
  19       * Method to create an Http instance.
  20       *
  21       * @param   array|\ArrayAccess  $options   Client options array.
  22       * @param   array|string        $adapters  Adapter (string) or queue of adapters (array) to use for communication.
  23       *
  24       * @return  Http
  25       *
  26       * @since   1.0
  27       * @throws  \InvalidArgumentException
  28       * @throws  \RuntimeException
  29       */
  30  	public function getHttp($options = [], $adapters = null)
  31      {
  32          if (!\is_array($options) && !($options instanceof \ArrayAccess))
  33          {
  34              throw new \InvalidArgumentException(
  35                  'The options param must be an array or implement the ArrayAccess interface.'
  36              );
  37          }
  38  
  39          if (!$driver = $this->getAvailableDriver($options, $adapters))
  40          {
  41              throw new \RuntimeException('No transport driver available.');
  42          }
  43  
  44          return new Http($options, $driver);
  45      }
  46  
  47      /**
  48       * Finds an available TransportInterface object for communication
  49       *
  50       * @param   array|\ArrayAccess  $options  Options for creating TransportInterface object
  51       * @param   array|string        $default  Adapter (string) or queue of adapters (array) to use
  52       *
  53       * @return  TransportInterface|boolean  Interface sub-class or boolean false if no adapters are available
  54       *
  55       * @since   1.0
  56       * @throws  \InvalidArgumentException
  57       */
  58  	public function getAvailableDriver($options = [], $default = null)
  59      {
  60          if (!\is_array($options) && !($options instanceof \ArrayAccess))
  61          {
  62              throw new \InvalidArgumentException(
  63                  'The options param must be an array or implement the ArrayAccess interface.'
  64              );
  65          }
  66  
  67          if ($default === null)
  68          {
  69              $availableAdapters = $this->getHttpTransports();
  70          }
  71          else
  72          {
  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          {
  80              return false;
  81          }
  82  
  83          foreach ($availableAdapters as $adapter)
  84          {
  85              $class = __NAMESPACE__ . '\\Transport\\' . ucfirst($adapter);
  86  
  87              if (class_exists($class))
  88              {
  89                  if ($class::isSupported())
  90                  {
  91                      return new $class($options);
  92                  }
  93              }
  94          }
  95  
  96          return false;
  97      }
  98  
  99      /**
 100       * Get the HTTP transport handlers
 101       *
 102       * @return  string[]  An array of available transport handler types
 103       *
 104       * @since   1.0
 105       */
 106  	public function getHttpTransports()
 107      {
 108          $names    = [];
 109          $iterator = new \DirectoryIterator(__DIR__ . '/Transport');
 110  
 111          /** @var \DirectoryIterator $file */
 112          foreach ($iterator as $file)
 113          {
 114              $fileName = $file->getFilename();
 115  
 116              // Only load for php files.
 117              if ($file->isFile() && $file->getExtension() == 'php')
 118              {
 119                  $names[] = substr($fileName, 0, strrpos($fileName, '.'));
 120              }
 121          }
 122  
 123          // Keep alphabetical order across all environments
 124          sort($names);
 125  
 126          // If curl is available set it to the first position
 127          $key = array_search('Curl', $names);
 128  
 129          if ($key)
 130          {
 131              unset($names[$key]);
 132              array_unshift($names, 'Curl');
 133          }
 134  
 135          return $names;
 136      }
 137  }


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