barnythorpe/wp-config-containers

A simple package with auto-generated builder and DTO classes for Wordpress Post Types and Taxonomies.

0.3.0 2025-05-19 17:30 UTC

This package is auto-updated.

Last update: 2025-05-19 17:32:17 UTC


README

A Composer package for WordPress that provides strongly-typed DTO (Data Transfer Object) classes and Builder classes for various Wordpress configurations such as 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 Builder Classes and in some cases DTOs for:

The generators read the DocBlocks of WordPress' native classes and automatically create various classes to help construct the above configurations in a clean and safe manner.

📦 Installation

You’ll need Composer installed.

composer require barnythorpe/wp-config-containers

🚀 Usage

use function Barnythorpe\WpConfigContainers\post_type;

$postType = post_type()
    ->name('my_custom_post')
    ->public(true)
    ->build();

$configArray = $postType->toArray();

See the Tests directory for more examples of how to use.

🛠️ 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 types
    • Twig 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 or nette/php-generator to generate builder classes with fluent methods for setting those properties
  • Generated files are written to the src directory.

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

or

composer test