Skip to content

Instantly share code, notes, and snippets.

@ianmjones
Last active March 26, 2024 08:34
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ianmjones/55eb1ace80517f951e392b95a65f277b to your computer and use it in GitHub Desktop.
Save ianmjones/55eb1ace80517f951e392b95a65f277b to your computer and use it in GitHub Desktop.
A script for downloading the AWS PHP SDK v3, stripping down to S3 functionality and then applying a custom namespace.
#!/usr/bin/env bash
set -e
if [ ! -d src/amazon-s3-and-cloudfront ]; then
echo 'This script must be run from the repository root.'
exit 1
fi
for PROG in composer find sed unzip
do
which ${PROG}
if [ 0 -ne $? ]
then
echo "${PROG} not found in path."
exit 1
fi
done
REPO_ROOT=${PWD}
TMP_ROOT="${REPO_ROOT}/aws3-build"
TARGET_DIR="${REPO_ROOT}/src/amazon-s3-and-cloudfront/vendor/Aws3"
SOURCE_ZIP="https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip"
if [ -d "${TMP_ROOT}" ]; then
rm -rf "${TMP_ROOT}"
fi;
mkdir "${TMP_ROOT}"
cd "${TMP_ROOT}"
function log_step() {
echo
echo
echo ${1}
echo
echo
}
log_step "Install the latest v3 of the AWS SDK"
mkdir sdk
(
cd sdk
curl -sL ${SOURCE_ZIP} -o aws.zip
unzip aws.zip
rm aws.zip
# Delete everything from the SDK except for S3, CloudFront and Common files.
find Aws/ -mindepth 1 -maxdepth 1 -type d \
! -name S3 \
! -name CloudFront \
! -name Api \
! -name Credentials \
! -name Crypto \
! -name data \
! -name Endpoint \
! -name EndpointV2 \
! -name EndpointDiscovery \
! -name Arn \
! -name Exception \
! -name Handler \
! -name Multipart \
! -name Signature \
! -name ClientSideMonitoring \
! -name Sts \
! -name Retry \
! -name DefaultsMode \
! -name Token \
-exec rm -rf {} +
# Delete SDK /data subfolders that are not known to be used by S3 or CloudFront.
find Aws/data/ -mindepth 1 -maxdepth 1 -type d \
! -name s3 \
! -name cloudfront \
! -name sts \
-exec rm -rf {} +
# Remove polyfill, tests & docs
find . -type d -iname tests -exec rm -rf {} +
find . -type d -iname docs -exec rm -rf {} +
# Remove unused classes from the autoloader's classmap.
cat aws-autoloader.php | grep '__DIR__' | sed "s/^.*__DIR__ . '\///" | cut -d"'" -f1 | while read PHP_FILE_PATH
do
if [ ! -f $PHP_FILE_PATH ]
then
echo $PHP_FILE_PATH >> remove.list
fi
done
if [ -f remove.list ]
then
cat aws-autoloader.php | grep -v -f remove.list > aws-autoloader.new && mv aws-autoloader.new aws-autoloader.php && rm remove.list
fi
)
log_step "Run the prefixer, adding our namespace prefix" # Prefixed files are written into the ./sdk_prefixed directory.
../vendor/bin/php-scoper add-prefix --config=../scoper.inc.php --prefix="DeliciousBrains\\WP_Offload_Media\\Aws3" --output-dir=sdk_prefixed sdk/
(
cd sdk_prefixed
rm -rf composer
# Set the locale to prevent sed errors from characters with different encoding.
export LC_ALL=C
# Perform regex search replace to clean up any missed replacements in string literals (1 literal backslash = 4 in the command)
OS_NAME=`uname -s`
if [ "Darwin" = "${OS_NAME}" ]
then
find . -type f -name "*.php" -print0 | xargs -0 sed -i '' -E "s:'(Aws|GuzzleHttp|Psr|JmesPath|Symfony)\\\\\\\\:'DeliciousBrains\\\\\\\\WP_Offload_Media\\\\\\\\Aws3\\\\\\\\\1\\\\\\\\:g"
find . -type f -name "*.php" -print0 | xargs -0 sed -i '' -E "s:'\\\\\\\\(Aws|GuzzleHttp|Psr|JmesPath|Symfony)\\\\\\\\:'DeliciousBrains\\\\\\\\WP_Offload_Media\\\\\\\\Aws3\\\\\\\\\1\\\\\\\\:g"
find . -type f -name "*.php" -print0 | xargs -0 sed -i '' -E "s:\"(Aws|GuzzleHttp|Psr|JmesPath|Symfony)\\\\\\\\:\"DeliciousBrains\\\\\\\\WP_Offload_Media\\\\\\\\Aws3\\\\\\\\\1\\\\\\\\:g"
else
find . -type f -name "*.php" -print0 | xargs -0 sed -i'' -E "s:'(Aws|GuzzleHttp|Psr|JmesPath|Symfony)\\\\\\\\:'DeliciousBrains\\\\\\\\WP_Offload_Media\\\\\\\\Aws3\\\\\\\\\1\\\\\\\\:g"
find . -type f -name "*.php" -print0 | xargs -0 sed -i'' -E "s:'\\\\\\\\(Aws|GuzzleHttp|Psr|JmesPath|Symfony)\\\\\\\\:'DeliciousBrains\\\\\\\\WP_Offload_Media\\\\\\\\Aws3\\\\\\\\\1\\\\\\\\:g"
find . -type f -name "*.php" -print0 | xargs -0 sed -i'' -E "s:\"(Aws|GuzzleHttp|Psr|JmesPath|Symfony)\\\\\\\\:\"DeliciousBrains\\\\\\\\WP_Offload_Media\\\\\\\\Aws3\\\\\\\\\1\\\\\\\\:g"
fi
)
# Delete the target directory if it exists.
if [ -d "${TARGET_DIR}" ]; then
rm -rf "${TARGET_DIR}"
fi
# Move the prefixed SDK files to the plugin's vendor directory where they are referenced.
mv sdk_prefixed "${TARGET_DIR}"
# Clean up the temporary working directory.
rm -rf "${TMP_ROOT}"
log_step "Done!"
<?php
declare( strict_types=1 );
return [
// Patch files that have been processed to fix any missed or incorrectly prefixed strings.
'patchers' => [
static function ( string $filePath, string $prefix, string $contents ): string {
if ( false !== strpos( $filePath, 'Aws/Signature/SignatureV4.php' ) ) {
return str_replace(
'DeliciousBrains\\\\WP_Offload_Media\\\\Aws3\\\\Ymd\\\\THis\\\\Z',
'Ymd\\\\THis\\\\Z',
$contents
);
}
if ( false !== strpos( $filePath, 'gcp-build/sdk/vendor/composer/autoload_real.php' ) ) {
return str_replace(
'spl_autoload_unregister(array(\'ComposerAutoloader',
'spl_autoload_unregister(array(\'DeliciousBrains\\\\WP_Offload_Media\\\\Gcp\\\\ComposerAutoloader',
$contents
);
}
return $contents;
},
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment