1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Coroutine\Connectors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use SwooleTW\Http\Helpers\FW; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ConnectorFactory |
10
|
|
|
*/ |
11
|
|
|
class ConnectorFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Version with breaking changes |
15
|
|
|
* |
16
|
|
|
* @const string |
17
|
|
|
*/ |
18
|
|
|
public const CHANGE_VERSION = '5.6'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Swoole connector class |
22
|
|
|
* |
23
|
|
|
* @const string |
24
|
|
|
*/ |
25
|
|
|
public const CONNECTOR_CLASS = 'SwooleTW\Http\Coroutine\Connectors\MySqlConnector'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Swoole connector path |
29
|
|
|
* |
30
|
|
|
* @const string |
31
|
|
|
*/ |
32
|
|
|
public const CONNECTOR_CLASS_PATH = __DIR__ . '/MySqlConnector.php'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $version |
36
|
|
|
* |
37
|
|
|
* @return \SwooleTW\Http\Coroutine\Connectors\MySqlConnector |
38
|
|
|
*/ |
39
|
|
|
public static function make(string $version): MySqlConnector |
40
|
|
|
{ |
41
|
|
|
$isMatch = static::isFileVersionMatch($version); |
42
|
|
|
$class = static::copy(static::stub($version), ! $isMatch); |
43
|
|
|
|
44
|
|
|
return new $class; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $version |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public static function stub(string $version): string |
53
|
|
|
{ |
54
|
|
|
return static::hasBreakingChanges($version) |
55
|
|
|
? __DIR__ . '/../../../stubs/5.6/MySqlConnector.stub' |
56
|
|
|
: __DIR__ . '/../../../stubs/5.5/MySqlConnector.stub'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $stub |
61
|
|
|
* @param bool $rewrite |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public static function copy(string $stub, bool $rewrite = false): string |
66
|
|
|
{ |
67
|
|
|
if (! file_exists(static::CONNECTOR_CLASS_PATH) || $rewrite) { |
68
|
|
|
copy($stub, static::CONNECTOR_CLASS_PATH); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return static::CONNECTOR_CLASS; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $version |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
protected static function isFileVersionMatch(string $version): bool |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$fileVersion = null; |
83
|
|
|
if (class_exists(self::CONNECTOR_CLASS)) { |
84
|
|
|
$ref = new \ReflectionClass(self::CONNECTOR_CLASS); |
85
|
|
|
if (preg_match(FW::VERSION_WITHOUT_BUG_FIX, $ref->getDocComment(), $result)) { |
86
|
|
|
$fileVersion = Arr::first($result); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return version_compare($fileVersion, $version, '>='); |
91
|
|
|
} catch (\Exception $e) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $version |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected static function hasBreakingChanges(string $version): bool |
102
|
|
|
{ |
103
|
|
|
return version_compare($version, self::CHANGE_VERSION, '>='); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|