|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Desemax\ArtisanUI\Http\Controllers\API; |
|
4
|
|
|
|
|
5
|
|
|
use Desemax\ArtisanUI\ArtisanUI; |
|
6
|
|
|
use Desemax\ArtisanUI\Http\Controllers\Controller; |
|
7
|
|
|
use Desemax\ArtisanUI\Http\Requests\RunCommandRequest; |
|
8
|
|
|
use Desemax\ArtisanUI\Http\Resources\Command; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
11
|
|
|
|
|
12
|
|
|
class CommandsController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ArtisanUI |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $artisanUI; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(ArtisanUI $artisanUI) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->artisanUI = $artisanUI; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function index() |
|
25
|
|
|
{ |
|
26
|
|
|
$commands = Command::collection($this->artisanUI->commands()); |
|
27
|
|
|
|
|
28
|
|
|
return response()->json(compact('commands')); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function getArguments(Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
return array_merge($request->get('arguments', []), $this->getOptions($request)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function getOptions(Request $request) |
|
37
|
|
|
{ |
|
38
|
|
|
$result = []; |
|
39
|
|
|
foreach ($request->get('options', []) as $option) { |
|
40
|
|
|
$result['--'.$option] = true; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function run(RunCommandRequest $request) |
|
47
|
|
|
{ |
|
48
|
|
|
$arguments = $this->getArguments($request); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$status = ! empty($arguments) ? |
|
52
|
|
|
Artisan::call($request->get('name'), $arguments) : |
|
53
|
|
|
Artisan::call($request->get('name')); |
|
54
|
|
|
} catch (\Exception $exception) { |
|
55
|
|
|
return response()->json([ |
|
56
|
|
|
'status' => 1, |
|
57
|
|
|
'message' => $exception->getMessage(), |
|
58
|
|
|
], 422); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return response()->json([ |
|
62
|
|
|
'status' => $status, |
|
63
|
|
|
'output' => Artisan::output(), |
|
64
|
|
|
], 201); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|