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

Feature/filacms 12 new content resource command #14

Merged
merged 3 commits into from
Mar 12, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function up(): void
$table->id();
$table->string('name');
$table->timestamps();

$table->index('name');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function up(): void
$table->string('name');
$table->foreignId('taxonomy_id')->constrained();
$table->timestamps();
$table->index('name');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function up(): void
$table->string('last_name')->nullable();
$table->boolean('is_individual')->default(true);
$table->timestamps();
$table->index('first_name');
$table->index('last_name');
$table->index('is_individual');
});
}

Expand Down
4 changes: 4 additions & 0 deletions database/migrations/2024_02_09_400000_create_pages_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function up(): void
$table->foreignId('updated_user_id')->constrained('users');
$table->foreignId('author_id')->nullable()->constrained('authors');

$table->index('title');
$table->index('slug');
$table->index(['is_draft', 'publish_at', 'expire_at']);

$table->timestamps();
$table->softDeletes();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function up(): void
Schema::create('taxonomy_resources', function (Blueprint $table) {
$table->string('resource_class');
$table->foreignId('taxonomy_id')->constrained()->cascadeOnDelete();

$table->index(['resource_class', 'taxonomy_id']);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function up(): void
$table->foreignId('taxonomyable_id');
$table->string('taxonomyable_type');
$table->timestamps();

$table->index(['taxonomy_term_id', 'taxonomyable_id', 'taxonomyable_type']);
});
}

Expand Down
58 changes: 58 additions & 0 deletions src/Commands/MakeContentMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Portable\FilaCms\Commands;

use Filament\Forms\Commands\Concerns\CanGenerateForms;
use Filament\Support\Commands\Concerns\CanIndentStrings;
use Filament\Support\Commands\Concerns\CanManipulateFiles;
use Filament\Support\Commands\Concerns\CanReadModelSchemas;
use Filament\Tables\Commands\Concerns\CanGenerateTables;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

use function Laravel\Prompts\text;

class MakeContentMigration extends Command
{
use CanGenerateForms;
use CanGenerateTables;
use CanIndentStrings;
use CanManipulateFiles;
use CanReadModelSchemas;

protected $description = 'Create a new database migration for a content resource model';

protected $signature = 'make:filacms-content-migration {name?}';

public function handle(): int
{
$model = (string) str($this->argument('name') ?? text(
label: 'What is the model name?',
placeholder: 'BlogPost',
required: true,
))
->studly()
->beforeLast('Resource')
->trim('/')
->trim('\\')
->trim(' ')
->studly()
->replace('/', '\\');

$migrationFile = now()->format('Y_m_d_His').'_create_'.Str::plural(Str::snake($model)).'_table.php';
$path = database_path('migrations');

$this->copyStubToApp('database/migrations/create_content_model', $path.'/'.$migrationFile, [
'table' => Str::plural(Str::snake($model)),
]);

$this->components->info("FilaCms migration [{$migrationFile}] created successfully.");

return static::SUCCESS;
}

public function getDefaultStubPath(): string
{
return realpath(__DIR__.'/../../stubs');
}
}
59 changes: 59 additions & 0 deletions src/Commands/MakeContentModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Portable\FilaCms\Commands;

use Filament\Forms\Commands\Concerns\CanGenerateForms;
use Filament\Support\Commands\Concerns\CanIndentStrings;
use Filament\Support\Commands\Concerns\CanManipulateFiles;
use Filament\Support\Commands\Concerns\CanReadModelSchemas;
use Filament\Tables\Commands\Concerns\CanGenerateTables;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

use function Laravel\Prompts\text;

class MakeContentModel extends Command
{
use CanGenerateForms;
use CanGenerateTables;
use CanIndentStrings;
use CanManipulateFiles;
use CanReadModelSchemas;

protected $description = 'Create a new model for a content resource model';

protected $signature = 'make:filacms-content-model {name?}';

public function handle(): int
{
$model = (string) str($this->argument('name') ?? text(
label: 'What is the model name?',
placeholder: 'BlogPost',
required: true,
))
->studly()
->beforeLast('Resource')
->trim('/')
->trim('\\')
->trim(' ')
->studly()
->replace('/', '\\');

$path = app_path('Models');

$this->copyStubToApp('Models/ContentModel', $path.'/'.$model.'.php', [
'model' => $model,
'resourceName' => $model.'Resource',
'table' => Str::plural(Str::snake($model)),
]);

$this->components->info("FilaCms content model [{$model}.php] created successfully.");

return static::SUCCESS;
}

public function getDefaultStubPath(): string
{
return realpath(__DIR__.'/../../stubs');
}
}
66 changes: 66 additions & 0 deletions src/Commands/MakeContentPermissionSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Portable\FilaCms\Commands;

use Filament\Forms\Commands\Concerns\CanGenerateForms;
use Filament\Support\Commands\Concerns\CanIndentStrings;
use Filament\Support\Commands\Concerns\CanManipulateFiles;
use Filament\Support\Commands\Concerns\CanReadModelSchemas;
use Filament\Tables\Commands\Concerns\CanGenerateTables;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

use function Laravel\Prompts\text;

class MakeContentPermissionSeeder extends Command
{
use CanGenerateForms;
use CanGenerateTables;
use CanIndentStrings;
use CanManipulateFiles;
use CanReadModelSchemas;

protected $description = 'Create a new seeder to create permissions for a content resource model';

protected $signature = 'make:filacms-content-permissions {name?}';

public function handle(): int
{
$model = (string) str($this->argument('name') ?? text(
label: 'What is the model name?',
placeholder: 'BlogPost',
required: true,
))
->studly()
->beforeLast('Resource')
->trim('/')
->trim('\\')
->trim(' ')
->studly()
->replace('/', '\\');

$path = database_path('seeders');

$plural = Str::plural(\Filament\Support\get_model_label($model));

$this->copyStubToApp('database/seeders/add_permissions_seeder', $path.'/'.$model.'RoleAndPermissionSeeder.php', [
'class' => $model,
'plural' => $plural,
]);

$this->components->info("FilaCms seeder [{$model}.php] created successfully.");

$answer = $this->ask('Would you like to run the seeder now? (Y/n)');

if (Str::lower($answer) === 'y') {
$this->call('db:seed', ['--class' => $model.'RoleAndPermissionSeeder']);
}

return static::SUCCESS;
}

public function getDefaultStubPath(): string
{
return realpath(__DIR__.'/../../stubs');
}
}
93 changes: 93 additions & 0 deletions src/Commands/MakeContentResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Portable\FilaCms\Commands;

use Filament\Forms\Commands\Concerns\CanGenerateForms;
use Filament\Support\Commands\Concerns\CanIndentStrings;
use Filament\Support\Commands\Concerns\CanManipulateFiles;
use Filament\Support\Commands\Concerns\CanReadModelSchemas;
use Filament\Tables\Commands\Concerns\CanGenerateTables;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

use function Laravel\Prompts\text;

class MakeContentResource extends Command
{
use CanGenerateForms;
use CanGenerateTables;
use CanIndentStrings;
use CanManipulateFiles;
use CanReadModelSchemas;

protected $description = 'Create a new Filament resource class and default page classes';

protected $signature = 'make:filacms-content-resource {name?}';

public function handle(): int
{
$model = (string) str($this->argument('name') ?? text(
label: 'What is the model name?',
placeholder: 'BlogPost',
required: true,
))
->studly()
->beforeLast('Resource')
->trim('/')
->trim('\\')
->trim(' ')
->studly()
->replace('/', '\\');

$path = app_path('Filament/Resources/');

$baseResourcePath = (string) str($model.'Resource')
->prepend('/')
->prepend($path)
->replace('\\', '/')
->replace('//', '/');

$listResourcePageClass = 'List'.Str::plural($model);
$createResourcePageClass = 'Create'.$model;
$editResourcePageClass = 'Edit'.$model;

$resourcePath = "{$baseResourcePath}.php";
$resourcePagesDirectory = "{$baseResourcePath}/Pages";
$listResourcePagePath = "{$resourcePagesDirectory}/{$listResourcePageClass}.php";
$createResourcePagePath = "{$resourcePagesDirectory}/{$createResourcePageClass}.php";
$editResourcePagePath = "{$resourcePagesDirectory}/{$editResourcePageClass}.php";

$this->copyStubToApp('Resources/ContentResource', $resourcePath, [
'class' => $model,
'pluralClass' => Str::plural($model),
'model' => $model,
]);

$this->copyStubToApp('Resources/ContentResource/Pages/CreateResource', $createResourcePagePath, [
'class' => $model,
'pluralClass' => Str::plural($model),
'model' => $model,
]);

$this->copyStubToApp('Resources/ContentResource/Pages/EditResource', $editResourcePagePath, [
'class' => $model,
'pluralClass' => Str::plural($model),
'model' => $model,
]);

$this->copyStubToApp('Resources/ContentResource/Pages/ListResources', $listResourcePagePath, [
'class' => $model,
'pluralClass' => Str::plural($model),
'model' => $model,
]);

$this->components->info("FilaCms content resource [{$resourcePath}] created successfully.");

return static::SUCCESS;
}

public function getDefaultStubPath(): string
{
return realpath(__DIR__.'/../../stubs');
}
}
7 changes: 0 additions & 7 deletions src/Filament/Resources/PageResource/Pages/EditPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@

namespace Portable\FilaCms\Filament\Resources\PageResource\Pages;

use Filament\Actions;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages\EditAbstractContentResource;
use Portable\FilaCms\Filament\Resources\PageResource;

class EditPage extends EditAbstractContentResource
{
protected static string $resource = PageResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
8 changes: 0 additions & 8 deletions src/Filament/Resources/PageResource/Pages/ListPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@

namespace Portable\FilaCms\Filament\Resources\PageResource\Pages;

use Filament\Actions;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages\ListAbstractContentResources;
use Portable\FilaCms\Filament\Resources\PageResource;

class ListPages extends ListAbstractContentResources
{
protected static string $resource = PageResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
4 changes: 4 additions & 0 deletions src/Providers/FilaCmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public function boot()
\Portable\FilaCms\Commands\InstallCommand::class,
\Portable\FilaCms\Commands\AddUserConcerns::class,
\Portable\FilaCms\Commands\MakeUser::class,
\Portable\FilaCms\Commands\MakeContentResource::class,
\Portable\FilaCms\Commands\MakeContentMigration::class,
\Portable\FilaCms\Commands\MakeContentModel::class,
\Portable\FilaCms\Commands\MakeContentPermissionSeeder::class,
]);
}

Expand Down
Loading
Loading