barnythorpe/wp-config-containers

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

0.1.0 2025-04-16 12:15 UTC

This package is auto-updated.

Last update: 2025-04-16 12:25:22 UTC


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 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 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 and WP_Taxonomy)
  • phpDocumentor (phpdocumentor/reflection-docblock)
  • Twig (twig/twig)