ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

db_config_set.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class db_config_set_1_0_0{
27
38 var $Config = false;
39
50 var $ConfigPath = false;
51
62 var $CachedMultyFS = false;
63
78 function __construct()
79 {
80 try
81 {
82 $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
83 }
84 catch( Exception $e )
85 {
86 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
87 }
88 }
89
108 function load_config( $ConfigPath )
109 {
110 try
111 {
112 $this->Config = $this->CachedMultyFS->file_get_contents( $ConfigPath );
113
114 $this->Config = str_replace( "\r" , "\n" , $this->Config );
115 $this->Config = str_replace( "\n\n" , "\n" , $this->Config );
116 $this->Config = explode( "\n" , $this->Config );
117
118 $this->ConfigPath = $ConfigPath;
119 }
120 catch( Exception $e )
121 {
122 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
123 }
124 }
125
148 function optimize_config_file( $i , $ConfigLine )
149 {
150 try
151 {
152 if( $i != 0 )
153 {
154 $Content = $ConfigLine."\r\n";
155
156 foreach( $this->Config as $j => $ConfigLine2 )
157 {
158 if( $i != $j && $ConfigLine2 != '' )
159 {
160 $Content .= $ConfigLine2."\r\n";
161 }
162 }
163 $this->Config = $this->CachedMultyFS->file_put_contents( $this->ConfigPath , $Content );
164 }
165 }
166 catch( Exception $e )
167 {
168 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
169 }
170 }
171
202 function get_connection_for_config_line( $DBAdapter , $i , $ConfigLine )
203 {
204 try
205 {
206 if( $ConfigLine != '' )
207 {
208 $DBAdapter->connect( $ConfigLine );
209
210 $this->optimize_config_file( $i , $ConfigLine );
211
212 return( $DBAdapter->get_connection() );
213 }
214
215 return( false );
216 }
217 catch( Exception $e )
218 {
219 return( false );
220 }
221 }
222
241 function connect( $DBAdapter )
242 {
243 try
244 {
245 if( $this->Config == false )
246 {
247 throw( new Exception( 'Config was not loaded' ) );
248 }
249
250 foreach( $this->Config as $i => $ConfigLine )
251 {
252 $Connection = $this->get_connection_for_config_line( $DBAdapter , $i , $ConfigLine );
253
254 if( $Connection !== false )
255 {
256 return( $Connection );
257 }
258 }
259
260 throw( new Exception( "Unable to connect to the database" ) );
261 }
262 catch( Exception $e )
263 {
264 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
265 }
266 }
267 }
268
269 ?>