Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Generator Bundle Laravel Package

ano/generator-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require ano/generator-bundle
    

    Ensure your AppKernel.php includes the bundle:

    new Ano\GeneratorBundle\AnoGeneratorBundle(),
    
  2. First Use Case:

    • The bundle provides a Symfony command for generating code snippets.
    • Run the demo command to see available generators:
      php bin/console ano:generator:list
      
    • Generate a basic template (e.g., a controller) via:
      php bin/console ano:generator:generate controller --name=TestController
      
  3. Where to Look First:

    • Command Reference: Check ano:generator:* commands in bin/console.
    • Templates: Default templates are stored in vendor/ano/generator-bundle/Resources/templates/.
    • Configuration: Override defaults via config/packages/ano_generator.yaml (if supported).

Implementation Patterns

Core Workflows

  1. Template-Based Generation:

    • Use predefined templates (e.g., controllers, services, entities) with customizable parameters.
    • Example: Generate a CRUD controller with:
      php bin/console ano:generator:generate crud --entity=User --bundle=AppBundle
      
  2. Custom Templates:

    • Extend the bundle by placing custom templates in src/AnoGeneratorBundle/Resources/templates/ (or a custom namespace).
    • Override the GeneratorService to inject your templates.
  3. Integration with Existing Code:

    • Entities: Generate entities with Doctrine annotations or YAML/XML configs.
    • Services: Auto-generate service classes with dependency injection.
    • Commands: Scaffold console commands with predefined arguments.
  4. Parameterized Generation:

    • Pass dynamic values via CLI arguments or config:
      php bin/console ano:generator:generate service --name=UserService --interface=UserInterface
      

Best Practices

  • Namespace Management: Ensure generated classes use consistent namespaces (e.g., AppBundle\Entity).
  • Validation: Validate generated code before merging (e.g., check for duplicate methods).
  • Testing: Test generators with mock templates to avoid runtime errors.

Gotchas and Tips

Common Pitfalls

  1. Template Overrides:

    • If custom templates aren’t picked up, verify:
      • The template path is correct (e.g., Resources/templates/generators/).
      • The bundle is recompiled (composer dump-autoload).
  2. Symfony 4+ Compatibility:

    • The bundle targets Symfony 2.x ("symfony/framework-bundle":"2.*"). For Symfony 4/5/6:
      • Use flex or manually configure the bundle in config/bundles.php.
      • Replace AppKernel.php registration with:
        // config/bundles.php
        return [
            // ...
            Ano\GeneratorBundle\AnoGeneratorBundle::class => ['all' => true],
        ];
        
  3. Dependency Injection:

    • If generators fail, ensure required services (e.g., twig, filesystem) are available.
    • Override the GeneratorService to inject custom dependencies:
      # config/services.yaml
      services:
          App\Custom\GeneratorService:
              decorates: 'ano.generator.service'
              arguments: ['@App\Custom\GeneratorService.inner']
      
  4. File Conflicts:

    • Avoid overwriting existing files. Use --force cautiously:
      php bin/console ano:generator:generate --force
      

Debugging Tips

  • Verbose Output: Enable debug mode for generator commands:
    php bin/console ano:generator:generate --debug
    
  • Template Rendering: Check Twig errors if templates fail to render. Test with:
    php bin/console debug:twig
    
  • Logs: Review var/log/dev.log for generation errors.

Extension Points

  1. New Generators:

    • Create a custom command extending AnoGeneratorBundle\Command\GeneratorCommand.
    • Example:
      use Ano\GeneratorBundle\Command\GeneratorCommand;
      class CustomGeneratorCommand extends GeneratorCommand {
          protected function configure() {
              $this->setName('ano:custom:generate');
          }
      }
      
  2. Dynamic Templates:

    • Use Twig’s include to modularize templates:
      {# src/AnoGeneratorBundle/Resources/templates/custom.twig #}
      {% include 'base.twig' %}
      
  3. Post-Generation Hooks:

    • Extend the GeneratorService to run post-generation tasks (e.g., database migrations):
      public function generate($template, array $params) {
          $result = parent::generate($template, $params);
          $this->postGenerateHook($result);
          return $result;
      }
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ilhamsyabani/laravel-volt-starter
thethunderturner/filament-latex
ghostcompiler/laravel-querybuilder
webrek/laravel-telescope-mongodb
anousss007/blatui
zatona-eg/zatona-eg-api
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat