Release notes
New API for generators
DCG 2 generators are not API compatible with DCG 1 however porting them to DCG 2 should be trivial.
Generators must extend one of the following abstract classes.
Generator
└── DrupalGenerator
├── Theme generator
└── ModuleGenerator
└── PluginGenerator
Example:
/**
* Implements controller command.
*/
final class Controller extends ModuleGenerator {
protected $name = 'controller';
protected $description = 'Generates a controller';
protected $templatePath = __DIR__;
/**
* {@inheritdoc}
*/
protected function generate(array &$vars): void {
$this->collectDefault($vars);
$vars['class'] = $this->ask('Class', '{machine_name|camelize}Controller');
$this->collectServices($vars, FALSE);
if ($this->confirm('Would you like to create a route for this controller?')) {
$vars['route_name'] = $this->ask('Route name', '{machine_name}.example');
$vars['route_path'] = $this->ask('Route path', '/{machine_name|u2h}/example');
$vars['route_title'] = $this->ask('Route title', 'Example');
$vars['route_permission'] = $this->ask('Route permission', 'access content');
$this->addFile('{machine_name}.routing.yml', 'route.twig')->appendIfExists();
}
$this->addFile('src/Controller/{class}.php', 'controller.twig');
}
}
Updated requirements
DCG 2 requires PHP 7.3+, symfony/console
4+, twig/twig
2+ and therefore cannot be installed locally with Drupal 8.
Also, the generated code is supposed to work on Drupal 9 without triggering any deprecation notices. This means it
may not be compatible with Drupal 8. For this reason it is better to keep using DCG 1 for Drupal 8 development.
Generator namespaces have been changed
In DCG 1 generators were organized by Drupal core version. For example d7:hook
, d8:hook
, etc. With Drupal 9 release
it does not make much sense as the Drupal 9 API is mainly compatible with Drupal 8. So that all Drupal 8 generators
have been extracted into root namespace, i.e. d8:hook
=> hook
. Drupal 7 generators can be found under misc:d7
namespace.
Answer option now accepts multiple values
The --answer
option no longer accepts JSON encoded strings. Multiple answers can be passed as follows.
dcg controller -a Foo -a foo -a FooController -a No -a No
Improved entity type generators
DCG 2 is capable to generate entity types for existing modules.
Added support for inline templates.
$this->addFile('hello.txt')->inlineTemplate('Hello {{ name }}!');
Added support for symlinks.
$this->addSymlink('link.txt', 'source-file.txt');
New base class for generator tests
GeneratorTester
helper has been replaced with BaseGeneratorTest
class. Usage example.
New generate completion command
The command generates a completion script for DCG application.
dcg generate-completion --shell=bash >> ~/.bash_completion
Dry run mode
When running a generator with --dry-run
option the generated code will not be dumped to file system but
printed to terminal. To get information about rendered templates and collected variables enable debug verbosity (-vvv
).
PSR-3 logger
Generators and helpers may print messages to terminal using PSR-3 console logger.
$this->logger->debug('Example message.');
Extended result printer
Enable verbose mode (-v
) to print the information about generated assets in tabular form.
------ ---------------------------------- ------- --------
Type Path Lines Size
------ ---------------------------------- ------- --------
file example.links.action.yml 6 125
file example.links.menu.yml 11 320
file example.links.task.yml 23 630
file example.module 84 2199
file example.permissions.yml 4 83
file example.routing.yml 8 201
file src/ExampleInterface.php 15 342
file src/ExampleListBuilder.php 96 2798
file templates/example.html.twig 21 493
file src/Entity/Example.php 175 5173
file src/Form/ExampleForm.php 43 1216
file src/Form/ExampleSettingsForm.php 49 919
------ ---------------------------------- ------- --------
Total: 12 assets 535 14 KiB
------ ---------------------------------- ------- --------
Drupal context
When installed as local Composer package DCG is capable to bootstrap Drupal. This enables some advanced features
that can improve user experience. For example, autocompletion for Drupal modules and services.
Destination option
The new --destination
option allows to override destination directory for dumped assets.
Source code(tar.gz)
Source code(zip)
dcg.phar(1.81 MB)