|
| 1 | +### Project Guidelines |
| 2 | + |
| 3 | +#### Purpose |
| 4 | +The Statamic Builder speeds up building Statamic sites by providing a fluent, PHP-based API to define sites, blueprints, fieldsets, collections, navigations, and taxonomies. This approach enhances code readability and maintainability by replacing traditional YAML configuration with PHP classes. |
| 5 | + |
| 6 | +#### Build and Configuration |
| 7 | + |
| 8 | +This project is a Statamic addon that integrates deeply with Statamic's core systems and provides a fluent API for |
| 9 | +building blueprints and fieldsets. |
| 10 | + |
| 11 | +- **Requirements**: PHP 8.2+, Statamic 5.4+, Laravel 10/11/12. |
| 12 | +- **Installation**: |
| 13 | + ```bash |
| 14 | + composer install |
| 15 | + ``` |
| 16 | +- **Service Provider**: `Tdwesten\StatamicBuilder\ServiceProvider` is automatically registered via Laravel's package |
| 17 | + discovery. It handles component discovery, repository binding, and command registration. |
| 18 | +- **Configuration**: |
| 19 | + Publish the configuration file to customize discovery paths and register components manually: |
| 20 | + ```bash |
| 21 | + php artisan vendor:publish --tag=statamic |
| 22 | + ``` |
| 23 | +- **Exporting to YAML**: |
| 24 | + If you need to generate standard Statamic YAML files from your PHP definitions: |
| 25 | + ```bash |
| 26 | + php artisan statamic-builder:export |
| 27 | + ``` |
| 28 | + |
| 29 | +#### Testing |
| 30 | +The project uses [Pest](https://pestphp.com/) for testing, along with `orchestra/testbench` for Laravel/Statamic integration. |
| 31 | + |
| 32 | +- **Configuring Tests**: |
| 33 | + - Tests extend `Tests\TestCase`, which boots the Statamic environment. |
| 34 | + - No additional database configuration is typically required as it uses in-memory storage for testing. |
| 35 | +- **Running Tests**: |
| 36 | + Execute the following command to run the full test suite: |
| 37 | + ```bash |
| 38 | + ./vendor/bin/pest |
| 39 | + ``` |
| 40 | + To run tests and static analysis (Rector): |
| 41 | + ```bash |
| 42 | + composer test |
| 43 | + ``` |
| 44 | +- **Adding Tests**: |
| 45 | + - Place new tests in `tests/Unit` or `tests/Feature`. |
| 46 | + - For new field types, use the generator to create a starting test: `composer generate-field MyField`. |
| 47 | + |
| 48 | +**Verified Example Test Case:** |
| 49 | +```php |
| 50 | +<?php |
| 51 | + |
| 52 | +use Tdwesten\StatamicBuilder\Blueprint; |
| 53 | +use Tdwesten\StatamicBuilder\FieldTypes\Section; |
| 54 | +use Tdwesten\StatamicBuilder\FieldTypes\Tab; |
| 55 | +use Tdwesten\StatamicBuilder\FieldTypes\Text; |
| 56 | + |
| 57 | +test('it can build a simple blueprint', function () { |
| 58 | + $blueprint = new class('test_handle') extends Blueprint { |
| 59 | + public function registerTabs(): array |
| 60 | + { |
| 61 | + return [ |
| 62 | + Tab::make('main', [ |
| 63 | + Section::make('General', [ |
| 64 | + Text::make('title')->displayName('Title'), |
| 65 | + ]), |
| 66 | + ]), |
| 67 | + ]; |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + $array = $blueprint->toArray(); |
| 72 | + |
| 73 | + expect($blueprint->getHandle())->toBe('test_handle'); |
| 74 | + expect($array['tabs']['main']['sections'][0]['display'])->toBe('General'); |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +#### Development Information |
| 79 | + |
| 80 | +- **Mandatory Tasks**: Always perform the following tasks when completing a task or before submitting a pull request: |
| 81 | + - **Update README**: Ensure `README.md` reflects any new features or changes. |
| 82 | + - **Format Code**: Run `vendor/bin/pint` to maintain consistent code style. |
| 83 | + - **Run Tests**: Ensure all tests pass by running `./vendor/bin/pest`. |
| 84 | +- **Code Style**: Follow PSR-12 and Laravel coding standards. `laravel/pint` is included for formatting. |
| 85 | +- **Static Analysis/Refactoring**: Rector is used for automated refactoring and code quality. Run it via: |
| 86 | + ```bash |
| 87 | + ./vendor/bin/rector |
| 88 | + ``` |
| 89 | +- **Fluent API Design**: Always use the `make()` static method for instantiating fields and chainable setters (e.g., |
| 90 | + `->displayName()`, `->instructions()`, `->required()`) for configuration. |
| 91 | +- **Custom Field Types**: New field types should extend `Tdwesten\StatamicBuilder\FieldTypes\Field`. |
| 92 | +- **Field Generator**: |
| 93 | + ```bash |
| 94 | + composer generate-field MyFieldName |
| 95 | + ``` |
| 96 | + This command populates `src/FieldTypes/` and `tests/Unit/` using templates in the `field-generator/` directory. |
| 97 | + |
| 98 | +#### Auto Registration & Discovery |
| 99 | + |
| 100 | +The addon supports auto-discovery to avoid manual registration in `config/statamic/builder.php`. |
| 101 | + |
| 102 | +- **Enable**: Set `'auto_registration' => true` in the config. |
| 103 | +- **Requirements**: |
| 104 | + - **Blueprints**: Must implement `public static function handle(): string` and |
| 105 | + `public static function blueprintNamespace(): string`. |
| 106 | + - **Collections, Taxonomies, Globals, Navigations**: Must implement `public static function handle(): string`. |
| 107 | + - **Sites**: Must implement `public function handle(): string`. |
| 108 | +- **Default Paths**: Components are discovered in `app/Blueprints`, `app/Collections`, etc. These can be customized in |
| 109 | + the `auto_discovery` configuration. |
0 commit comments