PhpToolCase
Api Documentation Version 0.9.2
PtcDb.php
Go to the documentation of this file.
1 <?php
2 
3  /**
4  * PHP TOOLCASE DATABASE CONNECTION MANAGER CLASS
5  * PHP version 5.3
6  * @category Library
7  * @version 0.9.2
8  * @author Irony <carlo@salapc.com>
9  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
10  * @link http://phptoolcase.com
11  */
12 
13  class PtcDb
14  {
15  /**
16  * Adds a connection to the manager, will trigger an error if connection name is already present.
17  * See @ref db_getting_started
18  * @param array $options the details of the connection, see @ref connection_options
19  * @param string $name the name of the connection
20  * @return an array with the connection details as values or false if name is already present
21  */
22  public static function add( $options , $name = 'default' )
23  {
24  if( array_key_exists( $name , static::$_connections ) )
25  {
26  if( array_key_exists( $name , static::$_connections ) )
27  {
28  trigger_error( 'Connection name "' . $name .
29  '" already exists, use some other name!' , E_USER_ERROR );
30  return false;
31  }
32  }
33  foreach( $options as $k => $v )
34  {
35  if( !array_key_exists( $k , static::$_connectionOptions ) )
36  {
37  trigger_error( 'Unknown option "' . $k . '" passed as argument to PtcDb!',
38  E_USER_WARNING );
39  }
40  }
41  $options['name' ] = $name;
42  if( array_key_exists( 'pdo_attributes' , $options ) )
43  {
44  $options['pdo_attributes' ] = $options['pdo_attributes' ] +
45  static::$_connectionOptions[ 'pdo_attributes' ];
46  }
47  $options = array_merge( static::$_connectionOptions , $options );
48  static::$_connectionsDetails[ $name ] = $options;
49  static::_debug( static::$_connectionsDetails[ $name ] ,
50  'added connection <b>"' . $name . '"</b>' , 'Connection Manager' );
51  return static::$_connectionsDetails[ $name ] = $options;
52  }
53  /**
54  * Retrieves connection details previously configured. See @ref connectionDetails
55  * @param string $name the name of the connection to retrieve
56  * @return an array with the connection as values if $name is set, otherwise all connections set
57  */
58  public static function getConnection( $name = null )
59  {
60  if( !$name ){ return static::$_connectionsDetails; } // return all connections
61  if( !static::_checkConnectionName( $name ) ){ return false; }
62  return static::$_connectionsDetails[ $name ];
63  }
64  /**
65  * Retrieves the Pdo object. See @ref usingPdo
66  * @param string $name the name of the connection
67  * @return the Pdo object
68  */
69  public static function getPdo( $name )
70  {
71  if ( !static::_checkConnectionName( $name ) ){ return false; }
72  static::_initializeConnection( $name );
73  return static::$_connections[ $name ][ 'pdo_object' ];
74  }
75  /**
76  * Retreives the query builder object if present. See @ref usingQueryBuilder
77  * @param string $name the name of the connection
78  * @return the query builder object
79  */
80  public static function getQB( $name )
81  {
82  if( !static::_checkConnectionName( $name ) ){ return false; }
83  static::_initializeConnection( $name );
84  if( !static::$_connectionsDetails[ $name ][ 'query_builder' ] )
85  {
86  trigger_error( 'QueryBuilder was not set for connection "' . $name .
87  '"!', E_USER_ERROR );
88  return false;
89  }
90  return $connection = static::$_connections[ $name ][ 'query_builder' ];
91  //return function ( ) use ( $connection ){ return $connection }; // lambda time!
92 
93  }
94  /**
95  * Calls Pdo or query builder methods from the default connection directly
96  * @param string $method the name of the method to call
97  * @param array $args arguments for the method
98  */
99  public static function __callStatic( $method , $args = null )
100  {
101  $name = 'default'; // use the default connection
102  if ( !static::_initializeConnection( $name ) ){ return false; }
103  if ( $qb =@static::$_connections[ $name ][ 'query_builder' ] ) // call query builder
104  {
105  if( in_array( $method , get_class_methods( $qb ) ) )
106  {
107  return call_user_func_array( array( $qb , $method ) , $args );
108  }
109  }
110  else // call the pdo object methods
111  {
112  $pdo =static::$_connections[ $name ][ 'pdo_object' ];
113  return call_user_func_array( array( $pdo , $method ) , $args );
114  }
115  trigger_error( 'Call to undefined method "' .$method . '"!' , E_USER_ERROR );
116  return false;
117  }
118  /**
119  * Default connection options property, see @ref connection_options
120  */
121  protected static $_connectionOptions = array
122  (
123  'name' => 'default' , // the connection name
124  'driver' => 'mysql' , // the driver for the pdo object
125  'user' => 'root' , // the database username
126  'pass' => '' , // the username password
127  'host' => 'localhost' , // the database host
128  'db' => 'database' , // the database name
129  'charset' => 'utf8' , // the database charset
130  'query_builder' => false , // use the query builder component
131  'query_builder_class' => 'PtcQueryBuilder' , // the name of the query builder class
132  'pdo_attributes' => array // attributes for the pdo object
133  (
134  PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING ,
135  PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
136  )
137  );
138  /**
139  * Pdo and query builder objects property
140  */
141  protected static $_connections = array( );
142  /**
143  * Connection details property
144  */
145  protected static $_connectionsDetails = array( );
146  /**
147  * Initializes the pdo and query builder obejcts
148  * @param string $name the name of the connection
149  */
150  protected static function _initializeConnection( $name )
151  {
152  if ( !array_key_exists( $name , static::$_connections ) )
153  {
154  $options = static::$_connectionsDetails[ $name ];
155  static::$_connections[ $name ][ 'pdo_object' ] = new \PDO(
156  static::_pdoDriver( $options[ 'driver' ] , $options[ 'host' ] ) .
157  ';dbname=' . $options[ 'db' ] . ';charset:' . $options[ 'charset' ] .';' ,
158  $options[ 'user' ] , $options [ 'pass' ] );
159  if ( !static::$_connections[ $name ][ 'pdo_object' ]){ return false; } // pdo failed
160  foreach ( $options[ 'pdo_attributes' ] as $k => $v )
161  {
162  static::$_connections[ $name ][ 'pdo_object' ]->setAttribute( $k , $v );
163  }
164  if ( $options[ 'query_builder' ] )
165  {
166  $qb =false;
167  if ( class_exists( $options[ 'query_builder_class' ] ) ||
168  file_exists( dirname(__FILE__) . $options[ 'query_builder_class' ] . '.php' ) ){ $qb = true; }
169  if ( !$qb )
170  {
171  trigger_error( 'Class "' . $options[ 'query_builder_class' ] .
172  '" not found , please include the class file manually!' , E_USER_ERROR );
173  return false;
174  }
175  static::$_connections[ $name ][ 'query_builder' ] =
176  new $options[ 'query_builder_class' ]( static::$_connections[ $name ][ 'pdo_object' ] );
177  }
178  static::_debug( array( 'details' => static::$_connectionsDetails[ $name ] ,
179  'connection' => static::$_connections[ $name ] ) , 'connection <b>"' .
180  $name . '"</b> initialized' , 'Connection Manager' );
181  }
182  return true;
183  }
184  /**
185  * Checks if a given connection exists
186  * @param string $name the name of the connection to check
187  * @return true if connection exists, false otherwise
188  */
189  protected static function _checkConnectionName( $name )
190  {
191  if ( !array_key_exists( $name , static::$_connectionsDetails ) )
192  {
193  trigger_error( 'Could not find connection details with name "' .
194  $name . '"!' , E_USER_ERROR );
195  return false;
196  }
197  return true;
198  }
199  /**
200  * Builds the pdo driver
201  * @param string $driver the driver type
202  * @param string $host the database server host
203  * @return the Pdo driver
204  */
205  protected static function _pdoDriver( $driver , $host )
206  {
207  switch( $driver )
208  {
209  case 'mysql' :
210  default : return 'mysql:host=' . $host;
211  }
212  }
213  /**
214  * Sends messsages to the PtcDebug class if present
215  * @param mixed $string the string to pass
216  * @param mixed $statement some statement if required
217  * @param string $category some category
218  */
219  protected static function _debug( $string , $statement = null , $category = null )
220  {
221  if ( !defined( '_PTCDEBUG_NAMESPACE_' ) ) { return false; }
222  return @call_user_func_array( array( '\\' . _PTCDEBUG_NAMESPACE_ , 'bufferSql' ) ,
223  array( $string , $statement , $category ) );
224  }
225  }
static __callStatic($method, $args=null)
Calls Pdo or query builder methods from the default connection directly.
Definition: PtcDb.php:99
static getPdo($name)
Retrieves the Pdo object.
Definition: PtcDb.php:69
static getQB($name)
Retreives the query builder object if present.
Definition: PtcDb.php:80
static add($options, $name= 'default')
Adds a connection to the manager, will trigger an error if connection name is already present...
Definition: PtcDb.php:22
Definition: PtcDb.php:13
static $_connectionOptions
Default connection options property, see Connection Options.
Definition: PtcDb.php:121
static _debug($string, $statement=null, $category=null)
Sends messsages to the PtcDebug class if present.
Definition: PtcDb.php:219
static $_connections
Pdo and query builder objects property.
Definition: PtcDb.php:141
static _initializeConnection($name)
Initializes the pdo and query builder obejcts.
Definition: PtcDb.php:150
static _pdoDriver($driver, $host)
Builds the pdo driver.
Definition: PtcDb.php:205
static $_connectionsDetails
Connection details property.
Definition: PtcDb.php:145
static _checkConnectionName($name)
Checks if a given connection exists.
Definition: PtcDb.php:189
static getConnection($name=null)
Retrieves connection details previously configured.
Definition: PtcDb.php:58