Skip to content
全部文档

文件生成

简介

Filament 包含许多用于生成文件的 CLI 命令。本指南说明如何自定义生成的文件。

Filament 生成的绝大多数文件是 PHP 类。Filament 使用 nette/php-generator 以编程方式生成类,而不是模板文件。好处是生成文件更灵活,这对于支持 Filament 如此多的配置选项很重要。

每类类由一个 ClassGenerator 类生成。Filament 使用的 ClassGenerator 类列表如下:

  • Filament\Actions\Commands\FileGenerators\ExporterClassGenerator make:filament-exporter 命令使用。
  • Filament\Actions\Commands\FileGenerators\ImporterClassGenerator make:filament-importer 命令使用。
  • Filament\Forms\Commands\FileGenerators\FieldClassGenerator make:filament-form-field 命令使用。
  • Filament\Forms\Commands\FileGenerators\FormSchemaClassGenerator make:filament-form 命令使用。
  • Filament\Forms\Commands\FileGenerators\LivewireFormComponentClassGenerator make:filament-livewire-form 命令使用。
  • Filament\Infolists\Commands\FileGenerators\EntryClassGenerator make:filament-infolist-entry 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceCreateRecordPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceCustomPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceEditRecordPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceListRecordsPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceManageRecordsPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceManageRelatedRecordsPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\Pages\ResourceViewRecordPageClassGenerator make:filament-resourcemake:filament-page 命令使用。
  • Filament\Commands\FileGenerators\Resources\SchemasResourceFormSchemaClassGenerator make:filament-resource 命令使用。
  • Filament\Commands\FileGenerators\Resources\SchemasResourceInfolistSchemaClassGenerator make:filament-resource 命令使用。
  • Filament\Commands\FileGenerators\Resources\SchemasResourceTableClassGenerator make:filament-resource 命令使用。
  • Filament\Commands\FileGenerators\Resources\RelationManagerClassGenerator make:filament-relation-manager 命令使用。
  • Filament\Commands\FileGenerators\Resources\ResourceClassGenerator make:filament-resource 命令使用。
  • Filament\Commands\FileGenerators\ClusterClassGenerator make:filament-cluster 命令使用。
  • Filament\Commands\FileGenerators\CustomPageClassGenerator make:filament-page 命令使用。
  • Filament\Commands\FileGenerators\PanelProviderClassGenerator filament:installmake:filament-panel 命令使用。
  • Filament\Schemas\Commands\FileGenerators\ComponentClassGenerator make:filament-schema-component 命令使用。
  • Filament\Schemas\Commands\FileGenerators\LivewireSchemaComponentClassGenerator make:filament-livewire-schema 命令使用。
  • Filament\Schemas\Commands\FileGenerators\SchemaClassGenerator make:filament-schema 命令使用。
  • Filament\Tables\Commands\FileGenerators\ColumnClassGenerator make:filament-table-column 命令使用。
  • Filament\Tables\Commands\FileGenerators\LivewireTableComponentClassGenerator make:filament-livewire-table 命令使用。
  • Filament\Tables\Commands\FileGenerators\TableClassGenerator make:filament-table 命令使用。
  • Filament\Widgets\Commands\FileGenerators\ChartWidgetClassGenerator make:filament-widget 命令使用。
  • Filament\Widgets\Commands\FileGenerators\CustomWidgetClassGenerator make:filament-widget 命令使用。
  • Filament\Widgets\Commands\FileGenerators\StatsOverviewWidgetClassGenerator make:filament-widget 命令使用。
  • Filament\Widgets\Commands\FileGenerators\TableWidgetClassGenerator make:filament-widget 命令使用。

Class generator 的结构

了解 class generator 的最佳方式是查看其源码。它们模式非常相似,并使用 nette/php-generator 的功能。

值得关注的方法包括:

  • __construct() 接收传入 generator 的参数。这是你生成类时可获得的全部上下文信息。
  • getImports() 返回正在生成的类中使用的 imports。这不是完整列表;生成属性与方法时也可再添加 imports,若比事先全部提供更方便。
  • getExtends() 返回被继承类的完全限定名。
  • addTraitsToClass() 用于向正在生成的类添加 traits。会传入 nette/php-generatorClass 对象作为参数。
  • addPropertiesToClass() 用于向正在生成的类添加属性。会传入 nette/php-generatorClass 对象作为参数。
  • add*PropertyToClass() 方法用于向正在生成的类添加单个属性。会传入 nette/php-generatorClass 对象作为参数。通常由 addPropertiesToClass() 调用。
  • addMethodsToClass() 用于向正在生成的类添加方法。会传入 nette/php-generatorClass 对象作为参数。
  • add*MethodToClass() 方法用于向正在生成的类添加单个方法。会传入 nette/php-generatorClass 对象作为参数。通常由 addMethodsToClass() 调用。

替换 class generator

若要更改文件的生成方式,需先确定正确的 class generator(见上表)并替换它。

要替换它,创建一个继承目标 class generator 的新类。例如,若要替换 ResourceClassGenerator,可这样创建:

php
namespace App\Filament\Commands\FileGenerators\Resources;

use Filament\Commands\FileGenerators\Resources\ResourceClassGenerator as BaseResourceClassGenerator;

class ResourceClassGenerator extends BaseResourceClassGenerator
{
   // ...
}

还需在服务容器中注册绑定。可在如 AppServiceProvider 的 service provider 中完成:

php
use App\Filament\Commands\FileGenerators\Resources\ResourceClassGenerator;
use Filament\Commands\FileGenerators\Resources\ResourceClassGenerator as BaseResourceClassGenerator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        // ...
    
        $this->app->bind(BaseResourceClassGenerator::class, ResourceClassGenerator::class);
        
        // ...
    }
}

自定义类中已有属性或方法

查看 class generator 源码时,找到要自定义的属性或方法。可在你的 class generator 中定义同名方法来覆盖。例如,下面是向 resource 类添加 $navigationIcon 属性的方法:

php
use BackedEnum;
use Filament\Support\Icons\Heroicon;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;

protected function addNavigationIconPropertyToClass(ClassType $class): void
{
    $this->namespace->addUse(BackedEnum::class);
    $this->namespace->addUse(Heroicon::class);

    $property = $class->addProperty('navigationIcon', new Literal('Heroicon::OutlinedRectangleStack'))
        ->setProtected()
        ->setStatic()
        ->setType('string|BackedEnum|null');
    $this->configureNavigationIconProperty($property);
}

你可以覆盖该方法以改变行为,也可以只覆盖 configureNavigationIconProperty() 来介入属性配置。例如,将属性设为 public 而非 protected

php
use Nette\PhpGenerator\Property;

protected function configureNavigationIconProperty(Property $property): void
{
    $property->setPublic();
}

向类添加新属性或方法

要向类添加新属性或方法,可在 addPropertiesToClass()addMethodsToClass() 中完成。若要继承已有属性而非替换,请在方法开头调用 parent::addPropertiesToClass()parent::addMethodsToClass()。例如,向 resource 类添加新属性:

php
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\Literal;

protected function addPropertiesToClass(ClassType $class): void
{
    parent::addPropertiesToClass($class);

    $class->addProperty('navigationSort', 10)
        ->setProtected()
        ->setStatic()
        ->setType('?int');
}