mteu/typo3-typed-extconf

Aims to provide a type-safe extension configuration management for TYPO3, ensuring proper types instead of string-only values from backend configuration or mixed types from config/system/settings.php

Installs: 144

Dependents: 1

Suggesters: 0

Security: 0

Stars: 3

Watchers: 0

Forks: 1

Open Issues: 1

Type:typo3-cms-extension

0.2.0 2025-07-25 10:36 UTC

README

CGL Tests Coverage Maintainability

Extension Icon

TYPO3 Typed Extension Configuration

TYPO3 versions Latest version Stability PHP Version Require

This TYPO3 CMS extension aims to provide a type-safe extension configuration management for TYPO3, ensuring proper types instead of string-only values from backend configuration or mixed types from config/system/settings.php|additional.php (or custom solutions around those).

Warning

This extension is in a testing and review phase and hence marked beta. Be cautious when using this in production environments.

πŸš€ Features

  • Type Safety: Automatic conversion from TYPO3's string configuration to proper PHP types
  • Schema Definition: Define configuration using PHP attributes and constructor parameters
  • Path Mapping: Support for nested configuration with dot notation (api.endpoint)
  • Configuration Generation: Generate classes from ext_conf_template.txt or interactively
  • Dependency Injection: Configuration classes auto-registered as services

⚑️ Installation

Add this package to your TYPO3 Extension:

composer require mteu/typo3-typed-extconf

This extension relies heavily on these key dependencies:

πŸ’‘ Usage

Tip

For a comprehensive developer guide with advanced examples and best practices, check out the Developer Guide.

Note

If you're in a hurry you might want to have this package generate configuration classes automatically based on your extension's ext_conf_template.txt.

Run ./vendor/bin/typo3 typed-extconf:generate or consult the Command Guide.

Use with caution, though, since this functionality is not well tested, yet.

1. Define Configuration Schema

Create a configuration class for your extension using PHP attributes:

<?php

use mteu\TypedExtConf\Attribute\ExtConfProperty;
use mteu\TypedExtConf\Attribute\ExtensionConfig;

#[ExtensionConfig(extensionKey: 'my_extension')]
final readonly class MyExtensionConfig
{
    public function __construct(
        #[ExtConfProperty()]
        public int $maxItems = 10,

        #[ExtConfProperty(required: false)]
        public bool $enableFeature = true,

        #[ExtConfProperty(path: 'api.endpoint')]
        public string $apiEndpoint = '/api/v1',

        #[ExtConfProperty()]
        public array $allowedTypes = ['default', 'fallback'],
    ) {}
}

2. Access Typed Configuration

Option A: Direct Injection (Recommended)

Directly inject your configuration object using dependency injection:

<?php

final readonly class MyService
{
    public function __construct(
        private MyExtensionConfig $config,
    ) {}

    public function doSomething(): void
    {
        // All properties are guaranteed to have the correct types
        $maxItems = $this->config->maxItems; // int
        $isEnabled = $this->config->enableFeature; // bool
        $endpoint = $this->config->apiEndpoint; // string
        $types = $this->config->allowedTypes; // array
    }
}

Option B: Using the Provider

Alternatively, use the configuration provider service:

<?php

use mteu\TypedExtConf\Provider\ExtensionConfigurationProvider;

final readonly class MyService
{
    public function __construct(
        private ExtensionConfigurationProvider $configurationProvider,
    ) {}

    public function doSomething(): void
    {
        $config = $this->configurationProvider->get(MyExtensionConfig::class);

        // Use configuration...
    }
}

πŸ“™ Attribute Reference

#[ExtensionConfig]

Class-level attribute to specify which TYPO3 extension the configuration belongs to.

Parameters:

  • extensionKey (string, required): The TYPO3 extension key.

#[ExtConfProperty]

Property/parameter-level attribute for configuration value mapping.

Parameters:

  • path (string, optional): Custom configuration path using dot notation (e.g., 'api.endpoint')
  • required (bool, optional): Whether the configuration value is required (default: false)

Note

Default values are defined as PHP constructor parameter defaults, not in the attribute.

How It Works

TYPO3 stores extension configuration in $GLOBALS['TYPO3_CONF_VARS']['EXTENSIONS'] as an associative array with string values. Those strings could be manually altered by the developers to other types. This extension automatically converts those types to proper PHP types using your configuration class schema.

πŸ§‘β€πŸ’» Real-world example with nested configuration

#[ExtensionConfig(extensionKey: 'my_complex_ext')]
final readonly class ComplexConfiguration
{
    public function __construct(
        #[ExtConfProperty(path: 'api.endpoint')]
        public string $endpoint = '/api',

        // Nested configuration object
        public DatabaseConfiguration $database,

        #[ExtConfProperty()]
        public string $environment = 'production',
    ) {}
}

final readonly class DatabaseConfiguration
{
    public function __construct(
        #[ExtConfProperty(path: 'db.host')]
        public string $host = 'localhost',

        #[ExtConfProperty(path: 'db.port')]
        public int $port = 3306,

        #[ExtConfProperty(path: 'db.ssl')]
        public bool $enableSsl = true,
    ) {}
}

πŸ™ Credits

This project is built on the excellent CuyZ\Valinor library, which provides the core type mapping and validation functionality. Without Valinor's robust object mapping capabilities, this extension would not be possible.

Special thanks to:

  • CuyZ\Valinor for the powerful and flexible object mapping engine
  • Romain Canon and the Valinor contributors for their excellent work
  • Elias HΓ€ußler for his extensive help reviewing this extension and contributing substantial improvements. Be sure to check out his Composer packages β€” PHPUnit Attributes and Version Bumper β€” both of which greatly simplify maintaining this project.

🀝 Contributing

Contributions are very welcome! Please have a look at the Contribution Guide. It lays out the workflow of submitting new features or bugfixes.

πŸ”’ Security

Please refer to our security policy if you discover a security vulnerability in this extension. Be warned, though. I cannot afford bounty. This is private project.

⭐ License

This extension is licensed under the GPL-2.0-or-later license.

πŸ’¬ Support

For issues and feature requests, please use the GitHub issue tracker.