1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Base
5 *
6 * @copyright Copyright (C) 2005 - 2017 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 * @since 11.1
18 */
19 class JAdapter extends JObject
20 {
21 /**
22 * Associative array of adapters
23 *
24 * @var array
25 * @since 11.1
26 */
27 protected $_adapters = array();
28
29 /**
30 * Adapter Folder
31 * @var string
32 * @since 11.1
33 */
34 protected $_adapterfolder = 'adapters';
35
36 /**
37 * @var string Adapter Class Prefix
38 * @since 11.1
39 */
40 protected $_classprefix = 'J';
41
42 /**
43 * Base Path for the adapter instance
44 *
45 * @var string
46 * @since 11.1
47 */
48 protected $_basepath = null;
49
50 /**
51 * Database Connector Object
52 *
53 * @var JDatabaseDriver
54 * @since 11.1
55 */
56 protected $_db;
57
58 /**
59 * Constructor
60 *
61 * @param string $basepath Base Path of the adapters
62 * @param string $classprefix Class prefix of adapters
63 * @param string $adapterfolder Name of folder to append to base path
64 *
65 * @since 11.1
66 */
67 public function __construct($basepath, $classprefix = null, $adapterfolder = null)
68 {
69 $this->_basepath = $basepath;
70 $this->_classprefix = $classprefix ? $classprefix : 'J';
71 $this->_adapterfolder = $adapterfolder ? $adapterfolder : 'adapters';
72
73 $this->_db = JFactory::getDbo();
74 }
75
76 /**
77 * Get the database connector object
78 *
79 * @return JDatabaseDriver Database connector object
80 *
81 * @since 11.1
82 */
83 public function getDbo()
84 {
85 return $this->_db;
86 }
87
88 /**
89 * Return an adapter.
90 *
91 * @param string $name Name of adapter to return
92 * @param array $options Adapter options
93 *
94 * @return object Adapter of type 'name' or false
95 *
96 * @since 11.1
97 */
98 public function getAdapter($name, $options = array())
99 {
100 if (array_key_exists($name, $this->_adapters))
101 {
102 return $this->_adapters[$name];
103 }
104
105 if ($this->setAdapter($name, $options))
106 {
107 return $this->_adapters[$name];
108 }
109
110 return false;
111 }
112
113 /**
114 * Set an adapter by name
115 *
116 * @param string $name Adapter name
117 * @param object &$adapter Adapter object
118 * @param array $options Adapter options
119 *
120 * @return boolean True if successful
121 *
122 * @since 11.1
123 */
124 public function setAdapter($name, &$adapter = null, $options = array())
125 {
126 if (is_object($adapter))
127 {
128 $this->_adapters[$name] = &$adapter;
129
130 return true;
131 }
132
133 $fullpath = $this->_basepath . '/' . $this->_adapterfolder . '/' . strtolower($name) . '.php';
134
135 if (!file_exists($fullpath))
136 {
137 return false;
138 }
139
140 // Try to load the adapter object
141 $class = $this->_classprefix . ucfirst($name);
142
143 JLoader::register($class, $fullpath);
144
145 if (!class_exists($class))
146 {
147 return false;
148 }
149
150 $this->_adapters[$name] = new $class($this, $this->_db, $options);
151
152 return true;
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 /* @type $file DirectoryIterator */
169 foreach ($files as $file)
170 {
171 $fileName = $file->getFilename();
172
173 // Only load for php files.
174 if (!$file->isFile() || $file->getExtension() != '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[$name] = clone $adapter;
194 }
195 }
196 }
197