PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/alloy/Plugin/Spot/lib/Spot/Adapter/AdapterAbstract.php

http://github.com/alloyphp/alloy
PHP | 338 lines | 201 code | 32 blank | 105 comment | 27 complexity | 532ed22157c91216b76d35771394eefd MD5 | raw file
  1. <?php
  2. namespace Spot\Adapter;
  3. /**
  4. * Abstract Adapter
  5. *
  6. * @package Spot
  7. * @link http://spot.os.ly
  8. */
  9. abstract class AdapterAbstract
  10. {
  11. // Format for date columns, formatted for PHP's date() function
  12. protected $_format_date;
  13. protected $_format_time;
  14. protected $_format_datetime;
  15. // Connection details
  16. protected $_connection;
  17. protected $_dsn;
  18. protected $_dsnParts;
  19. protected $_options;
  20. /**
  21. * @param mixed $dsn DSN string or pre-existing Mongo object
  22. * @param array $options
  23. */
  24. public function __construct($dsn, array $options = array())
  25. {
  26. $this->_dsn = $dsn;
  27. if(is_string($dsn)) {
  28. // Parse DSN string
  29. $this->_dsnParts = self::parseDSN($dsn);
  30. } elseif(is_array($dsn)) {
  31. // Array of keys => values
  32. $this->_dsnParts = $dsn;
  33. } elseif(null === $dsn) {
  34. // No DSN string given
  35. $this->_dsnParts = array();
  36. }
  37. $this->_options = $options;
  38. }
  39. /**
  40. * Get database format
  41. *
  42. * @return string Date format for PHP's date() function
  43. */
  44. public function dateFormat()
  45. {
  46. return $this->_format_date;
  47. }
  48. /**
  49. * Get database time format
  50. *
  51. * @return string Time format for PHP's date() function
  52. */
  53. public function timeFormat()
  54. {
  55. return $this->_format_time;
  56. }
  57. /**
  58. * Get database format
  59. *
  60. * @return string DateTime format for PHP's date() function
  61. */
  62. public function dateTimeFormat()
  63. {
  64. return $this->_format_datetime;
  65. }
  66. /**
  67. * Get date
  68. *
  69. * @return object DateTime
  70. */
  71. public function date($format = null)
  72. {
  73. if(null === $format) {
  74. $format = $this->dateFormat();
  75. }
  76. return $this->dateTimeObject($format . ' ' . $this->timeFormat());
  77. }
  78. /**
  79. * Get database time format
  80. *
  81. * @return object DateTime
  82. */
  83. public function time($format = null)
  84. {
  85. if(null === $format) {
  86. $format = $this->timeFormat();
  87. }
  88. return $this->dateTimeObject($this->dateFormat() . ' ' . $format);
  89. }
  90. /**
  91. * Get datetime
  92. *
  93. * @return object DateTIme
  94. */
  95. public function dateTime($format = null)
  96. {
  97. if(null === $format) {
  98. $format = $this->dateTimeFormat();
  99. }
  100. return $this->dateTimeObject($format);
  101. }
  102. /**
  103. * Turn formstted date into timestamp
  104. * Also handles input timestamps
  105. *
  106. * @return DateTime object
  107. */
  108. protected function dateTimeObject($format)
  109. {
  110. // Already a timestamp?
  111. if(is_int($format) || is_float($format)) { // @link http://www.php.net/manual/en/function.is-int.php#97006
  112. $dt = new \DateTime();
  113. $dt->setTimestamp($format); // Timestamps must be prefixed with '@' symbol
  114. } else {
  115. $dt = new \DateTime();
  116. $dt->format($format);
  117. }
  118. return $dt;
  119. }
  120. /**
  121. * Taken from PHPUnit 3.4:
  122. * @link http://github.com/sebastianbergmann/phpunit/blob/3.4/PHPUnit/Util/PDO.php
  123. *
  124. * Returns the Data Source Name as a structure containing the various parts of the DSN.
  125. *
  126. * Additional keys can be added by appending a URI query string to the
  127. * end of the DSN.
  128. *
  129. * The format of the supplied DSN is in its fullest form:
  130. * <code>
  131. * adapter(dbsyntax)://username:password@protocol+host/database?option=8&another=true
  132. * </code>
  133. *
  134. * Most variations are allowed:
  135. * <code>
  136. * adapter://username:password@protocol+host:110//usr/db_file.db?mode=0644
  137. * adapter://username:password@host/database_name
  138. * adapter://username:password@host
  139. * adapter://username@host
  140. * adapter://host/database
  141. * adapter://host
  142. * adapter(dbsyntax)
  143. * adapter
  144. * </code>
  145. *
  146. * This function is 'borrowed' from PEAR /DB.php .
  147. *
  148. * @param string $dsn Data Source Name to be parsed
  149. *
  150. * @return array an associative array with the following keys:
  151. * + adapter: Database backend used in PHP (mysql, odbc etc.)
  152. * + dbsyntax: Database used with regards to SQL syntax etc.
  153. * + protocol: Communication protocol to use (tcp, unix etc.)
  154. * + host: Host specification (hostname[:port])
  155. * + database: Database to use on the DBMS server
  156. * + username: User name for login
  157. * + password: Password for login
  158. */
  159. public static function parseDSN( $dsn )
  160. {
  161. if ( $dsn == 'sqlite::memory:' )
  162. {
  163. $dsn = 'sqlite://:memory:';
  164. }
  165. $parsed = array(
  166. 'adapter' => FALSE,
  167. 'dbsyntax' => FALSE,
  168. 'username' => FALSE,
  169. 'password' => FALSE,
  170. 'protocol' => FALSE,
  171. 'host' => FALSE,
  172. 'port' => FALSE,
  173. 'socket' => FALSE,
  174. 'database' => FALSE,
  175. );
  176. if ( is_array( $dsn ) )
  177. {
  178. $dsn = array_merge( $parsed, $dsn );
  179. if ( !$dsn['dbsyntax'] )
  180. {
  181. $dsn['dbsyntax'] = $dsn['adapter'];
  182. }
  183. return $dsn;
  184. }
  185. // Find phptype and dbsyntax
  186. if ( ( $pos = strpos( $dsn, '://' ) ) !== FALSE )
  187. {
  188. $str = substr( $dsn, 0, $pos );
  189. $dsn = substr( $dsn, $pos + 3 );
  190. }
  191. else
  192. {
  193. $str = $dsn;
  194. $dsn = NULL;
  195. }
  196. // Get phptype and dbsyntax
  197. // $str => phptype(dbsyntax)
  198. if ( preg_match( '|^(.+?)\((.*?)\)$|', $str, $arr ) )
  199. {
  200. $parsed['adapter'] = $arr[1];
  201. $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
  202. }
  203. else
  204. {
  205. $parsed['adapter'] = $str;
  206. $parsed['dbsyntax'] = $str;
  207. }
  208. if ( !count( $dsn ) )
  209. {
  210. return $parsed;
  211. }
  212. // Get (if found): username and password
  213. // $dsn => username:password@protocol+host/database
  214. if ( ( $at = strrpos( (string) $dsn, '@' ) ) !== FALSE )
  215. {
  216. $str = substr( $dsn, 0, $at );
  217. $dsn = substr( $dsn, $at + 1 );
  218. if ( ( $pos = strpos( $str, ':' ) ) !== FALSE )
  219. {
  220. $parsed['username'] = rawurldecode( substr( $str, 0, $pos ) );
  221. $parsed['password'] = rawurldecode( substr( $str, $pos + 1 ) );
  222. }
  223. else
  224. {
  225. $parsed['username'] = rawurldecode( $str );
  226. }
  227. }
  228. // Find protocol and host
  229. if ( preg_match( '|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match ) )
  230. {
  231. // $dsn => proto(proto_opts)/database
  232. $proto = $match[1];
  233. $proto_opts = $match[2] ? $match[2] : FALSE;
  234. $dsn = $match[3];
  235. }
  236. else
  237. {
  238. // $dsn => protocol+host/database (old format)
  239. if ( strpos( $dsn, '+' ) !== FALSE )
  240. {
  241. list( $proto, $dsn ) = explode( '+', $dsn, 2 );
  242. }
  243. if ( strpos( $dsn, '/' ) !== FALSE )
  244. {
  245. list( $proto_opts, $dsn ) = explode( '/', $dsn, 2 );
  246. }
  247. else
  248. {
  249. $proto_opts = $dsn;
  250. $dsn = NULL;
  251. }
  252. }
  253. // process the different protocol options
  254. $parsed['protocol'] = ( !empty( $proto ) ) ? $proto : 'tcp';
  255. $proto_opts = rawurldecode( $proto_opts );
  256. if ( $parsed['protocol'] == 'tcp' )
  257. {
  258. if ( strpos( $proto_opts, ':' ) !== FALSE )
  259. {
  260. list( $parsed['host'], $parsed['port'] ) = explode( ':', $proto_opts );
  261. }
  262. else
  263. {
  264. $parsed['host'] = $proto_opts;
  265. }
  266. }
  267. elseif ( $parsed['protocol'] == 'unix' )
  268. {
  269. $parsed['socket'] = $proto_opts;
  270. }
  271. // Get dabase if any
  272. // $dsn => database
  273. if ( $dsn )
  274. {
  275. if ( ( $pos = strpos( $dsn, '?' ) ) === FALSE )
  276. {
  277. // /database
  278. $parsed['database'] = rawurldecode( $dsn );
  279. }
  280. else
  281. {
  282. // /database?param1=value1&param2=value2
  283. $parsed['database'] = rawurldecode( substr( $dsn, 0, $pos ) );
  284. $dsn = substr( $dsn, $pos + 1 );
  285. if ( strpos( $dsn, '&') !== FALSE )
  286. {
  287. $opts = explode( '&', $dsn );
  288. }
  289. else
  290. { // database?param1=value1
  291. $opts = array( $dsn );
  292. }
  293. foreach ( $opts as $opt )
  294. {
  295. list( $key, $value ) = explode( '=', $opt );
  296. if ( !isset( $parsed[$key] ) )
  297. {
  298. // don't allow params overwrite
  299. $parsed[$key] = rawurldecode( $value );
  300. }
  301. }
  302. }
  303. }
  304. return $parsed;
  305. }
  306. }