-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
inertia
controller statement (#718)
- Loading branch information
1 parent
e050c91
commit da8dabd
Showing
7 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Blueprint\Models\Statements; | ||
|
||
use Blueprint\Concerns\HasParameters; | ||
|
||
class InertiaStatement | ||
{ | ||
use HasParameters; | ||
|
||
private string $view; | ||
|
||
public function __construct(string $view, array $data = []) | ||
{ | ||
$this->view = $view; | ||
$this->data = $data; | ||
} | ||
|
||
public function output(): string | ||
{ | ||
$code = "return Inertia::render('" . $this->view() . "'"; | ||
|
||
if ($this->data()) { | ||
$code .= ', ' . $this->buildParameters(); | ||
} | ||
|
||
$code .= ');'; | ||
|
||
return $code; | ||
} | ||
|
||
public function view(): string | ||
{ | ||
return $this->view; | ||
} | ||
|
||
private function buildParameters(): string | ||
{ | ||
$parameters = array_map( | ||
fn ($parameter) => sprintf( | ||
"%s'%s' => \$%s%s,%s", | ||
str_pad(' ', 12), | ||
$parameter, | ||
in_array($parameter, $this->properties()) ? 'this->' : '', | ||
$parameter, | ||
PHP_EOL | ||
), | ||
$this->data() | ||
); | ||
|
||
return sprintf( | ||
'[%s%s%s]', | ||
PHP_EOL, | ||
implode($parameters), | ||
str_pad(' ', 8) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Customer; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
|
||
class CustomerController extends Controller | ||
{ | ||
public function show(Request $request, Customer $customer): Response | ||
{ | ||
$customers = Customer::all(); | ||
|
||
return Inertia::render('customer.show', [ | ||
'customer' => $customer, | ||
'customers' => $customers, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
controllers: | ||
Customer: | ||
show: | ||
query: all | ||
inertia: customer.show with:customer,customers |