1
<?php
2
/**
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <contact@terryl.in>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 * 
10
 * php version 7.1.0
11
 * 
12
 * @category  Web-security
13
 * @package   Shieldon
14
 * @author    Terry Lin <contact@terryl.in>
15
 * @copyright 2019 terrylinooo
16
 * @license   https://github.com/terrylinooo/shieldon/blob/2.x/LICENSE MIT
17
 * @link      https://github.com/terrylinooo/shieldon
18
 * @see       https://shieldon.io
19
 */
20

21
declare(strict_types=1);
22

23
namespace Shieldon\Firewall\Kernel;
24

25
use Shieldon\Firewall\Driver\DriverProvider;
26
use Shieldon\Event\Event;
27
use RuntimeException;
28
use function php_sapi_name;
29

30
/*
31
 * Messenger Trait is loaded in Kernel instance only.
32
 */
33
trait DriverTrait
34
{
35
    /**
36
     *   Public methods       | Desctiotion
37
     *  ----------------------|---------------------------------------------
38
     *   setDriver            | Set a data driver.
39
     *   setChannel           | Set a data channel.
40
     *   disableDbBuilder     | disable creating data tables.
41
     *  ----------------------|---------------------------------------------
42
     */
43

44
    /**
45
     * Driver for storing data.
46
     *
47
     * @var \Shieldon\Firewall\Driver\DriverProvider
48
     */
49
    public $driver;
50

51
    /**
52
     * This is for creating data tables automatically
53
     * Turn it off, if you don't want to check data tables every connection.
54
     *
55
     * @var bool
56
     */
57
    protected $isCreateDatabase = true;
58

59
    /**
60
     * Set a data driver.
61
     *
62
     * @param DriverProvider $driver Query data from the driver you choose to use.
63
     *
64
     * @return void
65
     */
66 3
    public function setDriver(DriverProvider $driver): void
67
    {
68 3
        $this->driver = $driver;
69

70
        /**
71
         * [Hook] `set_channel` - After initializing data driver.
72
         */
73 3
        Event::doDispatch('set_driver', [
74 3
            'driver' => $this->driver,
75
        ]);
76

77 3
        $this->driver->init($this->isCreateDatabase);
78

79 3
        $period = $this->sessionLimit['period'] ?: 300;
80

81
        /**
82
         * [Hook] `set_driver` - After initializing data driver.
83
         */
84 3
        Event::doDispatch('set_session_driver', [
85 3
            'driver'         => $this->driver,
86 3
            'gc_expires'     => $period,
87 3
            'gc_probability' => 1,
88 3
            'gc_divisor'     => 100,
89 3
            'psr7'           => $this->psr7,
90
        ]);
91
    }
92

93
    /**
94
     * Set a data channel.
95
     *
96
     * This will create databases for the channel.
97
     *
98
     * @param string $channel Specify a channel.
99
     *
100
     * @return void
101
     */
102 3
    public function setChannel(string $channel): void
103
    {
104 3
        if (!is_null($this->driver)) {
105 3
            $this->driver->setChannel($channel);
106
        } else {
107 3
            Event::AddListener('set_driver',
108
                function ($args) use ($channel) {
109 3
                    $args['driver']->setChannel($channel);
110
                }
111
            );
112
        }
113
    }
114

115
    /**
116
     * Shieldon creating data tables automatically.
117
     * Turning it off when the data tables exist overwise checling 
118
     * every pageview.
119
     * 
120
     * @return void
121
     */
122 3
    public function disableDbBuilder(): void
123
    {
124 3
        $this->isCreateDatabase = false;
125

126 3
        if (php_sapi_name() === 'cli') {
127
            // Unit testing needs.
128 3
            $this->isCreateDatabase = true;
129
        }
130
    }
131

132
    /**
133
     * Check the data driver, throw an exception if not set.
134
     *
135
     * @return void
136
     */
137 3
    protected function assertDriver(): void
138
    {
139 3
        if (!isset($this->driver)) {
140 3
            throw new RuntimeException(
141 3
                'Data driver must be set.'
142
            );
143
        }
144
    }
145
}

Read our documentation on viewing source code .

Loading