1 | <?php |
||
2 | |||
3 | namespace Micheledamo\LaravelWebArtisan\Controllers; |
||
4 | |||
5 | use Illuminate\Http\Request; |
||
6 | use Illuminate\Support\Facades\Artisan; |
||
7 | use Symfony\Component\Console\Exception\CommandNotFoundException; |
||
8 | |||
9 | class WebArtisanCommandController extends WebArtisanBaseController |
||
10 | { |
||
11 | /** |
||
12 | * The command string |
||
13 | * |
||
14 | * @var |
||
15 | */ |
||
16 | protected $command; |
||
17 | |||
18 | /** |
||
19 | * Run the command |
||
20 | * |
||
21 | * @param Request $request |
||
22 | * @return string |
||
23 | */ |
||
24 | public function run(Request $request) |
||
25 | { |
||
26 | $this->command = $request->has('command') |
||
27 | ? $request->get('command') |
||
28 | : null; |
||
29 | |||
30 | if($this->command) { |
||
31 | $commandPrepared = $this->prepareCommand($this->command); |
||
32 | if(!config('webartisan.use_authentication') |
||
33 | or (config('webartisan.use_authentication') and $this->webartisan->isAuthenticated())) { |
||
34 | try { |
||
35 | Artisan::call($commandPrepared); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
36 | } |
||
37 | catch(\Exception $e) { |
||
38 | return $this->prepareResultToHtml($e->getMessage(), 'error'); |
||
39 | } |
||
40 | } |
||
41 | else { |
||
42 | return $this->prepareResultToHtml('Please, authenticate yourself before to start using Web Artisan.', 'error'); |
||
43 | } |
||
44 | return $this->prepareResultToHtml(Artisan::output(), 'success'); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Prepare command for Artisan |
||
50 | * |
||
51 | * @param $command |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function prepareCommand($command) |
||
55 | { |
||
56 | $command = $this->clearPhpArtisan($command); |
||
57 | return $command; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Remove "php artisan" if present |
||
62 | * |
||
63 | * @param $command |
||
64 | * @return string|string[] |
||
65 | */ |
||
66 | public function clearPhpArtisan($command) |
||
67 | { |
||
68 | return str_replace("php artisan ", "", $command); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Convert to HTML |
||
73 | * and present with type colors |
||
74 | * |
||
75 | * @param $string |
||
76 | * @param $type |
||
77 | * @return string|string[] |
||
78 | */ |
||
79 | public function prepareResultToHtml($string, $type) |
||
80 | { |
||
81 | $string = str_replace("\n", '<br>', |
||
82 | str_replace("\r", '<br>', |
||
83 | str_replace("\t", ' ', |
||
84 | str_replace(" ", ' ', |
||
85 | $string)))); |
||
86 | |||
87 | switch ($type) { |
||
88 | case "error": { $string = '<span class="webartisan__window__results__error">' . $string . '</span>'; }break; |
||
89 | case "success": { $string = '<span class="webartisan__window__results__success">' . $string . '</span>'; }break; |
||
90 | } |
||
91 | |||
92 | return $string; |
||
93 | } |
||
94 | } |