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