Symfony MakerBundle

repository·1.x·Indexed 25 days ago

https://github.com/symfony/maker-bundle

A code generation tool for Symfony applications that provides CLI commands under the `make:` namespace to scaffold essential components such as controllers, forms, entities, and commands. It includes features for creating custom makers by extending `AbstractMaker` and specialized commands like `make:reset-password` for automating password reset systems.

Tokens
1.9K
Snippets
5
Records
12
Agent score
86%

What's inside symfony-maker-bundle

  1. Overview of Symfony MakerBundle

    1.x
    The MakerBundle is a development tool designed to accelerate Symfony application development by generating common code structures. It can be used to scaffold commands, controllers, form classes, event subscribers, and more.
  2. Build documentation locally

    1.x

    If you need to debug documentation issues or read the MakerBundle documentation offline, you can build it locally using the following steps:

    1. Navigate to the documentation build directory.
    2. Install dependencies via Composer.
    3. Run the build script.
    4. Serve the generated output using the PHP built-in server.
    cd _docs_build/
    
    composer install
    
    php build.php
    
    # After generating docs, serve them:
    php -S localhost:8000 -t output/
  3. Create your own custom Maker

    1.x

    To generate custom boilerplate code for your application, create a class that extends AbstractMaker in your src/Maker/ directory.

    Ensure your class is registered as a service and tagged with maker.command. If you are using the standard Symfony services.yaml configuration, this tagging happens automatically.

  4. Use MakerBundle commands

    1.x

    The bundle provides several commands under the make: namespace to generate boilerplate code like controllers, entities, and commands.

    To see a full list of available commands, run:

    $ php bin/console list make

    To see the specific arguments and options for a command, use the --help flag:

    $ php bin/console make:controller --help

    Note: make:entity requires doctrine/orm to be installed and configured. This maker only supports ORM, not ODM.

  5. Configure MakerBundle settings

    1.x

    You can customize the behavior of generated classes in config/packages/maker.yaml.

    Available configuration keys:

    • root_namespace (string): The root namespace used for all generated classes (default: App).
    • generate_final_classes (boolean): If true, all generated classes (except doctrine entities) will use the final keyword (default: true).
    • generate_final_entities (boolean): If true, doctrine entity classes will also use the final keyword (default: false).
    # config/packages/maker.yaml
    when@dev:
        maker:
            root_namespace: 'App'
            generate_final_classes: true
            generate_final_entities: false
  6. Configure the ResetPasswordBundle repository

    1.x

    The make:reset-password command attempts to automatically configure the symfonycasts/reset-password-bundle by setting the request_password_repository key in config/packages/reset_password.yaml.

    If the configuration file does not exist, you must manually set the repository class in your configuration to ensure the bundle knows which repository to use for managing reset requests.

  7. Configure the Mercure service via Docker Compose

    1.x

    The mercure service can be configured using environment variables to set the server name, JWT keys for publishing and subscribing, and extra directives.

    Note: The default JWT keys (!ChangeThisMercureHubJWTSecretKey!) should be changed to secure values in a production environment.

    services:
        mercure:
            image: dunglas/mercure
            environment:
                SERVER_NAME: :1337
                MERCURE_PUBLISHER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
                MERCURE_SUBSCRIBER_JWT_KEY: '!ChangeThisMercureHubJWTSecretKey!'
                MERCURE_EXTRA_DIRECTIVES: |
                    anonymous
                    cors_origins *
            ports:
                - 1337:1337
  8. Configure custom PHP-CS-Fixer paths

    1.x

    MakerBundle uses php-cs-fixer to enforce coding standards on generated .php files. You can override the default bundled version and configuration by setting the following environment variables:

    • MAKER_PHP_CS_FIXER_BINARY_PATH: Path to a custom php-cs-fixer binary.
    • MAKER_PHP_CS_FIXER_CONFIG_PATH: Path to a custom php-cs-fixer configuration file.
  9. MakerBundle Backwards Compatibility Promise

    1.x

    The MakerBundle follows the Symfony backwards compatibility promise, with two important clarifications for users:

    1. Command Interfaces: Input arguments or options for commands may change between minor releases. If you use MakerBundle commands in automated scripts, you should account for potential changes in command signatures.
    2. Generated Code: The code generated by the bundle may change between minor releases to allow for continuous improvements to the templates.
  10. Understand the MakerCommand lifecycle

    1.x

    The MakerCommand is the base class used for all maker operations in the Symfony MakerBundle. While it is marked as @internal, understanding its lifecycle is useful for developers building custom makers. The command follows this execution flow:

    1. Configuration: The command is configured via the MakerInterface::configureCommand method.
    2. Initialization: It checks for required dependencies using DependencyBuilder. If dependencies are missing, a RuntimeCommandException is thrown.
    3. Interaction: If run in interactive mode, the command prompts the user for missing arguments. It also checks if the application namespace matches the generator's root namespace.
    4. Execution: The core logic is delegated to MakerInterface::generate. After generation, the command performs linting on the generated files.

    Note on Non-Interactive Mode: Makers are primarily designed for interactive use. If run in non-interactive mode, a warning is issued via ConsoleStyle.

  11. Generate reset password functionality with make:reset-password

    1.x

    The make:reset-password command automates the creation of a complete password reset system using the symfonycasts/reset-password-bundle.

    It generates the following components:

    • Controller: ResetPasswordController to handle the reset logic.
    • Entities: ResetPasswordRequest entity to store reset tokens.
    • Repositories: ResetPasswordRequestRepository for database interactions.
    • Forms: ResetPasswordRequestFormType and ChangePasswordFormType.
    • Templates: Twig templates for checking email, the reset request form, and the password reset form.
    • Tests: A PHPUnit test class ResetPasswordControllerTest (if configured).

    Requirements & Prerequisites:

    • symfonycasts/reset-password-bundle version 1.6 or greater must be installed.
    • A config/packages/security.yaml file must exist (PHP and XML formats are not currently supported).
    • To run the generated tests, you must have symfony/test-pack installed.

    Interactive Configuration: During execution, the command will prompt you for:

    1. The User entity class to be used.
    2. The route to redirect users to after a successful reset (defaults to app_home).
    3. The sender email address (e.g., mailer@your-domain.com).
    4. The sender name (e.g., Acme Mail Bot).

    Post-Generation Steps:

    1. Run make:migration to create the database migration for the new ResetPasswordRequest entity.
    2. Review and customize forms in src/Form.
    3. Review and customize templates in templates/reset_password.
    4. Ensure your MAILER_DSN environment variable is correctly configured.
    5. Add a link to the app_forgot_password_request route on your login form.