/classes/Base/DB/DataSource.php

https://github.com/spadefoot/kohana-orm-leap · PHP · 282 lines · 145 code · 28 blank · 109 comment · 28 complexity · 809acb063b0bbfb3e2eda9e6d641d2c0 MD5 · raw file

  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Copyright © 2011–2013 Spadefoot Team.
  4. *
  5. * Unless otherwise noted, LEAP is licensed under the Apache License,
  6. * Version 2.0 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License
  8. * at:
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * This class wraps the connection's configurations.
  20. *
  21. * @package Leap
  22. * @category Connection
  23. * @version 2013-02-03
  24. *
  25. * @abstract
  26. */
  27. abstract class Base_DB_DataSource extends Core_Object {
  28. /**
  29. * This constant represents a master instance of a database.
  30. *
  31. * @access public
  32. * @const integer
  33. */
  34. const MASTER_INSTANCE = 0;
  35. /**
  36. * This constant represents a slave instance of a database.
  37. *
  38. * @access public
  39. * @const integer
  40. */
  41. const SLAVE_INSTANCE = 1;
  42. /**
  43. * This variable stores the settings for the data source.
  44. *
  45. * @access protected
  46. * @var array
  47. */
  48. protected $settings;
  49. /**
  50. * This function loads the configurations.
  51. *
  52. * @access public
  53. * @param mixed $config the data source configurations
  54. * @throws Throwable_InvalidArgument_Exception indicates a data type mismatch
  55. * @throws Throwable_InvalidProperty_Exception indicates that the database group is undefined
  56. */
  57. public function __construct($config) {
  58. if (empty($config)) {
  59. $id = 'database.default';
  60. if (($config = static::config($id)) === NULL) {
  61. throw new Throwable_InvalidProperty_Exception('Message: Unable to load data source. Reason: Database group :id is undefined.', array(':id' => $id));
  62. }
  63. $this->init($config, $id);
  64. }
  65. else if (is_string($config)) {
  66. $id = 'database.' . $config;
  67. if (($config = static::config($id)) === NULL) {
  68. throw new Throwable_InvalidProperty_Exception('Message: Unable to load data source. Reason: Database group :id is undefined.', array(':id' => $id));
  69. }
  70. $this->init($config, $id);
  71. }
  72. else if (is_array($config)) {
  73. $this->init($config);
  74. }
  75. else if (is_object($config) AND ($config instanceof DB_DataSource)) {
  76. $this->settings = $config->settings;
  77. }
  78. else {
  79. throw new Throwable_InvalidArgument_Exception('Message: Unable to load data source. Reason: Data type :type is mismatched.', array(':type' => gettype($config)));
  80. }
  81. }
  82. /**
  83. * This function returns the value associated with the specified property.
  84. *
  85. * @access public
  86. * @override
  87. * @param string $name the name of the property
  88. * @return mixed the value of the property
  89. * @throws Throwable_InvalidProperty_Exception indicates that the specified property is
  90. * either inaccessible or undefined
  91. */
  92. public function __get($name) {
  93. switch ($name) {
  94. case 'cache':
  95. case 'charset':
  96. case 'database':
  97. case 'dialect':
  98. case 'driver':
  99. case 'host':
  100. case 'id':
  101. case 'password':
  102. case 'port':
  103. case 'type':
  104. case 'username':
  105. case 'role':
  106. return $this->settings[$name];
  107. default:
  108. throw new Throwable_InvalidProperty_Exception('Message: Unable to get the specified property. Reason: Property :key is either inaccessible or undefined.', array(':key' => $name));
  109. }
  110. }
  111. /**
  112. * This function determines whether a specific property has been set.
  113. *
  114. * @access public
  115. * @override
  116. * @param string $name the name of the property
  117. * @return boolean indicates whether the specified property
  118. * has been set
  119. */
  120. public function __isset($name) {
  121. if (isset($this->settings[$name]) AND ($name != 'persistent')) {
  122. return (FALSE === empty($this->settings[$name]));
  123. }
  124. return NULL;
  125. }
  126. /**
  127. * This function handles the initialization of the data source's settings.
  128. *
  129. * @access protected
  130. * @param array $settings the settings to be used
  131. * @param string $id the data source's id
  132. */
  133. protected function init($settings, $id = NULL) {
  134. $this->settings = array();
  135. if ($id === NULL) {
  136. // TODO Verify that config id does not already exist in the "database.php" config file.
  137. $this->settings['id'] = (isset($settings['id']))
  138. ? (string) $settings['id']
  139. : 'unique_id.' . uniqid();
  140. }
  141. else {
  142. $this->settings['id'] = (string) $id;
  143. }
  144. $cache = array();
  145. $cache['enabled'] = (isset($settings['caching'])) ? (bool) $settings['caching'] : FALSE;
  146. $cache['lifetime'] = (class_exists('Kohana')) ? Kohana::$cache_life : 60;
  147. $cache['force'] = FALSE;
  148. $this->settings['cache'] = (object) $cache;
  149. $this->settings['charset'] = (isset($settings['charset']))
  150. ? (string) $settings['charset'] // e.g. utf8
  151. : '';
  152. $this->settings['database'] = (isset($settings['connection']['database']))
  153. ? (string) $settings['connection']['database']
  154. : '';
  155. if (isset($settings['dialect'])) {
  156. $this->settings['dialect'] = (string) $settings['dialect'];
  157. }
  158. else if (isset($settings['type'])) { // deprecated
  159. $this->settings['dialect'] = (string) $settings['type'];
  160. }
  161. else {
  162. $this->settings['dialect'] = 'MySQL';
  163. }
  164. $this->settings['driver'] = (isset($settings['driver']))
  165. ? (string) $settings['driver']
  166. : 'Standard';
  167. $this->settings['host'] = (isset($settings['connection']['hostname']))
  168. ? (string) $settings['connection']['hostname']
  169. : '';
  170. $this->settings['persistent'] = (isset($settings['connection']['persistent']))
  171. ? (bool) $settings['connection']['persistent']
  172. : FALSE;
  173. $this->settings['password'] = (isset($settings['connection']['password']))
  174. ? (string) $settings['connection']['password']
  175. : '';
  176. $this->settings['port'] = (isset($settings['connection']['port']))
  177. ? (string) $settings['connection']['port']
  178. : '';
  179. $this->settings['type'] = (isset($settings['type']))
  180. ? (string) $settings['type']
  181. : 'SQL'; // e.g. SQL, NoSQL, LDAP
  182. $this->settings['username'] = (isset($settings['connection']['username']))
  183. ? (string) $settings['connection']['username']
  184. : '';
  185. $this->settings['role'] = (isset($settings['connection']['role']))
  186. ? (string) $settings['connection']['role']
  187. : '';
  188. }
  189. /**
  190. * This function determines whether the connection is persistent.
  191. *
  192. * @access public
  193. * @return boolean whether the connection is persistent
  194. */
  195. public function is_persistent() {
  196. return $this->settings['persistent'];
  197. }
  198. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199. /**
  200. * This variable stores an array of singleton instances of this class.
  201. *
  202. * @access protected
  203. * @static
  204. * @var array
  205. */
  206. protected static $instances = array();
  207. /**
  208. * This function returns configurations settings for the specified path.
  209. *
  210. * @access public
  211. * @static
  212. * @param string $path the path to be used
  213. * @return mixed the configuration settings for the
  214. * specified path
  215. */
  216. public static function config($path) {
  217. return Kohana::$config->load($path);
  218. }
  219. /**
  220. * This function returns a singleton instance of this class.
  221. *
  222. * @access public
  223. * @static
  224. * @param mixed $config the data source configurations
  225. * @return DB_DataSource a singleton instance of this class
  226. */
  227. public static function instance($config = 'default') {
  228. if (is_string($config)) {
  229. if ( ! isset(static::$instances[$config])) {
  230. static::$instances[$config] = new DB_DataSource($config);
  231. }
  232. return static::$instances[$config];
  233. }
  234. else if (is_object($config) AND ($config instanceof DB_DataSource)) {
  235. $id = $config->id;
  236. if ( ! isset(static::$instances[$id])) {
  237. static::$instances[$id] = $config;
  238. }
  239. return $config;
  240. }
  241. else if (is_array($config) AND isset($config['id'])) {
  242. $id = $config['id'];
  243. if ( ! isset(static::$instances[$id])) {
  244. static::$instances[$id] = new DB_DataSource($config);
  245. }
  246. return static::$instances[$id];
  247. }
  248. else {
  249. $data_source = new DB_DataSource($config);
  250. static::$instances[$data_source->id] = $data_source;
  251. return $data_source;
  252. }
  253. }
  254. }