Skip to content

Commit e91fe70

Browse files
committed
Fix printing site variables for CLI invocations.
1 parent 2225530 commit e91fe70

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

bin/request-matcher-site-variables

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ elseif (is_file($autoload = __DIR__ . '/../../../../../vendor/autoload.php')) {
2020
require($autoload);
2121
}
2222

23-
echo \drunomics\MultisiteRequestMatcher\RequestMatcher::getSiteVariables();
23+
echo \drunomics\MultisiteRequestMatcher\RequestMatcher::printSiteVariables();

src/RequestMatcher.php

+45-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace drunomics\MultisiteRequestMatcher;
44

55

6+
use Drupal\Core\Render\Markup;
67
use Symfony\Component\HttpFoundation\Request;
78

89
/**
@@ -123,27 +124,58 @@ private function getRequestFromGlobals() {
123124
}
124125

125126
/**
126-
* Gets the same site variables as set during request matching.
127+
* Determines the currently active site.
127128
*
128-
* Useful for setting the same environment variables during CLI invocations as
129-
* during regular request.
129+
* @return string
130+
* The active site's name.
130131
*/
131-
public static function getSiteVariables() {
132+
public static function determineActiveSite() {
132133
$site = getenv('SITE') ?: getenv('APP_DEFAULT_SITE');
133134
if (!$site) {
134135
$sites = explode(' ', getenv('APP_SITES'));
135136
$site = reset($sites);
136137
}
137-
$vars = 'SITE=' . $site . "\n";
138-
$vars .= 'SITE_VARIANT=' . "\n";
138+
return $site;
139+
}
140+
141+
/**
142+
* Gets the same site variables as set during request matching.
143+
*
144+
* Useful for setting the same environment variables during CLI invocations as
145+
* during regular request.
146+
*
147+
* @param string $site
148+
* (optional) The site to use.
149+
*
150+
* @return array
151+
* The array of site variables.
152+
*/
153+
public static function getSiteVariables($site = NULL) {
154+
$site = $site ?: static::determineActiveSite();
155+
$vars = [];
156+
$vars['SITE'] = $site;
157+
$vars['SITE_VARIANT'] = '';
139158
if ($domain = getenv('APP_MULTISITE_DOMAIN')) {
140159
$host = $site . getenv('APP_MULTISITE_DOMAIN_PREFIX_SEPARATOR') . $domain;
141160
}
142161
else {
143162
$host = getenv('APP_SITE_DOMAIN--' . $site);
144163
}
145-
$vars .= 'SITE_HOST=' . $host . "\n";
146-
$vars .= 'SITE_MAIN_HOST=' . $host . "\n";
164+
$vars['SITE_HOST'] = $host;
165+
$vars['SITE_MAIN_HOST'] = $host;
166+
return $vars;
167+
}
168+
169+
/**
170+
* Gets the site variables as string.
171+
*
172+
* @return string
173+
*/
174+
public static function printSiteVariables() {
175+
$vars = '';
176+
foreach (static::getSiteVariables() as $variable => $value) {
177+
$vars .= "$variable=$value\n";
178+
}
147179
return $vars;
148180
}
149181

@@ -168,8 +200,11 @@ public static function getSiteVariables() {
168200
public function match(Request $request = NULL) {
169201
// Do not attempt to match on CLI but apply the default site.
170202
if (!$request && php_sapi_name() == "cli") {
171-
putenv(static::getSiteVariables());
172-
return getenv('SITE');
203+
$site = static::determineActiveSite();
204+
foreach (static::getSiteVariables($site) as $variable => $value) {
205+
putenv("$variable=$value");
206+
}
207+
return $site;
173208
}
174209

175210
if (!$request) {

0 commit comments

Comments
 (0)