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