Completed
Push — master ( 8d94c9...c35610 )
by Joao
02:06
created

Thread::setThreadHandlerArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ByJG\PHPThread;
4
5
use ByJG\PHPThread\Handler\ForkHandler;
6
use ByJG\PHPThread\Handler\PThreadHandler;
7
use ByJG\PHPThread\Handler\ThreadInterface;
8
use InvalidArgumentException;
9
use RuntimeException;
10
11
/**
12
 * Native Implementation of Threads in PHP.
13
 *
14
 * A class to spawn a thread. Only works in *nix environments,
15
 * as Windows platform is missing libpcntl.
16
 *
17
 * Forks the process.
18
 */
19
class Thread implements ThreadInterface
20
{
21
    /**
22
     * @var ThreadInterface
23
     */
24
    private $threadInstance = null;
25
26
    /**
27
     * constructor method
28
     *
29
     * @param mixed $callable string with the function name or a array with the instance and the method name
30
     * @throws RuntimeException
31
     * @throws InvalidArgumentException
32
     */
33
    public function __construct(callable $callable)
34
    {
35
        $this->setCallable($callable);
36
    }
37
38
    private $threadHandlerArguments = [];
39
40
    /**
41
     * @param array $arguments
42
     */
43
    public function setThreadHandlerArguments($arguments)
44
    {
45
        $this->threadHandlerArguments = $arguments;
46
    }
47
48
    private function getThreadHandlerArguments($property)
49
    {
50
        return isset($this->threadHandlerArguments[$property]) ? $this->threadHandlerArguments[$property] : null;
51
    }
52
53
    /**
54
     * @return ThreadInterface
55
     */
56
    public function getThreadInstance()
57
    {
58
        if (!is_null($this->threadInstance)) {
59
            return $this->threadInstance;
60
        }
61
62
        if (class_exists('\Thread', true)) {
63
            $this->threadInstance = new PThreadHandler();
64
        } elseif (function_exists('pcntl_fork')) {
65
            $this->threadInstance = new ForkHandler(
66
                $this->getThreadHandlerArguments('max-size'),
67
                $this->getThreadHandlerArguments('default-permission')
68
            );
69
        } else {
70
            throw new RuntimeException(
71
                'PHP need to be compiled with ZTS extension or compiled with the --enable-pcntl. ' .
72
                'Windows is not supported.'
73
            );
74
        }
75
76
        return $this->threadInstance;
77
    }
78
79
80
    /**
81
     * Start the thread
82
     *
83
     * @throws RuntimeException
84
     */
85
    public function execute()
86
    {
87
        $args = func_get_args();
88
        call_user_func_array([$this->getThreadInstance(), 'execute'], $args);
89
    }
90
91
    /**
92
     * Get the thread result from the shared memory block and erase it
93
     *
94
     * @return mixed
95
     */
96
    public function getResult()
97
    {
98
        return $this->getThreadInstance()->getResult();
99
    }
100
101
    /**
102
     * Kill a thread
103
     *
104
     * @param int $signal
105
     * @param bool $wait
106
     */
107
    public function stop($signal = SIGKILL, $wait = false)
108
    {
109
        return $this->getThreadInstance()->stop($signal, $wait);
110
    }
111
112
    /**
113
     * Check if the forked process is alive
114
     * @return bool
115
     */
116
    public function isAlive()
117
    {
118
        return $this->getThreadInstance()->isAlive();
119
    }
120
121
    public function waitFinish()
122
    {
123
        $this->getThreadInstance()->waitFinish();
124
    }
125
126
    /**
127
     * Set the thread callable method
128
     * @param callable $callable
129
     * @return mixed
130
     */
131
    public function setCallable(callable $callable)
132
    {
133
        $this->getThreadInstance()->setCallable($callable);
134
    }
135
}
136