Skip to content

Commit

Permalink
[generate] Implement GeneratorInterface fixes. (#3735)
Browse files Browse the repository at this point in the history
* [generate] Implemente GeneratorInterface fixes.

* [generate] Indent array key/values.
  • Loading branch information
jmolivas authored Jan 23, 2018
1 parent 52c9638 commit 4b480ce
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 66 deletions.
26 changes: 14 additions & 12 deletions src/Command/Generate/ModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$twigTemplate = $input->getOption('twigtemplate');

$this->generator->generate(
$module,
$machineName,
$modulePath,
$description,
$core,
$package,
$moduleFile,
$featuresBundle,
$composer,
$dependencies,
$test,
$twigTemplate
[
'module' => $module,
'machine_name' => $machineName,
'module_path' => $modulePath,
'description' => $description,
'core' => $core,
'package' => $package,
'module_file' => $moduleFile,
'features_bundle' => $featuresBundle,
'composer' => $composer,
'dependencies' => $dependencies,
'test' => $test,
'twig_template' => $twigTemplate
]
);

return 0;
Expand Down
39 changes: 19 additions & 20 deletions src/Generator/CommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function generate(array $parameters) {

public function generate(array $parameters)
{
$extension = $parameters['extension'];
$extensionType = $parameters['extension_type'];
$name = $parameters['name'];
Expand All @@ -57,18 +57,19 @@ public function generate(array $parameters) {

$command_key = str_replace(':', '.', $name);

$extensionObject = $this->extensionManager
$extensionInstance = $this->extensionManager
->getDrupalExtension($extensionType, $extension);
$extensionObjectPath = $extensionObject->getPath();

$extensionObjectPath = $extensionInstance->getPath();

$parameters = array_merge(
$parameters,[
'command_key' => $command_key,
'tags' => [ 'name' => 'drupal.command' ],
'class_path' => sprintf('Drupal\%s\Command\%s', $extension, $class),
'file_exists' => file_exists($extensionObjectPath . '/console.services.yml'),
'class_generator_path' => sprintf('Drupal\%s\Command\%s', $extension, $class_generator),
]);
$parameters, [
'command_key' => $command_key,
'tags' => [ 'name' => 'drupal.command' ],
'class_path' => sprintf('Drupal\%s\Command\%s', $extension, $class),
'file_exists' => file_exists($extensionObjectPath . '/console.services.yml'),
]
);

$commandServiceName = $extension . '.' . str_replace(':', '_', $name);
$generatorServiceName = $commandServiceName . '_generator';
Expand All @@ -86,7 +87,7 @@ public function generate(array $parameters) {

$this->renderFile(
'module/src/Command/command.php.twig',
$extensionObject->getCommandDirectory() . $class . '.php',
$extensionInstance->getCommandDirectory() . $class . '.php',
$parameters
);

Expand All @@ -105,12 +106,6 @@ public function generate(array $parameters) {
);

if ($generator) {
$this->renderFile(
'module/src/Generator/generator.php.twig',
$extensionObject->getGeneratorDirectory() . $class_generator . '.php',
$parameters
);

$parameters = array_merge(
$parameters,
[
Expand All @@ -120,11 +115,15 @@ public function generate(array $parameters) {
'tags' => [ 'name' => 'drupal.generator' ],
'class_path' => sprintf('Drupal\%s\Generator\%s', $extension, $class_generator),
'file_exists' => file_exists($extensionObjectPath . '/console.services.yml'),
'class_generator' => $class_generator,
'class_generator_path' => sprintf('Drupal\%s\Generator\%s', $extension, $class_generator),
]
);

$this->renderFile(
'module/src/Generator/generator.php.twig',
$extensionInstance->getGeneratorDirectory() . $class_generator . '.php',
$parameters
);

$this->renderFile(
'module/services.yml.twig',
$extensionObjectPath .'/console.services.yml',
Expand Down
1 change: 1 addition & 0 deletions src/Generator/HelpGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\Console\Generator;

use Drupal\Console\Core\Generator\GeneratorInterface;
use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Core\Generator\GeneratorInterface;
use Drupal\Console\Extension\Manager;
Expand Down
59 changes: 28 additions & 31 deletions src/Generator/ModuleGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,125 +24,122 @@ class ModuleGenerator extends Generator implements GeneratorInterface
public function generate(array $parameters)
{
$machineName = $parameters['machine_name'];
$dir = $parameters['dir'];
$modulePath = $parameters['module_path'];
$moduleFile = $parameters['module_file'];
$featuresBundle = $parameters['features_bundle'];
$composer = $parameters['composer'];
$test = $parameters['test'];
$twigtemplate = $parameters['twigtemplate'];
$twigTemplate = $parameters['twig_template'];

$dir = ($dir == '/' ? '': $dir) . '/' . $machineName;
if (file_exists($dir)) {
if (!is_dir($dir)) {
$moduleDirectory = ($modulePath == '/' ? '': $modulePath) . '/' . $machineName;
if (file_exists($moduleDirectory)) {
if (!is_dir($moduleDirectory)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the module as the target directory "%s" exists but is a file.',
realpath($dir)
realpath($moduleDirectory)
)
);
}
$files = scandir($dir);
$files = scandir($moduleDirectory);
if ($files != ['.', '..']) {
throw new \RuntimeException(
sprintf(
'Unable to generate the module as the target directory "%s" is not empty.',
realpath($dir)
realpath($moduleDirectory)
)
);
}
if (!is_writable($dir)) {
if (!is_writable($moduleDirectory)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the module as the target directory "%s" is not writable.',
realpath($dir)
realpath($moduleDirectory)
)
);
}
}

$parameters = [
'type' => 'module',
];
$parameters['type'] = 'module';

$this->renderFile(
'module/info.yml.twig',
$dir . '/' . $machineName . '.info.yml',
$moduleDirectory . '/' . $machineName . '.info.yml',
$parameters
);

if (!empty($featuresBundle)) {
$this->renderFile(
'module/features.yml.twig',
$dir . '/' . $machineName . '.features.yml',
$moduleDirectory . '/' . $machineName . '.features.yml',
[
'bundle' => $featuresBundle,
]
);
}

if ($moduleFile) {
// Generate '.module' file.
$this->createModuleFile($dir, $parameters);
$this->createModuleFile($moduleDirectory, $parameters);
}

if ($composer) {
$this->renderFile(
'module/composer.json.twig',
$dir . '/' . 'composer.json',
$moduleDirectory . '/' . 'composer.json',
$parameters
);
}

if ($test) {
$this->renderFile(
'module/src/Tests/load-test.php.twig',
$dir . '/tests/src/Functional/' . 'LoadTest.php',
$moduleDirectory . '/tests/src/Functional/' . 'LoadTest.php',
$parameters
);
}
if ($twigtemplate) {
if ($twigTemplate) {
// If module file is not created earlier, create now.
if (!$moduleFile) {
// Generate '.module' file.
$this->createModuleFile($dir, $parameters);
$this->createModuleFile($moduleDirectory, $parameters);
}
$this->renderFile(
'module/module-twig-template-append.twig',
$dir . '/' . $machineName . '.module',
$moduleDirectory . '/' . $machineName . '.module',
$parameters,
FILE_APPEND
);
$dir .= '/templates/';
if (file_exists($dir)) {
if (!is_dir($dir)) {
$moduleDirectory .= '/templates/';
if (file_exists($moduleDirectory)) {
if (!is_dir($moduleDirectory)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" exists but is a file.',
realpath($dir)
realpath($moduleDirectory)
)
);
}
$files = scandir($dir);
$files = scandir($moduleDirectory);
if ($files != ['.', '..']) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" is not empty.',
realpath($dir)
realpath($moduleDirectory)
)
);
}
if (!is_writable($dir)) {
if (!is_writable($moduleDirectory)) {
throw new \RuntimeException(
sprintf(
'Unable to generate the templates directory as the target directory "%s" is not writable.',
realpath($dir)
realpath($moduleDirectory)
)
);
}
}
$this->renderFile(
'module/twig-template-file.twig',
$dir . str_replace('_', '-', $machineName) . '.html.twig',
$moduleDirectory . str_replace('_', '-', $machineName) . '.html.twig',
$parameters
);
}
Expand Down
6 changes: 3 additions & 3 deletions templates/module/src/Generator/generator.php.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "base/class.php.twig" %}

{% block file_path %}
\Drupal\{{extension}}\Generator\{{ class_generator }}.
\Drupal\{{extension}}\Generator\{{ class }}.
{% endblock %}

{% block namespace_class %}
Expand All @@ -15,11 +15,11 @@ use Drupal\Console\Core\Generator\GeneratorInterface;

{% block class_declaration %}
/**
* Class {{ class_generator }}
* Class {{ class_name }}
*
* @package Drupal\Console\Generator
*/
class {{ class_generator }} extends Generator implements GeneratorInterface
class {{ class_name }} extends Generator implements GeneratorInterface
{% endblock %}

{% block class_methods %}
Expand Down

0 comments on commit 4b480ce

Please sign in to comment.