Further improvements to relation command flow

This commit is contained in:
Dan Harrin
2025-09-07 11:30:33 +01:00
parent 759200f1a6
commit cbbf0e64f5
4 changed files with 71 additions and 88 deletions
@@ -586,27 +586,29 @@ class MakePageCommand extends Command
);
};
if (! $this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_SCHEMAS)) {
$formSchemaFqn = $this->askForSchema(
intialQuestion: 'Should an existing form schema class be used?',
question: 'Which form schema class would you like to use?',
questionPlaceholder: app()->getNamespace() . 'Filament\\Resources\\Users\\Schemas\\UserForm',
);
}
if (blank($formSchemaFqn)) {
$askForIsGeneratedIfNotAlready()
? $askForRelatedModelFqnIfNotAlready()
: $askForRecordTitleAttributeIfNotAlready();
}
if (confirm(
'Would you like to generate a read-only view modal for the table?',
default: false,
)) {
$hasViewOperation = true;
}
$askForIsGeneratedIfNotAlready();
if ($isGenerated) {
$askForRelatedModelFqnIfNotAlready();
}
if (! $isGenerated) {
if (! $this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_SCHEMAS)) {
$formSchemaFqn = $this->askForSchema(
intialQuestion: 'Should an existing form schema class be used?',
question: 'Which form schema class would you like to use?',
questionPlaceholder: app()->getNamespace() . 'Filament\\Resources\\Users\\Schemas\\UserForm',
);
}
if ($hasViewOperation && (! $this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_SCHEMAS))) {
$infolistSchemaFqn = $this->askForSchema(
intialQuestion: 'Would you like to use an existing infolist schema class?',
question: 'Which infolist schema class would you like to use?',
@@ -614,30 +616,26 @@ class MakePageCommand extends Command
);
}
if (blank($infolistSchemaFqn)) {
$askForRecordTitleAttributeIfNotAlready();
if (! $this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_TABLES)) {
$tableFqn = $this->askForSchema(
intialQuestion: 'Would you like to use an existing table class?',
question: 'Which table class would you like to use?',
questionPlaceholder: app()->getNamespace() . 'Filament\\Resources\\Users\\Tables\\UsersTable',
);
}
}
if ($this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_TABLES)) {
$askForIsGeneratedIfNotAlready()
? $askForRelatedModelFqnIfNotAlready()
: $askForRecordTitleAttributeIfNotAlready();
} else {
$tableFqn = $this->askForSchema(
intialQuestion: 'Would you like to use an existing table class?',
question: 'Which table class would you like to use?',
questionPlaceholder: app()->getNamespace() . 'Filament\\Resources\\Users\\Tables\\UsersTable',
);
if (blank($formSchemaFqn) && (! $isGenerated)) {
$askForRecordTitleAttributeIfNotAlready();
}
if ($hasViewOperation && blank($infolistSchemaFqn) && (! $isGenerated)) {
$askForRecordTitleAttributeIfNotAlready();
}
if (blank($tableFqn)) {
$askForRecordTitleAttributeIfNotAlready();
$askForIsGeneratedIfNotAlready(
question: 'Should the table columns be generated from the current database columns?',
) && $askForRelatedModelFqnIfNotAlready();
$isSoftDeletable = (filled($relatedModelFqn) && static::$shouldCheckModelsForSoftDeletes && class_exists($relatedModelFqn))
? in_array(SoftDeletes::class, class_uses_recursive($relatedModelFqn))
: confirm(
@@ -247,47 +247,35 @@ class MakeRelationManagerCommand extends Command
if (blank($this->relatedResourceFqn)) {
$this->configureHasViewOperation();
$this->configureFormSchemaFqn();
$this->configureIsGeneratedIfNotAlready();
if ($this->hasViewOperation) {
$this->configureInfolistSchemaFqn();
if ($this->isGenerated) {
$this->configureRelatedModelFqnIfNotAlready();
}
if (! $this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_TABLES)) {
$this->configureTableFqn();
if (! $this->isGenerated) {
$this->configureFormSchemaFqn();
if ($this->hasViewOperation) {
$this->configureInfolistSchemaFqn();
}
if (! $this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_TABLES)) {
$this->configureTableFqn();
}
}
if (blank($this->formSchemaFqn)) {
$this->configureIsGeneratedIfNotAlready();
$this->isGenerated
? $this->configureRelatedModelFqnIfNotAlready()
: $this->configureRecordTitleAttributeIfNotAlready();
}
if ($this->hasViewOperation && blank($this->infolistSchemaFqn)) {
if (blank($this->formSchemaFqn) && (! $this->isGenerated)) {
$this->configureRecordTitleAttributeIfNotAlready();
}
if ($this->hasFileGenerationFlag(FileGenerationFlag::EMBEDDED_PANEL_RESOURCE_TABLES)) {
$this->configureIsGeneratedIfNotAlready();
$this->isGenerated
? $this->configureRelatedModelFqnIfNotAlready()
: $this->configureRecordTitleAttributeIfNotAlready();
if ($this->hasViewOperation && blank($this->infolistSchemaFqn) && (! $this->isGenerated)) {
$this->configureRecordTitleAttributeIfNotAlready();
}
if (blank($this->tableFqn)) {
$this->configureRecordTitleAttributeIfNotAlready();
$this->configureIsGeneratedIfNotAlready(
question: 'Should the table columns be generated from the current database columns?',
);
if ($this->isGenerated) {
$this->configureRelatedModelFqnIfNotAlready();
}
$this->configureIsSoftDeletable();
$this->configureRelationshipType();
@@ -366,9 +366,9 @@ it('can generate a manage related records page class in a resource', function ()
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -399,9 +399,9 @@ it('can generate a manage related records page class in a resource with a genera
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], true)
->expectsQuestion($questions['relatedModel'], 'Filament\\Tests\\Fixtures\\Models\\Team')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -417,9 +417,9 @@ it('can generate a manage related records page class in a resource with a view o
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasViewOperation'], true)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], true)
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -434,9 +434,9 @@ it('can generate a manage related records page class in a resource with soft-del
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isSoftDeletable'], true)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -451,9 +451,9 @@ it('can generate a manage related records page class in a resource for a `HasMan
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], HasMany::class);
@@ -347,7 +347,6 @@ $generateManageRelatedRecordsPageCommandQuestions = [
'relatedModel' => 'What is the related model?',
'titleAttribute' => 'What is the title attribute for this model?',
'formSchemaClass' => 'Which form schema class would you like to use?',
'isGeneratedTable' => 'Should the table columns be generated from the current database columns?',
'hasViewOperation' => 'Would you like to generate a read-only view modal for the table?',
'hasInfolistSchemaClass' => 'Would you like to use an existing infolist schema class?',
'infolistSchemaClass' => 'Which infolist schema class would you like to use?',
@@ -363,11 +362,11 @@ it('can generate a manage related records page class in a resource', function ()
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -398,12 +397,12 @@ it('can generate a manage related records page class in a resource with a form s
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], true)
->expectsQuestion($questions['formSchemaClass'], app()->getNamespace() . 'Filament\\Resources\\Teams\\Schemas\\TeamForm')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isGeneratedTable'], false)
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -418,11 +417,9 @@ it('can generate a manage related records page class in a resource with a genera
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], true)
->expectsQuestion($questions['relatedModel'], 'Filament\\Tests\\Fixtures\\Models\\Team')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -438,12 +435,12 @@ it('can generate a manage related records page class in a resource with a view o
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], true)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasInfolistSchemaClass'], false)
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -458,13 +455,13 @@ it('can generate a manage related records page class in a resource with an infol
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], true)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasInfolistSchemaClass'], true)
->expectsQuestion($questions['infolistSchemaClass'], app()->getNamespace() . 'Filament\\Resources\\Teams\\Schemas\\TeamInfolist')
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -479,12 +476,12 @@ it('can generate a manage related records page class in a resource with a table
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasTableClass'], true)
->expectsQuestion($questions['tableClass'], app()->getNamespace() . 'Filament\\Resources\\Teams\\Tables\\TeamsTable');
->expectsQuestion($questions['tableClass'], app()->getNamespace() . 'Filament\\Resources\\Teams\\Tables\\TeamsTable')
->expectsQuestion($questions['titleAttribute'], 'name');
assertFileExists($path = app_path('Filament/Resources/Users/Pages/ManageUserTeams.php'));
expect(file_get_contents($path))
@@ -497,11 +494,11 @@ it('can generate a manage related records page class in a resource with soft-del
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], true)
->expectsQuestion($questions['relationshipType'], BelongsToMany::class);
@@ -516,11 +513,11 @@ it('can generate a manage related records page class in a resource for a `HasMan
$runGenerateManageRelatedRecordsPageCommand($this)
->expectsQuestion($questions['relationship'], 'teams')
->expectsQuestion($questions['hasRelatedResource'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['hasViewOperation'], false)
->expectsQuestion($questions['isGenerated'], false)
->expectsQuestion($questions['hasFormSchemaClass'], false)
->expectsQuestion($questions['hasTableClass'], false)
->expectsQuestion($questions['titleAttribute'], 'name')
->expectsQuestion($questions['isSoftDeletable'], false)
->expectsQuestion($questions['relationshipType'], HasMany::class);