1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Database; |
20
|
|
|
|
21
|
|
|
use PDOException; |
22
|
|
|
|
23
|
|
|
define('PDO_DEFAULT_ADAPTER', 'mysql'); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* \addtogroup Modules |
27
|
|
|
* @{ |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** \class Handler Handler Database connection handler |
31
|
|
|
*/ |
32
|
|
|
trait Handler |
33
|
|
|
{ |
34
|
|
|
/** @} */ |
35
|
|
|
|
36
|
|
|
/** Pdo connection to the database. */ |
37
|
|
|
public $pdo; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* \addtogroup Bot Bot |
41
|
|
|
* @{ |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* \brief Open a database connection using PDO. |
46
|
|
|
* \details Provides a simpler way to initialize a database connection |
47
|
|
|
* and create a PDO instance. |
48
|
|
|
* @param array $params Parameters for initialize connection. |
49
|
|
|
* Index required: |
50
|
|
|
* - <code>username</code> |
51
|
|
|
* - <code>password</code> (can be a null string) |
52
|
|
|
* Optional index: |
53
|
|
|
* - <code>adapter</code> <b>Default</b>: <code>mysql</code> |
54
|
|
|
* - <code>host</code> <b>Default</b>: <code>localhost</code> |
55
|
|
|
* - <code>options</code> (<i>Array of options passed when creating pdo object</i>) |
56
|
|
|
* @return bool True when the connection is succefully created. |
57
|
|
|
*/ |
58
|
|
|
public function connect(array $params) : bool |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$config = $this->getDNS($this->mergeWithDefaults($params)); |
|
|
|
|
62
|
|
|
|
63
|
1 |
|
$this->pdo = new \PDO($config, $params['username'], $params['password'], $params['option']); |
64
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
65
|
1 |
|
|
66
|
1 |
|
return true; |
67
|
|
|
} catch (PDOException $e) { |
68
|
|
|
echo 'Unable to connect to database, an error occured:' . $e->getMessage(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
2 |
|
} |
73
|
|
|
|
74
|
2 |
|
/** \brief (<i>Internal</i>) Add default connection value to parameter passed to pdo. |
75
|
2 |
|
* @param array $params Parameter for PDO connection. |
76
|
2 |
|
* @return array Parameter with defaults value. |
77
|
|
|
*/ |
78
|
2 |
|
protected function addDefaultValue(array $params) : array |
79
|
|
|
{ |
80
|
|
|
static $defaults = [ 'adapter' => PDO_DEFAULT_ADAPTER, 'host' => 'localhost' ]; |
81
|
|
|
return array_merge($defaults, $params); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
/** \brief (<i>Internal</i>) Returns a string that can passed to PDO as DNS parameter in order to open connection. |
85
|
|
|
* @param array $params Array containing parameter of the connection |
86
|
|
|
* @return string Parameters contained in array $params sanitized in a string that can be passed as DNS param of PDO object creation. |
87
|
|
|
*/ |
88
|
|
|
protected function getDNS($params) : string |
89
|
2 |
|
{ |
90
|
|
|
$response = $params['adapter'] . ':'; |
91
|
|
|
unset($params['adapter']); |
92
|
2 |
|
$fields = []; |
93
|
|
|
|
94
|
|
|
foreach ($params as $field => $value) { |
95
|
|
|
/** |
96
|
|
|
*Check if the current field matches one of the fields |
97
|
|
|
* that are passed to PDO in another way and so don't need |
98
|
|
|
* to be included in the string. |
99
|
|
|
*/ |
100
|
|
|
if ($field === 'username' || $field === 'password') { |
101
|
|
|
unset($params[$field]); |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$fields[] = $field . '=' . $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $response . join(';', $fields); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @} */ |
112
|
|
|
} |
113
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.