1
+ <?php
2
+ namespace bazilio \async \models ;
3
+
4
+ /**
5
+ * Class AsyncExecuteTask
6
+ */
7
+ class AsyncExecuteTask extends AsyncTask
8
+ {
9
+ public $ class ;
10
+ public $ method ;
11
+ public $ arguments = [];
12
+
13
+ public function rules ()
14
+ {
15
+ return array (
16
+ [['class ' , 'method ' , 'arguments ' ], 'required ' ],
17
+ [['class ' , 'method ' ], 'string ' ],
18
+ [['class ' ], 'validateClass ' ],
19
+ [['method ' ], 'validateMethod ' ],
20
+ [['arguments ' ], 'validateArguments ' ],
21
+ );
22
+ }
23
+
24
+ public function validateClass ($ attribute , $ params )
25
+ {
26
+ if (!class_exists ($ this ->$ attribute )) {
27
+ $ this ->addError ($ attribute , "Class {$ this ->$ attribute } does not exist " );
28
+ }
29
+ }
30
+
31
+ public function validateMethod ($ attribute , $ params )
32
+ {
33
+ if (!isset (array_flip (get_class_methods ($ this ->class ))[$ this ->$ attribute ])) {
34
+ $ this ->addError (
35
+ $ attribute ,
36
+ "Method {$ this ->$ attribute } of class {$ this ->class } does not exist "
37
+ );
38
+ }
39
+ }
40
+
41
+ public function validateArguments ($ attribute , $ params ) {
42
+ if (!isset (array_flip (get_class_methods ($ this ->class ))[$ this ->method ])) {
43
+ $ this ->addError (
44
+ $ attribute ,
45
+ "Method {$ this ->method } of class {$ this ->class } does not exist "
46
+ );
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Returns the list of attribute names of the model.
52
+ * @return array list of attribute names.
53
+ */
54
+ public function attributeNames ()
55
+ {
56
+ return [
57
+ 'class ' ,
58
+ 'method ' ,
59
+ 'arguments '
60
+ ];
61
+ }
62
+
63
+ static function nope ($ return )
64
+ {
65
+ return $ return ;
66
+ }
67
+
68
+ public function execute ()
69
+ {
70
+ return call_user_func_array (array ($ this ->class , $ this ->method ), $ this ->arguments );
71
+ }
72
+ }
0 commit comments