Skip to content

Commit

Permalink
[generate:module] Show multisite dir to generate (#4053)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and enzolutions committed May 21, 2019
1 parent 1247f20 commit 1141d03
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/services/generate.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
25 changes: 22 additions & 3 deletions src/Command/Generate/ModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,12 @@ class ModuleCommand extends Command
*/
protected $chainQueue;

/**
* @var Site
*/
protected $site;


/**
* ModuleCommand constructor.
*
Expand All @@ -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(
Expand All @@ -76,6 +84,7 @@ public function __construct(
StringConverter $stringConverter,
DrupalApi $drupalApi,
ChainQueue $chainQueue,
Site $site,
$twigtemplate = null
) {
$this->generator = $generator;
Expand All @@ -84,6 +93,7 @@ public function __construct(
$this->stringConverter = $stringConverter;
$this->drupalApi = $drupalApi;
$this->chainQueue = $chainQueue;
$this->site = $site;
$this->twigtemplate = $twigtemplate;
parent::__construct();
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions src/Command/Generate/ThemeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)) {
Expand Down
54 changes: 47 additions & 7 deletions src/Utils/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +31,11 @@ class Site
*/
protected $cacheServicesFile;

/**
* @var DrupalStyle
*/
protected $io;

/**
* Site constructor.
*
Expand All @@ -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)
Expand Down Expand Up @@ -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',
Expand All @@ -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()
Expand Down

0 comments on commit 1141d03

Please sign in to comment.