Skip to content

Commit 36d7664

Browse files
PHP: Allow 'version:none' (devcontainers#439)
* PHP: Allow 'version:none' * fix tests
1 parent c997f9a commit 36d7664

8 files changed

Lines changed: 87 additions & 53 deletions

File tree

src/php/devcontainer-feature.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "php",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"name": "PHP",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/php",
66
"options": {
@@ -10,7 +10,8 @@
1010
"latest",
1111
"8",
1212
"8.2",
13-
"8.2.0"
13+
"8.2.0",
14+
"none"
1415
],
1516
"default": "latest",
1617
"description": "Select or enter a PHP version"

src/php/install.sh

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -123,38 +123,13 @@ find_version_from_git_tags() {
123123

124124
# Install PHP Composer
125125
addcomposer() {
126-
"${PHP_INSTALL_DIR}/bin/php" -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
126+
"${PHP_SRC}" -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
127127
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"
128-
"${PHP_INSTALL_DIR}/bin/php" -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
129-
"${PHP_INSTALL_DIR}/bin/php" composer-setup.php
130-
"${PHP_INSTALL_DIR}/bin/php" -r "unlink('composer-setup.php');"
131-
132-
mv composer.phar "${PHP_INSTALL_DIR}/bin/composer"
128+
"${PHP_SRC}" -r "if (hash_file('sha384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
129+
"${PHP_SRC}" composer-setup.php --install-dir="/usr/local/bin" --filename=composer
130+
"${PHP_SRC}" -r "unlink('composer-setup.php');"
133131
}
134132

135-
# Install PHP if it's missing
136-
137-
138-
# Persistent / runtime dependencies
139-
RUNTIME_DEPS="wget ca-certificates git build-essential xz-utils"
140-
141-
# PHP dependencies
142-
PHP_DEPS="libssl-dev libcurl4-openssl-dev libedit-dev libsqlite3-dev libxml2-dev zlib1g-dev libsodium-dev libonig-dev"
143-
144-
. /etc/os-release
145-
146-
if [ "${VERSION_CODENAME}" = "bionic" ]; then
147-
PHP_DEPS="${PHP_DEPS} libargon2-0-dev"
148-
else
149-
PHP_DEPS="${PHP_DEPS} libargon2-dev"
150-
fi
151-
152-
# Dependencies required for running "phpize"
153-
PHPIZE_DEPS="autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c"
154-
155-
# Install dependencies
156-
check_packages $RUNTIME_DEPS $PHP_DEPS $PHPIZE_DEPS
157-
158133
install_php() {
159134
PHP_VERSION="$1"
160135
PHP_INSTALL_DIR="${PHP_DIR}/${PHP_VERSION}"
@@ -216,30 +191,48 @@ install_php() {
216191
echo "xdebug.mode = debug" >> "${XDEBUG_INI}"
217192
echo "xdebug.start_with_request = yes" >> "${XDEBUG_INI}"
218193
echo "xdebug.client_port = 9003" >> "${XDEBUG_INI}"
194+
}
219195

220-
# Install PHP Composer if needed
221-
if [[ "${INSTALL_COMPOSER}" = "true" ]] || [[ $(composer --version) = "" ]]; then
222-
addcomposer
223-
fi
196+
if [ "${PHP_VERSION}" != "none" ]; then
197+
# Persistent / runtime dependencies
198+
RUNTIME_DEPS="wget ca-certificates git build-essential xz-utils"
224199

225-
CURRENT_DIR="${PHP_DIR}/current"
226-
if [[ ! -d "${CURRENT_DIR}" ]]; then
227-
ln -s -r "${PHP_INSTALL_DIR}" ${CURRENT_DIR}
228-
fi
200+
# PHP dependencies
201+
PHP_DEPS="libssl-dev libcurl4-openssl-dev libedit-dev libsqlite3-dev libxml2-dev zlib1g-dev libsodium-dev libonig-dev"
229202

230-
if [ "${OVERRIDE_DEFAULT_VERSION}" = "true" ]; then
231-
if [[ $(ls -l ${CURRENT_DIR}) != *"-> ${PHP_INSTALL_DIR}"* ]] ; then
232-
rm "${CURRENT_DIR}"
233-
ln -s -r "${PHP_INSTALL_DIR}" "${CURRENT_DIR}"
234-
fi
203+
. /etc/os-release
204+
205+
if [ "${VERSION_CODENAME}" = "bionic" ]; then
206+
PHP_DEPS="${PHP_DEPS} libargon2-0-dev"
207+
else
208+
PHP_DEPS="${PHP_DEPS} libargon2-dev"
235209
fi
236210

237-
rm -rf "${PHP_SRC_DIR}"
238-
updaterc "if [[ \"\${PATH}\" != *\"${CURRENT_DIR}\"* ]]; then export PATH=\"${CURRENT_DIR}/bin:\${PATH}\"; fi"
239-
}
211+
# Dependencies required for running "phpize"
212+
PHPIZE_DEPS="autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c"
240213

241-
find_version_from_git_tags PHP_VERSION https://github.com/php/php-src "tags/php-"
242-
install_php "${PHP_VERSION}"
214+
# Install dependencies
215+
check_packages $RUNTIME_DEPS $PHP_DEPS $PHPIZE_DEPS
216+
217+
find_version_from_git_tags PHP_VERSION https://github.com/php/php-src "tags/php-"
218+
install_php "${PHP_VERSION}"
219+
220+
PHP_SRC="${PHP_INSTALL_DIR}/bin/php"
221+
else
222+
set +e
223+
PHP_SRC=$(which php)
224+
set -e
225+
fi
226+
227+
# Install PHP Composer if needed
228+
if [[ "${INSTALL_COMPOSER}" = "true" ]]; then
229+
if [ -z "${PHP_SRC}" ]; then
230+
echo "(!) Could not install Composer. PHP not found."
231+
exit 1
232+
fi
233+
234+
addcomposer
235+
fi
243236

244237
# Additional php versions to be installed but not be set as default.
245238
if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
@@ -253,9 +246,26 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then
253246
IFS=$OLDIFS
254247
fi
255248

256-
chown -R "${USERNAME}:php" "${PHP_DIR}"
257-
chmod -R g+r+w "${PHP_DIR}"
258-
find "${PHP_DIR}" -type d -print0 | xargs -n 1 -0 chmod g+s
249+
if [ "${PHP_VERSION}" != "none" ]; then
250+
CURRENT_DIR="${PHP_DIR}/current"
251+
if [[ ! -d "${CURRENT_DIR}" ]]; then
252+
ln -s -r "${PHP_INSTALL_DIR}" ${CURRENT_DIR}
253+
fi
254+
255+
if [ "${OVERRIDE_DEFAULT_VERSION}" = "true" ]; then
256+
if [[ $(ls -l ${CURRENT_DIR}) != *"-> ${PHP_INSTALL_DIR}"* ]] ; then
257+
rm "${CURRENT_DIR}"
258+
ln -s -r "${PHP_INSTALL_DIR}" "${CURRENT_DIR}"
259+
fi
260+
fi
261+
262+
rm -rf "${PHP_SRC_DIR}"
263+
updaterc "if [[ \"\${PATH}\" != *\"${CURRENT_DIR}\"* ]]; then export PATH=\"${CURRENT_DIR}/bin:\${PATH}\"; fi"
264+
265+
chown -R "${USERNAME}:php" "${PHP_DIR}"
266+
chmod -R g+r+w "${PHP_DIR}"
267+
find "${PHP_DIR}" -type d -print0 | xargs -n 1 -0 chmod g+s
268+
fi
259269

260270
# Clean up
261271
rm -rf /var/lib/apt/lists/*

test/php/install_additional_php.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ check "php version 8.1.4 installed as default" php --version | grep 8.1.4
99
check "php version 8.0.17 installed" ls -l /usr/local/php | grep 8.0.17
1010
check "php version 8.0.3 installed" ls -l /usr/local/php | grep 8.0.3
1111

12+
check "composer-version" composer --version
13+
1214
# Report result
1315
reportResults

test/php/install_only_composer.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Optional: Import test library
5+
source dev-container-features-test-lib
6+
7+
check "composer-version" composer --version
8+
9+
# Report result
10+
reportResults

test/php/install_php_8.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set -e
66
source dev-container-features-test-lib
77

88
check "php-version-8-is-installed" bash -c "php --version | grep '8.'"
9+
check "composer-version" composer --version
910

1011
# Report result
1112
reportResults

test/php/install_php_8_2.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set -e
66
source dev-container-features-test-lib
77

88
check "php-version-8.2-is-installed" bash -c "php --version | grep '8.2'"
9+
check "composer-version" composer --version
910

1011
# Report result
1112
reportResults

test/php/scenarios.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@
2323
"version": "8.2"
2424
}
2525
}
26+
},
27+
"install_only_composer": {
28+
"image": "mcr.microsoft.com/devcontainers/php:latest",
29+
"features": {
30+
"php": {
31+
"version": "none",
32+
"installComposer": true
33+
}
34+
}
2635
}
2736
}

test/php/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ source dev-container-features-test-lib
77

88
check "PHP version" php --version
99
check "Mbstring loaded" php -r "extension_loaded('mbstring') || throw new Error('Extension Mbstring is not loaded');"
10-
check "Composer version" composer --version
10+
check "composer-version" composer --version
1111

1212
# Report result
1313
reportResults

0 commit comments

Comments
 (0)