panlatent / craft-enums
dev-main
2024-05-11 01:33 UTC
Requires
- php: >=8.2
- craftcms/cms: ^4.0|^5.0
- craftcms/generator: ^1.3|^2.0
This package is auto-updated.
Last update: 2024-09-11 02:22:16 UTC
README
Craft Enum 是一个 PHP 枚举生成器。它生成所有字段、部分、卷等,并将写入代码作为case处理。
// Get a section $section = Section::Posts->section() // Create a entry with sectionId $entry = Section::Posts->new() // Entry query with section $entryQuery = Section::Post->find(); // Replace string handle (Not recommended) $section = Craft::$app->secionts->getSectionByHandle(Section::Posts->value);
Note: You cannot use PHP enumerations to directly replace string. You should write `getSectionByHandle(Section::Posts->value)` instead of `getSectionByHandle(Section::Posts)`(error).
Maybe, never try to use enums instead of strings or constants, but use the convenience methods of the enum class.
要求
- PHP 8.2
- Craft CMS 4.3.5 或更高版本。
安装
composer require panlatent/craft-enum --dev
使用
生成
- 手册
参考: CraftCMS 文档
php craft make enums
将枚举生成到 src/enums
ddev craft make enums --path=src
- 自动
建议您注册事件以实时生成
use panlatent\craft\enums\Enums; Event::on(ProjectConfig::class, ProjectConfig::EVENT_AFTER_WRITE_YAML_FILES), function() { (new \Symfony\Component\Process\Process(['php', 'craft', 'make', 'enums', '--path=src', '--interactive=1'], Craft::getAlias('@root'), [Enums::DISABLE_INTERACTIVE => true]))->mustRun(); });
我们使用 Symfony Process
组件在项目更改时运行构建命令,使用 --interactive=1
和 Enums::DISABLE_INTERACTIVE => true
以绕过CraftCMS限制。
编码
参见生成代码。
参考
许可证
该项目是开源软件,许可协议为 MIT 协议。