Skip to content

Commit 295a6bc

Browse files
committed
Completed src files
Completed src files and added example.
1 parent 808a962 commit 295a6bc

File tree

6 files changed

+471
-69
lines changed

6 files changed

+471
-69
lines changed

src/autoload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Load the models
44
*
5-
* Sadly we can't use an autoloader here incase the end-user
5+
* Sadly we can't use an autoloader here in the case that the end-user
66
* is using one. Multiple autoloaders can cause conflicts
77
*
88
* Likel/Session/Handler can be called like this:
@@ -18,5 +18,5 @@
1818
*/
1919

2020
// Require the models
21-
require_once(__DIR__ . '/models/Handler.php');
2221
require_once(__DIR__ . '/models/DB.php');
22+
require_once(__DIR__ . '/models/Session/Handler.php');

src/example.php

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

13-
ini_set('display_errors', 1);
14-
ini_set('display_startup_errors', 1);
15-
error_reporting(E_ALL);
13+
header('Content-Type: text/plain');
1614

15+
// Load the scripts, alternatively use a custom PSR-4 autoloader
1716
include('autoload.php');
1817

18+
// This looks nicer and makes the session Handler easier to call
1919
use Likel\Session\Handler as LikelSession;
2020

21+
// Create a new session. Example parameters include:
22+
// $session = new LikelSession(array(
23+
// 'session_name' => "YourCustomSessionName",
24+
// 'credentials_location' => "/path/to/new/credentials.ini",
25+
// 'secure' => true
26+
// ));
2127
$session = new LikelSession();
22-
$session->getSession();
28+
29+
// Set some session variables
30+
if(!isset($session["user_id"])) {
31+
$session["user_id"] = 1;
32+
$session["name"] = "Liam";
33+
$session["preferences"] = array(
34+
'language' => 'en-AU'
35+
);
36+
$_SESSION["test"] = "works";
37+
}
38+
39+
// Unset our name
40+
unset($session["name"]);
41+
42+
// Dump some variables and the session
43+
echo "The session_id is: " . session_id() . PHP_EOL . PHP_EOL;
44+
echo "The name is (should be blank): " . $session["name"] . PHP_EOL . PHP_EOL;
45+
echo "The user_id is: " . $session["user_id"] . PHP_EOL . PHP_EOL;
46+
echo "The preferences are: " . print_r($session["preferences"], true) . PHP_EOL;
47+
echo "The \$_SESSION variable still: " . $_SESSION["test"] . PHP_EOL . PHP_EOL;
48+
var_dump($session);
49+
50+
// You should no check the database table to see if a row has been added

src/ini/credentials.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
;
77
; You can set the .ini path when creating a new LikelSession object like so:
88
;
9-
; $session = new LikelSession("/path/to/new/ini/credentials.ini");
9+
; $session = new LikelSession(array(
10+
; "credentials_location" => "/path/to/new/credentials.ini"
11+
; ));
1012
;
1113
; @package php-simple-sessions
1214
; @author Liam Kelly <https://github.com/likel>
@@ -21,3 +23,6 @@ username = "root"
2123
password = "root"
2224
db_name = "likel_sessions"
2325
table_prefix = "likel_"
26+
27+
[likel_session]
28+
secret_hash = "BnFEC}}[CvP_djqM~[sbcXZp`/N:Y3Vu28m9zX~@:[gM,BNTYA]nFwd}>(nJ*hse"

src/models/DB.php

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* The database object which helps abstract database functions
3+
* The database object which helps to abstract database functions
44
*
55
* Uses and requires PDO, generally available after PHP 5.1
66
*
@@ -11,32 +11,41 @@
1111
* @link https://github.com/likel/php-simple-sessions
1212
* @version 1.0.0
1313
*/
14-
namespace Likel\Session;
14+
namespace Likel;
1515

1616
class DB
1717
{
18-
private $dbh;
19-
private $stmt;
20-
private $table_prefix;
18+
private $database_handler; // Stores the database connection
19+
private $statement; // The MySQL query with prepared values
20+
private $table_prefix; // The table prefix from the credentials.ini file
2121

2222
/**
2323
* Construct the database object
2424
*
2525
* @param string $credentials_location The location of the credential file
2626
* @return void
2727
*/
28-
public function __construct($credentials_location = __DIR__ . '/../ini/credentials.ini')
28+
public function __construct($credentials_location)
2929
{
3030
try {
3131
$db_credentials = parse_ini_file($credentials_location, true);
32-
$this->dbh = $this->loadDatabase($db_credentials["likel_db"]);
32+
$this->database_handler = $this->loadDatabase($db_credentials["likel_db"]);
3333
$this->table_prefix = $db_credentials["likel_db"]["table_prefix"];
3434
} catch (\Exception $ex) {
3535
throw $ex;
3636
}
3737
}
3838

39-
private function loadDatabase($credentials) {
39+
/**
40+
* Attempt to retrieve the likel_db ini array and connect to the database
41+
*
42+
* @param array $credentials likel_db from the credentials.ini file
43+
* @return mixed
44+
* @throws \Exception If credentials empty or not found
45+
* @throws \PDOException If PDO connection is unsuccessful
46+
*/
47+
private function loadDatabase($credentials)
48+
{
4049
if(!empty($credentials)){
4150
try {
4251
$dsn = 'mysql:host=' . $credentials['host'] . ';dbname=' . $credentials['db_name'];
@@ -51,7 +60,7 @@ private function loadDatabase($credentials) {
5160
return $pdo_object;
5261

5362
} catch(\PDOException $e) {
54-
throw new \Exception($e);
63+
throw new \Exception($e->getMessage());
5564
}
5665

5766
} else {
@@ -61,15 +70,23 @@ private function loadDatabase($credentials) {
6170

6271
/**
6372
* Prepare the query from a supplied query string
73+
*
74+
* @param string $query The prepared query
75+
* @return void
6476
*/
6577
public function query($query)
6678
{
67-
$this->stmt = $this->dbh->prepare($query);
79+
$this->statement = $this->database_handler->prepare($query);
6880
}
6981

7082
/**
7183
* Bind properties to the statement
7284
* E.G. $DB->bind(':fname', 'Liam');
85+
*
86+
* @param string $param The parameter to replace
87+
* @param mixed $value The value replacement
88+
* @param mixed $type Force the PDO::PARAM type
89+
* @return void
7390
*/
7491
public function bind($param, $value, $type = null)
7592
{
@@ -89,46 +106,56 @@ public function bind($param, $value, $type = null)
89106
}
90107
}
91108

92-
$this->stmt->bindValue($param, $value, $type);
109+
$this->statement->bindValue($param, $value, $type);
93110
}
94111

95112
/**
96113
* Execute the statement
97114
* Use result()/results() for insert queries
115+
*
116+
* @return bool
98117
*/
99118
public function execute()
100119
{
101-
return $this->stmt->execute();
120+
return $this->statement->execute();
102121
}
103122

104123
/**
105124
* Return multiple rows
125+
*
126+
* @return array
106127
*/
107128
public function results()
108129
{
109130
$this->execute();
110-
return $this->stmt->fetchAll(\PDO::FETCH_ASSOC);
131+
return $this->statement->fetchAll(\PDO::FETCH_ASSOC);
111132
}
112133

113134
/**
114135
* Return a single row
136+
*
137+
* @return array
115138
*/
116139
public function result()
117140
{
118141
$this->execute();
119-
return $this->stmt->fetch(\PDO::FETCH_ASSOC);
142+
return $this->statement->fetch(\PDO::FETCH_ASSOC);
120143
}
121144

122145
/**
123146
* Return the row count
147+
*
148+
* @return int
124149
*/
125150
public function rowCount()
126151
{
127-
return $this->stmt->rowCount();
152+
return $this->statement->rowCount();
128153
}
129154

130155
/**
131156
* Return if rows exists
157+
*
158+
* @return bool
132159
*/
133160
public function rowsExist()
134161
{
@@ -137,38 +164,49 @@ public function rowsExist()
137164

138165
/**
139166
* Return the id of the last inserted row
167+
*
168+
* @return mixed
140169
*/
141170
public function lastInsertId()
142171
{
143-
return $this->dbh->lastInsertId();
172+
return $this->database_handler->lastInsertId();
144173
}
145174

146175
/**
147176
* Begin a transaction for multiple statements
177+
*
178+
* @return bool
148179
*/
149180
public function beginTransaction()
150181
{
151-
return $this->dbh->beginTransaction();
182+
return $this->database_handler->beginTransaction();
152183
}
153184

154185
/**
155186
* Commit the transaction for multiple statements
187+
*
188+
* @return bool
156189
*/
157190
public function endTransaction()
158191
{
159-
return $this->dbh->commit();
192+
return $this->database_handler->commit();
160193
}
161194

162195
/**
163196
* Roll back the transaction
197+
*
198+
* @return bool
164199
*/
165200
public function cancelTransaction()
166201
{
167-
return $this->dbh->rollBack();
202+
return $this->database_handler->rollBack();
168203
}
169204

170205
/**
171206
* Return the table name with prefix
207+
*
208+
* @param string $table_name The table name that's accessed
209+
* @return string
172210
*/
173211
public function getTableName($table_name)
174212
{
@@ -177,9 +215,11 @@ public function getTableName($table_name)
177215

178216
/**
179217
* Dump the statement's current parameters
218+
*
219+
* @return void
180220
*/
181221
public function dumpStatement()
182222
{
183-
return $this->stmt->debugDumpParams();
223+
$this->statement->debugDumpParams();
184224
}
185225
}

src/models/Handler.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)