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
|
|
|
private $hasError; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Thread constructor. |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->getLoader(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return ClassLoader |
35
|
|
|
*/ |
36
|
|
|
public function getLoader() |
37
|
|
|
{ |
38
|
|
|
if (!is_null($this->loader)) { |
39
|
|
|
return $this->loader; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$path = __DIR__ . '/../../vendor/autoload.php'; |
43
|
|
|
if (!file_exists($path)) { |
44
|
|
|
$path = __DIR__ . '/../../../../autoload.php'; |
45
|
|
|
if (!file_exists($path)) { |
46
|
|
|
throw new \RuntimeException("Autoload path '$path' not found"); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
$this->loader = require("$path"); |
50
|
|
|
|
51
|
|
|
return $this->loader; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function threadError() |
55
|
|
|
{ |
56
|
|
|
$this->hasError = error_get_last(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Here you are in a threaded environment |
61
|
|
|
*/ |
62
|
|
|
public function run() |
63
|
|
|
{ |
64
|
|
|
register_shutdown_function([$this, 'threadError']); |
65
|
|
|
|
66
|
|
|
$this->getLoader()->register(); |
67
|
|
|
|
68
|
|
|
$this->result = call_user_func_array($this->callable, $this->args); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Start the thread |
73
|
|
|
* |
74
|
|
|
* @throws \RuntimeException |
75
|
|
|
*/ |
76
|
|
|
public function execute() |
77
|
|
|
{ |
78
|
|
|
$this->args = func_get_args(); |
79
|
|
|
return parent::start(); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the thread result |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
* @throws \RuntimeException |
87
|
|
|
*/ |
88
|
|
|
public function getResult() |
89
|
|
|
{ |
90
|
|
|
if ($this->hasError && ( $this->hasError['type'] == E_ERROR || $this->hasError['type'] == E_USER_ERROR )) { |
|
|
|
|
91
|
|
|
throw new \RuntimeException( |
92
|
|
|
sprintf( |
93
|
|
|
'Thread error: "%s", in "%s" at line %d. <<--- ', |
94
|
|
|
$this->hasError['message'], |
95
|
|
|
$this->hasError['file'], |
96
|
|
|
$this->hasError['line'] |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
return $this->result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Kill a thread |
105
|
|
|
* |
106
|
|
|
* @param int $signal |
107
|
|
|
* @param bool $wait |
108
|
|
|
*/ |
109
|
|
|
public function stop($signal = SIGKILL, $wait = false) |
110
|
|
|
{ |
111
|
|
|
parent::kill(); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checkif the thread is not Terminated |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function isAlive() |
120
|
|
|
{ |
121
|
|
|
if ($this->isRunning()) { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!$this->isJoined()) { |
126
|
|
|
$this->join(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the thread callable method |
134
|
|
|
* @param callable $callable |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public function setCallable(callable $callable) |
138
|
|
|
{ |
139
|
|
|
$this->callable = $callable; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function waitFinish() |
143
|
|
|
{ |
144
|
|
|
$this->join(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.