33namespace DrupalCodeGenerator \Command ;
44
55use DrupalCodeGenerator \Application ;
6- use Symfony \Component \Console \Question \Question ;
76
87/**
98 * Implements composer command.
@@ -15,19 +14,24 @@ final class Composer extends DrupalGenerator {
1514 protected string $ alias = 'composer.json ' ;
1615 protected string $ label = 'composer.json ' ;
1716 protected string $ templatePath = Application::TEMPLATE_PATH . '/composer ' ;
18- protected ?string $ nameQuestion = NULL ;
19- protected ?string $ machineNameQuestion = 'Project machine name ' ;
2017
2118 /**
2219 * {@inheritdoc}
2320 */
2421 protected function generate (array &$ vars ): void {
25- $ this ->collectDefault ($ vars );
22+ // @see https://getcomposer.org/doc/04-schema.md#name
23+ $ validator = static function (string $ input ): string {
24+ if (!\preg_match ('#^[a-z0-9]([_.-]?[a-z0-9]+)*/[a-z0-9](([_.]?|-{0,2})[a-z0-9]+)*$# ' , $ input )) {
25+ throw new \UnexpectedValueException ('The package name sdsdf is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name. ' );
26+ }
27+ return $ input ;
28+ };
29+ $ vars ['project_name ' ] = $ this ->ask ('Project name ' , 'drupal/example ' , $ validator );
30+ [, $ vars ['machine_name ' ]] = \explode ('/ ' , $ vars ['project_name ' ]);
31+
2632 $ vars ['description ' ] = $ this ->ask ('Description ' );
2733
28- $ type_question = new Question ('Type ' , 'drupal-module ' );
29- $ type_question ->setValidator ([self ::class, 'validateRequired ' ]);
30- $ type_question ->setAutocompleterValues ([
34+ $ type_choices = [
3135 'drupal-module ' ,
3236 'drupal-custom-module ' ,
3337 'drupal-theme ' ,
@@ -36,20 +40,20 @@ protected function generate(array &$vars): void {
3640 'drupal-profile ' ,
3741 'drupal-custom-profile ' ,
3842 'drupal-drush ' ,
39- ]) ;
40- $ vars ['type ' ] = $ this ->io -> askQuestion ( $ type_question );
43+ ];
44+ $ vars ['type ' ] = $ this ->choice ( ' Project type ' , \array_combine ( $ type_choices , $ type_choices ) );
4145
4246 $ custom_types = [
4347 'drupal-custom-module ' ,
4448 'drupal-custom-theme ' ,
4549 'drupal-custom-profile ' ,
4650 ];
47- if (!\in_array ($ vars ['type ' ], $ custom_types )) {
48- // If project type is custom, there is no reason to ask this.
49- $ vars ['drupal_org ' ] = $ this ->confirm ('Is this project hosted on drupal.org? ' , FALSE );
51+ if (\in_array ($ vars ['type ' ], $ custom_types )) {
52+ $ vars ['drupal_org ' ] = FALSE ;
5053 }
5154 else {
52- $ vars ['drupal_org ' ] = FALSE ;
55+ // If project type is custom, there is no reason to ask this.
56+ $ vars ['drupal_org ' ] = $ this ->confirm ('Is this project hosted on drupal.org? ' , FALSE );
5357 }
5458
5559 $ this ->addFile ('composer.json ' , 'composer ' );
0 commit comments