2020年4月14日 星期二

DesignPatternsPHP

下載

https://github.com/domnikl/DesignPatternsPHP
$ git clone git@github.com:domnikl/DesignPatternsPHP.git

瀏覽文檔(可選)

修改端口

因為8080被laradock佔用了,所以要改端口
docker-compose.yml
services:
  nginx:
    port:
      - "8090:80"

# docker-compose up --build
然後打開 http://192.168.1.x:8090/ 即可瀏覽像  https://designpatternsphp.readthedocs.io/en/latest/README.html 一樣的文檔
這個步驟新增了幾個images:
designpatternsphp_nginx   latest              3867bfce0be3        2 minutes ago       139MB
composer                  latest              b7fba84aad57        20 hours ago        179MB
python                    latest              d47898c6f4b0        9 days ago          933MB
nginx                     latest              ed21b7a8aee9        10 days ago         127MB

進入容器後發現沒php

# docker exec -it designpatternsphp_nginx_1 bash
root@adcb4fe4d5bb:/usr/share/nginx/html# php -v
bash: php: command not found

因為這個commit 把 php:7.3.8-cli 拿掉了..
commit be66dcc037f8b2293134efe3cc3f2e87a25b8f80
Author: Dominik Liebler <liebler.dominik@gmail.com>
Date:   Fri Apr 3 13:56:33 2020 +0200

    #376 use multi-stage Docker build

diff --git a/Dockerfile b/Dockerfile
index d62cca0..e319867 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,9 +1,17 @@
-FROM php:7.3.8-cli
+FROM composer AS composer
...

所以容器內跑不動 phpunit ...而且還沒composer install,所以 ./vendor/bin/phpunit 還不存在

跑composer install

我的hyper-v PHP 版本是7.3
# composer install
...
  Problem 1
    - This package requires php >=7.4 but your HHVM version does not satisfy that requirement.
因為
commit 579a5ac946330c8b966a23ba3a53434487c26941
Author: Dominik Liebler <liebler.dominik@gmail.com>
Date:   Sat Dec 14 12:50:05 2019 +0100

    update deps & install rector
...
diff --git a/composer.json b/composer.json
...
-        "php": ">=7.2",
+        "php": ">=7.4",

使用composer容器做composer install

[root@localhost DesignPatternsPHP]# docker run --rm --interactive --tty \
>   --volume $PWD:/app \
>   composer install

--rm : 自動移除容器,當離開時
--interactive :即-i ,保持STDIN開啟,即使不attached
--tty : 即-t
--volume :即-v

即可順利composer install 建立vendor/
但是還是不能用 php 7.3 跑 DesignPatternsPHP,因為用到了7.4的語法
# ./vendor/bin/phpunit
PHP Parse error:  syntax error, unexpected 'Handler' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /root/DesignPatternsPHP/Behavioral/ChainOfResponsibilities/Tests/ChainTest.php on line 14



使用php7.4-cli

新建 Dockerfile2 
FROM php:7.4-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN pecl install xdebug-2.8.1 \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.idekey=PHPSTORM" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.remote_log=/tmp/xdebug.log" >> /usr/local/etc/php/php.ini

CMD [ "php", "./vendor/bin/phpunit" ]

[root@localhost DesignPatternsPHP]# docker build -f Dockerfile2 -t my-php-7.4-cli .
這個步驟新增了幾個images:
my-php-7.4-cli            latest              11a27014b3a0        35 seconds ago      489MB
php                       7.4-cli             76a5dae0c884        9 days ago          405MB

要用run -it ,如果 run -d容器會起來後跑完 ./vendor/bin/phpunit 後就 Exited
# docker run -it --name my-running-php-7.4-cli -v /root/DesignPatternsPHP:/usr/src/myapp -e XDEBUG_CONFIG="remote_host=192.168.1.7 idekey=XDEBUG" -e PHP_IDE_CONFIG="serverName=localhost" my-php-7.4-cli:latest bash

-e :進入容器前設定環境變數,等同在容器內執行 export XDEBUG_CONFIG="remote_host=192.168.1.7 idekey=XDEBUG" 和 export PHP_IDE_CONFIG="serverName=localhost"

使用PHPStorm 時idekey要設PHPSTORM =》
export XDEBUG_CONFIG="remote_host=192.168.1.7 idekey=PHPSTORM"
export PHP_IDE_CONFIG="serverName=localhost"


PHPStorm中可能的報錯

To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session.
解法:
https://www.jetbrains.com/help/phpstorm/debugging-a-php-cli-script.html
容器中執行 export PHP_IDE_CONFIG="serverName=localhost"
Can't find a source position. Server with name 'localhost' doesn't exist.
點擊"Configure servers" 設定Servers:localhost
# ./vendor/bin/phpunit
Cannot find a local copy of the file on server /usr/src/myapp/Standard input code
解法:
指定Tests不要一次全跑
# ./vendor/bin/phpunit Creational/AbstractFactory/Tests/AbstractFactoryTest.php























沒有留言:

張貼留言