PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/RedBean/Setup.php

http://github.com/gabordemooij/redbean
PHP | 91 lines | 43 code | 10 blank | 38 comment | 14 complexity | 030001547cf67d820126ae9f51e235f7 MD5 | raw file
  1. <?php
  2. /**
  3. * RedBean Setup
  4. * Helper class to quickly setup RedBean for you.
  5. *
  6. * @file RedBean/Setup.php
  7. * @desc Helper class to quickly setup RedBean for you
  8. * @author Gabor de Mooij and the RedBeanPHP community
  9. * @license BSD/GPLv2
  10. *
  11. * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
  12. * This source file is subject to the BSD/GPLv2 License that is bundled
  13. * with this source code in the file license.txt.
  14. */
  15. class RedBean_Setup
  16. {
  17. /**
  18. * This method checks the DSN string.
  19. * Checks the validity of the DSN string.
  20. * If the DSN contains an invalid database identifier this method
  21. * will trigger an error.
  22. *
  23. * @param string $dsn
  24. *
  25. * @return boolean
  26. */
  27. private static function checkDSN( $dsn )
  28. {
  29. if ( !preg_match( '/^(mysql|sqlite|pgsql|cubrid|oracle):/', strtolower( trim( $dsn ) ) ) ) {
  30. trigger_error( 'Unsupported DSN' );
  31. }
  32. return TRUE;
  33. }
  34. /**
  35. * Initializes the database and prepares a toolbox.
  36. * The kickstart method assembles a toolbox based on your DSN and
  37. * credentials and returns it.
  38. * The toolbox contains all the necessary core components for
  39. * RedBeanPHP to start working with your database. Most RedBeanPHP
  40. * components are stand-alone and require a toolbox to work.
  41. *
  42. * @param string|PDO $dsn Database Connection String (or PDO instance)
  43. * @param string $username Username for database
  44. * @param string $password Password for database
  45. * @param boolean $frozen Start in frozen mode?
  46. *
  47. * @return RedBean_ToolBox
  48. */
  49. public static function kickstart( $dsn, $username = NULL, $password = NULL, $frozen = FALSE )
  50. {
  51. if ( $dsn instanceof PDO ) {
  52. $db = new RedBean_Driver_PDO( $dsn );
  53. $dsn = $db->getDatabaseType();
  54. } else {
  55. self::checkDSN( $dsn );
  56. if ( strpos( $dsn, 'oracle' ) === 0 ) {
  57. $db = new RedBean_Driver_OCI( $dsn, $username, $password );
  58. } else {
  59. $db = new RedBean_Driver_PDO( $dsn, $username, $password );
  60. }
  61. }
  62. $adapter = new RedBean_Adapter_DBAdapter( $db );
  63. if ( strpos( $dsn, 'pgsql' ) === 0 ) {
  64. $writer = new RedBean_QueryWriter_PostgreSQL( $adapter );
  65. } else if ( strpos( $dsn, 'sqlite' ) === 0 ) {
  66. $writer = new RedBean_QueryWriter_SQLiteT( $adapter );
  67. } else if ( strpos( $dsn, 'cubrid' ) === 0 ) {
  68. $writer = new RedBean_QueryWriter_CUBRID( $adapter );
  69. } else if ( strpos( $dsn, 'oracle' ) === 0 ) {
  70. $writer = new RedBean_QueryWriter_Oracle( $adapter );
  71. } else {
  72. $writer = new RedBean_QueryWriter_MySQL( $adapter );
  73. }
  74. $redbean = new RedBean_OODB( $writer );
  75. if ( $frozen ) {
  76. $redbean->freeze( TRUE );
  77. }
  78. $toolbox = new RedBean_ToolBox( $redbean, $adapter, $writer );
  79. return $toolbox;
  80. }
  81. }