Skip to content

Commit

Permalink
Modernize view data syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Dec 12, 2024
1 parent a56a9d9 commit ead0426
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 26 deletions.
32 changes: 23 additions & 9 deletions src/Models/Statements/RenderStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,42 @@ public function __construct(string $view, array $data = [])
$this->data = $data;
}

public function view(): string
{
return $this->view;
}

public function output(): string
{
$code = "return view('" . $this->view() . "'";

if ($this->data()) {
$code .= ', compact(' . $this->buildParameters() . ')';
$code .= ', ' . $this->buildParameters();
}

$code .= ');';

return $code;
}

private function buildParameters(): string
public function view(): string
{
$parameters = array_map(fn ($parameter) => "'" . $parameter . "'", $this->data());
return $this->view;
}

return implode(', ', $parameters);
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)
);
}
}
5 changes: 4 additions & 1 deletion tests/fixtures/components/properties-statements.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function update()

return redirect()->route('user.show', [$this->user]);

return view('user.show', compact('user', 'extra'));
return view('user.show', [
'user' => $this->user,
'extra' => $extra,
]);

return new UserResource($this->user);

Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/controllers/controller-configured.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public function index(Request $request): View
{
$users = User::all();

return view('user.index', compact('users'));
return view('user.index', [
'users' => $users,
]);
}

public function store(UserStoreRequest $request): RedirectResponse
Expand Down
8 changes: 6 additions & 2 deletions tests/fixtures/controllers/crazy-eloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public function index(Request $request): View
{
$posts = Post::where('title', $title)->where('content', $content)->orderBy('published_at')->limit(5)->get();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function edit(Request $request, Post $post): View
{
$post = Post::find($id);

return view('post.edit', compact('post'));
return view('post.edit', [
'post' => $post,
]);
}

public function update(Request $request, Post $post): RedirectResponse
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/controllers/nested-components.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function index(Request $request): View
{
$users = User::all();

return view('admin.user.index', compact('users'));
return view('admin.user.index', [
'users' => $users,
]);
}

public function store(UserStoreRequest $request): RedirectResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function index(Request $request): View
{
$posts = Post::all();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function store(PostStoreRequest $request): RedirectResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public function index(Request $request): View
{
$posts = Post::all();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function store(PostStoreRequest $request): RedirectResponse
Expand Down
4 changes: 3 additions & 1 deletion tests/fixtures/controllers/readme-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function index(Request $request): View
{
$posts = Post::all();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function store(PostStoreRequest $request): RedirectResponse
Expand Down
12 changes: 9 additions & 3 deletions tests/fixtures/controllers/with-all-policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function index(Request $request): View

$posts = Post::all();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function create(Request $request): View
Expand All @@ -43,14 +45,18 @@ public function show(Request $request, Post $post): View
{
$this->authorize('show', $post);

return view('post.show', compact('post'));
return view('post.show', [
'post' => $post,
]);
}

public function edit(Request $request, Post $post): View
{
$this->authorize('edit', $post);

return view('post.edit', compact('post'));
return view('post.edit', [
'post' => $post,
]);
}

public function update(PostUpdateRequest $request, Post $post): RedirectResponse
Expand Down
12 changes: 9 additions & 3 deletions tests/fixtures/controllers/with-authorize-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function index(Request $request): View
{
$posts = Post::all();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function create(Request $request): View
Expand All @@ -39,12 +41,16 @@ public function store(PostStoreRequest $request): RedirectResponse

public function show(Request $request, Post $post): View
{
return view('post.show', compact('post'));
return view('post.show', [
'post' => $post,
]);
}

public function edit(Request $request, Post $post): View
{
return view('post.edit', compact('post'));
return view('post.edit', [
'post' => $post,
]);
}

public function update(PostUpdateRequest $request, Post $post): RedirectResponse
Expand Down
12 changes: 9 additions & 3 deletions tests/fixtures/controllers/with-some-policies.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function index(Request $request): View

$posts = Post::all();

return view('post.index', compact('posts'));
return view('post.index', [
'posts' => $posts,
]);
}

public function create(Request $request): View
Expand All @@ -38,12 +40,16 @@ public function show(Request $request, Post $post): View
{
$this->authorize('show', $post);

return view('post.show', compact('post'));
return view('post.show', [
'post' => $post,
]);
}

public function edit(Request $request, Post $post): View
{
return view('post.edit', compact('post'));
return view('post.edit', [
'post' => $post,
]);
}

public function update(PostUpdateRequest $request, Post $post): RedirectResponse
Expand Down

0 comments on commit ead0426

Please sign in to comment.