Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Make FilesystemAdapter macroable #38030

Merged
merged 1 commit into from
Jul 16, 2021
Merged

[8.x] Make FilesystemAdapter macroable #38030

merged 1 commit into from
Jul 16, 2021

Conversation

harrygulliford
Copy link
Contributor

@harrygulliford harrygulliford commented Jul 16, 2021

This PR makes the \Illuminate\Filesystem\FilesystemAdapter class macroable. This class is used by the Storage facade.

A simple use-case I have encountered is wanting a clean way to return the root path of a particular storage disk.

Storage::macro('rootPath', function () {
    return $this->path('');
});

// ...

Storage::rootPath();

A slightly more complex use-case is a function to move files from one disk to another. This would be especially useful on Vapor, moving files from temporary storage (such as a dynamically generated spreadsheet or invoice) to a permanent s3 bucket (for a user to download).

Storage::macro('moveToDisk', function (string $path, string $disk = null) {
    Storage::disk($disk)->put($path, $this->get($path));
    $this->delete($path);
});

// ...

Spreadsheet::create(Storage::path('spreadsheet.xlsx'))
    ->addRow([ /* ... */ ]);

Storage::moveToDisk('spreadsheet.xlsx', 's3');

@taylorotwell taylorotwell merged commit 943ed61 into laravel:8.x Jul 16, 2021
@harrygulliford harrygulliford deleted the macroable-filesystem-adapter branch July 20, 2021 11:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants