mautic のソースコードを WSL2 で開く
Published: 2021-06-16 23:29:32

最近は Ruby を書くにしても MacBook を開くよりも WSL2 の Linux 環境で済ませることが多くなってきたので、mautic もそちらに clone して開いてみる。

```
PS > wsl -l -v
  NAME                   STATE           VERSION
* Ubuntu-18.04           Running         2
```

とりあえず clone する。
```
PS > git clone git@github.com:mautic/mautic.git
```
## ddev で動作させる
ルートに `.ddev/` があるので、そちらに乗ってみる。

[Installing Mautic using DDEV | Grav](https://kb.mautic.org/knowledgebase/development/how-to-install-mautic-using-ddev)

`ddev config` は記事通りに全部デフォルトで。
```sh
~/git/mautic$ ddev config
You are reconfiguring the project at /home/dan/git/mautic.
The existing configuration will be updated and replaced.
Project name (mautic):

The docroot is the directory from which your site is served.
This is a relative path from your project root at /home/dan/git/mautic
You may leave this value blank if your site files are in the project root
Docroot Location (current directory):
Found a php codebase at /home/dan/git/mautic.
Project Type [backdrop, drupal6, drupal7, drupal8, drupal9, laravel, magento, magento2, php, shopware6, typo3, wordpress] (php):
Project type has no settings paths configured, so not creating settings file.
Configuration complete. You may now run 'ddev start'.
```

`.ddev/config.yaml` は以下を変更。
```diff
-router_http_port: "80"
-router_https_port: "443"
+router_http_port: "8081"
+router_https_port: "4431"
+timezone: Asia/Tokyo
```
`composer_version: "2"` ってのがそのままでいいのか気になりましたが、デフォルトが `feature` ブランチだったので、もう 4 系になってるからだと。

```
$ ddev start
略
================
Install complete
================
[2021-06-17 07:53:31]
[2021-06-17 07:53:31]  // Warming up the cache for the dev environment with debug true
[2021-06-17 07:53:31]
[2021-06-17 07:53:31]
[2021-06-17 07:53:31]  [OK] Cache for the "dev" environment (debug=true) was successfully warmed.
[2021-06-17 07:53:31]
[2021-06-17 07:53:31]
Enabling plugins...
13 new plugins were installed and 0 updated.
All done! Here's some useful information:
・ The default login is admin/mautic
・ To open the Mautic instance, go to https://mautic.ddev.site in your browser.
・ To open PHPMyAdmin for managing the database, go to https://mautic.ddev.site:8037 in your browser.
・ To open MailHog for seeing all emails that Mautic sent, go to https://mautic.ddev.site:8026 in your browser.
・ Run "ddev exec composer test" to run PHPUnit tests.
・ Run "ddev exec bin/console COMMAND" (like mautic:segments:update) to use the Mautic CLI. For an overview of all available CLI commands, go to https://mau.tc/cli
・ If you want to stop the instance, simply run "ddev stop".
Successfully started mautic
Project can be reached at https://mautic.ddev.site:4431 https://127.0.0.1:55445
```

記事には以下の記述があるので実行しておきます。
> **If you cloned Mautic from GitHub, there is one final step**: You need to connect to the running DDEV container and then have Composer install all of the Mautic dependencies. This is done by first running `ddev ssh` to connect to the container, and then from the project root (which DDEV by default connects you to), run `composer install` to install all dependencies.

実行はしてみたものの、`Nothing to install` なので実行は不要だったのかも。

```
/var/www/html$ composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Nothing to install, update or remove
Package misd/phone-number-bundle is abandoned, you should avoid using it. Use odolbeau/phone-number-bundle instead.
Package symfony/inflector is abandoned, you should avoid using it. Use use `EnglishInflector` from the String component instead instead.
Package rector/rector-prefixed is abandoned, you should avoid using it. Use rector/rector instead.
Generating autoload files
composer/package-versions-deprecated: Generating version class...
composer/package-versions-deprecated: ...done generating version class
129 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Symfony recipes are disabled: "symfony/flex" not found in the root composer.json

Executing script php -r "if(file_exists('./.git')&&file_exists('./build/hooks/pre-commit'.(PHP_OS=='WINNT'?'.win':''))){copy('./build/hooks/pre-commit'.(PHP_OS=='WINNT'?'.win':''),'./.git/hooks/pre-commit');} if(file_exists('./.git')&&file_exists('./build/hooks/post-checkout')){copy('./build/hooks/post-checkout','./.git/hooks/post-checkout');}" [OK]
Executing script php -r "if(file_exists('./.git/hooks/pre-commit')&&(PHP_OS!='WINNT')){chmod('./.git/hooks/pre-commit',0755);} if(file_exists('./.git/hooks/post-checkout')&&(PHP_OS!='WINNT')){chmod('./.git/hooks/post-checkout',0755);}" [OK]

> find . -mindepth 2 -type d -name .git | xargs rm -rf
```
### 開いてみる
https://mautic.ddev.site:4431

アカウントは先程のログにあったとおり `admin/mautic` でログインできます。

### 他のインストールアプリ

```
$ ddev describe
NAME    TYPE  LOCATION      URL                            STATUS
mautic  php   ~/git/mautic  https://mautic.ddev.site:4431  running

Project Information
-------------------
PHP version:            7.4
NFS mount enabled:      false
Database type:          mariadb
MariaDB version:        10.3

URLs
----
https://mautic.ddev.site:4431
https://127.0.0.1:55445
http://mautic.ddev.site:8081
http://127.0.0.1:55446

MySQL/MariaDB Credentials
-------------------------
Username: "db", Password: "db", Default database: "db"

or use root credentials when needed: Username: "root", Password: "root"

Database hostname and port INSIDE container: db:3306
To connect to db server inside container or in project settings files:
mysql --host=db --user=db --password=db --database=db
Database hostname and port from HOST: 127.0.0.1:55433
To connect to mysql from your host machine,
mysql --host=127.0.0.1 --port=55433 --user=db --password=db --database=db

Other Services
--------------
MailHog (https):        https://mautic.ddev.site:8026
MailHog:                http://mautic.ddev.site:8025
phpMyAdmin (https):     https://mautic.ddev.site:8037
phpMyAdmin:             http://mautic.ddev.site:8036

DDEV ROUTER STATUS: healthy
ssh-auth status: healthy
```

実はインストール時に `8025` が衝突して一度失敗したんですが、なるほどこちらでも `MailHog` をインストールしてたんですね。

上記の URL で問題なくアクセスできました。

## PhpStorm で開く

JetBrains プロダクトが今年あたりから WSL2 に対応しており、これまで VS Code でやっていたものが一気に便利になりました。（これまでも Windows 側にマウントしたディレクトリに配置していれば開けましたが、たまに変なことになるので待望でした。）

今回だと `\\wsl$\Ubuntu-18.04\home\dan\git\mautic` みたいなパスで開くことができます。（IDE 側で勝手に WSL 側も含めてエクスプローラー開いてくれるので直打ちすることはないですが）

### ddev の PHP を使う

WSL2 環境には PHP を入れていないので、IDE で使う PHP インタープリタを指定するのに ddev のを使うと楽です。

`docker ps` すると、ddev で立ち上がっているコンテナが分かります。PHP が入っているのは `ddrud/ddev-werbserver` コンテナなのでそれを使います。

```
$ docker ps
CONTAINER ID   IMAGE                                                  COMMAND                  CREATED          STATUS                    PORTS
                                                NAMES
f1b717ae4601   drud/ddev-router:v1.17.3                               "/app/docker-entrypo…"   11 minutes ago   Up 11 minutes (healthy)   127.0.0.1:4431->4431/tcp, 127.0.0.1:8025-8026->8025-8026/tcp, 127.0.0.1:8036-8037->8 036-8037/tcp, 80/tcp, 127.0.0.1:8081->8081/tcp   ddev-router
b4e107b098cf   drud/ddev-webserver:v1.17.4-mautic-built               "/start.sh"              11 minutes ago   Up 11 minutes (healthy)   8025/tcp, 127.0.0.1:55446->80/tcp, 127.0.0.1:55445->443/tcp
                                                ddev-mautic-web
f61027027880   phpmyadmin:5                                           "/docker-entrypoint.…"   11 minutes ago   Up 11 minutes             0.0.0.0:55444->80/tcp
                                                 ddev-mautic-dba
3a8e051f958e   drud/ddev-dbserver-mariadb-10.3:v1.17.3-mautic-built   "/docker-entrypoint.…"   11 minutes ago   Up 11 minutes (healthy)   127.0.0.1:55433->3306/tcp
                                                 ddev-mautic-db
```

![2021-06-17_08h24_37](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3wx7l4sedj9stg1rk6r.png)

上記のように設定すれば利用できます。今回は `Docker` で選びましたが、 `docker-compose` も選べたのでそちらの方がいいのかもしれません。