CodeIgniter 4.0を開発版にアップデートする post

CodeIgniter 4.0 を開発版に更新する方法です。

CodeIgniter 4.0.4 からかなり開発が進んでいますので、 執筆時点では、CodeIgniter 4.0.4 よりも開発版(develop ブランチ)を使用することをお薦めします。

動作確認環境

  • CodeIgniter 4.0.4
  • Composer 2.0.8
  • PHP 7.4.13
    • Xdebug 3.0.1
  • macOS 10.15.7

PHPUnit のアップデート

PHPUnit を 9 にアップデートします。以下で用いる Liaison Revision を使わない場合は、このアップデートは必須ではありません。

--- a/composer.json
+++ b/composer.json
@@ -12,7 +12,7 @@
    "require-dev": {
        "fzaninotto/faker": "^1.9@dev",
        "mikey179/vfsstream": "1.6.*",
-       "phpunit/phpunit": "^8.5"
+       "phpunit/phpunit": "^9.0"
    },
    "autoload-dev": {
        "psr-4": {
$ composer update

PHPUnit 9.5.0 にアップデートされました。

phpunit.xml の書き方が変更されていますので、移行します。

$ vendor/bin/phpunit --migrate-configuration
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

Created backup:         /Users/kenji/work/codeigniter/ci4app/phpunit.xml.dist.bak
Migrated configuration: /Users/kenji/work/codeigniter/ci4app/phpunit.xml.dist
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -1,52 +1,38 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<phpunit bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php"
-       backupGlobals="false"
-       colors="true"
-       convertErrorsToExceptions="true"
-       convertNoticesToExceptions="true"
-       convertWarningsToExceptions="true"
-       stopOnError="false"
-       stopOnFailure="false"
-       stopOnIncomplete="false"
-       stopOnSkipped="false">
-   <testsuites>
-       <testsuite name="app">
-           <directory>./tests</directory>
-       </testsuite>
-   </testsuites>
-
-   <filter>
-       <whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
+<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/codeigniter4/framework/system/Test/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
+  <coverage includeUncoveredFiles="true" processUncoveredFiles="true">
+    <include>
       <directory suffix=".php">./app</directory>
+    </include>
     <exclude>
       <directory suffix=".php">./app/Views</directory>
       <file>./app/Config/Routes.php</file>
     </exclude>
-       </whitelist>
-   </filter>
-
+    <report>
+      <clover outputFile="build/logs/clover.xml"/>
+      <html outputDirectory="build/logs/html"/>
+      <php outputFile="build/logs/coverage.serialized"/>
+      <text outputFile="php://stdout" showUncoveredFiles="false"/>
+    </report>
+  </coverage>
+  <testsuites>
+    <testsuite name="app">
+      <directory>./tests</directory>
+    </testsuite>
+  </testsuites>
   <logging>
-       <log type="coverage-html" target="build/logs/html"/>
-       <log type="coverage-clover" target="build/logs/clover.xml"/>
-       <log type="coverage-php" target="build/logs/coverage.serialized"/>
-       <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
-       <log type="testdox-html" target="build/logs/testdox.html"/>
-       <log type="testdox-text" target="build/logs/testdox.txt"/>
-       <log type="junit" target="build/logs/logfile.xml"/>
+    <testdoxHtml outputFile="build/logs/testdox.html"/>
+    <testdoxText outputFile="build/logs/testdox.txt"/>
+    <junit outputFile="build/logs/logfile.xml"/>
   </logging>
-
   <php>
     <server name="app.baseURL" value="http://example.com"/>
-
     <!-- Directory containing phpunit.xml -->
     <const name="HOMEPATH" value="./"/>
-
     <!-- Directory containing the Paths config file -->
     <const name="CONFIGPATH" value="./app/Config/"/>
-
     <!-- Directory containing the front controller (index.php) -->
     <const name="PUBLICPATH" value="./public/"/>
-
     <!-- Database configuration -->
     <!--   <env name="database.tests.hostname" value="localhost"/>  -->
     <!--   <env name="database.tests.database" value="tests"/>      -->

CodeIgniter4 を開発版に変更

プロジェクトのルートフォルダで builds development コマンドを実行すると、最新の開発版に変更できます。

$ php builds development
The following files were modified:
 * /Users/kenji/work/codeigniter/ci4app/composer.json
 * /Users/kenji/work/codeigniter/ci4app/app/Config/Paths.php
 * /Users/kenji/work/codeigniter/ci4app/phpunit.xml.dist
Run `composer update` to sync changes with your vendor folder

上記ファイル内のパスなどが変更されました。

Composer パッケージを更新します。

$ composer update
Loading composer repositories with package information
Updating dependencies                                 
Lock file operations: 1 install, 1 update, 1 removal
  - Removing codeigniter4/framework (v4.0.4)
  - Locking codeigniter4/codeigniter4 (dev-develop e5721a4)
  - Downgrading fzaninotto/faker (dev-master 5ffe7db => v1.9.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 1 update, 1 removal
  - Syncing codeigniter4/codeigniter4 (dev-develop e5721a4) into cache
  - Syncing fzaninotto/faker (v1.9.2) into cache
  - Removing codeigniter4/framework (v4.0.4)
  - Installing codeigniter4/codeigniter4 (dev-develop e5721a4): Cloning e5721a4bcd from cache
  - Downgrading fzaninotto/faker (dev-master 5ffe7db => v1.9.2): Checking out 848d812523 from cache
Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
28 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Generating autoload files
Generated autoload files

CodeIgniter v4.0.4 が codeigniter4/codeigniter4 の develop ブランチに変更されました。

Faker のアップデート

Faker のパッケージが変更されていますので、更新します。

--- a/composer.json
+++ b/composer.json
@@ -11,7 +11,7 @@
"liaison/revision": "1.x-dev"
},
"require-dev": {
-        "fzaninotto/faker": "^1.9@dev",
+        "fakerphp/faker": "^1.9",
         "mikey179/vfsstream": "1.6.*",
         "phpunit/phpunit": "^9.0"
  },
$ composer update
Loading composer repositories with package information
Updating dependencies                                 
Lock file operations: 1 install, 0 updates, 1 removal
  - Removing fzaninotto/faker (v1.9.2)
  - Locking fakerphp/faker (v1.13.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 1 removal
  - Downloading fakerphp/faker (v1.13.0)
  - Removing fzaninotto/faker (v1.9.2)
  - Installing fakerphp/faker (v1.13.0): Extracting archive
Generating autoload files
31 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Generating autoload files
Generated autoload files

system 以外のアップデート

composer updatevendor/ 以下にある CodeIgniter4 の system フォルダは更新されますが、それ以外の app/ フォルダ内のファイルなどは更新されません。

それらを更新してくれるパッケージが Liaison Revision です。

Liaison Revision のインストール

Liaison Revision は PHP 7.3 以上、CodeIgniter 4.0.4 以上が必要です。

$ composer require liaison/revision
Using version 1.x-dev for liaison/revision
./composer.json has been updated
Running composer update liaison/revision
Loading composer repositories with package information
Updating dependencies                                 
Lock file operations: 4 installs, 0 updates, 0 removals
  - Locking liaison/revision (dev-develop 9bb7912)
  - Locking symfony/filesystem (v5.2.1)
  - Locking symfony/polyfill-php80 (v1.20.0)
  - Locking symfony/process (v5.2.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Syncing liaison/revision (dev-develop 9bb7912) into cache
  - Installing symfony/polyfill-php80 (v1.20.0): Extracting archive
  - Installing symfony/process (v5.2.1): Extracting archive
  - Installing symfony/filesystem (v5.2.1): Extracting archive
  - Installing liaison/revision (dev-develop 9bb7912): Cloning 9bb791224b from cache
Generating autoload files
31 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Generating autoload files
Generated autoload files

インストールが完了したら、既存のファイルは全て Git で管理しておきましょう。 commit されていない変更は commit してください。

ファイルのアップデート

それでは、system 以外のファイルをアップデートします。

spark revision:update コマンドを実行し、対話型で作業を進めます。

$ php spark revision:update
CodeIgniter v4.0.4 Command Line Tool - Server Time: 2020-12-29 20:04:47 UTC+09:00

Liaison Revision
Version: 1.0.0
Run Date: Tue, 29 December 2020, 20:04:47 UTC+09:00

Loaded configuration settings from: Liaison\Revision\Config\Revision.
+---------------------------+--------------------------------------------------------+
| Setting                   | Value                                                  |
+---------------------------+--------------------------------------------------------+
| Root Path                 | /Users/kenji/work/codeigniter/ci4app/                  |
| Write Path                | /Users/kenji/work/codeigniter/ci4app/writable/         |
| Ignored Directories Count | 0                                                      |
| Ignored Files Count       | 0                                                      |
| Allow Gitignore Entry     | Allowed                                                |
| Fall Through to Project   | Allowed                                                |
| Maximum Retries           | 10                                                     |
| Consolidator              | Liaison\Revision\Consolidation\DefaultConsolidator     |
| Upgrader                  | Liaison\Revision\Upgrade\ComposerUpgrader              |
| Pathfinder                | Liaison\Revision\Paths\DefaultPathfinder               |
| Diff Output Builder       | SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder |
| Log Handlers Count        | 2                                                      |
+---------------------------+--------------------------------------------------------+

Starting software updates...

Loading composer repositories with package information
Updating dependencies                                 
Nothing to modify in lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
31 packages you are using are looking for funding.
Use the `composer fund` command to find out more!

Found 45 files to consolidate.
[p] Proceed.
[l] List all files to consolidate.
[c] List created files only (2).
[m] List modified files only (43).
[d] List deleted files only (0).
[a] Abort.

What shall I do? [p, l, c, m, d, a]: 

p を選択し、処理を進めます。

What shall I do? [p, l, c, m, d, a]: p

Found 43 files in conflict.
[l] List all files in conflict.
[o] Overwrite all.
[b] Create backup files then safely overwrite all.
[s] Skip all.
[r] Resolve each conflict.
[a] Abort.

What shall I do? [l, o, b, s, r, a]:

ファイルは Git 管理されていますので、o を選択し、上書きします。

What shall I do? [l, o, b, s, r, a]: o


Logs for this run can be found here:
/Users/kenji/work/codeigniter/ci4app/writable/revision/logs/

Terminating: Application update was successful.
Software updates finished in 2.496 minutes.

以下のファイルが変更されました。結構、変わってます。

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   app/Config/App.php
    modified:   app/Config/Autoload.php
    modified:   app/Config/Boot/development.php
    modified:   app/Config/Boot/production.php
    modified:   app/Config/Boot/testing.php
    modified:   app/Config/Cache.php
    modified:   app/Config/Constants.php
    modified:   app/Config/ContentSecurityPolicy.php
    modified:   app/Config/Database.php
    modified:   app/Config/DocTypes.php
    modified:   app/Config/Email.php
    modified:   app/Config/Encryption.php
    modified:   app/Config/Events.php
    modified:   app/Config/Exceptions.php
    modified:   app/Config/Filters.php
    modified:   app/Config/ForeignCharacters.php
    modified:   app/Config/Format.php
    modified:   app/Config/Honeypot.php
    modified:   app/Config/Images.php
    modified:   app/Config/Kint.php
    modified:   app/Config/Logger.php
    modified:   app/Config/Migrations.php
    modified:   app/Config/Mimes.php
    modified:   app/Config/Modules.php
    modified:   app/Config/Pager.php
    modified:   app/Config/Paths.php
    modified:   app/Config/Routes.php
    modified:   app/Config/Services.php
    modified:   app/Config/Toolbar.php
    modified:   app/Config/UserAgents.php
    modified:   app/Config/Validation.php
    modified:   app/Config/View.php
    modified:   app/Controllers/BaseController.php
    modified:   app/Controllers/Home.php
    modified:   app/Views/errors/cli/error_404.php
    modified:   app/Views/errors/cli/error_exception.php
    modified:   app/Views/errors/html/debug.css
    modified:   app/Views/errors/html/debug.js
    modified:   app/Views/errors/html/error_exception.php
    modified:   env
    modified:   public/.htaccess
    modified:   public/index.php
    modified:   spark

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    app/Config/Generators.php
    app/Config/Security.php
    writable/revision/

Git で差分を確認しつつ、必要ない差分は削除します。

app/Config/Filter.phpcsrf フィルタの有効化と app/Config/Paths.php$systemDirectory は元に戻しました。

--- a/app/Config/Paths.php
+++ b/app/Config/Paths.php
@@ -26,7 +26,7 @@ class Paths
     *
     * @var string
     */
-   public $systemDirectory = __DIR__ . '/../../system';
+   public $systemDirectory = __DIR__ . '/../../vendor/codeigniter4/codeigniter4/system';

    /**
     * ---------------------------------------------------------------

PHPUnit テストの実行

テストが通るか確認しておきましょう。

CodeIgniter プロジェクトのルートフォルダで、以下のコマンドを実行します。

$ composer test
> phpunit
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

Warning:       XDEBUG_MODE=coverage or xdebug.mode=coverage has to be set

.....                                                               5 / 5 (100%)

Time: 00:00.153, Memory: 10.00 MB

OK (5 tests, 6 assertions)

通りました。

開発版のユーザガイド

開発版のユーザガイドは以下にあります。

リリース版のユーザガイドの誤りも修正されていますので、こちらを参照しましょう。

参考

Date: 2020/12/30

Tags: codeigniter, codeigniter4