1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\PHPThread\Handler; |
4
|
|
|
|
5
|
|
|
use Composer\Autoload\ClassLoader; |
6
|
|
|
|
7
|
|
|
class PThreadHandler extends \Thread implements ThreadInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ClassLoader |
11
|
|
|
*/ |
12
|
|
|
private $loader; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var callable |
16
|
|
|
*/ |
17
|
|
|
private $callable; |
18
|
|
|
|
19
|
|
|
private $args; |
20
|
|
|
|
21
|
|
|
private $result; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Thread constructor. |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->getLoader(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return ClassLoader |
33
|
|
|
*/ |
34
|
|
|
public function getLoader() |
35
|
|
|
{ |
36
|
|
|
if (!is_null($this->loader)) { |
37
|
|
|
return $this->loader; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$path = __DIR__ . '/../../vendor/autoload.php'; |
41
|
|
|
if (!file_exists($path)) { |
42
|
|
|
$path = __DIR__ . '/../../../../autoload.php'; |
43
|
|
|
if (!file_exists($path)) { |
44
|
|
|
throw new \RuntimeException("Autoload path '$path' not found"); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
$this->loader = require("$path"); |
48
|
|
|
|
49
|
|
|
return $this->loader; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Here you are in a threaded environment |
54
|
|
|
*/ |
55
|
|
|
public function run() |
56
|
|
|
{ |
57
|
|
|
$this->getLoader()->register(); |
58
|
|
|
|
59
|
|
|
$callable = $this->callable; |
60
|
|
|
if (!is_string($callable)) { |
61
|
|
|
$callable = (array) $this->callable; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$this->result = call_user_func_array($callable, (array)$this->args); |
66
|
|
|
// Executed only in PHP 7, will not match in PHP 5.x |
67
|
|
|
} catch (\Throwable $ex) { |
|
|
|
|
68
|
|
|
$this->result = $ex; |
69
|
|
|
// Executed only in PHP 5. Remove when PHP 5.x is no longer necessary. |
70
|
|
|
} catch (\Exception $ex) { |
71
|
|
|
$this->result = $ex; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Start the thread |
77
|
|
|
* |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
*/ |
80
|
|
|
public function execute() |
81
|
|
|
{ |
82
|
|
|
$this->args = func_get_args(); |
83
|
|
|
return parent::start(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the thread result |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
* @throws \Error |
91
|
|
|
* @throws \Throwable |
92
|
|
|
*/ |
93
|
|
|
public function getResult() |
94
|
|
|
{ |
95
|
|
|
$result = $this->result; |
96
|
|
View Code Duplication |
if (is_object($result) && |
|
|
|
|
97
|
|
|
($result instanceof \Exception |
98
|
|
|
|| $result instanceof \Throwable |
99
|
|
|
|| $result instanceof \Error |
|
|
|
|
100
|
|
|
) |
101
|
|
|
) { |
102
|
|
|
throw $result; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Kill a thread |
110
|
|
|
* |
111
|
|
|
* @param int $signal |
112
|
|
|
* @param bool $wait |
113
|
|
|
*/ |
114
|
|
|
public function stop($signal = SIGKILL, $wait = false) |
115
|
|
|
{ |
116
|
|
|
parent::kill(); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Checkif the thread is not Terminated |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isAlive() |
125
|
|
|
{ |
126
|
|
|
if ($this->isRunning()) { |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!$this->isJoined()) { |
131
|
|
|
$this->join(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the thread callable method |
139
|
|
|
* @param callable $callable |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
public function setCallable(callable $callable) |
143
|
|
|
{ |
144
|
|
|
$this->callable = $callable; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function waitFinish() |
148
|
|
|
{ |
149
|
|
|
$this->join(); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.