a8a9fcf41667c3c581e3c336a88de4c5f57bce44
[mailer.git] / inc / database.php
1 <?php
2 /**
3  * Initializes the database layer
4  *
5  * @author              Roland Haeder <webmaster@mxchange.org>
6  * @version             0.3.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.mxchange.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 // Initialize layer
26 $layer = null;
27
28 // Is the type defined?
29 if (!defined('_DB_TYPE')) {
30         // Abort here
31         ApplicationEntryPoint::app_die("[Main:] Please define a database type first!<br />
32 E.g.:<pre>&#36;GLOBALS[&#39;cfg&#39]->defineDatabaseType(&quot;local&quot;);</pre>
33 This will choose the local-file-based database type (layer)");
34 }
35
36 // Generate FQFN for the database layer
37 $INC = sprintf("%sinc/database/lib-%s%s", PATH, _DB_TYPE, FrameworkConfiguration::getInstance()->readConfig("php_extension"));
38
39 // Load the database layer include
40 if ((file_exists($INC)) && (is_file($INC)) && (is_readable($INC))) {
41         // Load the layer
42         require_once($INC);
43 } else {
44         // Layer is missing!
45         ApplicationEntryPoint::app_die(sprintf("[Main:] Database layer is missing! (%s) -&gt; R.I.P.",
46                 _DB_TYPE
47         ));
48 }
49
50 // Clean it up
51 unset($INC);
52
53 // Prepare database instance
54 try {
55         $db = DatabaseConnection::createDatabaseConnection(DebugMiddleware::getInstance(), $layer);
56 } catch (NullPointerException $e) {
57         ApplicationEntryPoint::app_die(sprintf("[Main:] Datenbank-System nicht initialisiert. Reason: <strong>%s</strong><br />\n",
58                 $e->getMessage()
59         ));
60 } catch (InvalidDirectoryResourceException $e) {
61         ApplicationEntryPoint::app_die(sprintf("[Main:] Datenbank-System nicht initialisiert. Reason: <strong>%s</strong><br />\n",
62                 $e->getMessage()
63         ));
64 } catch (PathIsEmptyException $e) {
65         ApplicationEntryPoint::app_die(sprintf("[Main:] Datenbank-System nicht initialisiert. Reason: <strong>%s</strong><br />\n",
66                 $e->getMessage()
67         ));
68 } catch (PathIsNoDirectoryException $e) {
69         ApplicationEntryPoint::app_die(sprintf("[Main:] Datenbank-System nicht initialisiert. Reason: <strong>%s</strong><br />\n",
70                 $e->getMessage()
71         ));
72 } catch (PathReadProtectedException $e) {
73         ApplicationEntryPoint::app_die(sprintf("[Main:] Datenbank-System nicht initialisiert. Reason: <strong>%s</strong><br />\n",
74                 $e->getMessage()
75         ));
76 } catch (DirPointerNotOpenedException $e) {
77         ApplicationEntryPoint::app_die(sprintf("[Main:] Datenbank-System nicht initialisiert. Reason: <strong>%s</strong><br />\n",
78                 $e->getMessage()
79         ));
80 }
81
82 // Datenbankobjekt debuggen
83 if (defined('DEBUG_DATABASE_OBJ')) {
84         DebugMiddleware::getInstance()->output(sprintf("Die Datenbank-Schicht sieht wie folgt aus:<br />
85 <pre>%s</pre>\n",
86                 print_r($db, true)
87         ));
88 }
89
90 // [EOF]
91 ?>