1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* vipnytt/RobotsTxtParser |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/VIPnytt/RobotsTxtParser |
6
|
|
|
* @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace vipnytt\RobotsTxtParser; |
10
|
|
|
|
11
|
|
|
use vipnytt\RobotsTxtParser\Client\Cache; |
12
|
|
|
use vipnytt\RobotsTxtParser\Client\Delay; |
13
|
|
|
use vipnytt\RobotsTxtParser\Exceptions\DatabaseException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Database |
17
|
|
|
* |
18
|
|
|
* @package vipnytt\RobotsTxtParser |
19
|
|
|
*/ |
20
|
|
|
class Database |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* MySQL driver name |
24
|
|
|
*/ |
25
|
|
|
const DRIVER_MYSQL = 'mysql'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Database handler |
29
|
|
|
* @var \PDO |
30
|
|
|
*/ |
31
|
|
|
private $pdo; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
private $driver; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Cache\ManageInterface|null |
40
|
|
|
*/ |
41
|
|
|
private $cache; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Delay\ManageInterface|null |
45
|
|
|
*/ |
46
|
|
|
private $delay; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Database constructor. |
50
|
|
|
* |
51
|
|
|
* @param \PDO $pdo |
52
|
|
|
*/ |
53
|
|
|
public function __construct(\PDO $pdo) |
54
|
|
|
{ |
55
|
|
|
$this->pdo = $pdo; |
56
|
|
|
$this->driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); |
57
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_NATURAL); |
58
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_NATURAL); |
59
|
|
|
$this->initialize(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Driver special treatment |
64
|
|
|
* |
65
|
|
|
* @return int|bool |
66
|
|
|
*/ |
67
|
|
|
private function initialize() |
68
|
|
|
{ |
69
|
|
|
switch ($this->driver) { |
70
|
|
|
case self::DRIVER_MYSQL: |
71
|
|
|
return $this->pdo->exec('SET NAMES utf8;'); |
72
|
|
|
} |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Cache manager |
78
|
|
|
* |
79
|
|
|
* @return Cache\ManageInterface |
80
|
|
|
* @throws DatabaseException |
81
|
|
|
*/ |
82
|
|
|
public function cache() |
83
|
|
|
{ |
84
|
|
|
if ($this->cache !== null) { |
85
|
|
|
return $this->cache; |
86
|
|
|
} |
87
|
|
|
switch ($this->driver) { |
88
|
|
|
case self::DRIVER_MYSQL: |
89
|
|
|
return $this->cache = new Cache\MySQL\Manage($this->pdo); |
90
|
|
|
} |
91
|
|
|
throw new DatabaseException('Unsupported PDO driver.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Delay manager |
96
|
|
|
* |
97
|
|
|
* @return Delay\ManageInterface |
98
|
|
|
* @throws DatabaseException |
99
|
|
|
*/ |
100
|
|
|
public function delay() |
101
|
|
|
{ |
102
|
|
|
if ($this->delay !== null) { |
103
|
|
|
return $this->delay; |
104
|
|
|
} |
105
|
|
|
switch ($this->driver) { |
106
|
|
|
case self::DRIVER_MYSQL: |
107
|
|
|
return $this->delay = new Delay\MySQL\Manage($this->pdo); |
108
|
|
|
} |
109
|
|
|
throw new DatabaseException('Unsupported PDO driver.'); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|