1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Plasma Driver MySQL component |
4
|
|
|
* Copyright 2018-2019 PlasmaPHP, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://github.com/PlasmaPHP |
7
|
|
|
* License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Plasma\Drivers\MySQL; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The Driver Factory is responsible for creating the driver correctly. |
14
|
|
|
*/ |
15
|
|
|
class DriverFactory implements \Plasma\DriverFactoryInterface { |
16
|
|
|
/** |
17
|
|
|
* @var \React\EventLoop\LoopInterface |
18
|
|
|
*/ |
19
|
|
|
protected $loop; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $options; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Plasma\Drivers\MySQL\AuthPlugins\AuthPluginInterface[] |
28
|
|
|
*/ |
29
|
|
|
protected static $authPlugins = array( |
30
|
|
|
\Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SECURE_CONNECTION => \Plasma\Drivers\MySQL\AuthPlugins\AuthSecureConnection::class, |
31
|
|
|
'mysql_native_password' => \Plasma\Drivers\MySQL\AuthPlugins\AuthSecureConnection::class |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \React\Filesystem\FilesystemInterface|null |
36
|
|
|
*/ |
37
|
|
|
protected static $filesystem; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* The driver supports the following options: |
43
|
|
|
* ``` |
44
|
|
|
* array( |
45
|
|
|
* 'connector' => ConnectorInstance, (a custom connector instance, which MUST return a `Connection` instance from the `react/socket` package) |
46
|
|
|
* 'characters.set' => string, (the character set to use, defaults to utf8mb4) |
47
|
|
|
* 'characters.collate' => string, (the collate to use, defaults to the charset default) |
48
|
|
|
* 'compression.enable' => bool, (whether compression should be used if available, defaults to true) |
49
|
|
|
* 'localInFile.enable' => bool, (whether local in file requests are enabled, defaults to false, see issue #16) |
50
|
|
|
* 'tls.context' => array, (socket TLS context options) |
51
|
|
|
* 'tls.force' => bool, (whether non-localhost connections are forced to use TLS, defaults to true) |
52
|
|
|
* 'tls.forceLocal' => bool, (whether localhost connections are forced to use TLS, defaults to false) |
53
|
|
|
* ) |
54
|
|
|
* ``` |
55
|
|
|
* |
56
|
|
|
* @param \React\EventLoop\LoopInterface $loop |
57
|
|
|
* @param array $options |
58
|
|
|
*/ |
59
|
90 |
|
function __construct(\React\EventLoop\LoopInterface $loop, array $options) { |
60
|
90 |
|
if(!\function_exists('stream_socket_enable_crypto')) { |
61
|
|
|
throw new \LogicException('Encryption is not supported on your platform'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
try { |
65
|
90 |
|
\Plasma\Types\TypeExtensionsManager::registerManager('driver-mysql', null); |
66
|
89 |
|
} catch (\Plasma\Exception $e) { |
67
|
|
|
/* One already exists, continue regardless */ |
68
|
|
|
} |
69
|
|
|
|
70
|
90 |
|
$this->loop = $loop; |
71
|
90 |
|
$this->options = $options; |
72
|
90 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates a new driver instance. |
76
|
|
|
* @return \Plasma\DriverInterface |
77
|
|
|
*/ |
78
|
87 |
|
function createDriver(): \Plasma\DriverInterface { |
79
|
87 |
|
return (new \Plasma\Drivers\MySQL\Driver($this->loop, $this->options)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Adds an auth plugin. `$condition` is either an int (for server capabilities), or a string (for auth plugin name). |
84
|
|
|
* @param string|int $condition |
85
|
|
|
* @param string $classname A class implementing `AuthPluginInterface`. |
86
|
|
|
* @return void |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
*/ |
89
|
3 |
|
static function addAuthPlugin($condition, string $classname): void { |
90
|
3 |
|
if(isset(static::$authPlugins[$condition])) { |
91
|
1 |
|
throw new \InvalidArgumentException('Auth plugin for specified condition already exists'); |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
if(!\in_array(\Plasma\Drivers\MySQL\AuthPlugins\AuthPluginInterface::class, \class_implements($classname, true))) { |
95
|
1 |
|
throw new \InvalidArgumentException('Specified auth plugin does not implement interface'); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
static::$authPlugins[$condition] = $classname; |
99
|
2 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the registered auth plugins. |
103
|
|
|
* @return \Plasma\Drivers\MySQL\AuthPlugins\AuthPluginInterface[] |
104
|
|
|
*/ |
105
|
65 |
|
static function getAuthPlugins(): array { |
106
|
65 |
|
return static::$authPlugins; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the React Filesystem to use. |
111
|
|
|
* @param \React\Filesystem\FilesystemInterface|null $filesystem |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
1 |
|
static function setFilesystem(?\React\Filesystem\FilesystemInterface $filesystem): void { |
115
|
1 |
|
static::$filesystem = $filesystem; |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the React Filesystem, or null. The filesystem must be set by the user, in order to not get `null`. |
120
|
|
|
* @return \React\Filesystem\FilesystemInterface|null |
121
|
|
|
*/ |
122
|
2 |
|
static function getFilesystem(): ?\React\Filesystem\FilesystemInterface { |
123
|
2 |
|
return static::$filesystem; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|