ITの隊長のブログ

ITの隊長のブログです。Pythonを使って仕事しています。最近はWebに戻ってきたお(^ω^ = ^ω^)

Laravelのインストール

スポンサードリンク

ひさびさにLaravelやっているが、遊び場みたいなものがほしいなって思ったのでインストールしようとしたメモ。

環境

$ sw_vers
ProductName:    macOS
ProductVersion: 12.4
BuildVersion:   21F79

$ composer --version
Composer version 2.3.8 2022-07-01 12:10:47

$ php -v
PHP 8.1.7 (cli) (built: Jun  9 2022 14:21:07) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.7, Copyright (c), by Zend Technologies

$ docker -v
Docker version 20.10.14, build a224086

ログ

インストール

入らない、ワロタ(涙)

$ composer create-project laravel/laravel sandbox --no-secure-http
Creating a "laravel/laravel" project at "./sandbox"
Warning: Accessing packagist.jp over http which is an insecure protocol.
Info from http://packagist.jp: #StandWithUkraine
Installing laravel/laravel (v9.2.0)
  - Installing laravel/laravel (v9.2.0): Extracting archive
Created project in /Users/kazumatamaki/ghq/github.com/aipacommander/laravel-sandbox/sandbox
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
http://packagist.jp could not be fully loaded (Your configuration does not allow connections to http://packagist.jp/packages.json. See https://getcomposer.org/doc/06-config.md#secure-http for details.), package information was loaded from the local cache and may be out of date

In Config.php line 609:

  Your configuration does not allow connections to http://packagist.jp/p/laravel/framework%24284b0566e5faa9a9179b7211e56b72041e822e696c74e3637e51619ef4957970.json. See https://getcomposer.org/doc/06-co
  nfig.md#secure-http for details.


create-project [-s|--stability STABILITY] [--prefer-source] [--prefer-dist] [--prefer-install PREFER-INSTALL] [--repository REPOSITORY] [--repository-url REPOSITORY-URL] [--add-repository] [--dev] [--no-dev] [--no-custom-installers] [--no-scripts] [--no-progress] [--no-secure-http] [--keep-vcs] [--remove-vcs] [--no-install] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--ask] [--] [<package> [<directory> [<version>]]]

あのいつものやつだろ。って思ったけど、それやっても入らない・・・・なぜ??????

環境構築〜〜〜って思ってたら下記サイトに出会う。

omkz.net

$ curl -s "https://laravel.build/example-app" | bash

なにこれどゆこと????

ブラウザでURLを確認するとシェルスクリプトがでてきて理解した。なるほどdockerコンテナ使ってインストールしているのか。便利。

とはいえ、 example-app って名前のディレクトリが入ってきたので気に入らないなと思い修正しました。

$ curl -s "https://laravel.build/sandbox" | bash

 _                               _
| |                             | |
| |     __ _ _ __ __ ___   _____| |
| |    / _` | '__/ _` \ \ / / _ \ |
| |___| (_| | | | (_| |\ V /  __/ |
|______\__,_|_|  \__,_| \_/ \___|_|

# 省略

Please provide your password so we can make some final adjustments to your application's permissions.

Password:

Thank you! We hope you build something incredible. Dive in with: cd example-app && ./vendor/bin/sail up

最後にPCのパスワードを求められるのでそこだけ入力してあげたら完了した。神。

起動

$ cd sandbox/
$ ./vendor/bin/sail up

$ docker compose ps
NAME                     COMMAND                  SERVICE             STATUS               PORTS
sandbox-laravel.test-1   "start-container"        laravel.test        running              0.0.0.0:80->80/tcp, 0.0.0.0:5173->5173/tcp
sandbox-mailhog-1        "MailHog"                mailhog             running              0.0.0.0:1025->1025/tcp, 0.0.0.0:8025->8025/tcp
sandbox-meilisearch-1    "tini -- /bin/sh -c …"   meilisearch         running (starting)   0.0.0.0:7700->7700/tcp
sandbox-mysql-1          "/entrypoint.sh mysq…"   mysql               running (starting)   0.0.0.0:3306->3306/tcp
sandbox-redis-1          "docker-entrypoint.s…"   redis               running (starting)   0.0.0.0:6379->6379/tcp
sandbox-selenium-1       "/opt/bin/entry_poin…"   selenium            running              5900/tcp

いっぱい立ち上がった。sailとなるものがあるらしい。

laravel.com

dockerしらなくてもこれ使ったらコンテナ操作やらその辺簡易にできるよ。というふんわり理解。

Makefileを用意する

$ touch Makefile
$ vim Makefile
test:
    ./vendor/bin/sail artisan migrate:refresh --seed
    ./vendor/bin/sail artisan test --parallel

phpcsとphpcbfの導入

Makefileを更新する

export PWD=`pwd`
export COMPOSER_RUN=docker run --rm -u "$(id -u):$(id -g)" -v $(PWD):/var/www/html -w /var/www/html laravelsail/php81-composer:latest composer
export INSTALL=$(COMPOSER_RUN) install --ignore-platform-reqs
export REQUIRE=$(COMPOSER_RUN) require

install:
    $(INSTALL)
install_args:
    $(REQUIRE) $(package)
test:
    ./vendor/bin/sail artisan migrate:refresh --seed
    ./vendor/bin/sail artisan test --parallel
phpcs:
    ./vendor/bin/sail composer phpcs
phpcbf:
    ./vendor/bin/sail composer phpcbf
all:
    @make test
    @make phpcbf
    @make phpcs

composerで叩いたら実行できるようにscriptの修正

composer.json

// 省略
    "scripts": {
// 省略
        "phpcs": [
            "./vendor/bin/phpcs --standard=phpcs.xml ./"
        ],
        "phpcbf": [
            "./vendor/bin/phpcbf --standard=phpcs.xml ./"
        ]
    },
// 省略

phpcsとphpcbfを実行するパッケージをインストールする。

$ make install_args package="squizlabs/php_codesniffer"

qiita.com

記事の通りに、 phpcs.xml を用意。

用意できたら試す。

$ make phpcbf
> ./vendor/bin/phpcbf --standard=phpcs.xml ./
.F.........F.......FF.FF.F....F.................. 49 / 49 (100%)



PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/var/www/html/app/Http/Controllers/Controller.php     1      0
/var/www/html/app/Models/User.php                     1      0
/var/www/html/config/broadcasting.php                 8      1
/var/www/html/config/cache.php                        2      0
/var/www/html/config/database.php                     2      0
/var/www/html/config/filesystems.php                  2      0
/var/www/html/config/logging.php                      6      0
/var/www/html/config/session.php                      2      0
----------------------------------------------------------------------
A TOTAL OF 24 ERRORS WERE FIXED IN 8 FILES
----------------------------------------------------------------------

Time: 24.96 secs; Memory: 12MB


Script ./vendor/bin/phpcbf --standard=phpcs.xml ./ handling the phpcbf event returned with error code 1

こいつは勝手にきれいにしてくれるとのこと、2回目

$ make phpcbf
./vendor/bin/sail composer phpcbf
> ./vendor/bin/phpcbf --standard=phpcs.xml ./
................................................. 49 / 49 (100%)



No fixable errors were found

Time: 4.09 secs; Memory: 10MB

phpcsも動くか確認する

$ make phpcs
./vendor/bin/sail composer phpcs
> ./vendor/bin/phpcs --standard=phpcs.xml ./
...................W..........................E.E 49 / 49 (100%)



FILE: /var/www/html/config/broadcasting.php
--------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------
 39 | WARNING | Line exceeds 120 characters; contains 165 characters
    |         | (Generic.Files.LineLength.TooLong)
--------------------------------------------------------------------------------------------------------


FILE: /var/www/html/tests/Feature/ExampleTest.php
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 15 | ERROR | Method name "ExampleTest::test_the_application_returns_a_successful_response" is not in camel caps format
    |       | (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: /var/www/html/tests/Unit/ExampleTest.php
--------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------
 14 | ERROR | Method name "ExampleTest::test_that_true_is_true" is not in camel caps format
    |       | (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
--------------------------------------------------------------------------------------------------------------------------------------------

Time: 3.01 secs; Memory: 10MB

Script ./vendor/bin/phpcs --standard=phpcs.xml ./ handling the phpcs event returned with error code 1
make: *** [phpcs] Error 1

おーらい

とりあえず今日はここまで。