[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Adapter/ -> Adapter.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2008 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\Adapter;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Object\CMSObject;
  14  use Joomla\Database\DatabaseAwareInterface;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Adapter Class
  22   * Retains common adapter pattern functions
  23   * Class harvested from joomla.installer.installer
  24   *
  25   * @since       1.6
  26   * @deprecated  5.0 Will be removed without replacement
  27   */
  28  class Adapter extends CMSObject
  29  {
  30      /**
  31       * Associative array of adapters
  32       *
  33       * @var    static[]
  34       * @since  1.6
  35       */
  36      protected $_adapters = array();
  37  
  38      /**
  39       * Adapter Folder
  40       *
  41       * @var    string
  42       * @since  1.6
  43       */
  44      protected $_adapterfolder = 'adapters';
  45  
  46      /**
  47       * Adapter Class Prefix
  48       *
  49       * @var    string
  50       * @since  1.6
  51       */
  52      protected $_classprefix = 'J';
  53  
  54      /**
  55       * Base Path for the adapter instance
  56       *
  57       * @var    string
  58       * @since  1.6
  59       */
  60      protected $_basepath = null;
  61  
  62      /**
  63       * Database Connector Object
  64       *
  65       * @var    \Joomla\Database\DatabaseDriver
  66       * @since  1.6
  67       */
  68      protected $_db;
  69  
  70      /**
  71       * Constructor
  72       *
  73       * @param   string  $basepath       Base Path of the adapters
  74       * @param   string  $classprefix    Class prefix of adapters
  75       * @param   string  $adapterfolder  Name of folder to append to base path
  76       *
  77       * @since   1.6
  78       */
  79      public function __construct($basepath, $classprefix = null, $adapterfolder = null)
  80      {
  81          $this->_basepath = $basepath;
  82          $this->_classprefix = $classprefix ?: 'J';
  83          $this->_adapterfolder = $adapterfolder ?: 'adapters';
  84  
  85          $this->_db = Factory::getDbo();
  86  
  87          // Ensure BC, when removed in 5, then the db must be set with setDatabase explicitly
  88          if ($this instanceof DatabaseAwareInterface) {
  89              $this->setDatabase($this->_db);
  90          }
  91      }
  92  
  93      /**
  94       * Get the database connector object
  95       *
  96       * @return  \Joomla\Database\DatabaseDriver  Database connector object
  97       *
  98       * @since   1.6
  99       */
 100      public function getDbo()
 101      {
 102          return $this->_db;
 103      }
 104  
 105      /**
 106       * Return an adapter.
 107       *
 108       * @param   string  $name     Name of adapter to return
 109       * @param   array   $options  Adapter options
 110       *
 111       * @return  static|boolean  Adapter of type 'name' or false
 112       *
 113       * @since   1.6
 114       */
 115      public function getAdapter($name, $options = array())
 116      {
 117          if (array_key_exists($name, $this->_adapters)) {
 118              return $this->_adapters[$name];
 119          }
 120  
 121          if ($this->setAdapter($name, $options)) {
 122              return $this->_adapters[$name];
 123          }
 124  
 125          return false;
 126      }
 127  
 128      /**
 129       * Set an adapter by name
 130       *
 131       * @param   string  $name     Adapter name
 132       * @param   object  $adapter  Adapter object
 133       * @param   array   $options  Adapter options
 134       *
 135       * @return  boolean  True if successful
 136       *
 137       * @since   1.6
 138       */
 139      public function setAdapter($name, &$adapter = null, $options = array())
 140      {
 141          if (is_object($adapter)) {
 142              $this->_adapters[$name] = &$adapter;
 143  
 144              return true;
 145          }
 146  
 147          $class = rtrim($this->_classprefix, '\\') . '\\' . ucfirst($name);
 148  
 149          if (class_exists($class)) {
 150              $this->_adapters[$name] = new $class($this, $this->_db, $options);
 151  
 152              return true;
 153          }
 154  
 155          $class = rtrim($this->_classprefix, '\\') . '\\' . ucfirst($name) . 'Adapter';
 156  
 157          if (class_exists($class)) {
 158              $this->_adapters[$name] = new $class($this, $this->_db, $options);
 159  
 160              return true;
 161          }
 162  
 163          $fullpath = $this->_basepath . '/' . $this->_adapterfolder . '/' . strtolower($name) . '.php';
 164  
 165          if (!is_file($fullpath)) {
 166              return false;
 167          }
 168  
 169          // Try to load the adapter object
 170          $class = $this->_classprefix . ucfirst($name);
 171  
 172          \JLoader::register($class, $fullpath);
 173  
 174          if (!class_exists($class)) {
 175              return false;
 176          }
 177  
 178          $this->_adapters[$name] = new $class($this, $this->_db, $options);
 179  
 180          return true;
 181      }
 182  
 183      /**
 184       * Loads all adapters.
 185       *
 186       * @param   array  $options  Adapter options
 187       *
 188       * @return  void
 189       *
 190       * @since   1.6
 191       */
 192      public function loadAllAdapters($options = array())
 193      {
 194          $files = new \DirectoryIterator($this->_basepath . '/' . $this->_adapterfolder);
 195  
 196          /** @type  $file  \DirectoryIterator */
 197          foreach ($files as $file) {
 198              $fileName = $file->getFilename();
 199  
 200              // Only load for php files.
 201              if (!$file->isFile() || $file->getExtension() != 'php') {
 202                  continue;
 203              }
 204  
 205              // Try to load the adapter object
 206              require_once $this->_basepath . '/' . $this->_adapterfolder . '/' . $fileName;
 207  
 208              // Derive the class name from the filename.
 209              $name = str_ireplace('.php', '', ucfirst(trim($fileName)));
 210              $class = $this->_classprefix . ucfirst($name);
 211  
 212              if (!class_exists($class)) {
 213                  // Skip to next one
 214                  continue;
 215              }
 216  
 217              $adapter = new $class($this, $this->_db, $options);
 218              $this->_adapters[$name] = clone $adapter;
 219          }
 220      }
 221  }


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