Skip to content

Commit 1c69a51

Browse files
committed
WIP
1 parent 6977924 commit 1c69a51

File tree

6 files changed

+170
-14
lines changed

6 files changed

+170
-14
lines changed

app/Console/Commands/CurlCommand.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class CurlCommand extends Command
8+
{
9+
protected $signature = 'curl {--X|request=GET} {--H|header=*} {--d|data=*} {--F|form=*} {url}';
10+
11+
protected $description = 'Parse UNIX curl command';
12+
13+
public function handle()
14+
{
15+
$json = [
16+
'method' => $this->option('request'),
17+
'url' => $this->argument('url'),
18+
'headers' => $this->option('header'),
19+
'data' => $this->option('data'),
20+
'fields' => $this->option('form'),
21+
// TODO: map more options...
22+
];
23+
24+
$this->line(json_encode($json));
25+
26+
return 0;
27+
}
28+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Requests\StoreConverter;
6+
use App\Support\HttpCall;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Artisan;
9+
10+
class ConverterController extends Controller
11+
{
12+
/**
13+
* Show the form for creating a new resource.
14+
*
15+
* @return \Illuminate\Http\Response
16+
*/
17+
public function create()
18+
{
19+
return view('home');
20+
}
21+
22+
/**
23+
* Store a newly created resource in storage.
24+
*
25+
* @param \Illuminate\Http\Request $request
26+
* @return \Illuminate\Http\Response
27+
*/
28+
public function store(StoreConverter $request)
29+
{
30+
$curl = $request->input('curl');
31+
32+
// TODO: catch any "input" errors as validation errors
33+
Artisan::call($curl);
34+
35+
$data = json_decode(trim(Artisan::output()), true);
36+
37+
$request = \App\Models\Request::create($data);
38+
$code = HttpCall::format($request);
39+
40+
return view('home', ['curl' => $curl, 'http' => $code]);
41+
}
42+
}

app/Http/Requests/StoreConverter.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreConverter extends FormRequest
8+
{
9+
/**
10+
* Get the validation rules that apply to the request.
11+
*
12+
* @return array<string, mixed>
13+
*/
14+
public function rules()
15+
{
16+
return [
17+
'curl' => [
18+
'required',
19+
'string',
20+
'starts_with:curl',
21+
],
22+
];
23+
}
24+
}

app/Models/Request.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
class Request
6+
{
7+
private string $url;
8+
private string $method;
9+
private array $headers = [];
10+
private array $data = [];
11+
private bool $multipartFormData = false;
12+
13+
private function __construct($url, $method)
14+
{
15+
$this->url = $url;
16+
$this->method = strtoupper($method);
17+
}
18+
19+
public static function create(array $data): self
20+
{
21+
$request = new self($data['url'], $data['method']);
22+
23+
if (!empty($data['headers'])) {
24+
$request->headers = $data['headers'];
25+
}
26+
27+
if (!empty($data['data'])) {
28+
$request->data = $data['data'];
29+
}
30+
31+
if (!empty($data['fields'])) {
32+
$request->data = $data['fields'];
33+
$request->multipartFormData = true;
34+
}
35+
36+
return $request;
37+
}
38+
39+
public function data(): array
40+
{
41+
return $this->data;
42+
}
43+
44+
public function headers(): array
45+
{
46+
return $this->headers;
47+
}
48+
49+
public function method(): string
50+
{
51+
return $this->method;
52+
}
53+
54+
public function url(): string
55+
{
56+
return $this->url;
57+
}
58+
59+
public function isMultipartFormData()
60+
{
61+
return $this->multipartFormData;
62+
}
63+
}

app/Support/HttpCall.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Support;
4+
5+
class HttpCall
6+
{
7+
public static function format(\App\Models\Request $request)
8+
{
9+
// TODO:
10+
return 'Http::get(' . $request->url() . ');';
11+
}
12+
}

routes/web.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,4 @@
22

33
use Illuminate\Support\Facades\Route;
44

5-
/*
6-
|--------------------------------------------------------------------------
7-
| Web Routes
8-
|--------------------------------------------------------------------------
9-
|
10-
| Here is where you can register web routes for your application. These
11-
| routes are loaded by the RouteServiceProvider within a group which
12-
| contains the "web" middleware group. Now create something great!
13-
|
14-
*/
15-
16-
Route::get('/', function () {
17-
return view('welcome');
18-
});
5+
Route::resource('/', \App\Http\Controllers\ConverterController::class)->only('create', 'store');

0 commit comments

Comments
 (0)