Skip to content

Commit

Permalink
Implementation of generator interface (#3731)
Browse files Browse the repository at this point in the history
* Implementation of GeneratorInterface, replacement of generate method calls

* AuthenticationProviderGenerator implementation of GeneratorInterface

* BreakPointGenerator implementation of GeneratorInterface

* CacheContextCommandGenerator implementation of GeneratorInterface

* CommandGenerator implementation of GeneratorInterface

* ControllerGenerator implementation of GeneratorInterface. Update the comments for generate method

* ControllerGenerator, EntityBundleGenerator implementation of GeneratorInterface. Update the comments for generate method

* EntityConfigGenerator implementation of GeneratorInterface. Update the comments for generate method

* EntityContentCommand : Implementation of GeneratorInterface. Refactoring of generator method

* EventSubscriberCommand : Implementation of GeneratorInterface. Refactoring of generator method

* Refactor module path variables
  • Loading branch information
LOBsTerr authored and jmolivas committed Jan 23, 2018
1 parent b0a14a8 commit e203274
Show file tree
Hide file tree
Showing 26 changed files with 311 additions and 340 deletions.
4 changes: 2 additions & 2 deletions src/Command/Generate/AjaxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->generator->generate(
[
'module' => $module,
'class' => $class,
'$method' => $method
'class_name' => $class,
'method' => $method,
]
);

Expand Down
6 changes: 5 additions & 1 deletion src/Command/Generate/AuthenticationProviderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$class = $this->validator->validateClassName($input->getOption('class'));
$provider_id = $input->getOption('provider-id');

$this->generator->generate($module, $class, $provider_id);
$this->generator->generate([
'module' => $module,
'class' => $class,
'provider_id' => $provider_id,
]);

return 0;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Command/Generate/BreakPointCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$breakpoints = $this->explodeInlineArray($breakpoints);
}

$this->generator->generate(
$theme,
$breakpoints,
$machine_name
);
$this->generator->generate([
'theme' => $theme,
'breakpoints' => $breakpoints,
'machine_name' => $machine_name,
]);

return 0;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Command/Generate/CacheContextCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
// @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
$buildServices = $this->buildServices($services);

$this->generator->generate($module, $cache_context, $class, $buildServices);
$this->generator->generate([
'module' => $module,
'cache_context' => $cache_context,
'class' => $class,
'services' => $buildServices,
]);

$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
}
Expand Down
24 changes: 12 additions & 12 deletions src/Command/Generate/CommandCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$class_generator = str_replace('Command', 'Generator', $class);
}

$this->generator->generate(
$extension,
$extensionType,
$name,
$initialize,
$interact,
$class,
$containerAware,
$build_services,
$generator,
$class_generator
);
$this->generator->generate([
'extension' => $extension,
'extension_type' => $extensionType,
'name' => $name,
'initialize' => $initialize,
'interact' => $interact,
'class_name' => $class,
'container_aware' => $containerAware,
'services' => $build_services,
'class_generator' => $class_generator,
'generator' => $generator,
]);

$this->site->removeCachedServicesFile();

Expand Down
14 changes: 7 additions & 7 deletions src/Command/Generate/ControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$build_services = $this->buildServices($services);

//$this->generator->setLearning($learning);
$this->generator->generate(
$module,
$class,
$routes,
$test,
$build_services
);
$this->generator->generate([
'module' => $module,
'class_name' => $class,
'routes' => $routes,
'test' => $test,
'services' => $build_services
]);

// Run cache rebuild to see changes in Web UI
$this->chainQueue->addCommand('router:rebuild', []);
Expand Down
7 changes: 5 additions & 2 deletions src/Command/Generate/EntityBundleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundleName = $input->getOption('bundle-name');
$bundleTitle = $input->getOption('bundle-title');

$generator = $this->generator;
//TODO:
// $generator->setLearning($learning);
$generator->generate($module, $bundleName, $bundleTitle);
$this->generator->generate([
'module' => $module,
'bundle_name' => $bundleName,
'bundle_title' => $bundleTitle,
]);

return 0;
}
Expand Down
15 changes: 11 additions & 4 deletions src/Command/Generate/EntityConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function __construct(
parent::__construct();
}


/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setEntityType('EntityConfig');
Expand Down Expand Up @@ -93,8 +95,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundle_of = $input->getOption('bundle-of');
$base_path = $input->getOption('base-path');

$this
->generator
->generate($module, $entity_name, $entity_class, $label, $base_path, $bundle_of);
$this->generator->generate([
'module' => $module,
'entity_name' => $entity_name,
'entity_class' => $entity_class,
'label' => $label,
'base_path' => $base_path,
'bundle_of' => $bundle_of,
]);
}
}
13 changes: 11 additions & 2 deletions src/Command/Generate/EntityContentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$has_bundles = $input->getOption('has-bundles');
$base_path = $input->getOption('base-path');
$learning = $input->hasOption('learning')?$input->getOption('learning'):false;
$bundle_entity_name = $has_bundles ? $entity_name . '_type' : null;
$bundle_entity_type = $has_bundles ? $entity_name . '_type' : null;
$is_translatable = $input->hasOption('is-translatable') ? $input->getOption('is-translatable') : true;
$revisionable = $input->hasOption('revisionable') ? $input->getOption('revisionable') : false;

Expand All @@ -153,7 +153,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
//@TODO:
//$generator->setLearning($learning);

$generator->generate($module, $entity_name, $entity_class, $label, $base_path, $is_translatable, $bundle_entity_name, $revisionable);
$generator->generate([
'module' => $module,
'entity_name' => $entity_name,
'entity_class' => $entity_class,
'label' => $label,
'bundle_entity_type' => $bundle_entity_type,
'base_path' => $base_path,
'is_translatable' => $is_translatable,
'revisionable' => $revisionable,
]);

if ($has_bundles) {
$this->chainQueue->addCommand(
Expand Down
8 changes: 7 additions & 1 deletion src/Command/Generate/EventSubscriberCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
// @see Drupal\Console\Command\Shared\ServicesTrait::buildServices
$buildServices = $this->buildServices($services);

$this->generator->generate($module, $name, $class, $events, $buildServices);
$this->generator->generate([
'module' => $module,
'name' => $name,
'class' => $class,
'events' => $events,
'services' => $buildServices,
]);

$this->chainQueue->addCommand('cache:rebuild', ['cache' => 'all']);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Command/Shared/ServicesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function servicesQuestion()
*/
public function buildServices($services)
{
$buildServices = [];
if (!empty($services)) {
$buildServices = [];
foreach ($services as $service) {
$class = get_class($this->container->get($service));
$shortClass = explode('\\', $class);
Expand All @@ -69,10 +69,8 @@ public function buildServices($services)
'short' => end($shortClass),
];
}

return $buildServices;
}

return [];
return $buildServices;
}
}
8 changes: 5 additions & 3 deletions src/Generator/AjaxCommandGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,23 @@ public function generate(array $parameters)
$class = $parameters['class_name'];
$module = $parameters['module'];

$moduleInstance = $this->extensionManager->getModule($module);
$moduleDir = $moduleInstance->getPath();
$this->renderFile(
'module/src/Ajax/ajax-command.php.twig',
$this->extensionManager->getModule($module)->getAjaxPath().'/'.$class.'.php',
$moduleInstance->getAjaxPath() . '/' . $class . '.php',
$parameters
);

$this->renderFile(
'module/js/commands.php.twig',
$this->extensionManager->getModule($module)->getPath().'/js'.'/'.'custom.js',
$moduleDir . '/js/custom.js',
$parameters
);

$this->renderFile(
'module/module-libraries.yml.twig',
$this->extensionManager->getModule($module)->getPath().'/'.$module.'.libraries.yml',
$moduleDir . '/' . $module . '.libraries.yml',
$parameters
);
}
Expand Down
36 changes: 17 additions & 19 deletions src/Generator/AuthenticationProviderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

namespace Drupal\Console\Generator;

use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Core\Generator\GeneratorInterface;

class AuthenticationProviderGenerator extends Generator
class AuthenticationProviderGenerator extends Generator implements GeneratorInterface
{
/**
* @var Manager
Expand All @@ -29,46 +30,43 @@ public function __construct(
}

/**
* Generator Plugin Block.
*
* @param $module
* @param $class
* @param $provider_id
* {@inheritdoc}
*/
public function generate($module, $class, $provider_id)
public function generate(array $parameters)
{
$parameters = [
'module' => $module,
'class' => $class,
];
$module = $parameters['module'];
$class = $parameters['class'];
$provider_id = $parameters['provider_id'];
$moduleInstance = $this->extensionManager->getModule($module);
$modulePath = $moduleInstance->getPath() . '/' . $module;

$this->renderFile(
'module/src/Authentication/Provider/authentication-provider.php.twig',
$this->extensionManager->getModule($module)->getAuthenticationPath('Provider'). '/' . $class . '.php',
$moduleInstance->getAuthenticationPath('Provider') . '/' . $class . '.php',
$parameters
);

$parameters = [
$parameters = array_merge($parameters, [
'module' => $module,
'class' => $class,
'class_path' => sprintf('Drupal\%s\Authentication\Provider\%s', $module, $class),
'name' => 'authentication.'.$module,
'name' => 'authentication.' . $module,
'services' => [
['name' => 'config.factory'],
['name' => 'entity_type.manager'],
],
'file_exists' => file_exists($this->extensionManager->getModule($module)->getPath() .'/'.$module.'.services.yml'),
'file_exists' => file_exists($modulePath . '.services.yml'),
'tags' => [
'name' => 'authentication_provider',
'provider_id' => $provider_id,
'priority' => '100',
],
];
]);

$this->renderFile(
'module/services.yml.twig',
$this->extensionManager->getModule($module)->getPath() . '/' . $module . '.services.yml',
$parameters,
$modulePath . '.services.yml',
$parameters,
FILE_APPEND
);
}
Expand Down
29 changes: 9 additions & 20 deletions src/Generator/BreakPointGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

namespace Drupal\Console\Generator;

use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Core\Generator\Generator;
use Drupal\Console\Core\Generator\GeneratorInterface;

/**
* Class BreakPointGenerator
*
* @package Drupal\Console\Generator
*/
class BreakPointGenerator extends Generator
class BreakPointGenerator extends Generator implements GeneratorInterface
{
/**
* @var Manager
Expand All @@ -27,34 +28,22 @@ class BreakPointGenerator extends Generator
*
* @param Manager $extensionManager
*/
public function __construct(
Manager $extensionManager
) {
public function __construct(Manager $extensionManager) {
$this->extensionManager = $extensionManager;
}


/**
* Generator BreakPoint.
*
* @param $theme
* @param $breakpoints
* @param $machine_name
* {@inheritdoc}
*/
public function generate($theme, $breakpoints, $machine_name)
public function generate(array $parameters)
{
$parameters = [
'theme' => $theme,
'breakpoints' => $breakpoints,
'machine_name' => $machine_name
];

$theme_path = $this->extensionManager->getTheme($theme)->getPath();
$theme_path = $this->extensionManager->getTheme($parameters['theme'])->getPath();

$this->renderFile(
'theme/breakpoints.yml.twig',
$theme_path . '/' . $machine_name . '.breakpoints.yml',
$parameters,
$theme_path . '/' . $parameters['machine_name'] . '.breakpoints.yml',
$parameters,
FILE_APPEND
);
}
Expand Down
Loading

0 comments on commit e203274

Please sign in to comment.