Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* New comand to generate module file * Update generate:module file command
- Loading branch information
1 parent
25853f4
commit 6732752
Showing
4 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
description: 'Generate a .module file' | ||
help: 'The <info>generate:module:file</info> command helps you generates a new .module file' | ||
welcome: 'Welcome to the Drupal module generator' | ||
options: | ||
module: 'The Module name' | ||
questions: | ||
module: 'Enter the new module name' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Command\Generate\ModuleFileCommand. | ||
*/ | ||
|
||
namespace Drupal\Console\Command\Generate; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Drupal\Console\Generator\ModuleFileGenerator; | ||
use Drupal\Console\Command\Shared\ConfirmationTrait; | ||
use Drupal\Console\Command\Shared\ModuleTrait; | ||
use Drupal\Console\Command\GeneratorCommand; | ||
use Drupal\Console\Style\DrupalStyle; | ||
|
||
|
||
class ModuleFileCommand extends GeneratorCommand | ||
{ | ||
use ConfirmationTrait; | ||
use ModuleTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('generate:module:file') | ||
->setDescription($this->trans('commands.generate.module.file.description')) | ||
->setHelp($this->trans('commands.generate.module.file.help')) | ||
->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module')); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new DrupalStyle($input, $output); | ||
|
||
// @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration | ||
if (!$this->confirmGeneration($io, $yes)) { | ||
return; | ||
} | ||
|
||
$machine_name = $input->getOption('module'); | ||
$file_path = $this->getSite()->getModulePath($machine_name); | ||
|
||
$generator = $this->getGenerator(); | ||
$generator->generate( | ||
$machine_name, | ||
$file_path | ||
); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function interact(InputInterface $input, OutputInterface $output) | ||
{ | ||
|
||
$io = new DrupalStyle($input, $output); | ||
|
||
// --module option | ||
$module = $input->getOption('module'); | ||
|
||
if (!$module) { | ||
// @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion | ||
$module = $this->moduleQuestion($io); | ||
} | ||
|
||
$input->setOption('module', $module); | ||
|
||
} | ||
|
||
/** | ||
* @return ModuleFileGenerator | ||
*/ | ||
protected function createGenerator() | ||
{ | ||
return new ModuleFileGenerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Console\Generator\ModuleFileGenerator. | ||
*/ | ||
|
||
namespace Drupal\Console\Generator; | ||
|
||
/** | ||
* Class ModuleFileGenerator | ||
* @package Drupal\Console\Generator | ||
*/ | ||
class ModuleFileGenerator extends Generator | ||
{ | ||
/** | ||
* @param $machine_name | ||
* @param $file_path | ||
*/ | ||
public function generate( | ||
$machine_name, | ||
$file_path | ||
) { | ||
$dir = $file_path .'/'. $machine_name. '.module'; | ||
|
||
if (file_exists($dir)) { | ||
if (!is_dir($dir)) { | ||
throw new \RuntimeException( | ||
sprintf( | ||
'Unable to generate the .module file , it already exist at "%s"', | ||
realpath($dir) | ||
) | ||
); | ||
} | ||
|
||
} | ||
|
||
$parameters = array( | ||
'machine_name' => $machine_name, | ||
'file_path' => $file_path , | ||
); | ||
|
||
if ($machine_name) { | ||
$this->renderFile( | ||
'module/module-file.twig', | ||
$file_path . '/' . $machine_name . '.module', | ||
$parameters | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{% extends "base/file.php.twig" %} | ||
|
||
{% block file_path %}{{machine_name}}.module.{% endblock %} | ||
|
||
|