Skip to content

Commit 808a962

Browse files
committed
WIP models and example
1 parent 7aeb15f commit 808a962

File tree

5 files changed

+66
-55
lines changed

5 files changed

+66
-55
lines changed

src/autoload.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
* Sadly we can't use an autoloader here incase the end-user
66
* is using one. Multiple autoloaders can cause conflicts
77
*
8-
* Likel/Session/Handler can be called with the friendly name
9-
* LikelSession, for example:
8+
* Likel/Session/Handler can be called like this:
109
*
11-
* $session = new LikelSession();
10+
* $session = new Likel\Session\Handler();
1211
*
1312
* @package php-simple-sessions
1413
* @author Liam Kelly <https://github.com/likel>
@@ -17,10 +16,7 @@
1716
* @link https://github.com/likel/php-simple-sessions
1817
* @version 1.0.0
1918
*/
20-
namespace Likel\Session;
2119

2220
// Require the models
2321
require_once(__DIR__ . '/models/Handler.php');
2422
require_once(__DIR__ . '/models/DB.php');
25-
26-
use Likel\Session\Handler as LikelSession;

src/example.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
* @version 1.0.0
1111
*/
1212

13-
require_once('autoload.php');
13+
ini_set('display_errors', 1);
14+
ini_set('display_startup_errors', 1);
15+
error_reporting(E_ALL);
1416

15-
$db = new LikelSession();
17+
include('autoload.php');
18+
19+
use Likel\Session\Handler as LikelSession;
20+
21+
$session = new LikelSession();
22+
$session->getSession();

src/ini/credentials.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
host = "localhost"
2020
username = "root"
2121
password = "root"
22-
db_name = "likel_session"
23-
table_prefix = "likel"
22+
db_name = "likel_sessions"
23+
table_prefix = "likel_"

src/models/DB.php

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
/**
3-
* This is the DB object
3+
* The database object which helps abstract database functions
4+
*
5+
* Uses and requires PDO, generally available after PHP 5.1
46
*
57
* @package php-simple-sessions
68
* @author Liam Kelly <https://github.com/likel>
@@ -15,142 +17,150 @@ class DB
1517
{
1618
private $dbh;
1719
private $stmt;
20+
private $table_prefix;
1821

22+
/**
23+
* Construct the database object
24+
*
25+
* @param string $credentials_location The location of the credential file
26+
* @return void
27+
*/
1928
public function __construct($credentials_location = __DIR__ . '/../ini/credentials.ini')
2029
{
2130
try {
2231
$db_credentials = parse_ini_file($credentials_location, true);
23-
$this->dbh = loadDatabase($db_credentials["likel_db"]);
24-
} catch (Exception $ex) {
32+
$this->dbh = $this->loadDatabase($db_credentials["likel_db"]);
33+
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
34+
} catch (\Exception $ex) {
2535
throw $ex;
2636
}
2737
}
2838

2939
private function loadDatabase($credentials) {
30-
if(!empty($credentials["likel_db"])){
40+
if(!empty($credentials)){
3141
try {
32-
$dsn = 'mysql:host=' . $credentials["likel_db"]['host'] . ';dbname=' . $credentials["likel_db"]['db_name'];
42+
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
3343

3444
$options = array(
35-
PDO::ATTR_PERSISTENT => true,
36-
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
45+
\PDO::ATTR_PERSISTENT => true,
46+
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
3747
);
3848

39-
$pdo_object = new PDO($dsn, $credentials["likel_db"]['username'], $credentials["likel_db"]['password'], $options);
49+
$pdo_object = new \PDO($dsn, $credentials['username'], $credentials['password'], $options);
4050

4151
return $pdo_object;
4252

43-
} catch(PDOException $e) {
44-
throw new Exception('An error occured when attempting to connect to the database.');
53+
} catch(\PDOException $e) {
54+
throw new \Exception($e);
4555
}
4656

4757
} else {
48-
throw new Exception('The credential file could not be located or is empty.');
58+
throw new \Exception('The credential file could not be located or is empty.');
4959
}
5060
}
5161

5262
/**
53-
* Prepare the query from a supplied query string.
63+
* Prepare the query from a supplied query string
5464
*/
5565
public function query($query)
5666
{
5767
$this->stmt = $this->dbh->prepare($query);
5868
}
5969

6070
/**
61-
* Bind properties to the statement.
71+
* Bind properties to the statement
6272
* E.G. $DB->bind(':fname', 'Liam');
6373
*/
6474
public function bind($param, $value, $type = null)
6575
{
6676
if (is_null($type)) {
6777
switch (true) {
6878
case is_int($value):
69-
$type = PDO::PARAM_INT;
79+
$type = \PDO::PARAM_INT;
7080
break;
7181
case is_bool($value):
72-
$type = PDO::PARAM_BOOL;
82+
$type = \PDO::PARAM_BOOL;
7383
break;
7484
case is_null($value):
75-
$type = PDO::PARAM_NULL;
85+
$type = \PDO::PARAM_NULL;
7686
break;
7787
default:
78-
$type = PDO::PARAM_STR;
88+
$type = \PDO::PARAM_STR;
7989
}
8090
}
8191

8292
$this->stmt->bindValue($param, $value, $type);
8393
}
8494

8595
/**
86-
* Execute the statement.
87-
* Use result()/results() for insert queries.
96+
* Execute the statement
97+
* Use result()/results() for insert queries
8898
*/
8999
public function execute()
90100
{
91101
return $this->stmt->execute();
92102
}
93103

94104
/**
95-
* Return multiple rows.
105+
* Return multiple rows
96106
*/
97107
public function results()
98108
{
99109
$this->execute();
100-
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
110+
return $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
101111
}
102112

103113
/**
104-
* Return a single row.
114+
* Return a single row
105115
*/
106116
public function result()
107117
{
108118
$this->execute();
109-
return $this->stmt->fetch(PDO::FETCH_ASSOC);
119+
return $this->stmt->fetch(\PDO::FETCH_ASSOC);
110120
}
111121

112122
/**
113-
* Return the row count.
123+
* Return the row count
114124
*/
115125
public function rowCount()
116126
{
117127
return $this->stmt->rowCount();
118128
}
119129

120130
/**
121-
* Return if rows exists.
131+
* Return if rows exists
122132
*/
123133
public function rowsExist()
124134
{
125135
return $this->rowCount() != 0;
126136
}
127137

128138
/**
129-
* Return the id of the last inserted row.
139+
* Return the id of the last inserted row
130140
*/
131141
public function lastInsertId()
132142
{
133143
return $this->dbh->lastInsertId();
134144
}
135145

136146
/**
137-
* Begin a transaction for multiple statements.
147+
* Begin a transaction for multiple statements
138148
*/
139149
public function beginTransaction()
140150
{
141151
return $this->dbh->beginTransaction();
142152
}
143153

144154
/**
145-
* Commit the transaction for multiple statements.
155+
* Commit the transaction for multiple statements
146156
*/
147157
public function endTransaction()
148158
{
149159
return $this->dbh->commit();
150160
}
151161

152162
/**
153-
* Roll back the transaction.
163+
* Roll back the transaction
154164
*/
155165
public function cancelTransaction()
156166
{
@@ -160,13 +170,13 @@ public function cancelTransaction()
160170
/**
161171
* Return the table name with prefix
162172
*/
163-
public function tb($tablename)
173+
public function getTableName($table_name)
164174
{
165-
return $this->tableprefix.$tablename;
175+
return $this->table_prefix . $table_name;
166176
}
167177

168178
/**
169-
* Dump the statement's current parameters.
179+
* Dump the statement's current parameters
170180
*/
171181
public function dumpStatement()
172182
{

src/models/Handler.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,30 @@
1313

1414
class Handler
1515
{
16-
// Helper variables used in the object
17-
private $DB;
16+
// Store the database variable
17+
private $db;
1818

1919
/**
20-
* Construct the FizzBuzz object
21-
* Sets up the range, defaults to 1-100 if no range set
20+
* Construct the session Handler object
2221
*
23-
* @param int $range_min The minimum range for the sequence
24-
* @param int $range_max The maximum range for the sequence
2522
* @return void
2623
*/
2724
function __construct()
2825
{
29-
$this->DB = new Likel\Session\DB();
26+
$this->db = new DB();
3027
}
3128

3229
/**
33-
* Sets the range for the FizzBuzz test
34-
* Expects is_numeric params
3530
*
36-
* @param int $range_min The minimum range for the sequence
37-
* @param int $range_max The maximum range for the sequence
38-
* @return void
31+
*
32+
* @return
3933
*/
40-
public function setRange($range_min, $range_max)
34+
public function getSession()
4135
{
36+
$this->db->query("
37+
SELECT * FROM {$this->db->getTableName("sessions")}
38+
");
4239

40+
return $this->db->execute();
4341
}
4442
}

0 commit comments

Comments
 (0)