Installation:
composer require avro/generator-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Avro\GeneratorBundle\AvroGeneratorBundle::class => ['all' => true],
];
First Use Case: Generate a basic bundle skeleton:
php app/console avro:generate:bundle --name=Acme/DemoBundle
--dir=src).Key Files:
Resources/views/AvroGeneratorBundle/ (extend or override these).config/packages/avro_generator.yaml (or config/avro_generator.yaml in older Symfony versions).Entity-to-Code Generation:
# config/packages/avro_generator.yaml
avro_generator:
generators:
entity_to_crud:
template: 'AvroGeneratorBundle:Entity:CRUD'
mapping:
Acme\DemoBundle\Entity\Post: 'AcmeDemoBundle'
php app/console avro:generate:entity-to-crud
Custom Templates:
templates/AvroGeneratorBundle/YourNamespace/YourTemplate.twig:
{# templates/AvroGeneratorBundle/Custom/Service.twig #}
namespace {{ namespace }};
class {{ class_name }} extends {{ parent_class }}
{
public function __construct({{ dependencies }})
{
// ...
}
}
generators:
custom_service:
template: 'Custom:Service'
mapping: { Acme\DemoBundle\Entity\Post: 'PostService' }
Incremental Generation:
--dry-run to preview changes:
php app/console avro:generate:bundle --dry-run
--force (caution: use sparingly).Integration with Workflows:
post-deploy in composer.json to auto-generate code:
"scripts": {
"post-install-cmd": [
"php bin/console avro:generate:entity-to-crud --env=prod"
]
}
Template Inheritance:
AvroGeneratorBundle:Base:template.html.twig) to avoid rendering issues.{# Correct #}
{% extends 'AvroGeneratorBundle:Base:template.html.twig' %}
{% block content %}{{ content }}{% endblock %}
Namespace Collisions:
mapping keys in YAML use fully qualified class names (e.g., Acme\DemoBundle\Entity\Post), not short names.template: 'Entity:CRUD').Doctrine Metadata Cache:
php app/console doctrine:cache:clear-metadata
var/cache/dev/doctrine/orm/Proxies/__CG__....File Overwrites:
--force flag silently overwrites files. Use --dry-run first to inspect changes:
php app/console avro:generate:bundle --dry-run --force
Enable Twig Debugging:
Add to config/packages/twig.yaml:
twig:
debug: true
strict_variables: true
var/log/dev.log.Generator Logs:
debug: true in avro_generator config to log template rendering:
avro_generator:
debug: true
Template Variables:
{% dump _context.getAvailableFunctions() %}
{% dump entity.metadata %}
Custom Command: Extend the base generator command:
// src/Command/CustomGenerateCommand.php
namespace App\Command;
use Avro\GeneratorBundle\Command\GenerateCommand;
class CustomGenerateCommand extends GenerateCommand {
protected function configure() {
$this->setName('avro:generate:custom');
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this->generate('custom_service', $output);
}
}
Dynamic Templates:
Use Twig’s include to modularize templates:
{% include 'AvroGeneratorBundle:Parts:useStatements.twig' %}
Pre/Post-Generation Hooks: Override the generator service to add logic:
# config/services.yaml
services:
Avro\GeneratorBundle\Generator\Generator:
class: App\Service\CustomGenerator
decorates: 'avro_generator.generator'
// src/Service/CustomGenerator.php
use Avro\GeneratorBundle\Generator\Generator as BaseGenerator;
class CustomGenerator extends BaseGenerator {
public function generate($name, OutputInterface $output) {
// Pre-generation logic
parent::generate($name, $output);
// Post-generation logic
}
}
Environment-Specific Config:
Use Symfony’s environment-aware config (e.g., config/packages/avro_generator_{env}.yaml) to toggle generators:
# config/packages/avro_generator_prod.yaml
avro_generator:
generators:
entity_to_crud: ~ # Disabled in prod
How can I help you explore Laravel packages today?