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-resource 和 make:filament-page 命令使用。Filament\Commands\FileGenerators\Resources\Pages\ResourceCustomPageClassGenerator 由 make:filament-resource 和 make:filament-page 命令使用。Filament\Commands\FileGenerators\Resources\Pages\ResourceEditRecordPageClassGenerator 被 make:filament-resource 和 make:filament-page 命令使用。Filament\Commands\FileGenerators\Resources\Pages\ResourceListRecordsPageClassGenerator 由 make:filament-resource 和 make:filament-page 命令使用。Filament\Commands\FileGenerators\Resources\Pages\ResourceManageRecordsPageClassGenerator 被 make:filament-resource 和 make:filament-page 命令使用。Filament\Commands\FileGenerators\Resources\Pages\ResourceManageRelatedRecordsPageClassGenerator 由 make:filament-resource 和 make:filament-page 命令使用。Filament\Commands\FileGenerators\Resources\Pages\ResourceViewRecordPageClassGenerator 被 make:filament-resource 和 make: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:install 和 make: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 命令使用。了解类生成器的最佳方式是查看它们的源代码。它们都遵循非常相似的模式,并使用了来自 nette/php-generator 的功能。
以下是一些需要注意的方法:
__construct() 接受传入生成器的参数。这就是您在生成类时,作为上下文所能访问到的所有信息。getImports() 返回正在生成的类中使用的导入。这不是一个排他性列表,如果比提前提供它们更容易,那么在生成属性和方法时也可以添加导入。getExtends() 返回被扩展的类的完全限定名称。addTraitsToClass() 用于向正在生成的类添加特性。来自 nette/php-generator 的 Class 对象作为参数传入。addPropertiesToClass() 用于向正在生成的类添加属性。来自 nette/php-generator 的 Class 对象作为参数传入。add*PropertyToClass() 方法用于向正在生成的类添加单个属性。nette/php-generator 中的 Class 对象作为参数传入。它们通常从 addPropertiesToClass() 调用。addMethodsToClass() 用于向正在生成的类添加方法。来自 nette/php-generator 的 Class 对象作为参数传入。add*MethodToClass() 方法用于向正在生成的类添加单个方法。 来自 nette/php-generator 的 Class 对象作为参数传入。 它们通常从 addMethodsToClass() 调用。为了能够修改文件生成方式,你需要识别正确的类生成器(见上方列表)并替换它。
要替换它,创建一个新类,该类继承自您想要替换的类生成器。例如,如果您想替换 ResourceClassGenerator,则创建一个新类,如下所示:
namespace App\Filament\Commands\FileGenerators\Resources;
use Filament\Commands\FileGenerators\Resources\ResourceClassGenerator as BaseResourceClassGenerator;
class ResourceClassGenerator extends BaseResourceClassGenerator
{
// ...
}你还需要将其注册为服务容器中的一个绑定。你可以在一个服务提供者中这样做 例如 AppServiceProvider:
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);
// ...
}
}在查看类生成器的源代码时,找到你想要自定义的属性或方法。你可以在你的类生成器中通过简单地定义一个同名的新方法来覆盖它。例如,这里有一个你可以找到的方法,用于将 $navigationIcon 属性添加到资源类中:
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:
use Nette\PhpGenerator\Property;
protected function configureNavigationIconProperty(Property $property): void
{
$property->setPublic();
}要向类添加新属性或方法,可以在 addPropertiesToClass() 或 addMethodsToClass() 方法中进行。要继承现有属性而非替换它们,请确保在方法的开头调用 parent::addPropertiesToClass() 或 parent::addMethodsToClass()。例如,以下是如何向资源类添加新属性:
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');
}