|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for logging PDO errors. Subscriber for PDO-adapter |
|
4
|
|
|
* |
|
5
|
|
|
* @file PdoErrorLogger.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 7.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2018 Alexander Yancharuk |
|
11
|
|
|
* @date 2013-12-31 15:44 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\DataBase\Loggers; |
|
17
|
|
|
|
|
18
|
|
|
use SplSubject; |
|
19
|
|
|
use Veles\DataBase\Adapters\DbAdapterInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class PdoErrorLogger |
|
23
|
|
|
* |
|
24
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
25
|
|
|
*/ |
|
26
|
|
|
class PdoErrorLogger implements \SplObserver |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $path; |
|
30
|
|
|
/** @var \PDO */ |
|
31
|
|
|
protected $conn; |
|
32
|
|
|
/** @var \PDOStatement */ |
|
33
|
|
|
protected $stmt; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Set log path |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $path |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function setPath($path) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->path = $path; |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Getting log path |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function getPath() |
|
51
|
|
|
{ |
|
52
|
2 |
|
return $this->path; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Update subscriber |
|
57
|
|
|
* |
|
58
|
|
|
* @param SplSubject $subject |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function update(SplSubject $subject) |
|
61
|
|
|
{ |
|
62
|
6 |
|
$this->updateConnParams($subject); |
|
63
|
6 |
|
$conn_err = $this->conn->errorCode(); |
|
64
|
6 |
|
$stmt_err = $this->stmt->errorCode(); |
|
65
|
|
|
|
|
66
|
6 |
|
if ('00000' === $conn_err && '00000' === $stmt_err) { |
|
67
|
2 |
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
$error_info = ('00000' === $conn_err) |
|
71
|
2 |
|
? implode('; ', $this->stmt->errorInfo()) . PHP_EOL |
|
72
|
4 |
|
: implode('; ', $this->conn->errorInfo()) . PHP_EOL; |
|
73
|
|
|
|
|
74
|
4 |
|
error_log($error_info, 3, $this->getPath()); |
|
75
|
4 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set connection and statement properties |
|
79
|
|
|
* |
|
80
|
|
|
* @param SplSubject $subject |
|
81
|
|
|
*/ |
|
82
|
8 |
|
protected function updateConnParams(SplSubject $subject) |
|
83
|
|
|
{ |
|
84
|
8 |
|
if (!$subject instanceof DbAdapterInterface) { |
|
85
|
2 |
|
$msg = '$subject must be instance of InvalidArgumentException'; |
|
86
|
2 |
|
throw new \InvalidArgumentException($msg); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
$this->conn = $subject->getResource(); |
|
90
|
6 |
|
$this->stmt = $subject->getStmt(); |
|
91
|
6 |
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|