barnythorpe / wp-config-containers
A simple package with auto-generated builder and DTO classes for Wordpress Post Types and Taxonomies.
Requires (Dev)
- barnythorpe/clarify: ^0.1.0
- laravel/pint: ^1.22
- pestphp/pest: ^3.8
- php-stubs/wordpress-stubs: ^6.7
- phpdocumentor/reflection-docblock: ^5.6
- phpdocumentor/type-resolver: ^1.10
- phpstan/phpstan: ^2.1
- roots/wordpress: ^6.7
- symfony/var-dumper: ^7.2
- twig/string-extra: ^3.20
- twig/twig: ^3.20
README
A Composer package for WordPress that provides strongly-typed DTO (Data Transfer Object) classes and Builder classes for WordPress Post Types and Taxonomies.
This package helps bring structure, clarity, and type safety to WordPress projects by wrapping Post Type and Taxonomy configurations in clean, typed objects with fluent builder interfaces.
đź“– About
This package auto-generates DTO and Builder classes for:
WP_Post_Type
WP_Taxonomy
The generators read the DocBlocks of WordPress' native classes and automatically create:
- A
Config
DTO containing typed public properties for each field. - A
Builder
class with fluent methods for configuring those properties.
Example:
$postType = (new PostTypeBuilder()) ->name('custom_post') ->public(true) ->build();
The output is a strongly-typed PostTypeConfig
object containing your configuration.
📦 Installation
You’ll need Composer installed.
composer require barnythorpe/wp-config-containers
🚀 Usage
use Barnythorpe\WpConfigContainers\Builders\PostTypeBuilder; $postType = (new PostTypeBuilder()) ->name('my_custom_post') ->public(true) ->build(); $configArray = $postType->toArray();
🛠️ Contributing & Developer Notes
If you're working on this package or maintaining it in the future, here’s what you need to know:
The Generator
- Located in:
src/WpConfigContainersGenerator/Generator.php
- Uses:
phpDocumentor
to parse DocBlocks and typesTwig
templates to generate DTO and Builder class files
- Templates:
src/WpConfigContainersGenerator/templates/dto.twig
src/WpConfigContainersGenerator/templates/builder.twig
To run the generator:
php generate.php
How It Works:
- The Generator reflects on the original WordPress classes (
WP_Post_Type
,WP_Taxonomy
). - It parses each property’s
@var
annotation to determine type and description. - Uses
Twig
to generate:- A DTO class with typed properties
- A Builder class with fluent methods for setting those properties
- Generated files are written to:
src/DTO/
src/Builders/
Testing
We use PestPHP for testing.
Tests are located in:
/tests/
Fixtures and test doubles are in:
/tests/Fixtures/
Run tests with:
vendor/bin/pest
📚 Requirements
- PHP 8.0+
- Composer
- WordPress (for accessing
WP_Post_Type
andWP_Taxonomy
) - phpDocumentor (
phpdocumentor/reflection-docblock
) - Twig (
twig/twig
)