Laravel Prompts のファイル選択フォーム追加用パッケージ file-selector-prompt の紹介です。
file-selector-promptとは
Laravel Promptsにファイル選択フォームを追加するためのパッケージです。
対話入力モードにおいて、迅速かつ正確なファイル入力を支援してくれます。
file-selector-promptは、ローカルファイルシステムのカレントディレクトリ上のエントリーを、選択肢としてリストします。
選択肢を選んで[TAB]を押すことで入力補完してくれ、続けて入力することが可能です。
選択肢のリストは、入力文字列にマッチしたものにリアルタイムで切り替わります。
また、存在しない架空のファイルパスを入力することも可能です。
もちろん、form() での使用も可能です。
※Laravel Prompts公式リポジトリにPRしたのですが、
コード量を減らすことを検討中のようで却下され、
パッケージとしてリリースすることをTaylor氏から提案されたため、「file-selector-prompt」としてリリースしました。
前提条件
- PHP8.2以降インストール済
- Composerインストール済
インストール
プロジェクトフォルダ内で実行します。
composer require macocci7/file-selector-prompt
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_01.png)
※laravel/promptsもインストールされます。
基本的な使い方
▼ src/fileselector.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use function Macocci7\FileSelectorPrompt\fileselector;
$path = fileselector('Select a file to import.');
var_dump($path);
▼実行結果
※ファイルパス入力欄が表示されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_02.png)
[↑][↓]キーを押すと、カレントディレクトリ内のファイル一覧が表示されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_03.png)
[↑][↓]で選択肢を選び[TAB]で入力補完されます。
入力文字列がディレクトリの場合は、選択肢がそのディレクトリ内のリストに自動で切り替わります。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_04.png)
入力補完に続けてそのまま入力を続けることができます。
入力文字列に応じて、マッチした選択肢にリアルタイムで切り替わります。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_05.png)
選択肢を選んで[Enter]を押すと入力完了となります。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_06.png)
入力した文字列が返されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_07.png)
プレイスホルダー、デフォルト、必須メッセージ、ヒント
次のように、プレイスホルダー、デフォルト、必須メッセージ、ヒントを設定できます。
$path = fileselector(
label: 'Select a file to import.',
placeholder: 'E.g. ./vendor/autoload.php',
default: '',
required: 'The file path is required.',
hint: 'Input the file path.',
);
▼実行結果
※プレイスホルダー、ヒントが表示されています。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_08.png)
※ 何も入力しないで[Enter]で確定すると叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_09.png)
バリデーション
入力値のバリデーションをコールバックで指定できます。
$path = fileselector(
label: 'Select a file to import.',
placeholder: 'E.g. ./vendor/autoload.php',
hint: 'Input the file path.',
validate: fn (string $value) => match (true) {
!is_readable($value) => 'Cannot read the file.',
default => null,
},
);
この例では、入力した文字列のパスが「存在し且つ読取可能」でない場合に警告表示します。
▼実行結果
※ 何も入力しないで[Enter]で確定すると叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_10.png)
※ 読取権限のないファイルを選択して[Enter]で確定すると叱られます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_11.png)
※ 「存在し且つ読取可能」なファイルは指定できます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_12.png)
ファイル拡張子での絞り込み
選択肢に表示されるファイル一覧をファイル拡張子で絞りこみするには、「extensions」引数に配列でファイル拡張子を指定します。
$path = fileselector(
label: 'Select a file to import.',
placeholder: 'E.g. ./vendor/autoload.php',
hint: 'Input the file path.',
validate: fn (string $value) => match (true) {
!is_readable($value) => 'Cannot read the file.',
default => null,
},
extensions: [
'.json',
'.php',
],
);
※ ディレクトリは全て表示されます。
※ファイルの場合は、「extensions」のいずれかにマッチするものだけが表示されます。
※厳密には、ファイル名が「extensions」のいずれかで終わる物が表示されます。
※「extensions」が空配列の場合はフィルタされません。
▼実行結果
※ 「composer.lock」が表示されていません。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_13.png)
form()
form() で fileselector() を使う場合は、
「Laravel\Prompts\form()」ではなく、
「Macocci7\FileSelectorPrompt\form()」を使います。
「Laravel\Prompts\form()」のラッパーになっているので、text()、textarea()、confirm() 等のLaravel Promptsのメソッドもそのまま使えます。
▼ src/form.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use function Macocci7\FileSelectorPrompt\form;
$responses = form()
->fileselector(
label: 'Select a file to import.',
placeholder: 'E.g. ./vendor/autoload.php',
hint: 'Input the file path.',
validate: fn (string $value) => match (true) {
!is_readable($value) => 'Cannot read the file.',
default => null,
},
extensions: [
'.json',
'.php',
],
name: 'path',
)->submit();
var_dump($responses);
▼実行結果
※ name引数で指定した「path」をキーとした配列で返されます。
![](https://macocci7.net/blog/wp-content/uploads/2024/06/file_selector_prompt_14.png)
以上です。
コメント