1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* ParseDbConfigTask, Получает настройки бд.
|
4
|
|
|
*
|
5
|
|
|
* Настройки БД берутся из файла protected/config/db.php. Устанавливаются свойства db.driver, db.host, db.username, db.pass и db.prefix
|
6
|
|
|
* @author Fedor A Borshev <[email protected]>
|
7
|
|
|
* @link https://github.com/shogodev/argilla/
|
8
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
9
|
|
|
* @license http://argilla.ru/LICENSE
|
10
|
|
|
* @package build.tasks.ParseDbConfigTask
|
11
|
|
|
*
|
12
|
|
|
*/
|
13
|
|
|
require_once "phing/Task.php";
|
14
|
|
|
|
15
|
|
|
class ParseDbConfigTask extends Task
|
16
|
|
|
{
|
17
|
|
|
private $file;
|
18
|
|
|
|
19
|
|
|
private $pdo;
|
20
|
|
|
|
21
|
|
|
private $connected = false;
|
22
|
|
|
|
23
|
|
|
public function setFile($file)
|
24
|
|
|
{
|
25
|
|
|
$this->file = $file;
|
26
|
|
|
}
|
27
|
|
|
|
28
|
|
|
public function main()
|
29
|
|
|
{
|
30
|
|
|
if (!file_exists($this->file))
|
31
|
|
|
{
|
32
|
|
|
throw new BuildException('Cannot open db config file');
|
33
|
|
|
}
|
34
|
|
|
|
35
|
|
|
$db = require ($this->file);
|
36
|
|
|
|
37
|
|
|
$this->project->setProperty('db.username', $db['username']);
|
38
|
|
|
$this->project->setProperty('db.password', $db['password']);
|
39
|
|
|
$this->project->setProperty('db.prefix', $db['tablePrefix']);
|
40
|
|
|
|
41
|
|
|
$connectionString = preg_replace('/\s*/', '', $db['connectionString']);
|
42
|
|
|
|
43
|
|
|
$dbType = 'mysql';
|
|
|
|
|
44
|
|
|
preg_match('/^([^\:]+):/', $connectionString, $q);
|
45
|
|
|
$dbType = $q[1];
|
46
|
|
|
|
47
|
|
|
$this->project->setProperty('db.driver', $dbType);
|
48
|
|
|
|
49
|
|
|
$dsnVars = preg_split('/:|;/', preg_replace("/^$dbType:/",'',$connectionString)); // получаем список всех свойств драйвера из $dsn в виде key = value
|
50
|
|
|
|
51
|
|
|
foreach($dsnVars as $dsnVar) // переносим все свойства в проект, устанавливаем db.<свойство>
|
52
|
|
|
{
|
53
|
|
|
list($key, $value) = preg_split('/=/', $dsnVar);
|
54
|
|
|
$this->project->setProperty('db.' . $key, $value);
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
$this->connect();
|
58
|
|
|
$this->getMysqlUserHost();
|
59
|
|
|
}
|
60
|
|
|
|
61
|
|
|
private function getMysqlUserHost()
|
62
|
|
|
{
|
63
|
|
|
$q = $this->pdo->query('SELECT CURRENT_USER();');
|
64
|
|
|
$q->execute();
|
65
|
|
|
|
66
|
|
|
$row = $q->fetch();
|
67
|
|
|
|
68
|
|
|
list($user, $host) = explode('@', $row[0]);
|
69
|
|
|
|
70
|
|
|
$this->project->setProperty('db.mysqlUser', $user);
|
71
|
|
|
$this->project->setProperty('db.mysqlHost', $host);
|
72
|
|
|
}
|
73
|
|
|
|
74
|
|
|
private function connect()
|
75
|
|
|
{
|
76
|
|
|
$dsn = $this->project->getProperty('db.driver') . ':' . 'host=' . $this->project->getProperty('db.host');
|
77
|
|
|
try
|
78
|
|
|
{
|
79
|
|
|
$this->pdo = new PDO($dsn,
|
80
|
|
|
$this->project->getProperty('db.username'),
|
81
|
|
|
$this->project->getProperty('db.password')
|
82
|
|
|
);
|
83
|
|
|
} catch (PDOException $e) {
|
84
|
|
|
throw new BuildException("ParseDbConfigTask: Could not connect to PDO: $dsn");
|
85
|
|
|
}
|
86
|
|
|
$this->connected = true;
|
87
|
|
|
}
|
88
|
|
|
}
|
89
|
|
|
?>
|
|
|
|
|
90
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.