カテゴリー
Linux

PHP 5.3 の 1 ファイルプログラムを Composer で管理し始めるためにやること

はじめに

  • PHP 5.3.3 のサーバで動いている 1 ファイルの PHP を修正したい。
  • Composer を使っていなかったが、使いたい。
  • 手元のローカル開発環境は PHP 7.3

このような環境で、開発をするときの注意事項がわかりましたので、ノートいたします。

やるべきこと

composer.json が次のようになっていれば、どのような PHP バージョンの開発環境でも、 PHP 5.3.3 で動くパッケージを扱うことができます。

{
    "config": {
        "platform": {
            "php": "5.3.3"
        }
    }
}

次のように、コマンドで追加することもできます。

composer config platform.php 5.3.3

参考ページ

実際に依存パッケージを追加してみる

composer.json を作るところから、やってみました。

PHPUnit を追加してみました。PHP 5.3.3 で動く、もっとも高いバージョンの PHPUnit が自動的に追加されました。

なんと簡単なのでしょうか!

ちなみに、ドキュメントです。

$ echo "{}" > composer.json
$ cat composer.json
{}
$
$ composer config platform.php 5.3.3
$ cat composer.json
{
    "config": {
        "platform": {
            "php": "5.3.3"
        }
    }
}
$
$ composer require phpunit/phpunit --dev
Using version ^4.8 for phpunit/phpunit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 18 installs, 0 updates, 0 removals
  - Installing sebastian/version (1.0.6): Downloading (100%)
  - Installing sebastian/global-state (1.1.1): Downloading (100%)
  - Installing sebastian/recursion-context (1.0.5): Downloading (100%)
  - Installing sebastian/exporter (1.2.2): Downloading (100%)
  - Installing sebastian/environment (1.3.8): Downloading (100%)
  - Installing sebastian/diff (1.4.3): Downloading (100%)
  - Installing sebastian/comparator (1.2.4): Downloading (100%)
  - Installing symfony/yaml (v2.6.13): Downloading (100%)
  - Installing doctrine/instantiator (1.0.5): Downloading (100%)
  - Installing phpdocumentor/reflection-docblock (2.0.5): Downloading (100%)
  - Installing phpspec/prophecy (1.8.1): Loading from cache
  - Installing phpunit/php-text-template (1.2.1): Loading from cache
  - Installing phpunit/phpunit-mock-objects (2.3.8): Downloading (100%)
  - Installing phpunit/php-timer (1.0.9): Loading from cache
  - Installing phpunit/php-token-stream (1.4.12): Downloading (100%)
  - Installing phpunit/php-file-iterator (1.4.5): Loading from cache
  - Installing phpunit/php-code-coverage (2.2.4): Downloading (100%)
  - Installing phpunit/phpunit (4.8.36): Downloading (100%)
sebastian/global-state suggests installing ext-uopz (*)
phpdocumentor/reflection-docblock suggests installing dflydev/markdown (~1.0)
phpdocumentor/reflection-docblock suggests installing erusev/parsedown (~1.0)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.2.1)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files
$
$ cat composer.json
{
    "config": {
        "platform": {
            "php": "5.3.3"
        }
    },
    "require-dev": {
        "phpunit/phpunit": "^4.8"
    }
}
$

補足。 platform.php を設定しないとどうなるか?

たとえ PHP 5.3.3 の環境であったとしても、依存関係のエラーとなり、 PHPUnit を追加することできませんでした。

# php -v
PHP 5.3.3 (cli) (built: Mar 22 2017 12:27:09) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
#
# composer require phpunit/phpunit
Using version ^4.8 for phpunit/phpunit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpunit/phpunit 4.8.9 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.8 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.7 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.6 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.5 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.4 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.36 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.35 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.34 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.33 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.32 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.31 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.30 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.3 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.29 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.28 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.27 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.26 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.25 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.24 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.23 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.22 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.21 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.20 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.2 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.19 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.18 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.17 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.16 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.15 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.14 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.13 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.12 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.11 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.10 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.1 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - phpunit/phpunit 4.8.0 requires ext-dom * -> the requested PHP extension dom is missing from your system.
    - Installation request for phpunit/phpunit ^4.8 -> satisfiable by phpunit/phpunit[4.8.0, 4.8.1, 4.8.10, 4.8.11, 4.8.12, 4.8.13, 4.8.14, 4.8.15, 4.8.16, 4.8.17, 4.8.18, 4.8.19, 4.8.2, 4.8.20, 4.8.21, 4.8.22, 4.8.23, 4.8.24, 4.8.25, 4.8.26, 4.8.27, 4.8.28, 4.8.29, 4.8.3, 4.8.30, 4.8.31, 4.8.32, 4.8.33, 4.8.34, 4.8.35, 4.8.36, 4.8.4, 4.8.5, 4.8.6, 4.8.7, 4.8.8, 4.8.9].

  To enable extensions, verify that they are enabled in your .ini files:
    - /etc/php.ini
    - /etc/php.d/curl.ini
    - /etc/php.d/fileinfo.ini
    - /etc/php.d/json.ini
    - /etc/php.d/phar.ini
    - /etc/php.d/zip.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.

Installation failed, reverting ./composer.json to its original content.
#

おわりに

本投稿は、 PHP 5.3 の Composer 環境を Docker で構築する – oki2a24 の続きとなります。

Docker で古い環境を作りました、そこで Composer を扱うには、設定を工夫する必要がございました。

今回の点を考慮することで、古い PHP 環境でも動かせる Composer を手に入れられるようになりました♩

以上です。

コメントを残す