Forráskód Böngészése

auto-config of xdebug on ap2i

Olivier Massot 2 éve
szülő
commit
d80fa1c32b

+ 3 - 0
.env.skeleton

@@ -1,6 +1,9 @@
 #OS : MAC ou LINUX
 OS=LINUX
 
+# Hostname (as returned by the command hostname)
+HOST=host
+
 #Composer hash for instal (check validity here : https://getcomposer.org/download/)
 COMPOSER_HASH=756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3
 

+ 1 - 0
docker-compose.yml

@@ -106,6 +106,7 @@ services:
       args:
         - COMPOSER_HASH=${COMPOSER_HASH}
         - OS=${OS}
+        - HOST=${HOST}
     restart: always
     environment:
       - PHP_IDE_CONFIG=serverName=ap2i

+ 10 - 3
docker/ap2i/Dockerfile

@@ -2,7 +2,9 @@
 FROM php:8.2.1-fpm
 ARG COMPOSER_HASH
 ARG OS
+ARG HOST
 ARG DEBIAN_FRONTEND=noninteractive
+
 RUN apt-get update && apt-get install -y --fix-missing \
     apt-utils \
     gnupg
@@ -79,15 +81,20 @@ RUN version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") \
     && printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" > $PHP_INI_DIR/conf.d/blackfire.ini \
     && rm -rf /tmp/blackfire /tmp/blackfire-probe.tar.gz
 ########   BLACKFIRE #########
-######## XDebug ########
-RUN pecl install xdebug \
- && docker-php-ext-enable xdebug
+
 ######## Php conf ########
 COPY /.ssh /root/.ssh
 COPY /docker/ap2i/conf/memory.ini /usr/local/etc/php/conf.d
 COPY /docker/ap2i/conf/apc.ini /usr/local/etc/php/conf.d
 COPY /docker/ap2i/conf/execution_time.ini /usr/local/etc/php/conf.d
 COPY /docker/ap2i/conf/xdebug.ini /usr/local/etc/php/conf.d
+
+######## XDebug ########
+COPY /docker/ap2i/conf/update_xdebug_ini.php /usr/local/etc/php/conf.d
+RUN pecl install xdebug \
+ && docker-php-ext-enable xdebug \
+ && php /usr/local/etc/php/conf.d/update_xdebug_ini.php $OS $HOST
+
 ######## FACL and Start ########
 ## Create .env.local file
 RUN echo "APP_ENV=docker" > .env.local

+ 32 - 0
docker/ap2i/conf/update_xdebug_ini.php

@@ -0,0 +1,32 @@
+<?php
+/**
+ * Mert à jour la valeur de xdebug.client_host en fonction de l'os et du nom de la machine qui héberge le container
+ */
+$host_os = $argv[1];
+$host_name = $argv[2];
+
+$client_host = strtolower($host_os) === "linux" ? $host_name : 'host.docker.internal';
+
+$content = file_get_contents('/usr/local/etc/php/conf.d/xdebug.ini');
+
+$lines = explode(PHP_EOL, $content);
+$new_lines = [];
+
+foreach ($lines as $line) {
+    if (preg_match("/^\s*xdebug.client_host\s*=\s*.+$/im", $line)) {
+        $line = "xdebug.client_host=" . $client_host;
+    }
+    $new_lines[] = $line;
+}
+
+$f = fopen('/usr/local/etc/php/conf.d/xdebug.ini', 'w+');
+try {
+    fwrite($f, implode(PHP_EOL, $new_lines));
+} catch (\Exception $e) {
+    echo($e);
+    echo(PHP_EOL);
+} finally {
+    fclose($f);
+}
+
+echo('XDebug - client_host set to ' . $client_host . PHP_EOL);

+ 3 - 6
docker/ap2i/conf/xdebug.ini

@@ -7,9 +7,6 @@ xdebug.max_nesting_level=400
 xdebug.start_with_request=trigger
 xdebug.client_port = 9003
 
-; Uncomment this line if docker is running on Mac or Windows
-; xdebug.remote_host = host.docker.internal
-
-; OR uncomment this one if docker is on Unix, then replace the hostname
-;    by the your current machine's name (type 'hostname' in a terminal)
-xdebug.client_host = dev
+; Client host is "host.docker.internal" on Mac or Windows,
+; or your current machine's name on unix (type 'hostname' in a terminal)
+xdebug.client_host = host.docker.internal