This library generates new classes from templates by resolving attributes into executable logic. Ideal for scenarios like creating database migrations from templates, configuration generation, etc.
Define templates using PHP attributes to dynamically set class names, constants, and other properties:
#[Rename(new EvalString('sprintf("Version%s", date("YmdHis"))')]
class MigrationTemplate {
#[SetValue(new Guid())]
public const ID = '';
}
Use built-in providers to resolve values at runtime:
EvalString
: Execute PHP expressions (e.g.,new EvalString('uniqid()')
)Guid
: Generates UUIDsExpression
: Evaluate Symfony expressionsParam
: Inject values from context
composer require maximaster/attributemplate
// examples/001/input.php
namespace App;
use Maximaster\Attributemplate\TemplateAttribute\Rename;
use Maximaster\Attributemplate\TemplateAttribute\SetValue;
use Maximaster\Attributemplate\JustScalar\EvalString;
use Ramsey\Uuid\Uuid;
#[Rename(new EvalString('sprintf("Version%s", date("YmdHis"))'))]
class Migration
{
#[SetValue(new EvalString('Uuid::uuid5("namespace", "name")->toString()'))]
public const ID = '';
}
// examples/001/output.php
namespace App;
class Version20230101120000
{
public const ID = '550e8400-e29b-41d4-a716-446655440000';
}
Dynamically rename the class:
#[Rename(new EvalString('MyDynamicName'))]
class Base {}
// Output: class MyDynamicName {}
Set constant/property values:
#[SetValue(new Guid())]
public const UUID = '';
// Output: public const UUID = '123e4567-e89b-12d3-a456-426614174000';
EvalString
executes arbitrary PHP code - only use trusted expressions.
- Classes
- Class constants
- Parameters
- Properties
- test with
composer test
- lint with
composer lint
(also seecomposer fix
) composer qa
shortcuts bothtest
andlint