From 9266002b7227fd30ec583b74ffa6469a8f69d7b9 Mon Sep 17 00:00:00 2001
From: Andrew Nicols <andrew@nicols.co.uk>
Date: Wed, 15 Mar 2023 21:55:40 +0800
Subject: [PATCH] Add simple environment-based PHP configuration

This adds a layer of common sense on top of the other changes I've
worked on to allow easy configuration variable setting.

Fixes #166
---
 README.md                                     | 16 ++++++++++
 .../local/bin/moodle-docker-php-entrypoint    |  5 ++++
 root/usr/local/bin/moodle-docker-php-ini      | 29 +++++++++++++++++++
 3 files changed, 50 insertions(+)
 create mode 100755 root/usr/local/bin/moodle-docker-php-ini

diff --git a/README.md b/README.md
index 4a6231e..3f2c26a 100644
--- a/README.md
+++ b/README.md
@@ -74,6 +74,22 @@ docker run \
 
 These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8.
 
+## PHP Configuration
+
+As a lightweight alternative to a full PHP configuration file, you can specify a set of prefixed environment variables when starting your container with these variables turned into ini-format configuration.
+
+Any environment variable whose name is prefixed with `INI-` will have the prefix removed, and will be added to a new ini file before the main command starts.
+
+```
+docker run \
+    --name web0 \
+    -p 8080:80 \
+    -v $PWD/moodle:/var/www/html
+    -e INI-upload_max_filesize=200M \
+    -e INI-post_max_size=210M \
+    moodle-php-apache:latest
+```
+
 ## See also
 This container is part of a set of containers for Moodle development, see also:
 
diff --git a/root/usr/local/bin/moodle-docker-php-entrypoint b/root/usr/local/bin/moodle-docker-php-entrypoint
index 50d5041..17184ab 100755
--- a/root/usr/local/bin/moodle-docker-php-entrypoint
+++ b/root/usr/local/bin/moodle-docker-php-entrypoint
@@ -25,8 +25,13 @@ docker_process_init_files() {
   done
 }
 
+echo "Running PHP Configuration fetcher"
+/usr/local/bin/moodle-docker-php-ini
+echo ""
+
 echo "Running entrypoint files from /docker-entrypoint-initdb.d/*"
 docker_process_init_files /docker-entrypoint-initdb.d/*
+echo ""
 
 echo "Starting docker-php-entrypoint with $@"
 source /usr/local/bin/docker-php-entrypoint
diff --git a/root/usr/local/bin/moodle-docker-php-ini b/root/usr/local/bin/moodle-docker-php-ini
new file mode 100755
index 0000000..deb74a4
--- /dev/null
+++ b/root/usr/local/bin/moodle-docker-php-ini
@@ -0,0 +1,29 @@
+#!/usr/bin/env bash
+
+set -e
+
+echo "Checking for php configuration in environment"
+
+localinifile="/usr/local/etc/php/conf.d/10-local.ini"
+
+cat <<'EOF' > $localinifile
+; --
+; Automatically generated php ini configuration for Moodle
+; --
+
+EOF
+
+env | while IFS= read -r line; do
+  value=${line#*=}
+  fullname=${line%%=*}
+  if [[ $fullname = INI-* ]]; then
+    name=`echo $fullname | sed 's/^INI-//'`
+    echo "=> Found '${name}' with value '${value}'"
+
+    cat << EOF >> $localinifile
+; $fullname=$value
+$name = $value
+
+EOF
+  fi
+done