PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Phergie/Plugin/Db.php

https://github.com/markizano/phergie
PHP | 233 lines | 97 code | 21 blank | 115 comment | 10 complexity | 383f62d0e4aa045a9072d6f2f7c4c031 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Phergie
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://phergie.org/license
  13. *
  14. * @category Phergie
  15. * @package Phergie_Plugin_Db
  16. * @author Phergie Development Team <team@phergie.org>
  17. * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
  18. * @license http://phergie.org/license New BSD License
  19. * @link http://pear.phergie.org/package/Phergie_Plugin_Db
  20. */
  21. /**
  22. * TODO CREATE CLASS DESCRIPTION
  23. *
  24. * @category Phergie
  25. * @package Phergie_Plugin_Db
  26. * @author Jared Folkins <jfolkins@gmail.com>
  27. * @license http://phergie.org/license New BSD License
  28. * @link http://pear.phergie.org/package/Phergie_Plugin_Db
  29. * @uses Phergie_Plugin_UserInfo pear.phergie.org
  30. */
  31. class Phergie_Plugin_Db extends Phergie_Plugin_Abstract
  32. {
  33. const DEBUG = true;
  34. /**
  35. * Checks to see if the root rbac user has been set in *
  36. *
  37. * @return void
  38. */
  39. public function onLoad()
  40. {
  41. $this->doesPdoExist();
  42. }
  43. /**
  44. * Validates that the Pdo Extenstion Exists.
  45. *
  46. * @return void
  47. */
  48. private function doesPdoExist()
  49. {
  50. if (!extension_loaded('PDO') || !extension_loaded('pdo_sqlite')) {
  51. $this->fail('PDO and pdo_sqlite extensions must be installed');
  52. }
  53. }
  54. /**
  55. * Initializes database
  56. *
  57. * @param string $directory plugin name
  58. * @param string $dbFile database name
  59. * @param string $schemaFile schema filename
  60. *
  61. * @return object
  62. */
  63. public function init($directory, $dbFile, $schemaFile)
  64. {
  65. $this->isResourceDirectory($directory);
  66. $doesDbFileExist = is_readable($dbFile);
  67. try {
  68. $db = new PDO('sqlite:' . $dbFile);
  69. } catch (PDO_Exception $e) {
  70. throw new Phergie_Plugin_Exception($e->getMessage());
  71. }
  72. if (!$doesDbFileExist) {
  73. $this->createTablesFromSchema($db, $schemaFile);
  74. }
  75. return $db;
  76. }
  77. /**
  78. * fully specified file name as string
  79. * used to check that the resource directory does exist
  80. *
  81. * @param string $directory Directory to check
  82. *
  83. * @return void
  84. *
  85. */
  86. public function isResourceDirectory($directory)
  87. {
  88. if (!is_dir($directory)) {
  89. $this->fail('The Resource directory: ' . $directory . ' does not exist');
  90. }
  91. }
  92. /**
  93. * fully specified file name as string
  94. * used to check that the schema file does exist
  95. *
  96. * @param string $file File to check
  97. *
  98. * @return void
  99. *
  100. */
  101. public function isSchemaFile($file)
  102. {
  103. if (!is_readable($file)) {
  104. $this->fail(
  105. 'The schema file: ' . $file
  106. . ' is not readable or does not exist'
  107. );
  108. }
  109. }
  110. /**
  111. * Supply sql statement and one word type parameter
  112. * (IE, create, update, insert, delete)
  113. * and the method will validate that the sql contains that syntax
  114. *
  115. * @param string $sql TODO Desc
  116. * @param string $type TODO Desc
  117. *
  118. * @return bool
  119. */
  120. public function validateSqlType($sql, $type)
  121. {
  122. preg_match('/^'.strtolower($type).'/', strtolower($sql), $matches);
  123. return ($matches[0]) ? true : false;
  124. }
  125. /**
  126. * Creates database table
  127. *
  128. * @param String $db database reference
  129. * @param String $sql create table sql statement
  130. *
  131. * @return void
  132. */
  133. public function createTable($db, $sql)
  134. {
  135. if (!$this->validateSqlType($sql, 'create')) {
  136. $this->fail('The SQL provided is not a create statement');
  137. }
  138. try {
  139. $db->exec($sql);
  140. } catch (PDO_Exception $e) {
  141. throw new Phergie_Plugin_Exception($e->getMessage());
  142. }
  143. }
  144. /**
  145. * Loads the schema file into array then searches
  146. * for each table and if not found creates the table
  147. *
  148. * @param String $db Database Table
  149. * @param String $file Filename
  150. *
  151. * @return void
  152. */
  153. public function createTablesFromSchema($db, $file)
  154. {
  155. $this->isSchemaFile($file);
  156. $file = strtolower(file_get_contents($file));
  157. preg_match_all('/create\stable\s([a-z_]+).*;/', $file, $matches);
  158. if (count($matches[0]) != count($matches[1])) {
  159. $this->fail(
  160. 'Schema array key value mismatch, '
  161. . 'the regular expression must not be working correctly'
  162. );
  163. }
  164. $tables = array_combine($matches[1], $matches[0]);
  165. foreach ($tables as $name => $sql) {
  166. if (!$this->hasTable($db, $name)) {
  167. $this->createTable($db, $sql);
  168. }
  169. }
  170. }
  171. /**
  172. * Validates that database table exists
  173. *
  174. * @param String $db database name
  175. * @param String $name table name
  176. *
  177. * @return bool
  178. */
  179. public function hasTable($db, $name)
  180. {
  181. $sql = 'SELECT COUNT(*)
  182. FROM sqlite_master
  183. WHERE name = :tableName';
  184. $statement = $db->prepare($sql);
  185. $statement->execute(array(':tableName' => $db->quote($name)));
  186. return (bool) $statement->fetchColumn();
  187. }
  188. /**
  189. * TODO Desc
  190. *
  191. * @param String $name table name
  192. *
  193. * @return bool
  194. */
  195. public function dropTable($name)
  196. {
  197. }
  198. /**
  199. * Jared's crap debug method
  200. *
  201. * @param String $message TODO Desc
  202. *
  203. * @return void
  204. */
  205. private function debug($message)
  206. {
  207. if (self::DEBUG) {
  208. echo 'DEBUG: ['. date('c') . '] - '. $message . "\n";
  209. }
  210. }
  211. }
  212. ?>