adachsoft / codegen-core
Composable code generation primitives for parsing plans and describing file changes.
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
Requires (Dev)
- adachsoft/php-code-style: ^0.5.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.2
- rector/rector: ^2.5
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A domain-neutral PHP library for composing code-generation pipelines. It parses operation plans, routes contexts to sources, validates source data, and returns typed descriptions of file changes. It does not modify the filesystem; the consuming application applies the returned changes.
Features
- Extensible
SourceInterfacestrategies selected by a context identifier. - JSON and array operation-plan parsing with input and relative-path validation.
- Built-in sources for writing, deleting, moving, and copying files.
- Immutable DTOs and typed collections for contexts, plans, and file changes.
CodegenEnginesupport checks and execution of one or all matching sources.
Installation
composer require adachsoft/codegen-core
Requirements
- PHP
^8.3 adachsoft/collectionfor immutable typed collections.
Usage
Register the built-in sources with the engine, parse a plan, and execute each operation. The application is responsible for applying each returned FileChangeInterface to its target filesystem or other storage.
use AdachSoft\CodegenCore\Collection\CodegenSourceCollection;
use AdachSoft\CodegenCore\Engine\CodegenEngine;
use AdachSoft\CodegenCore\Plan\CodegenPlanContextFactory;
use AdachSoft\CodegenCore\Plan\CodegenPlanParser;
use AdachSoft\CodegenCore\Plan\PlanOperationDataReader;
use AdachSoft\CodegenCore\Source\CopyFileSource;
use AdachSoft\CodegenCore\Source\MoveFileSource;
use AdachSoft\CodegenCore\Source\NewFileSource;
use AdachSoft\CodegenCore\Source\RemoveFileSource;
$plan = (new CodegenPlanParser())->parseJson($planJson);
$dataReader = new PlanOperationDataReader();
$engine = new CodegenEngine(new CodegenSourceCollection([
new RemoveFileSource($dataReader),
new MoveFileSource($dataReader),
new CopyFileSource($dataReader),
new NewFileSource($dataReader),
]));
$contextFactory = new CodegenPlanContextFactory();
foreach ($plan->operations as $operation) {
$context = $contextFactory->createForOperation($operation);
if (!$engine->supports($context)) {
throw new RuntimeException(sprintf('No source supports action "%s".', $context->identifier));
}
foreach ($engine->runOne($context) as $change) {
// Apply the returned file change in your application.
}
}
See the operation plan example for a complete plan and integration notes. Implement SourceInterface and register additional sources to support domain-specific operations.
Development
composer install
vendor/bin/phpunit --configuration phpunit.xml
composer stan
composer cs:check