Skip to content

Commit

Permalink
Fix printing site variables for CLI invocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
fago committed Mar 20, 2019
1 parent 2225530 commit e91fe70
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bin/request-matcher-site-variables
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ elseif (is_file($autoload = __DIR__ . '/../../../../../vendor/autoload.php')) {
require($autoload);
}

echo \drunomics\MultisiteRequestMatcher\RequestMatcher::getSiteVariables();
echo \drunomics\MultisiteRequestMatcher\RequestMatcher::printSiteVariables();
55 changes: 45 additions & 10 deletions src/RequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace drunomics\MultisiteRequestMatcher;


use Drupal\Core\Render\Markup;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -123,27 +124,58 @@ private function getRequestFromGlobals() {
}

/**
* Gets the same site variables as set during request matching.
* Determines the currently active site.
*
* Useful for setting the same environment variables during CLI invocations as
* during regular request.
* @return string
* The active site's name.
*/
public static function getSiteVariables() {
public static function determineActiveSite() {
$site = getenv('SITE') ?: getenv('APP_DEFAULT_SITE');
if (!$site) {
$sites = explode(' ', getenv('APP_SITES'));
$site = reset($sites);
}
$vars = 'SITE=' . $site . "\n";
$vars .= 'SITE_VARIANT=' . "\n";
return $site;
}

/**
* Gets the same site variables as set during request matching.
*
* Useful for setting the same environment variables during CLI invocations as
* during regular request.
*
* @param string $site
* (optional) The site to use.
*
* @return array
* The array of site variables.
*/
public static function getSiteVariables($site = NULL) {
$site = $site ?: static::determineActiveSite();
$vars = [];
$vars['SITE'] = $site;
$vars['SITE_VARIANT'] = '';
if ($domain = getenv('APP_MULTISITE_DOMAIN')) {
$host = $site . getenv('APP_MULTISITE_DOMAIN_PREFIX_SEPARATOR') . $domain;
}
else {
$host = getenv('APP_SITE_DOMAIN--' . $site);
}
$vars .= 'SITE_HOST=' . $host . "\n";
$vars .= 'SITE_MAIN_HOST=' . $host . "\n";
$vars['SITE_HOST'] = $host;
$vars['SITE_MAIN_HOST'] = $host;
return $vars;
}

/**
* Gets the site variables as string.
*
* @return string
*/
public static function printSiteVariables() {
$vars = '';
foreach (static::getSiteVariables() as $variable => $value) {
$vars .= "$variable=$value\n";
}
return $vars;
}

Expand All @@ -168,8 +200,11 @@ public static function getSiteVariables() {
public function match(Request $request = NULL) {
// Do not attempt to match on CLI but apply the default site.
if (!$request && php_sapi_name() == "cli") {
putenv(static::getSiteVariables());
return getenv('SITE');
$site = static::determineActiveSite();
foreach (static::getSiteVariables($site) as $variable => $value) {
putenv("$variable=$value");
}
return $site;
}

if (!$request) {
Expand Down

0 comments on commit e91fe70

Please sign in to comment.