1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resta\Worker; |
4
|
|
|
|
5
|
|
|
use Resta\Support\Utils; |
6
|
|
|
use Resta\Contracts\WorkerContracts; |
7
|
|
|
use Resta\Contracts\WorkerManagerContracts; |
8
|
|
|
|
9
|
|
|
class WorkerManager extends WorkerManagerAbstract implements WorkerManagerContracts |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var null|object |
13
|
|
|
*/ |
14
|
|
|
protected $resolve; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* execute for worker |
18
|
|
|
* |
19
|
|
|
* @return void|mixed |
20
|
|
|
*/ |
21
|
|
|
public function execute() |
22
|
|
|
{ |
23
|
|
|
//we check the presence of worker on container. |
24
|
|
|
if($this->isWorkerAvailable()){ |
25
|
|
|
$this->{$this->getApply()}(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* execute closure for worker |
31
|
|
|
* |
32
|
|
|
* @return void|mixed |
33
|
|
|
*/ |
34
|
|
|
public function executeClosure() |
35
|
|
|
{ |
36
|
|
|
$this->isWorkerClosure(); |
37
|
|
|
|
38
|
|
|
if($this->resolve instanceof \Closure){ |
39
|
|
|
$resolve = $this->resolve; |
40
|
|
|
echo $resolve($this->getData()).''.PHP_EOL; |
41
|
|
|
$this->pause(); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* execute object for worker |
47
|
|
|
* |
48
|
|
|
* @return void|mixed |
49
|
|
|
*/ |
50
|
|
|
public function executeObject() |
51
|
|
|
{ |
52
|
|
|
$this->isWorkerObject(); |
53
|
|
|
|
54
|
|
|
if($this->resolve instanceof WorkerContracts){ |
55
|
|
|
$this->resolve->handle(); |
56
|
|
|
echo $this->getWorker().' Worker : Ok'.PHP_EOL; |
57
|
|
|
$this->pause($this->resolve->getSleep()); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* is worker closure |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function isWorkerClosure() |
67
|
|
|
{ |
68
|
|
|
//we check the presence of worker on container. |
69
|
|
|
if($this->isWorkerAvailable()){ |
70
|
|
|
|
71
|
|
|
// if worker comes as an object, this object will be executed. |
72
|
|
|
// worker can have a closure function. |
73
|
|
|
if(is_callable($worker = $this->worker[$this->getWorker()])) { |
74
|
|
|
$this->resolve = $worker; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* is worker object |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function isWorkerObject() |
85
|
|
|
{ |
86
|
|
|
//we check the presence of worker on container. |
87
|
|
|
if($this->isWorkerAvailable()){ |
88
|
|
|
|
89
|
|
|
// if worker comes as an object, this object will be executed. |
90
|
|
|
// worker can have a closure function. |
91
|
|
|
if(!is_callable($worker = $this->worker[$this->getWorker()]) && Utils::isNamespaceExists($worker)) { |
92
|
|
|
$this->resolve = $this->app->resolve($worker,['data'=>$this->getData()]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|