You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.6 KiB

2 years ago
  1. #!/bin/sh
  2. set -e
  3. extDir="$(php -d 'display_errors=stderr' -r 'echo ini_get("extension_dir");')"
  4. cd "$extDir"
  5. usage() {
  6. echo "usage: $0 [options] module-name [module-name ...]"
  7. echo " ie: $0 gd mysqli"
  8. echo " $0 pdo pdo_mysql"
  9. echo " $0 --ini-name 0-apc.ini apcu apc"
  10. echo
  11. echo 'Possible values for module-name:'
  12. find -maxdepth 1 \
  13. -type f \
  14. -name '*.so' \
  15. -exec basename '{}' ';' \
  16. | sort \
  17. | xargs
  18. echo
  19. echo 'Some of the above modules are already compiled into PHP; please check'
  20. echo 'the output of "php -i" to see which modules are already loaded.'
  21. }
  22. opts="$(getopt -o 'h?' --long 'help,ini-name:' -- "$@" || { usage >&2 && false; })"
  23. eval set -- "$opts"
  24. iniName=
  25. while true; do
  26. flag="$1"
  27. shift
  28. case "$flag" in
  29. --help|-h|'-?') usage && exit 0 ;;
  30. --ini-name) iniName="$1" && shift ;;
  31. --) break ;;
  32. *)
  33. {
  34. echo "error: unknown flag: $flag"
  35. usage
  36. } >&2
  37. exit 1
  38. ;;
  39. esac
  40. done
  41. modules=
  42. for module; do
  43. if [ -z "$module" ]; then
  44. continue
  45. fi
  46. if ! [ -f "$module" ] && ! [ -f "$module.so" ]; then
  47. echo >&2 "error: '$module' does not exist"
  48. echo >&2
  49. usage >&2
  50. exit 1
  51. fi
  52. modules="$modules $module"
  53. done
  54. if [ -z "$modules" ]; then
  55. usage >&2
  56. exit 1
  57. fi
  58. pm='unknown'
  59. if [ -e /lib/apk/db/installed ]; then
  60. pm='apk'
  61. fi
  62. apkDel=
  63. if [ "$pm" = 'apk' ]; then
  64. if \
  65. [ -n "$PHPIZE_DEPS" ] \
  66. && ! apk info --installed .phpize-deps > /dev/null \
  67. && ! apk info --installed .phpize-deps-configure > /dev/null \
  68. ; then
  69. apk add --no-cache --virtual '.docker-php-ext-enable-deps' binutils
  70. apkDel='.docker-php-ext-enable-deps'
  71. fi
  72. fi
  73. for module in $modules; do
  74. moduleFile="$module"
  75. if [ -f "$module.so" ] && ! [ -f "$module" ]; then
  76. moduleFile="$module.so"
  77. fi
  78. if readelf --wide --syms "$moduleFile" | grep -q ' zend_extension_entry$'; then
  79. # https://wiki.php.net/internals/extensions#loading_zend_extensions
  80. line="zend_extension=$module"
  81. else
  82. line="extension=$module"
  83. fi
  84. ext="$(basename "$module")"
  85. ext="${ext%.*}"
  86. if php -d 'display_errors=stderr' -r 'exit(extension_loaded("'"$ext"'") ? 0 : 1);'; then
  87. # this isn't perfect, but it's better than nothing
  88. # (for example, 'opcache.so' presents inside PHP as 'Zend OPcache', not 'opcache')
  89. echo >&2
  90. echo >&2 "warning: $ext ($module) is already loaded!"
  91. echo >&2
  92. continue
  93. fi
  94. case "$iniName" in
  95. /*)
  96. # allow an absolute path
  97. ini="$iniName"
  98. ;;
  99. *)
  100. ini="$PHP_INI_DIR/conf.d/${iniName:-"docker-php-ext-$ext.ini"}"
  101. ;;
  102. esac
  103. if ! grep -qFx -e "$line" -e "$line.so" "$ini" 2>/dev/null; then
  104. echo "$line" >> "$ini"
  105. fi
  106. done
  107. if [ "$pm" = 'apk' ] && [ -n "$apkDel" ]; then
  108. apk del --no-network $apkDel
  109. fi