Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additions to the generate module command #2995

Merged
merged 4 commits into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/Command/Generate/ModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class ModuleCommand extends Command
*/
protected $site;

/**
* @var string
*/
protected $twigtemplate;


/**
* ModuleCommand constructor.
Expand All @@ -69,6 +74,7 @@ class ModuleCommand extends Command
* @param DrupalApi $drupalApi
* @param Client $httpClient
* @param Site $site
* @param $twigtemplate
*/
public function __construct(
ModuleGenerator $generator,
Expand All @@ -77,7 +83,8 @@ public function __construct(
StringConverter $stringConverter,
DrupalApi $drupalApi,
Client $httpClient,
Site $site
Site $site,
$twigtemplate
) {
$this->generator = $generator;
$this->validator = $validator;
Expand All @@ -86,6 +93,7 @@ public function __construct(
$this->drupalApi = $drupalApi;
$this->httpClient = $httpClient;
$this->site = $site;
$this->twigtemplate = $twigtemplate;
parent::__construct();
}

Expand Down Expand Up @@ -163,6 +171,12 @@ protected function configure()
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.module.options.test')
)
->addOption(
'twigtemplate',
'',
InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.module.options.twigtemplate')
);
}

Expand Down Expand Up @@ -192,6 +206,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$featuresBundle = $input->getOption('features-bundle');
$composer = $input->getOption('composer');
$test = $input->getOption('test');
$twigtemplate = $input->getOption('twigtemplate');

// Modules Dependencies, re-factor and share with other commands
$dependencies = $this->validator->validateModuleDependencies($input->getOption('dependencies'));
Expand Down Expand Up @@ -220,7 +235,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$featuresBundle,
$composer,
$dependencies,
$test
$test,
$twigtemplate
);
}

Expand Down Expand Up @@ -434,6 +450,15 @@ function ($core) {
);
$input->setOption('test', $test);
}

$twigtemplate = $input->getOption('twigtemplate');
if (!$twigtemplate) {
$twigtemplate = $io->confirm(
$this->trans('commands.generate.module.questions.twigtemplate'),
true
);
$input->setOption('twigtemplate', $twigtemplate);
}
}

/**
Expand Down
46 changes: 45 additions & 1 deletion src/Generator/ModuleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ModuleGenerator extends Generator
* @param $composer
* @param $dependencies
* @param $test
* @param $twigtemplate
*/
public function generate(
$module,
Expand All @@ -37,7 +38,8 @@ public function generate(
$featuresBundle,
$composer,
$dependencies,
$test
$test,
$twigtemplate
) {
$dir .= '/'.$machineName;
if (file_exists($dir)) {
Expand Down Expand Up @@ -77,6 +79,7 @@ public function generate(
'package' => $package,
'dependencies' => $dependencies,
'test' => $test,
'twigtemplate' => $twigtemplate,
);

$this->renderFile(
Expand Down Expand Up @@ -118,5 +121,46 @@ public function generate(
$parameters
);
}
if ($twigtemplate) {
$this->renderFile(
'module/module-twig-template-append.twig',
$dir .'/' . $machineName . '.module',
$parameters,
FILE_APPEND
);
$dir .= '/templates/';
if (file_exists($dir)) {
if (!is_dir($dir)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" exists but is a file.',
realpath($dir)
)
);
}
$files = scandir($dir);
if ($files != array('.', '..')) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" is not empty.',
realpath($dir)
)
);
}
if (!is_writable($dir)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" is not writable.',
realpath($dir)
)
);
}
}
$this->renderFile(
'module/twig-template-file.twig',
$dir . $machineName . '.html.twig',
$parameters
);
}
}
}
12 changes: 12 additions & 0 deletions templates/module/module-twig-template-append.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

/**
* Implements hook_theme().
*/
function {{machine_name}}_theme() {
return [
'{{machine_name}}' => [
'template' => '{{machine_name}}',
'render element' => 'children',
],
];
}
1 change: 1 addition & 0 deletions templates/module/twig-template-file.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- Add you custom twig html here -->