Skip to content

Instantly share code, notes, and snippets.

@Tjitse-E
Last active June 15, 2021 12:51
Show Gist options
  • Save Tjitse-E/b45f288ff96ec54aa622a5850ae0d6a7 to your computer and use it in GitHub Desktop.
Save Tjitse-E/b45f288ff96ec54aa622a5850ae0d6a7 to your computer and use it in GitHub Desktop.
Generate a Magento 2 store mapping
#!/usr/bin/env bash
# Generates the Magento multistore config based on the base url list that's provided by n98-magerun2
# You can place the output in pub/index.php
IFS='
'
magerun2=$(which n98-magerun2)
[ -z "$magerun2" ] && echo "Cannot find magerun2, exiting." && exit 1
echo "switch (\$_SERVER['HTTP_HOST']) {"
# Get all domains via magerun, clean the header and footer.
for line in $($magerun2 sys:store:config:base-url:list | tail -n +9 | sed '$ d')
do
# Variables
domain=$(echo $line | perl -ne '/(https:\/\/[a-z]{0,}\.[a-z\-]+\.[a-z\.]{2,5}\/)/ && print "$1\n"')
domain_without_subdomain=$(echo $line | perl -ne '/https:\/\/[a-z]{0,}\.([a-z\-]+\.[a-z\.]{2,5}\/)/ && print "$1\n"')
domain_with_no_tld=$(echo $line | perl -ne '/https:\/\/[a-z]{0,}\.([a-z\-]+)\.[a-z\.]{2,5}\// && print "$1\n"')
staging_domain="https://staging.$domain_without_subdomain"
test_domain="https://$domain_with_no_tld.test/"
domain_no_www="https://$domain_without_subdomain"
store_code=$(echo $line | perl -ne '/\|\s+\d+\s+\| (\w+)/ && print "$1\n"')
# Guards and debug
[ -z "$domain" ] && echo "Cannot extract domain, exiting." && echo $line && exit 1
[ -z "$domain_without_subdomain" ] && echo "Cannot extract subdomain, exiting." && echo $line && exit 1
[ -z "$domain_with_no_tld" ] && echo "Cannot extract test domain, exiting." && echo $line && exit 1
[ -z "$store_code" ] && echo "Cannot extract store_code, exiting." && echo $line && exit 1
# Output
echo " case \"$domain\":"
echo " case \"$domain_no_www\":"
echo " case \"$staging_domain\":"
echo " case \"$test_domain\":"
echo " \$_SERVER['MAGE_RUN_CODE'] = \"$store_code\";"
echo " \$_SERVER['MAGE_RUN_TYPE'] = \"store\";"
echo " break;"
done
echo "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment