|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TDbPluginModule class file |
|
5
|
|
|
* |
|
6
|
|
|
* @author Brad Anderson <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Util; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Data\TDataSourceConfig; |
|
14
|
|
|
use Prado\Data\TDbConnection; |
|
15
|
|
|
use Prado\Exceptions\TConfigurationException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TDbPluginModule class. |
|
19
|
|
|
* |
|
20
|
|
|
* TDbPluginModule adds database connectivity to the plugin modules. This standardizes |
|
21
|
|
|
* the Database Connectivity for Plugins. Also TParameterizeBehavior can be used to set |
|
22
|
|
|
* all TDbPluginModule::ConnectionID with one setting. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Brad Anderson <[email protected]> |
|
25
|
|
|
* @since 4.2.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
class TDbPluginModule extends TPluginModule implements \Prado\Util\IDbModule |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string the ID of TDataSourceConfig module |
|
31
|
|
|
*/ |
|
32
|
|
|
private $_connID = ''; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var TDbConnection the DB connection instance |
|
35
|
|
|
*/ |
|
36
|
|
|
private $_conn; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return string the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getConnectionID() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->_connID; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Sets the ID of a TDataSourceConfig module. |
|
48
|
|
|
* The datasource module will be used to establish the DB connection |
|
49
|
|
|
* that will be used by the plugin. |
|
50
|
|
|
* @param string $value module ID. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function setConnectionID($value) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->_connID = $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return TDbConnection the database connection that may be used to retrieve user data. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getDbConnection() |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->_conn === null) { |
|
63
|
|
|
$this->_conn = $this->createDbConnection($this->_connID); |
|
64
|
|
|
$this->_conn->setActive(true); |
|
65
|
|
|
} |
|
66
|
|
|
return $this->_conn; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Creates the DB connection. If no ConnectionId is available, this |
|
71
|
|
|
* will try to start a sqlite database if the subclass has a name via |
|
72
|
|
|
* {@see getSqliteDatabaseName}. |
|
73
|
|
|
* @param string $connectionID the module ID for TDataSourceConfig |
|
74
|
|
|
* @throws TConfigurationException if module ID is invalid or empty |
|
75
|
|
|
* without a Sqlite database. |
|
76
|
|
|
* @return TDbConnection the created DB connection |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function createDbConnection($connectionID) |
|
79
|
|
|
{ |
|
80
|
|
|
if ($connectionID !== '') { |
|
81
|
|
|
$conn = $this->getApplication()->getModule($connectionID); |
|
82
|
|
|
if ($conn instanceof TDataSourceConfig) { |
|
83
|
|
|
return $conn->getDbConnection(); |
|
84
|
|
|
} else { |
|
85
|
|
|
throw new TConfigurationException('dbpluginmodule_connectionid_invalid', $connectionID); |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
if ($file = $this->getSqliteDatabaseName()) { |
|
|
|
|
|
|
89
|
|
|
$db = new TDbConnection(); |
|
90
|
|
|
// default to SQLite3 database |
|
91
|
|
|
$dbFile = $this->getApplication()->getRuntimePath() . DIRECTORY_SEPARATOR . $file; |
|
|
|
|
|
|
92
|
|
|
$db->setConnectionString('sqlite:' . $dbFile); |
|
93
|
|
|
return $db; |
|
94
|
|
|
} else { |
|
95
|
|
|
throw new TConfigurationException('dbpluginmodule_connectionid_required'); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return null|string if the sub-class wants a sqlite db then return the name. |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getSqliteDatabaseName() |
|
104
|
|
|
{ |
|
105
|
|
|
return null; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.