Created
March 29, 2018 19:02
-
-
Save swichers/7c444b912ae12623840760edf97ec9bf to your computer and use it in GitHub Desktop.
Bootstrap-less ACSF utility for getting site names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acquia\Blt\Custom\Acsf; | |
use Symfony\Component\Yaml\Yaml; | |
use Consolidation\Config\Loader\YamlConfigLoader; | |
use Acquia\Blt\Robo\Config\YamlConfigProcessor; | |
/** | |
* ACSF utility functions for determining which ACSF site is currently running | |
* either remotely or locally. This is necessary because we need this pre | |
* Drupal bootstrap so the ACSF utilities are unavailable to us. | |
*/ | |
class AcsfUtility { | |
/** | |
* Load the BLT project config. | |
* | |
* @return array|FALSE | |
* The BLT project config. | |
*/ | |
protected function getBLTConfig() { | |
static $blt_config = NULL; | |
// Try and match the site based on the project config. | |
if (is_null($blt_config)) { | |
// Config files to merge together into a single file. | |
$config_files = [ | |
__DIR__ . '/../../project.yml', | |
__DIR__ . '/../../project.local.yml', | |
]; | |
// These are BLT provided classes that help to manage configuration. | |
$processor = new YamlConfigProcessor(); | |
$loader = new YamlConfigLoader(); | |
foreach ($config_files as $file) { | |
if (is_readable($file)) { | |
$processor->extend($loader->load($file)); | |
} | |
} | |
$blt_config = $processor->export(); | |
} | |
return $blt_config; | |
} | |
/** | |
* Gets the local to ACSF name associations. | |
* | |
* @return array | |
* An array of ACSF site names keyed by their local equivalents. | |
*/ | |
public function getNameMapping() { | |
$blt_config = $this->getBLTConfig(); | |
if (!empty($blt_config['acsf']['mappings'])) { | |
return $blt_config['acsf']['mappings']; | |
} | |
return []; | |
} | |
/** | |
* Gets the local name from the given ACSF name. | |
* | |
* @param string $name | |
* The local name to get the ACSF name for. | |
* | |
* @return string|false | |
* The local name or FALSE if not found. | |
*/ | |
public function getLocalNameFromAcsf($name) { | |
return array_search($name, $this->getNameMapping()); | |
} | |
/** | |
* Gets the ACSF name from the given local name. | |
* | |
* @param string $name | |
* The ACSF name to get the local name for. | |
* | |
* @return string|false | |
* The ACSF name or FALSE if not found. | |
*/ | |
public function getAcsfNameFromLocal($name) { | |
$names = $this->getNameMapping(); | |
return !empty($names[$name]) ? $names[$name] : FALSE; | |
} | |
/** | |
* Determines if this is a production environment. | |
* | |
* @return bool | |
* TRUE if production, FALSE otherwise. | |
*/ | |
public function isAcquiaProd() { | |
return getenv('AH_PRODUCTION') == TRUE; | |
} | |
/** | |
* Check if the site is running on an Acquia environment. | |
* | |
* @return string|false | |
* Returns the current Acquia environment or FALSE if not an Acquia | |
* environment. | |
*/ | |
public function isAcquiaEnv() { | |
$env = getenv('AH_SITE_ENVIRONMENT'); | |
return !empty($env) ? $env : FALSE; | |
} | |
/** | |
* Check if the site is running on the CI environment. | |
* | |
* @return boolean | |
* TRUE if running on CI, FALSE otherwise. | |
*/ | |
public function isCiEnv() { | |
$env = getenv('CI'); | |
return !empty($env) ? $env : FALSE; | |
} | |
/** | |
* Determine the ACSF site name for the current site. | |
* | |
* Note that this relies on an assumption that sites will always be keyed by | |
* ACSF in this format: | |
* | |
* <site key>.env-AH_SITE_GROUP.acsitefactory.com | |
* | |
* @return bool|false | |
* Returns the current ACSF site name or FALSE on failure. | |
*/ | |
public function getCurrentAcsfSiteName() { | |
$ah_env = getenv('AH_SITE_ENVIRONMENT'); | |
$ah_group = getenv('AH_SITE_GROUP'); | |
$acsf_sites_path = sprintf('/mnt/files/%s.%s/files-private/sites.json', $ah_group, $ah_env); | |
if (empty($ah_group) || !file_exists($acsf_sites_path)) { | |
return FALSE; | |
} | |
$acsf_sites = file_get_contents($acsf_sites_path); | |
if (empty($acsf_sites)) { | |
return FALSE; | |
} | |
$acsf_sites = json_decode($acsf_sites, TRUE); | |
$acsf_db_name = !empty($GLOBALS['gardens_site_settings']['conf']['acsf_db_name']) ? | |
$GLOBALS['gardens_site_settings']['conf']['acsf_db_name'] : | |
NULL; | |
// Find the configured site that has the same database as the currently | |
// active database, and then get the site key based on its name. This | |
// assumes the site key is always at the start of a domain that is in this | |
// pattern: <site key>.example.com | |
if (!empty($acsf_sites['sites']) && !empty($acsf_db_name)) { | |
foreach ($acsf_sites['sites'] as $domain => $config) { | |
if ($config['conf']['acsf_db_name'] === $acsf_db_name) { | |
$acsf_name = explode('.', $domain, 2); | |
return !empty($acsf_name) ? reset($acsf_name) : FALSE; | |
} | |
} | |
} | |
return FALSE; | |
} | |
/** | |
* Get the current site name. | |
* | |
* @return string | |
* The current local or ACSF name of the requested site. | |
*/ | |
public function getCurrentSiteName() { | |
if ($this->isAcquiaEnv()) { | |
return $this->getCurrentAcsfSiteName(); | |
} | |
$sites = $this->getNameMapping(); | |
// Try and match the site based on the current host. | |
$parts = explode('.', getenv('HTTP_HOST')); | |
foreach ($parts as $part) { | |
if (!empty($sites[$part])) { | |
return $part; | |
} | |
} | |
// Default to the project.yml setting when possible. | |
$blt_config = $this->getBLTConfig(); | |
if (!empty($blt_config['acsf']['local'])) { | |
return $blt_config['acsf']['local']; | |
} | |
return FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment