Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
4b063ac0
Commit
4b063ac0
authored
Jul 07, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring database and connector classes.
parent
77d65b89
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
20 deletions
+36
-20
system/db.php
+8
-15
system/db/connector.php
+28
-5
No files found.
system/db.php
View file @
4b063ac0
...
@@ -7,10 +7,13 @@ class DB {
...
@@ -7,10 +7,13 @@ class DB {
*
*
* @var array
* @var array
*/
*/
p
rivate
static
$connections
=
array
();
p
ublic
static
$connections
=
array
();
/**
/**
* Get a database connection. Database connections are managed as singletons.
* Get a database connection. If no database name is specified, the default
* connection will be returned as defined in the db configuration file.
*
* Note: Database connections are managed as singletons.
*
*
* @param string $connection
* @param string $connection
* @return PDO
* @return PDO
...
@@ -22,19 +25,9 @@ class DB {
...
@@ -22,19 +25,9 @@ class DB {
$connection
=
Config
::
get
(
'db.default'
);
$connection
=
Config
::
get
(
'db.default'
);
}
}
if
(
!
array_key_exists
(
$connection
,
static
::
$connections
))
return
array_key_exists
(
$connection
,
static
::
$connections
)
{
?
static
::
$connections
[
$connection
]
$config
=
Config
::
get
(
'db.connections'
);
:
static
::
$connections
[
$connection
]
=
DB\Connector
::
connect
(
$connection
);
if
(
!
array_key_exists
(
$connection
,
$config
))
{
throw
new
\Exception
(
"Database connection [
$connection
] is not defined."
);
}
static
::
$connections
[
$connection
]
=
DB\Connector
::
connect
((
object
)
$config
[
$connection
]);
}
return
static
::
$connections
[
$connection
];
}
}
/**
/**
...
...
system/db/connector.php
View file @
4b063ac0
<?php
namespace
System\DB
;
<?php
namespace
System\DB
;
use
System\Config
;
class
Connector
{
class
Connector
{
/**
/**
...
@@ -17,11 +19,13 @@ class Connector {
...
@@ -17,11 +19,13 @@ class Connector {
/**
/**
* Establish a PDO database connection.
* Establish a PDO database connection.
*
*
* @param
object $config
* @param
string $connection
* @return PDO
* @return PDO
*/
*/
public
static
function
connect
(
$con
fig
)
public
static
function
connect
(
$con
nection
)
{
{
$config
=
static
::
configuration
(
$connection
);
if
(
$config
->
driver
==
'sqlite'
)
if
(
$config
->
driver
==
'sqlite'
)
{
{
return
static
::
connect_to_sqlite
(
$config
);
return
static
::
connect_to_sqlite
(
$config
);
...
@@ -37,14 +41,14 @@ class Connector {
...
@@ -37,14 +41,14 @@ class Connector {
/**
/**
* Establish a PDO connection to a SQLite database.
* Establish a PDO connection to a SQLite database.
*
*
* SQLite database paths can be specified either relative to the application/db
* directory, or as an absolute path to any location on the file system.
*
* @param array $config
* @param array $config
* @return PDO
* @return PDO
*/
*/
private
static
function
connect_to_sqlite
(
$config
)
private
static
function
connect_to_sqlite
(
$config
)
{
{
// Database paths can either be specified relative to the application/storage/db
// directory, or as an absolute path.
if
(
file_exists
(
$path
=
APP_PATH
.
'storage/db/'
.
$config
->
database
.
'.sqlite'
))
if
(
file_exists
(
$path
=
APP_PATH
.
'storage/db/'
.
$config
->
database
.
'.sqlite'
))
{
{
return
new
\PDO
(
'sqlite:'
.
$path
,
null
,
null
,
static
::
$options
);
return
new
\PDO
(
'sqlite:'
.
$path
,
null
,
null
,
static
::
$options
);
...
@@ -84,4 +88,22 @@ class Connector {
...
@@ -84,4 +88,22 @@ class Connector {
return
$connection
;
return
$connection
;
}
}
/**
* Get the configuration options for a database connection.
*
* @param string $connection
* @return object
*/
private
static
function
configuration
(
$connection
)
{
$config
=
Config
::
get
(
'db.connections'
);
if
(
!
array_key_exists
(
$connection
,
$config
))
{
throw
new
\Exception
(
"Database connection [
$connection
] is not defined."
);
}
return
(
object
)
$config
[
$connection
];
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment