Readouble

Laravel 5.0 ファイルシステム/クラウドストレージ

イントロダクションIntroduction

LaravelはFrank de Jongeが作成した、ありがたいほど素晴らしい抽象ファイルシステムであるFlysystem PHPパッケージを提供しています。LaravelとFlysystemの統合により、ローカルのファイルシステム、Amazon S3、Rackspaceクラウドストレージを操作できる、シンプルなドライバーが提供できました。更に素晴らしいことに、それぞれのシステムに対し同じAPIを使用しているため、ストレージをとても簡単に変更できるのです!Laravel provides a wonderful filesystem abstraction thanks to the Flysystem[https://github.com/thephpleague/flysystem] PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple to use drivers for working with local filesystems, Amazon S3, and Rackspace Cloud Storage. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system!

設定Configuration

ファイルシステムの設定ファイルは、config/filesystems.phpです。このファイルの中に全「ディスク」の設定があります。それぞれのディスクは、特定のストレージドライバーと保存場所を表します。設定ファイルには、サーポート済みのドライバーそれぞれの設定例を用意しています。ですから、ストレージの設定と認証情報を反映するように、設定オプションを簡単に修正できます!The filesystem configuration file is located at config/filesystems.php. Within this file you may configure all of your "disks". Each disk represents a particular storage driver and storage location. Example configurations for each supported driver is included in the configuration file. So, simply modify the configuration to reflect your storage preferences and credentials!

S3とRackspaceドライバーを使用する場合は、それに適合するパッケージをComposerでインストールする必要があります。Before using the S3 or Rackspace drivers, you will need to install the appropriate package via Composer:

  • Amazon S3: league/flysystem-aws-s3-v2 ~1.0Amazon S3: league/flysystem-aws-s3-v2 ~1.0
  • Rackspace: league/flysystem-rackspace ~1.0Rackspace: league/flysystem-rackspace ~1.0

もちろん、好きなだけディスクを設定できますし、同じドライバーに対し複数のディスクを持つことも可能です。Of course, you may configure as many disks as you like, and may even have multiple disks that use the same driver.

localドライバーを使用する場合、設定ファイルで定義されている、rootディレクトリーからの相対位置として、全ファイルは操作されます。デフォルトでは、この値はstorage/appディレクトリーになっています。ですから、次のコードはstorage/app/file.txtへファイルを保存します。When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:

Storage::disk('local')->put('file.txt', 'Contents');

基本的な使用法Basic Usage

Storageファサードを使い、設定済みのディスクへの操作ができます。サービスコンテナにより依存解決が行われるクラスで、Illuminate\Contracts\Filesystem\Factory契約をタイプヒントで指定することもできます。The Storage facade may be used to interact with any of your configured disks. Alternatively, you may type-hint the Illuminate\Contracts\Filesystem\Factory contract on any class that is resolved via the Laravel service container[/docs/{{version}}/container].

特定のディスクの取得Retrieving A Particular Disk

$disk = Storage::disk('s3');

$disk = Storage::disk('local');

ファイルの存在チェックDetermining If A File Exists

$exists = Storage::disk('s3')->exists('file.jpg');

デフォルトディスクのメソッド呼び出しCalling Methods On The Default Disk

if (Storage::exists('file.jpg'))
{
	//
}

ファイルの内容の取得Retrieving A File's Contents

$contents = Storage::get('file.jpg');

ファイルへ内容を保存Setting A File's Contents

Storage::put('file.jpg', $contents);

ファイルの先頭に追加Prepend To A File

Storage::prepend('file.log', 'Prepended Text');

ファイルの最後に追加Append To A File

Storage::append('file.log', 'Appended Text');

ファイル削除Delete A File

Storage::delete('file.jpg');

Storage::delete(['file1.jpg', 'file2.jpg']);

ファイルを新しい場所へコピーCopy A File To A New Location

Storage::copy('old/file1.jpg', 'new/file1.jpg');

ファイルを新しい場所へ移動Move A File To A New Location

Storage::move('old/file1.jpg', 'new/file1.jpg');

ファイルサイズの取得Get File Size

$size = Storage::size('file1.jpg');

最後の修正時間の取得(Unix)Get The Last Modification Time (UNIX)

$time = Storage::lastModified('file1.jpg');

ディレクトリー中の全ファイル取得Get All Files Within A Directory

$files = Storage::files($directory);

// 再帰的…
$files = Storage::allFiles($directory);

ディレクトリー中の全ディレクトリー取得Get All Directories Within A Directory

$directories = Storage::directories($directory);

// Recursive...
$directories = Storage::allDirectories($directory);

ディレクトリー作成Create A Directory

Storage::makeDirectory($directory);

ディレクトリー削除Delete A Directory

Storage::deleteDirectory($directory);

カスタムファイルシステムCustom Filesystems

LaravelのFlysystem統合には、最初から様々な「ドライバー」が含まれています。しかし、Flysystemはこれらのドライバーに限定されず、他の保存領域システムにも適用できます。皆さんのLaravelアプリケーションに適合した保存システムのカスタムドライバーを作成することができます。ご心配なく、そんなに難しくありません!Laravel's Flysystem integration provides drivers for several "drivers" out of the box; however, Flysystem is not limited to these and has adapters for many other storage systems. You can create a custom driver if you want to use one of these additional adapters in your Laravel application. Don't worry, it's not too hard!

例えばDropboxFilesystemServiceProviderのような、作成するカスタムファイルシステムのために、サービスプロバイダーを用意してください。プロバイダーのbootメソッドの中へ、Illuminate\Contracts\Filesystem\Factory契約のインスタンスを注入し、extendメソッドを呼び出します。もしくは、Diskファサードのextendメソッドを使うこともできます。In order to set up the custom filesystem you will need to create a service provider such as DropboxFilesystemServiceProvider. In the provider's boot method, you can inject an instance of the Illuminate\Contracts\Filesystem\Factory contract and call the extend method of the injected instance. Alternatively, you may use the Disk facade's extend method.

extendメソッドの最初の引数はドライバーの名前で、2つ目は$app$config変数を受け取るクロージャーです。このリゾルバ−クロージャーは、League\Flysystem\Filesystemのインスタンスを返す必要があります。The first argument of the extend method is the name of the driver and the second is a Closure that receives the $app and $config variables. The resolver Closure must return an instance of League\Flysystem\Filesystem.

注意: $config変数は、config/filesystems.phpの中で定義されている、指定されたディスクの値を含んでいます。Note: The $config variable will already contain the values defined in config/filesystems.php for the specified disk.

Dropbox準備例Dropbox Example

<?php namespace App\Providers;

use Storage;
use League\Flysystem\Filesystem;
use Dropbox\Client as DropboxClient;
use League\Flysystem\Dropbox\DropboxAdapter;
use Illuminate\Support\ServiceProvider;

class DropboxFilesystemServiceProvider extends ServiceProvider {

	public function boot()
	{
		Storage::extend('dropbox', function($app, $config)
		{
			$client = new DropboxClient($config['accessToken'], $config['clientIdentifier']);

			return new Filesystem(new DropboxAdapter($client));
		});
	}

	public function register()
	{
		//
	}

}

章選択

Artisan CLI

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作