Issues (1474)

framework/Data/TDataSourceConfig.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * TDataSourceConfig class file.
5
 *
6
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Data;
12
13
use Prado\Exceptions\TConfigurationException;
14
use Prado\Prado;
15
use Prado\TApplication;
16
17
/**
18
 * TDataSourceConfig module class provides <module> configuration for database connections.
19
 *
20
 * Example usage: mysql connection
21
 * ```php
22
 * <modules>
23
 * 	<module id="db1">
24
 * 		<database ConnectionString="mysqli:host=localhost;dbname=test"
25
 * 			username="dbuser" password="dbpass" />
26
 * 	</module>
27
 * </modules>
28
 * ```
29
 *
30
 * Usage in php:
31
 * ```php
32
 * class Home extends TPage
33
 * {
34
 * 		function onLoad($param)
35
 * 		{
36
 * 			$db = $this->Application->Modules['db1']->DbConnection;
37
 * 			$db->createCommand('...'); //...
38
 * 		}
39
 * }
40
 * ```
41
 *
42
 * The properties of <connection> are those of the class TDbConnection.
43
 * Set {@see setConnectionClass} attribute for a custom database connection class
44
 * that extends the TDbConnection class.
45
 *
46
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
47
 * @since 3.1
48
 */
49
class TDataSourceConfig extends \Prado\TModule
50
{
51
	private $_connID = '';
52
	private $_conn;
53
	private $_connClass = \Prado\Data\TDbConnection::class;
54
55
	/**
56
	 * Initalize the database connection properties from attributes in <database> tag.
57
	 * @param \Prado\Xml\TXmlDocument $config xml or php configuration.
58
	 */
59
	public function init($config)
60
	{
61
		if ($this->getApplication()->getConfigurationType() == TApplication::CONFIG_TYPE_PHP) {
62
			if (isset($config['database']) && is_array($config['database'])) {
63
				$db = $this->getDbConnection();
64
				foreach ($config['database'] as $name => $value) {
65
					$db->setSubProperty($name, $value);
66
				}
67
			}
68
		} else {
69
			if ($prop = $config->getElementByTagName('database')) {
0 ignored issues
show
Are you sure the assignment to $prop is correct as $config->getElementByTagName('database') targeting Prado\Xml\TXmlElement::getElementByTagName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

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.

Loading history...
70
				$db = $this->getDbConnection();
71
				foreach ($prop->getAttributes() as $name => $value) {
72
					$db->setSubproperty($name, $value);
73
				}
74
			}
75
		}
76
		parent::init($config);
77
	}
78
79
	/**
80
	 * The module ID of another TDataSourceConfig. The {@see getDbConnection DbConnection}
81
	 * property of this configuration will equal to {@see getDbConnection DbConnection}
82
	 * of the given TDataSourceConfig module.
83
	 * @param string $value module ID.
84
	 */
85
	public function setConnectionID($value)
86
	{
87
		$this->_connID = $value;
88
	}
89
90
	/**
91
	 * @return string connection module ID.
92
	 */
93
	public function getConnectionID()
94
	{
95
		return $this->_connID;
96
	}
97
98
	/**
99
	 * Gets the TDbConnection from another module if {@see setConnectionID ConnectionID}
100
	 * is supplied and valid. Otherwise, a connection of type given by
101
	 * {@see setConnectionClass ConnectionClass} is created.
102
	 * @return \Prado\Data\TDbConnection database connection.
103
	 */
104 1
	public function getDbConnection()
105
	{
106 1
		if ($this->_conn === null) {
107 1
			if ($this->_connID !== '') {
108
				$this->_conn = $this->findConnectionByID($this->getConnectionID());
109
			} else {
110 1
				$this->_conn = Prado::createComponent($this->getConnectionClass());
111
			}
112
		}
113 1
		return $this->_conn;
114
	}
115
116
	/**
117
	 * Alias for getDbConnection().
118
	 * @return \Prado\Data\TDbConnection database connection.
119
	 */
120
	public function getDatabase()
121
	{
122
		return $this->getDbConnection();
123
	}
124
125
	/**
126
	 * @return string Database connection class name to be created.
127
	 */
128 1
	public function getConnectionClass()
129
	{
130 1
		return $this->_connClass;
131
	}
132
133
	/**
134
	 * The database connection class name to be created when {@see getDbConnection}
135
	 * method is called <b>and</b> {@see setConnectionID ConnectionID} is null. The
136
	 * {@see setConnectionClass ConnectionClass} property must be set before
137
	 * calling {@see getDbConnection} if you wish to create the connection using the
138
	 * given class name.
139
	 * @param string $value Database connection class name.
140
	 * @throws TConfigurationException when database connection is already established.
141
	 */
142
	public function setConnectionClass($value)
143
	{
144
		if ($this->_conn !== null) {
145
			throw new TConfigurationException('datasource_dbconnection_exists', $value);
146
		}
147
		$this->_connClass = $value;
148
	}
149
150
	/**
151
	 * Finds the database connection instance from the Application modules.
152
	 * @param string $id Database connection module ID.
153
	 * @throws TConfigurationException when module is not of TDbConnection or TDataSourceConfig.
154
	 * @return \Prado\Data\TDbConnection database connection.
155
	 */
156
	protected function findConnectionByID($id)
157
	{
158
		$conn = $this->getApplication()->getModule($id);
159
		if ($conn instanceof TDbConnection) {
160
			return $conn;
161
		} elseif ($conn instanceof TDataSourceConfig) {
162
			return $conn->getDbConnection();
163
		} else {
164
			throw new TConfigurationException('datasource_dbconnection_invalid', $id);
165
		}
166
	}
167
}
168