Symfony MakerBundle
repository·1.x·Indexed 25 days ago
https://github.com/symfony/maker-bundleA 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.
What's inside symfony-maker-bundle
- 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.
Build documentation locally
1.xIf you need to debug documentation issues or read the MakerBundle documentation offline, you can build it locally using the following steps:
- Navigate to the documentation build directory.
- Install dependencies via Composer.
- Run the build script.
- 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/Create your own custom Maker
1.xTo generate custom boilerplate code for your application, create a class that extends
AbstractMakerin yoursrc/Maker/directory.Ensure your class is registered as a service and tagged with
maker.command. If you are using the standard Symfonyservices.yamlconfiguration, this tagging happens automatically.Install the Symfony MakerBundle
1.xTo install and enable the MakerBundle in your Symfony application, run the following command in your terminal. It is recommended to install it as a dev dependency.
$ composer require --dev symfony/maker-bundleUse MakerBundle commands
1.xThe 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 makeTo see the specific arguments and options for a command, use the
--helpflag:$ php bin/console make:controller --helpNote:
make:entityrequiresdoctrine/ormto be installed and configured. This maker only supports ORM, not ODM.Configure MakerBundle settings
1.xYou 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): Iftrue, all generated classes (except doctrine entities) will use thefinalkeyword (default:true).generate_final_entities(boolean): Iftrue, doctrine entity classes will also use thefinalkeyword (default:false).
# config/packages/maker.yaml when@dev: maker: root_namespace: 'App' generate_final_classes: true generate_final_entities: falseConfigure the ResetPasswordBundle repository
1.xThe
make:reset-passwordcommand attempts to automatically configure thesymfonycasts/reset-password-bundleby setting therequest_password_repositorykey inconfig/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.
Configure the Mercure service via Docker Compose
1.xThe
mercureservice 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:1337Configure custom PHP-CS-Fixer paths
1.xMakerBundle uses
php-cs-fixerto enforce coding standards on generated.phpfiles. 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.
MakerBundle Backwards Compatibility Promise
1.xThe MakerBundle follows the Symfony backwards compatibility promise, with two important clarifications for users:
- 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.
- Generated Code: The code generated by the bundle may change between minor releases to allow for continuous improvements to the templates.
Understand the MakerCommand lifecycle
1.xThe
MakerCommandis 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:- Configuration: The command is configured via the
MakerInterface::configureCommandmethod. - Initialization: It checks for required dependencies using
DependencyBuilder. If dependencies are missing, aRuntimeCommandExceptionis thrown. - 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.
- 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.- Configuration: The command is configured via the
Generate reset password functionality with make:reset-password
1.xThe
make:reset-passwordcommand automates the creation of a complete password reset system using thesymfonycasts/reset-password-bundle.It generates the following components:
- Controller:
ResetPasswordControllerto handle the reset logic. - Entities:
ResetPasswordRequestentity to store reset tokens. - Repositories:
ResetPasswordRequestRepositoryfor database interactions. - Forms:
ResetPasswordRequestFormTypeandChangePasswordFormType. - 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-bundleversion 1.6 or greater must be installed.- A
config/packages/security.yamlfile must exist (PHP and XML formats are not currently supported). - To run the generated tests, you must have
symfony/test-packinstalled.
Interactive Configuration: During execution, the command will prompt you for:
- The User entity class to be used.
- The route to redirect users to after a successful reset (defaults to
app_home). - The sender email address (e.g.,
mailer@your-domain.com). - The sender name (e.g.,
Acme Mail Bot).
Post-Generation Steps:
- Run
make:migrationto create the database migration for the newResetPasswordRequestentity. - Review and customize forms in
src/Form. - Review and customize templates in
templates/reset_password. - Ensure your
MAILER_DSNenvironment variable is correctly configured. - Add a link to the
app_forgot_password_requestroute on your login form.
- Controller: