1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Phive Queue package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phive\Queue\Tests\Handler; |
13
|
|
|
|
14
|
|
|
use Phive\Queue\Queue; |
15
|
|
|
|
16
|
|
|
class PdoHandler extends Handler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \PDO |
20
|
|
|
*/ |
21
|
|
|
private $pdo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $driverName; |
27
|
|
|
|
28
|
|
|
public function getQueueName(Queue $queue) |
29
|
|
|
{ |
30
|
|
|
return parent::getQueueName($queue).'#'.$this->driverName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getQueueClass() |
34
|
|
|
{ |
35
|
|
|
$prefix = 'sqlite' === $this->driverName ? 'Sqlite' : 'Generic'; |
36
|
|
|
|
37
|
|
|
return '\\Phive\\Queue\\Pdo\\'.$prefix.'PdoQueue'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function createQueue() |
41
|
|
|
{ |
42
|
|
|
$class = $this->getQueueClass(); |
43
|
|
|
|
44
|
|
|
return new $class($this->pdo, $this->getOption('table_name')); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function reset() |
48
|
|
|
{ |
49
|
|
|
$sqlDir = realpath(__DIR__.'/../../res/'.$this->driverName); |
50
|
|
|
|
51
|
|
|
foreach (glob($sqlDir.'/*.sql') as $file) { |
52
|
|
|
$sql = strtr(file_get_contents($file), [ |
53
|
|
|
'{{table_name}}' => $this->getOption('table_name'), |
54
|
|
|
'{{routine_name}}' => $this->getOption('table_name').'_pop', |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$this->pdo->exec($sql); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function clear() |
62
|
|
|
{ |
63
|
|
|
$this->pdo->exec('DELETE FROM '.$this->getOption('table_name')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function configure() |
67
|
|
|
{ |
68
|
|
|
$this->pdo = new \PDO( |
69
|
|
|
$this->getOption('dsn'), |
70
|
|
|
$this->getOption('username'), |
71
|
|
|
$this->getOption('password') |
72
|
|
|
); |
73
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
74
|
|
|
$this->driverName = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); |
75
|
|
|
|
76
|
|
|
$this->configureDriver(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function configureDriver() |
80
|
|
|
{ |
81
|
|
|
switch ($this->driverName) { |
82
|
|
|
case 'sqlite': |
83
|
|
|
$this->pdo->exec('PRAGMA journal_mode=WAL'); |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|