/
Setup.php
executable file
·70 lines (70 loc) · 2.6 KB
/
Setup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* RedBean Setup
* Helper class to quickly setup RedBean for you.
*
* @file RedBean/Setup.php
* @desc Helper class to quickly setup RedBean for you
* @author Gabor de Mooij and the RedBeanPHP community
* @license BSD/GPLv2
*
* copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
* This source file is subject to the BSD/GPLv2 License that is bundled
* with this source code in the file license.txt.
*/
class RedBean_Setup {
/**
* This method checks the DSN string. If the DSN string contains a
* database name that is not supported by RedBean yet then it will
* throw an exception RedBean_Exception_NotImplemented. In any other
* case this method will just return boolean TRUE.
* @throws RedBean_Exception_NotImplemented
* @param string $dsn
* @return boolean $true
*/
private static function checkDSN($dsn) {
if (!preg_match('/^(mysql|sqlite|pgsql|cubrid|oracle):/',strtolower(trim($dsn)))) trigger_error('Unsupported DSN');
return true;
}
/**
* Generic Kickstart method.
* This is the generic kickstarter. It will prepare a database connection
* using the $dsn, the $username and the $password you provide.
*
* @param string|PDO $dsn Database Connection String (or PDO instance)
* @param string $username Username for database
* @param string $password Password for database
* @param boolean $frozen Start in frozen mode?
*
* @return RedBean_ToolBox $toolbox
*/
public static function kickstart($dsn, $username=NULL, $password=NULL, $frozen=false ) {
if ($dsn instanceof PDO) {
$db = new RedBean_Driver_PDO($dsn);
$dsn = $db->getDatabaseType();
}
else {
self::checkDSN($dsn);
if (strpos($dsn, 'oracle') === 0)
$db = new RedBean_Driver_OCI($dsn,$username,$password);
else
$db = new RedBean_Driver_PDO($dsn,$username,$password);
}
$adapter = new RedBean_Adapter_DBAdapter($db);
if (strpos($dsn,'pgsql')===0) {
$writer = new RedBean_QueryWriter_PostgreSQL($adapter);
} else if (strpos($dsn,'sqlite')===0) {
$writer = new RedBean_QueryWriter_SQLiteT($adapter);
} else if (strpos($dsn,'cubrid')===0) {
$writer = new RedBean_QueryWriter_CUBRID($adapter);
} else if (strpos($dsn,'oracle')===0) { $writer = new RedBean_QueryWriter_Oracle($adapter); } //layout important for unit test - this line cannot always be tested.
else {
$writer = new RedBean_QueryWriter_MySQL($adapter);
}
$redbean = new RedBean_OODB($writer);
if ($frozen) $redbean->freeze(true);
$toolbox = new RedBean_ToolBox($redbean,$adapter,$writer);
//deliver everything back in a neat toolbox
return $toolbox;
}
}