Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent creating non-existent models from container #15160

Open
wants to merge 7 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/forms/resources/js/components/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,23 @@ export default function selectFormComponent({

if (hasDynamicSearchResults) {
this.$refs.input.addEventListener('search', (event) => {
if (! this.select._isSearching) {
if (!this.select._isSearching) {
return
}

let search = event.detail.value?.trim()

this.select._displayNotice([null, undefined, ''].includes(search)
? loadingMessage
: searchingMessage)
this.select._displayNotice(
[null, undefined, ''].includes(search)
? loadingMessage
: searchingMessage,
)
})

this.$refs.input.addEventListener(
'search',
Alpine.debounce(async (event) => {
if (! this.select._isSearching) {
if (!this.select._isSearching) {
return
}

Expand Down Expand Up @@ -170,7 +172,7 @@ export default function selectFormComponent({
return
}

if (! this.isEmpty) {
if (!this.isEmpty) {
this.select._clearNotice()
}

Expand All @@ -184,7 +186,10 @@ export default function selectFormComponent({
this.select.setChoiceByValue(this.formatState(this.state))
}

if (this.isEmpty && (! [null, undefined, ''].includes(config.search))) {
if (
this.isEmpty &&
![null, undefined, ''].includes(config.search)
) {
this.select._displayNotice(noSearchResultsMessage)
}
},
Expand All @@ -210,7 +215,7 @@ export default function selectFormComponent({

let results = []

if (! [null, undefined, ''].includes(search)) {
if (![null, undefined, ''].includes(search)) {
results = await getSearchResultsUsing(search)
} else {
results = await getOptionsUsing()
Expand Down
6 changes: 5 additions & 1 deletion packages/support/src/Concerns/EvaluatesClosures.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionParameter;
Expand Down Expand Up @@ -81,7 +82,10 @@ protected function resolveClosureDependencyForEvaluation(ReflectionParameter $pa
return $this;
}

if (filled($typedParameterClassName)) {
if (
filled($typedParameterClassName)
&& (! is_subclass_of($typedParameterClassName, Model::class) || app()->bound($typedParameterClassName))
) {
return app()->make($typedParameterClassName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

use Filament\Resources\Resource;
use Filament\Tests\Fixtures\Models\PostCategory;
use Filament\Tests\Fixtures\Resources\PostCategories\Pages\CreatePostCategory;
use Filament\Tests\Fixtures\Resources\PostCategories\Pages\EditPostCategory;
use Filament\Tests\Fixtures\Resources\PostCategories\Pages\ListPostCategories;
use Filament\Tests\Fixtures\Resources\PostCategories\Pages\ViewPostCategory;

class PostCategoryResource extends Resource
{
Expand All @@ -20,10 +16,10 @@ class PostCategoryResource extends Resource
public static function getPages(): array
{
return [
'index' => ListPostCategories::route('/'),
'create' => CreatePostCategory::route('/create'),
'view' => ViewPostCategory::route('/{record}'),
'edit' => EditPostCategory::route('/{record}/edit'),
'index' => Pages\ListPostCategories::route('/'),
'create' => Pages\CreatePostCategory::route('/create'),
'view' => Pages\ViewPostCategory::route('/{record}'),
'edit' => Pages\EditPostCategory::route('/{record}/edit'),
];
}
}
12 changes: 4 additions & 8 deletions tests/src/Fixtures/Resources/Posts/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tests\Fixtures\Models\Post;
use Filament\Tests\Fixtures\Resources\Posts\Pages\CreatePost;
use Filament\Tests\Fixtures\Resources\Posts\Pages\EditPost;
use Filament\Tests\Fixtures\Resources\Posts\Pages\ListPosts;
use Filament\Tests\Fixtures\Resources\Posts\Pages\ViewPost;
use Illuminate\Database\Eloquent\Builder;

class PostResource extends Resource
Expand Down Expand Up @@ -74,10 +70,10 @@ public static function table(Table $table): Table
public static function getPages(): array
{
return [
'index' => ListPosts::route('/'),
'create' => CreatePost::route('/create'),
'view' => ViewPost::route('/{record}'),
'edit' => EditPost::route('/{record}/edit'),
'index' => Pages\ListPosts::route('/'),
'create' => Pages\CreatePost::route('/create'),
'view' => Pages\ViewPost::route('/{record}'),
'edit' => Pages\EditPost::route('/{record}/edit'),
];
}
}
12 changes: 4 additions & 8 deletions tests/src/Fixtures/Resources/Shop/Products/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

use Filament\Resources\Resource;
use Filament\Tests\Fixtures\Models\Product;
use Filament\Tests\Fixtures\Resources\Shop\Products\Pages\CreateProduct;
use Filament\Tests\Fixtures\Resources\Shop\Products\Pages\EditProduct;
use Filament\Tests\Fixtures\Resources\Shop\Products\Pages\ListProducts;
use Filament\Tests\Fixtures\Resources\Shop\Products\Pages\ViewProduct;

class ProductResource extends Resource
{
Expand All @@ -20,10 +16,10 @@ class ProductResource extends Resource
public static function getPages(): array
{
return [
'index' => ListProducts::route('/'),
'create' => CreateProduct::route('/create'),
'view' => ViewProduct::route('/{record}'),
'edit' => EditProduct::route('/{record}/edit'),
'index' => Pages\ListProducts::route('/'),
'create' => Pages\CreateProduct::route('/create'),
'view' => Pages\ViewProduct::route('/{record}'),
'edit' => Pages\EditProduct::route('/{record}/edit'),
];
}
}
12 changes: 4 additions & 8 deletions tests/src/Fixtures/Resources/Users/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tests\Fixtures\Models\User;
use Filament\Tests\Fixtures\Resources\Users\Pages\CreateUser;
use Filament\Tests\Fixtures\Resources\Users\Pages\EditUser;
use Filament\Tests\Fixtures\Resources\Users\Pages\ListUsers;
use Filament\Tests\Fixtures\Resources\Users\Pages\ViewUser;

class UserResource extends Resource
{
Expand Down Expand Up @@ -72,10 +68,10 @@ public static function table(Table $table): Table
public static function getPages(): array
{
return [
'index' => ListUsers::route('/'),
'create' => CreateUser::route('/create'),
'view' => ViewUser::route('/{record}'),
'edit' => EditUser::route('/{record}/edit'),
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'view' => Pages\ViewUser::route('/{record}'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}
}
136 changes: 136 additions & 0 deletions tests/src/Support/EvaluatesClosuresTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

use Filament\Support;
use Filament\Tests\TestCase;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model;

uses(TestCase::class);

it('will make any object from container', function () {
$isEvaluatingClosures = new IsEvaluatingClosures;

$isEvaluatingClosures->evaluate(function (IsEvaluatingClosures $isEvaluatingClosures) {
$this->expectNotToPerformAssertions();
});
});

it('will instantiate Eloquent Models provided by name', function () {
$isEvaluatingClosures = new IsEvaluatingClosures(
record: $recordModel = new RecordModel,
shouldResolveDefaultClosureDependencyForEvaluationByName: true,
shouldResolveDefaultClosureDependencyForEvaluationByType: false,
);

$isEvaluatingClosures->evaluate(function (RecordModel $record) use ($recordModel) {
expect($record)->toBe($recordModel);
});
});

it('will not instantiate Eloquent Models not provided by name', function () {
$isEvaluatingClosures = new IsEvaluatingClosures(
record: $recordModel = new RecordModel,
shouldResolveDefaultClosureDependencyForEvaluationByName: true,
shouldResolveDefaultClosureDependencyForEvaluationByType: false,
);

$this->expectException(BindingResolutionException::class);

$isEvaluatingClosures->evaluate(function (RecordModel $recordModel) {
throw new RuntimeException('Should not be called because named parameter not provided.');
});
});

it('will instantiate Eloquent Models provided by type', function () {
$isEvaluatingClosures = new IsEvaluatingClosures(
record: $recordModel = new RecordModel,
shouldResolveDefaultClosureDependencyForEvaluationByName: false,
shouldResolveDefaultClosureDependencyForEvaluationByType: true,
);

$isEvaluatingClosures->evaluate(function (RecordModel $record) use ($recordModel) {
expect($record)->toBe($recordModel);
});

$isEvaluatingClosures->evaluate(function (RecordModel $recordModelWithDifferentName) use ($recordModel) {
expect($recordModelWithDifferentName)->toBe($recordModel);
});
});

it('will not instantiate empty Models from container', function () {
$isEvaluatingClosures = new IsEvaluatingClosures;

$this->expectException(BindingResolutionException::class);

$isEvaluatingClosures->evaluate(function (RecordModel $recordModel) {
throw new RuntimeException('Should not be called.');
});
});

it('will instantiate empty Models from container if bound', function () {
$isEvaluatingClosures = new IsEvaluatingClosures;

$boundRecordModel = new RecordModel;

app()->bind(RecordModel::class, fn () => $boundRecordModel);

$isEvaluatingClosures->evaluate(function (RecordModel $recordModel) use ($boundRecordModel) {
expect($recordModel)->toBe($boundRecordModel);
});
});

it('will instantiate empty Models from container if bound as singleton', function () {
$isEvaluatingClosures = new IsEvaluatingClosures;

$boundRecordModel = new RecordModel;

app()->singleton(RecordModel::class, fn () => $boundRecordModel);

$isEvaluatingClosures->evaluate(function (RecordModel $recordModel) use ($boundRecordModel) {
expect($recordModel)->toBe($boundRecordModel);
});
});

it('will instantiate empty Models from container if bound as scoped', function () {
$isEvaluatingClosures = new IsEvaluatingClosures;

$boundRecordModel = new RecordModel;

app()->scoped(RecordModel::class, fn () => $boundRecordModel);

$isEvaluatingClosures->evaluate(function (RecordModel $recordModel) use ($boundRecordModel) {
expect($recordModel)->toBe($boundRecordModel);
});
});

class RecordModel extends Model
{
//
}

class IsEvaluatingClosures
{
use Support\Concerns\EvaluatesClosures;

public function __construct(
public ?RecordModel $record = null,
public bool $shouldResolveDefaultClosureDependencyForEvaluationByName = false,
public bool $shouldResolveDefaultClosureDependencyForEvaluationByType = false,
) {}

protected function resolveDefaultClosureDependencyForEvaluationByName(string $parameterName): array
{
return match (true) {
$this->shouldResolveDefaultClosureDependencyForEvaluationByName && $parameterName === 'record' => [$this->record],
default => [],
};
}

protected function resolveDefaultClosureDependencyForEvaluationByType(string $parameterType): array
{
return match (true) {
$this->shouldResolveDefaultClosureDependencyForEvaluationByType && $parameterType === $this->record::class => [$this->record],
default => [],
};
}
}