DebianベースのcomposerコンテナでphpDocumentorを動かした話

DockerPHP

TL;DR

  • alpineベースだとうまく動かないのでdebianベース等使え

動機

  • composerとかがなかった太古の時代のソースコードのドキュメント自動生成

    • したがって、composer配下ではない。composer.jsonとか置いてない
  • 普段遣いの環境にPHPを直接入れたくないので、dockerコンテナで実行

構成

  • windowsで作業していたのでtreeコマンドコンテナ使用
docker container run --rm -v ${pwd}:/root wandta/tree -L 3
.
├── app
│   ├── composer.json
│   └── src
│      └── Hoge.php
└── docker-compose.yml
  • docker-compose.yml
version: "3"
services:
  composer:
    image: composer
    volumes:
      - ./app:/app
  • app/composer.json

    • 普通にcomposer require phpdocumentor/phpdocumentorすると動かない(‘19/5/18現在)

    • ので真面目にcomposer.json書く
{
    "require-dev": {
        "phpdocumentor/phpdocumentor": "^2.9",
        "jms/serializer": "1.7.*"
    },
    "scripts": {
        "doc": [
            "vendor/bin/phpdoc -d src -t doc"
        ]
    }
}
  • app/src/Hoge.php
<?php

/**
 * Hogeクラス
 */
class Hoge
{
    /** @var int */
    private $member = 3;

    /**
     * @param int $input 入力
     * @return int 出力
     */
    function method (int $input): int
    {
        return 3;
    }
}

phpDocumentor実行、エラー出る

docker-compose run --rm composer composer install
docker-compose run --rm composer composer run doc

すごい勢いでエラー出る

> vendor/bin/phpdoc -d src -t doc
Collecting files .. OK
Initializing parser .. OK
Parsing files
Parsing /app/src/Hoge.php
Storing cache in "/app/doc" .. OK
Load cache                                                         ..    0.015s
Preparing template "clean"                                         ..    0.033s
Preparing 17 transformations                                       ..    0.000s
Build "elements" index                                             ..    0.000s
Replace textual FQCNs with object aliases                          ..    0.000s
Resolve @link and @see tags in descriptions                        ..    0.000s
Enriches inline example tags with their sources                    ..    0.000s
Build "packages" index                                             ..    0.004s
Build "namespaces" index and add namespaces to "elements"          ..    0.000s
Collect all markers embedded in tags                               ..    0.000s
Transform analyzed project into artifacts                          .. 
Applying 17 transformations
  Initialize writer "phpDocumentor\Plugin\Core\Transformer\Writer\FileIo"
  Initialize writer "phpDocumentor\Plugin\Twig\Writer\Twig"
  Initialize writer "phpDocumentor\Plugin\Graphs\Writer\Graph"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"

Warning: file_put_contents(/app/doc//): failed to open stream: Is a directory in /app/vendor/phpdocumentor/phpdocumentor/src/phpDocumentor/Plugin/Twig/Writer/Twig.php on line 117
  Execute transformation using writer "twig"

Warning: file_put_contents(/app/doc//): failed to open stream: Is a directory in /app/vendor/phpdocumentor/phpdocumentor/src/phpDocumentor/Plugin/Twig/Writer/Twig.php on line 117
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"

Warning: file_put_contents(/app/doc//): failed to open stream: Is a directory in /app/vendor/phpdocumentor/phpdocumentor/src/phpDocumentor/Plugin/Twig/Writer/Twig.php on line 117
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "Graph"
Unable to find the `dot` command of the GraphViz package. Is GraphViz correctly installed and present in your path?   0.566s
Analyze results and write report to log                            ..    0.000s

alpineベースだとうまく動かないよう

  • パスの連結まわりでエラーが出てるっぽい
Warning: file_put_contents(/app/doc//): 

It looks like it has to do something with the dubble / in your target path.
But I don’t know where this is comming from. Please check your config for that.

FROM php:7-alpine

...

COPY docker-entrypoint.sh /docker-entrypoint.sh

WORKDIR /app

ENTRYPOINT ["/bin/sh", "/docker-entrypoint.sh"]

CMD ["composer"]

debianベースのcomposerイメージ作る

  • マルチステージビルド的なことをする

    • composer公式イメージから、composer実行ファイルのみ引っこ抜いてdebianベースのphpイメージに突っ込む
  • そもそもcomposerがどこにあるのかをまず確認
docker-compose run --rm composer composer install
/usr/bin/composer
  • Dockerfile書く
  • composer-debian/Dockerfile
FROM composer as composer


FROM php:7.3.5-cli-stretch

COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN chmod +x /usr/bin/composer

# composerでパッケージ取得するためにGitとunzip入れる
RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install -y git unzip

WORKDIR /app

CMD ["composer"]
  • イメージビルド必要
  version: "3"
  services:
    composer:
-     image: composer
+     build:
+       context: .
+       dockerfile: ./composer-debian/Dockerfile
      volumes:
        - ./app:/app
docker-compose build
  • 再度phpDocumentor実行
docker-compose run --rm composer composer run doc
Collecting files .. OK
Initializing parser .. OK
Parsing files
Parsing /app/src/Hoge.php

Warning: count(): Parameter must be an array or an object that implements Countable in /app/vendor/phpdocumentor/phpdocumentor/src/phpDocumentor/Plugin/Core/Descriptor/Validator/Constraints/Functions/IsArgumentInDocBlockValidator.php on line 33
  No summary was found for this file
  No summary for method method()
  No summary for property $member
Storing cache in "/app/doc" .. OK
Load cache                                                         ..    0.007s
Preparing template "clean"                                         ..    0.016s
Preparing 17 transformations                                       ..    0.000s
Build "elements" index                                             ..    0.000s
Replace textual FQCNs with object aliases                          ..    0.000s
Resolve @link and @see tags in descriptions                        ..    0.000s
Enriches inline example tags with their sources                    ..    0.000s
Build "packages" index                                             ..    0.003s
Build "namespaces" index and add namespaces to "elements"          ..    0.000s
Collect all markers embedded in tags                               ..    0.000s
Transform analyzed project into artifacts                          .. 
Applying 17 transformations
  Initialize writer "phpDocumentor\Plugin\Core\Transformer\Writer\FileIo"
  Initialize writer "phpDocumentor\Plugin\Twig\Writer\Twig"
  Initialize writer "phpDocumentor\Plugin\Graphs\Writer\Graph"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "Graph"
Unable to find the `dot` command of the GraphViz package. Is GraphViz correctly installed and present in your path?   0.611s
Analyze results and write report to log                            ..    0.000s
  • failed to open streamエラーが消えた
  • いい感じに出力できている

    • app/doc/classes/Hoge.html

20190518233525

phpDocumentorさえ使えればいい場合

  • これを使えばよかったみたい(徒労)

    • debianベースのPHPイメージベース