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

Filesystem Extender and Tests #2732

Merged
merged 28 commits into from
Apr 19, 2021
Merged

Filesystem Extender and Tests #2732

merged 28 commits into from
Apr 19, 2021

Conversation

askvortsov1
Copy link
Member

Followup to #2729
Part of #1783
Part of flarum/issue-archive#121

Changes proposed in this pull request:

  • Use our own subclass of Laravel's filesystem manager, which gives us control over disk resolution, and most important, creating drivers for disks.
  • Add a filesystem extender that allows:
    • Declaring a new disk (see docblock for explanation)
    • Registering a filesystem driver

Currently, drivers for disks are determined via the following process:

  1. If a disk_driver.${DISK_NAME} setting key exists, proceed to step 2. Else return 'local'
  2. If a driver with the name returned by step 1 exists, use that. Else, use 'local'

This gives us safety in case the extension providing the filesystem driver being used is removed.

This PR does not attempt to introduce a UI for:

  • Selecting drivers for disks
  • Selecting settings for disk drivers

We might want to eventually add that though.

Reviewers should focus on:
Do we want to use config.php or settings? Right now, this PR selects the driver for each disk from settings. It passes both settings and config to drivers, so they can choose which they prefer. Should we also support setting drivers for disks in config?

I also put together a proof of concept extension for Azure at https://github.com/askvortsov1/flarum-azure-poc.

Confirmed

  • Frontend changes: tested on a local Flarum installation.
  • Backend changes: tests are green (run composer test).

@tankerkiller125
Copy link
Contributor

I think I'd prefer config.php, however I may have a bias towards that since Laravel uses a filesystem.php config file. However if a proper UI can be introduced I think that could work just as well, especially given that Flarum can't operate without a database anyways.

@askvortsov1
Copy link
Member Author

I think I'd prefer config.php, however I may have a bias towards that since Laravel uses a filesystem.php config file. However if a proper UI can be introduced I think that could work just as well, especially given that Flarum can't operate without a database anyways.

For mapping disks to drivers, we could support both: first check settings, then config.php, and if none have a value, default to local?

@tankerkiller125
Copy link
Contributor

That works too

Copy link
Member

@luceos luceos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we are extending the Laravel filesystem manager only to return a Flysystem Filesystem afterwards (see DriverInterface).

Other than that experience will tell whether this helps and callables might be hard to use as they don't have type hinting, but that was pointed out by @clarkwinkelmann in the past as well.

$config = $this->app->make(Config::class);
$settings = $this->app->make(SettingsRepositoryInterface::class);

$key = "disk_driver.$name";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike disk_driver it strokes against the Laravel naming convention and the manager elsewhere uses filesystems too. Can we use filesystem or the plural version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Laravel config approach, there is an array of "disks", with each "disk" having a "driver" field. In this particular place, that is not inaccurate.

We could replace the 'flarum.filesystem.drivers' binding key with 'flarum.filesystem.filesystems'?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree that disk_driver is more appropriate here.

@askvortsov1
Copy link
Member Author

I don't understand why we are extending the Laravel filesystem manager only to return a Flysystem Filesystem afterwards (see DriverInterface).

We are extending (and overriding) the filesystem manager so that we have control over:

  • How new disks are declared
  • How disks are mapped to drivers
  • How available drivers are registered
  • How config for drivers is obtained

That being said, I think you have a good point that it's a bit odd that the output is a Flysystem interface. I did this because the vast majority of drivers will probably use flysystem so we might as well wrap in FilesystemAdapter for them, but that's not too clean. We could probably require the output to match the laravel Cloud interface.

@askvortsov1 askvortsov1 requested a review from luceos April 9, 2021 12:32
Copy link
Member

@SychO9 SychO9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good!

Just have a few minor nitpicks.

src/Filesystem/FilesystemManager.php Outdated Show resolved Hide resolved
src/Extend/Filesystem.php Outdated Show resolved Hide resolved
src/Extend/Filesystem.php Outdated Show resolved Hide resolved
src/Filesystem/FilesystemManager.php Outdated Show resolved Hide resolved
src/Filesystem/FilesystemManager.php Outdated Show resolved Hide resolved
src/Filesystem/DriverInterface.php Outdated Show resolved Hide resolved
@askvortsov1 askvortsov1 requested a review from SychO9 April 15, 2021 02:03
src/Extend/Filesystem.php Outdated Show resolved Hide resolved
@askvortsov1 askvortsov1 requested a review from SychO9 April 16, 2021 12:45
@askvortsov1 askvortsov1 requested a review from SychO9 April 18, 2021 22:12
Base automatically changed from as/use-laravel-filesystem to master April 19, 2021 19:11
@askvortsov1 askvortsov1 merged commit c84939b into master Apr 19, 2021
@askvortsov1 askvortsov1 deleted the as/filesystem-extenders branch April 19, 2021 20:25
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.

4 participants