https://github.com/panlatent/craft-enums
Generates all Fields, Section, Volume ... enums in Craft CMS
https://github.com/panlatent/craft-enums
const craftcms enum yii2 yii2-extension
Last synced: 4 months ago
JSON representation
Generates all Fields, Section, Volume ... enums in Craft CMS
- Host: GitHub
- URL: https://github.com/panlatent/craft-enums
- Owner: panlatent
- License: mit
- Created: 2024-04-11T08:26:11.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-11T01:36:04.000Z (about 2 years ago)
- Last Synced: 2025-04-03T16:02:17.681Z (about 1 year ago)
- Topics: const, craftcms, enum, yii2, yii2-extension
- Language: PHP
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Craft Enums
============
The Craft Enum is a PHP enum generator.
It generates all Fields, Section, Volume ... handles write to code as cases.
```php
// 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.
Requirements
------------
+ PHP 8.2
+ Craft CMS 4.3.5 or later.
Installation
------------
```bash
composer require panlatent/craft-enum --dev
```
Usage
------
### Generate
+ Manual
Reference: [CraftCMS Docs](https://craftcms.com/docs/5.x/extend/generator.html#usage)
```bash
php craft make enums
```
Generate enums to `src/enums`
```bash
ddev craft make enums --path=src
```
+ Automatic
It is recommended that you register events to generate real-time:
```php
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();
});
```
We use the `Symfony Process` component to run build commands when the project changes, using `--interactive=1` and `Enums::DISABLE_INTERACTIVE => true` in order to bypass CraftCMS limitation.
### Coding
See generate codes.
References
-----------
License
-------
The project is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).