Source for file adapter.php

Documentation is available at adapter.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Base
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Adapter Class
  14.  * Retains common adapter pattern functions
  15.  * Class harvested from joomla.installer.installer
  16.  *
  17.  * @package     Joomla.Platform
  18.  * @subpackage  Base
  19.  * @since       11.1
  20.  */
  21. class JAdapter extends JObject
  22. {
  23.     /**
  24.      * Associative array of adapters
  25.      *
  26.      * @var    array 
  27.      * @since  11.1
  28.      */
  29.     protected $_adapters = array();
  30.  
  31.     /**
  32.      * Adapter Folder
  33.      * @var    string 
  34.      * @since  11.1
  35.      */
  36.     protected $_adapterfolder = 'adapters';
  37.  
  38.     /**
  39.      * @var    string    Adapter Class Prefix
  40.      * @since  11.1
  41.      */
  42.     protected $_classprefix = 'J';
  43.  
  44.     /**
  45.      * Base Path for the adapter instance
  46.      *
  47.      * @var    string 
  48.      * @since  11.1
  49.      */
  50.     protected $_basepath = null;
  51.  
  52.     /**
  53.      * Database Connector Object
  54.      *
  55.      * @var    JDatabaseDriver 
  56.      * @since  11.1
  57.      */
  58.     protected $_db;
  59.  
  60.     /**
  61.      * Constructor
  62.      *
  63.      * @param   string  $basepath       Base Path of the adapters
  64.      * @param   string  $classprefix    Class prefix of adapters
  65.      * @param   string  $adapterfolder  Name of folder to append to base path
  66.      *
  67.      * @since   11.1
  68.      */
  69.     public function __construct($basepath$classprefix null$adapterfolder null)
  70.     {
  71.         $this->_basepath = $basepath;
  72.         $this->_classprefix = $classprefix $classprefix 'J';
  73.         $this->_adapterfolder = $adapterfolder $adapterfolder 'adapters';
  74.  
  75.         $this->_db = JFactory::getDbo();
  76.     }
  77.  
  78.     /**
  79.      * Get the database connector object
  80.      *
  81.      * @return  JDatabaseDriver  Database connector object
  82.      *
  83.      * @since   11.1
  84.      */
  85.     public function getDBO()
  86.     {
  87.         return $this->_db;
  88.     }
  89.  
  90.     /**
  91.      * Set an adapter by name
  92.      *
  93.      * @param   string  $name      Adapter name
  94.      * @param   object  &$adapter  Adapter object
  95.      * @param   array   $options   Adapter options
  96.      *
  97.      * @return  boolean  True if successful
  98.      *
  99.      * @since   11.1
  100.      */
  101.     public function setAdapter($name&$adapter null$options array())
  102.     {
  103.         if (!is_object($adapter))
  104.         {
  105.             $fullpath $this->_basepath . '/' $this->_adapterfolder . '/' strtolower($name'.php';
  106.  
  107.             if (!file_exists($fullpath))
  108.             {
  109.                 return false;
  110.             }
  111.  
  112.             // Try to load the adapter object
  113.             require_once $fullpath;
  114.  
  115.             $class $this->_classprefix . ucfirst($name);
  116.  
  117.             if (!class_exists($class))
  118.             {
  119.                 return false;
  120.             }
  121.  
  122.             $adapter new $class($this$this->_db$options);
  123.         }
  124.  
  125.         $this->_adapters[$name&$adapter;
  126.  
  127.         return true;
  128.     }
  129.  
  130.     /**
  131.      * Return an adapter.
  132.      *
  133.      * @param   string  $name     Name of adapter to return
  134.      * @param   array   $options  Adapter options
  135.      *
  136.      * @return  object  Adapter of type 'name' or false
  137.      *
  138.      * @since   11.1
  139.      */
  140.     public function getAdapter($name$options array())
  141.     {
  142.         if (!array_key_exists($name$this->_adapters))
  143.         {
  144.             if (!$this->setAdapter($name$options))
  145.             {
  146.                 $false false;
  147.  
  148.                 return $false;
  149.             }
  150.         }
  151.  
  152.         return $this->_adapters[$name];
  153.     }
  154.  
  155.     /**
  156.      * Loads all adapters.
  157.      *
  158.      * @param   array  $options  Adapter options
  159.      *
  160.      * @return  void 
  161.      *
  162.      * @since   11.1
  163.      */
  164.     public function loadAllAdapters($options array())
  165.     {
  166.         $files new DirectoryIterator($this->_basepath . '/' $this->_adapterfolder);
  167.  
  168.         foreach ($files as $file)
  169.         {
  170.             $fileName $file->getFilename();
  171.  
  172.             // Only load for php files.
  173.             // Note: DirectoryIterator::getExtension only available PHP >= 5.3.6
  174.             if (!$file->isFile(|| substr($fileNamestrrpos($fileName'.'1!= 'php')
  175.             {
  176.                 continue;
  177.             }
  178.  
  179.             // Try to load the adapter object
  180.             require_once $this->_basepath . '/' $this->_adapterfolder . '/' $fileName;
  181.  
  182.             // Derive the class name from the filename.
  183.             $name str_ireplace('.php'''ucfirst(trim($fileName)));
  184.             $class $this->_classprefix . ucfirst($name);
  185.  
  186.             if (!class_exists($class))
  187.             {
  188.                 // Skip to next one
  189.                 continue;
  190.             }
  191.  
  192.             $adapter new $class($this$this->_db$options);
  193.             $this->_adapters[$nameclone $adapter;
  194.         }
  195.     }
  196. }

Documentation generated on Tue, 19 Nov 2013 14:53:22 +0100 by phpDocumentor 1.4.3