1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace GimliDev \Actions ;
5
+
6
+ use Clyde \Actions \Action_Base ;
7
+ use Clyde \Request \Request ;
8
+ use Clyde \Request \Request_Response ;
9
+ use GimliDev \Builders \File_Builder ;
10
+
11
+ use function GimliDev \load_config ;
12
+ use function Laravel \Prompts \confirm ;
13
+ use function Laravel \Prompts \text ;
14
+
15
+ class Create_Job_Action extends Action_Base {
16
+
17
+ const RESPONSE_NAMESPACE = "Gimli\Http\Response " ;
18
+
19
+ protected array $ job_config = [];
20
+
21
+ /**
22
+ * Execute the action
23
+ *
24
+ * @param Request $Request
25
+ * @return Request_Response
26
+ */
27
+ public function execute (Request $ Request ): Request_Response {
28
+
29
+ $ config = load_config ();
30
+
31
+ $ this ->job_config = $ config ['jobs ' ] ?? [
32
+ 'default_save_path ' => 'App/Jobs ' ,
33
+ 'namespace ' => 'App \\Jobs ' ,
34
+ 'extends ' => null
35
+ ];
36
+
37
+ return $ this ->buildSingleAction ($ Request );
38
+
39
+ }
40
+
41
+ /**
42
+ * Format the path for the job
43
+ *
44
+ * @param Request $Request
45
+ * @return array
46
+ */
47
+ protected function formatPath (Request $ Request ): array {
48
+ $ job_name = $ Request ->getArgument ('job_name ' );
49
+ $ path = $ Request ->getArgument ('save_path ' ) ?? '' ;
50
+
51
+ if (empty ($ path )) {
52
+ $ path = text (label: 'Where would you like to save the job? ' , default: $ this ->job_config ['default_save_path ' ]);
53
+ }
54
+
55
+ $ path = $ path [0 ] !== '/ ' ? '/ ' . $ path : $ path ;
56
+ $ path = $ path [strlen ($ path ) - 1 ] !== '/ ' ? $ path . '/ ' : $ path ;
57
+ $ job_path = ROOT . $ path . $ job_name . '.php ' ;
58
+
59
+ return [$ job_name , $ job_path ];
60
+ }
61
+
62
+ /**
63
+ * Build a Job
64
+ *
65
+ * @param Request $Request
66
+ * @return Request_Response
67
+ */
68
+ protected function buildSingleAction (Request $ Request ): Request_Response {
69
+ [$ job_name , $ job_path ] = $ this ->formatPath ($ Request );
70
+
71
+ $ namespace = $ Request ->getArgument ('namespace ' ) ?? '' ;
72
+
73
+ if (empty ($ namespace )) {
74
+ $ namespace = text (label: 'What is the namespace for the job? ' , default: $ this ->job_config ['namespace ' ]);
75
+ }
76
+
77
+ if (file_exists ($ job_path )) {
78
+ $ this ->Printer ->error ('A job with that name already exists. ' );
79
+ exit (1 );
80
+ }
81
+
82
+ $ extends = confirm ('Would you like to extend a base job or class? ' );
83
+ $ extends_path = '' ;
84
+ if ($ extends ) {
85
+ $ extends_path = text (label: 'What is the namespace of the class you would like to extend? ' , placeholder: $ this ->job_config ['extends ' ]);
86
+ }
87
+
88
+ $ File_Builder = new File_Builder ($ job_name , $ namespace , $ extends_path );
89
+ $ File_Builder ->addUseStatements ([self ::RESPONSE_NAMESPACE ]);
90
+ $ File_Builder ->addMethods ([
91
+ [
92
+ 'name ' => '__invoke ' ,
93
+ 'return ' => self ::RESPONSE_NAMESPACE ,
94
+ 'return_name ' => 'Response ' ,
95
+ 'params ' => [
96
+ [
97
+ 'name ' => 'Response ' ,
98
+ 'type ' => self ::RESPONSE_NAMESPACE ,
99
+ 'type_short ' => 'Response ' ,
100
+ 'comment ' => 'The Response object '
101
+ ],
102
+ [
103
+ 'name ' => 'subcommand ' ,
104
+ 'type ' => 'string ' ,
105
+ 'type_short ' => 'string ' ,
106
+ 'comment ' => 'Subcommand '
107
+ ],
108
+ [
109
+ 'name ' => 'options ' ,
110
+ 'type ' => 'array ' ,
111
+ 'type_short ' => 'array ' ,
112
+ 'comment ' => 'Array of options and their values '
113
+ ],
114
+ [
115
+ 'name ' => 'flags ' ,
116
+ 'type ' => 'array ' ,
117
+ 'type_short ' => 'array ' ,
118
+ 'comment ' => 'Array of passed flags '
119
+ ]
120
+ ]
121
+ ]
122
+ ]);
123
+
124
+ $ job = $ File_Builder ->getClass ();
125
+
126
+ file_put_contents ($ job_path , $ job );
127
+
128
+ $ this ->Printer ->success ("Job created at $ job_path " );
129
+
130
+ return new Request_Response (true );
131
+ }
132
+ }
0 commit comments