Skip to content

3.0.0

Compare
Choose a tag to compare
@Chi-teck Chi-teck released this 26 Mar 07:16
· 179 commits to 3.x since this release

Release Notes

New API for generators

DCG 3 generators are not API compatible with DCG 2, however porting them to DCG 3 is quite simple.

Example of DCG 3 generator

#[Generator(
  name: 'controller',
  description: 'Generates a controller',
  templatePath: __DIR__,
  type: GeneratorType::MODULE_COMPONENT,
)]
final class Controller extends BaseGenerator {

  /**
   * {@inheritdoc}
   */
  protected function generate(array &$vars, Assets $assets): void {
    $ir = $this->createInterviewer($vars);

    $vars['machine_name'] = $ir->askMachineName();
    $vars['name'] = $ir->askName();
    $vars['class'] = $ir->askClass(default: '{machine_name|camelize}Controller');

    $vars['services'] = $ir->askServices(FALSE);

    $assets->addFile('src/Controller/{class}.php', 'controller.twig');
  }

}

Key differences from DCG 2 generator:

  1. Generator metadata is defined through PHP attributes.
  2. Most interaction logic has been moved to a separate service called "interviewer".
  3. All generators, regardless of their type, directly extend the BaseGenerator class.

Updated requirements

DCG 3 requires PHP 8.1+, symfony/console 6, twig/twig 3.

Revised generators

DCG 2 generators often created a sort of working examples, not just code templates. That has been changed in DCG 3.
The generated code is now just a boilerplate, it typically does not perform any useful actions without customization. If
you are looking for examples check out the Examples module.

Removed generators

The following generators have been removed:

  • yml:theme-info
  • yml:module-info
  • theme:file
  • misc:project
  • misc:html-page
  • module-file
  • console:drupal-console-command
  • console:drush-command
  • misc:d7:* (all Drupal 7 generators)

Those generators had little value and were rarely used.

Improved PhpStorm metadata generator

The phpstorm-meta generator can now provide PhpStorm with way more information about the project. It's recommended to add this command to your local deployment process and exclude the .phpstorm.meta.php directory from version control using the .gitignore file.

Event system

DCG 3 dispatches events that modules can subscribe to.
Currently, the following events are available:

  1. Application - allows to alter DCG application
  2. Generator Info - allows to register new generators
  3. Generator Info Alter - allows to alter registered generators
  4. Asset Preprocess - allows to modify generated code

Note that using Drush services is still recommended way to register generators in the Drush application.