1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the mytheam/yii2-schedule. |
5
|
|
|
* |
6
|
|
|
* (c) mytheam <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the BSD license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace mythteam\schedule; |
13
|
|
|
|
14
|
|
|
use yii\base\Application; |
15
|
|
|
use yii\base\InvalidParamException; |
16
|
|
|
use yii\base\Object; |
17
|
|
|
|
18
|
|
|
class CallbackEvent extends Event |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The callback to call. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $callback; |
26
|
|
|
/** |
27
|
|
|
* The parameters to pass to the method. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $parameters; |
32
|
|
|
/** |
33
|
|
|
* @param string $callback |
34
|
|
|
* @param array $parameters |
35
|
|
|
* @param array $config |
36
|
|
|
*/ |
37
|
32 |
|
public function __construct($callback, array $parameters = [], $config = []) |
38
|
|
|
{ |
39
|
32 |
|
$this->callback = $callback; |
40
|
32 |
|
$this->parameters = $parameters; |
41
|
32 |
|
Object::__construct($config); |
42
|
32 |
|
if (!is_string($this->callback) && !is_callable($this->callback)) { |
43
|
1 |
|
throw new InvalidParamException( |
44
|
|
|
'Invalid scheduled callback event. Must be string or callable.' |
45
|
1 |
|
); |
46
|
|
|
} |
47
|
32 |
|
} |
48
|
|
|
/** |
49
|
|
|
* Run the given event. |
50
|
|
|
* |
51
|
|
|
* @param Application $app |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
1 |
|
public function run(Application $app) |
56
|
|
|
{ |
57
|
1 |
|
$response = call_user_func_array($this->callback, array_merge($this->parameters, [$app])); |
58
|
1 |
|
$this->callAfterCallbacks($app); |
59
|
|
|
|
60
|
1 |
|
return $response; |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* Get the summary of the event for display. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
1 |
|
public function getSummaryForDisplay() |
68
|
|
|
{ |
69
|
1 |
|
if (is_string($this->_description)) { |
70
|
1 |
|
return $this->_description; |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return is_string($this->callback) ? $this->callback : 'Closure'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|