Skip to content
Permalink
Browse files

[generate:module] Show multisite dir to generate (#4053)

  • Loading branch information...
harold20 authored and enzolutions committed May 21, 2019
1 parent 1247f20 commit 1141d03c884e9107ba6237cd1fda33166c69a917
Showing with 83 additions and 15 deletions.
  1. +1 −1 config/services/generate.yml
  2. +22 −3 src/Command/Generate/ModuleCommand.php
  3. +13 −4 src/Command/Generate/ThemeCommand.php
  4. +47 −7 src/Utils/Site.php
@@ -1,7 +1,7 @@
services:
console.generate_module:
class: Drupal\Console\Command\Generate\ModuleCommand
arguments: ['@console.module_generator', '@console.validator', '@app.root', '@console.string_converter', '@console.drupal_api', '@console.chain_queue']
arguments: ['@console.module_generator', '@console.validator', '@app.root', '@console.string_converter', '@console.drupal_api', '@console.chain_queue', '@console.site']
tags:
- { name: drupal.command }
console.generate_modulefile:
@@ -7,6 +7,7 @@
namespace Drupal\Console\Command\Generate;
use Drupal\Console\Utils\Site;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@@ -58,6 +59,12 @@ class ModuleCommand extends Command
*/
protected $chainQueue;
/**
* @var Site
*/
protected $site;
/**
* ModuleCommand constructor.
*
@@ -67,6 +74,7 @@ class ModuleCommand extends Command
* @param StringConverter $stringConverter
* @param DrupalApi $drupalApi
* @param ChainQueue $chainQueue
* @param Site $site
* @param $twigtemplate
*/
public function __construct(
@@ -76,6 +84,7 @@ public function __construct(
StringConverter $stringConverter,
DrupalApi $drupalApi,
ChainQueue $chainQueue,
Site $site,
$twigtemplate = null
) {
$this->generator = $generator;
@@ -84,6 +93,7 @@ public function __construct(
$this->stringConverter = $stringConverter;
$this->drupalApi = $drupalApi;
$this->chainQueue = $chainQueue;
$this->site = $site;
$this->twigtemplate = $twigtemplate;
parent::__construct();
}
@@ -188,11 +198,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Get the profile path and define a profile path if it is null
// Check that it is an absolute path or otherwise create an absolute path using appRoot
$modulePath = $input->getOption('module-path');
$modulePath = $modulePath == null ? 'modules/custom' : $modulePath;
if(is_null($modulePath)) {
$uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST);
$defaultModulePath = 'modules/custom';
$modulePath = $this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultModulePath : $defaultModulePath;
}
$modulePath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
$modulePath = $this->validator->validateModulePath($modulePath, true);
$machineName = $this->validator->validateMachineName($input->getOption('machine-name'));
$machineName = $input->getOption('machine-name') ?
$this->validator->validateMachineName($input->getOption('machine-name'))
:$this->stringConverter->createMachineName($module);
$description = $input->getOption('description');
$core = $input->getOption('core');
$package = $input->getOption('package');
@@ -298,9 +315,11 @@ function ($machine_name) use ($validator) {
$modulePath = $input->getOption('module-path');
if (!$modulePath) {
$uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST);
$defaultModulePath = 'modules/custom';
$modulePath = $this->getIo()->ask(
$this->trans('commands.generate.module.questions.module-path'),
'modules/custom',
$this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultModulePath : $defaultModulePath,
function ($modulePath) use ($machineName) {
$fullPath = Path::isAbsolute($modulePath) ? $modulePath : Path::makeAbsolute($modulePath, $this->appRoot);
$fullPath = $fullPath.'/'.$machineName;
@@ -188,11 +188,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
// Get the profile path and define a profile path if it is null
// Check that it is an absolute path or otherwise create an absolute path using appRoot
$theme_path = $input->getOption('theme-path');
$theme_path = $theme_path == null ? 'themes/custom' : $theme_path;
if(is_null($theme_path)) {
$uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST);
$defaultThemePath = 'themes/custom';
$theme_path = $this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultThemePath : $defaultThemePath;
}
$theme_path = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
$theme_path = $this->validator->validateModulePath($theme_path, true);
$machine_name = $this->validator->validateMachineName($input->getOption('machine-name'));
$machine_name = $input->getOption('machine-name') ?
$this->validator->validateMachineName($input->getOption('machine-name'))
:$this->stringConverter->createMachineName($theme);
$description = $input->getOption('description');
$core = $input->getOption('core');
$package = $input->getOption('package');
@@ -274,10 +281,12 @@ function ($machine_name) use ($validators) {
$theme_path = $input->getOption('theme-path');
if (!$theme_path) {
$uri = parse_url($input->getParameterOption(['--uri', '-l'], 'default'), PHP_URL_HOST);
$defaultThemePath = 'themes/custom';
$theme_path = $this->getIo()->ask(
$this->trans('commands.generate.theme.questions.theme-path'),
'themes/custom',
function ($theme_path) use ($machine_name) {
$this->site->multisiteMode($uri)? 'sites/'.$this->site->getMultisiteDir($uri).'/'.$defaultThemePath : $defaultThemePath,
function ($theme_path) use ($machine_name) {
$fullPath = Path::isAbsolute($theme_path) ? $theme_path : Path::makeAbsolute($theme_path, $this->appRoot);
$fullPath = $fullPath.'/'.$machine_name;
if (file_exists($fullPath)) {
@@ -2,6 +2,9 @@
namespace Drupal\Console\Utils;
use Drupal\Console\Core\Style\DrupalStyle;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Finder\Finder;
use Drupal\Core\DependencyInjection\ContainerBuilder;
@@ -28,6 +31,11 @@ class Site
*/
protected $cacheServicesFile;
/**
* @var DrupalStyle
*/
protected $io;
/**
* Site constructor.
*
@@ -40,6 +48,10 @@ public function __construct(
) {
$this->appRoot = $appRoot;
$this->configurationManager = $configurationManager;
$output = new ConsoleOutput();
$input = new ArrayInput([]);
$this->io = new DrupalStyle($input, $output);
}
public function loadLegacyFile($legacyFile, $relative = true)
@@ -180,9 +192,41 @@ public function multisiteMode($uri)
}
/**
* @param string $uri
*
* @return boolean
*/
public function validMultisite($uri)
{
$sites = $this->getAllMultisites();
if (isset($sites[$uri]) && is_dir($this->appRoot . "/sites/" . $sites[$uri])) {
return true;
}
return false;
}
/**
* @param string $uri
*
* @return string
*/
public function getMultisiteDir($uri)
{
if(!$this->validMultisite($uri)) {
$this->io->error('Invalid multisite, please debug multisite using command drupal debug:mulltisite and choose one');
exit();
}
return $this->getAllMultisites()[$uri];
}
/**
* @return mixed
*/
private function getAllMultisites()
{
$multiSiteFile = sprintf(
'%s/sites/sites.php',
@@ -191,15 +235,11 @@ public function validMultisite($uri)
if (file_exists($multiSiteFile)) {
include $multiSiteFile;
} else {
return false;
}
if (isset($sites[$uri]) && is_dir($this->appRoot . "/sites/" . $sites[$uri])) {
return true;
return $sites;
} else {
return null;
}
return false;
}
public function getCachedServicesFile()

0 comments on commit 1141d03

Please sign in to comment.
You can’t perform that action at this time.