1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Kernel; |
14
|
|
|
|
15
|
|
|
use Shieldon\Firewall\Driver\DriverProvider; |
16
|
|
|
use LogicException; |
17
|
|
|
use RuntimeException; |
18
|
|
|
|
19
|
|
|
/* |
20
|
|
|
* Messenger Trait is loaded in Kernel instance only. |
21
|
|
|
*/ |
22
|
|
|
trait DriverTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Driver for storing data. |
26
|
|
|
* |
27
|
|
|
* @var \Shieldon\Firewall\Driver\DriverProvider |
28
|
|
|
*/ |
29
|
|
|
public $driver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This is for creating data tables automatically |
33
|
|
|
* Turn it off, if you don't want to check data tables every connection. |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $autoCreateDatabase = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set a data driver. |
41
|
|
|
* |
42
|
|
|
* @param DriverProvider $driver Query data from the driver you choose to use. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function setDriver(DriverProvider $driver): void |
47
|
|
|
{ |
48
|
|
|
$this->driver = $driver; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set a data channel. |
53
|
|
|
* |
54
|
|
|
* This will create databases for the channel. |
55
|
|
|
* |
56
|
|
|
* @param string $channel Specify a channel. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function setChannel(string $channel): void |
61
|
|
|
{ |
62
|
|
|
if (!$this->driver instanceof DriverProvider) { |
|
|
|
|
63
|
|
|
throw new LogicException('setChannel method requires setDriver set first.'); |
64
|
|
|
} else { |
65
|
|
|
$this->driver->setChannel($channel); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* For first time installation only. This is for creating data tables automatically. |
71
|
|
|
* Turning it on will check the data tables exist or not at every single pageview, |
72
|
|
|
* it's not good for high traffic websites. |
73
|
|
|
* |
74
|
|
|
* @param bool $bool |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function createDatabase(bool $bool): void |
79
|
|
|
{ |
80
|
|
|
$this->autoCreateDatabase = $bool; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check the data driver, throw an exception if not set. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
protected function assertDriver(): void |
89
|
|
|
{ |
90
|
|
|
if (!isset($this->driver)) { |
91
|
|
|
throw new RuntimeException( |
92
|
|
|
'Data driver must be set.' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|