You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using Flysystem and so far so good. I want to create an adapter with a "custom" method that will perform a few actions, and yes, I can do exactly the same with the available functions from Flysystem but I would like to encapsulate all of that code into a function. This is how my custom Adapter class looks like:
class LocalAdapter extends LocalFilesystemAdapter {
public function MyCustomFunction() {
// do something here
}
}
then I initialize Flysystem using this LocalAdapter:
$adapter = new LocalAdapter(__DIR__);
$filesystem = new Filesystem($adapter);
but from there I cannot access $filesystem->MyCustomFunction() because the method is not callable:
Fatal error: Uncaught Error: Call to undefined method League\Flysystem\Filesystem::MyCustomFunction()
What I am missing here?
Q
A
Flysystem Version
3.28.0
The text was updated successfully, but these errors were encountered:
The filesystem abstracts the implementation using the strategy pattern.
If the function you're implementing could work on any adapter it's probably cleaner to implement it on the filesystem instead of the adapter.
Personally I'd prefer to use composition instead of extending library classes.
This can be done using functions or classes:
functionmySpecialFileSystemOperation(FilesystemWriter$filesystem) {
// Do your stuff here.
}
classSpecialFileSystemService {
publicfunction__construct(privateFilesystemWriter$filesystem) {
}
publicfunctiondoSpecialThingWithPath(string$path) {
// Your special stuff here
}
}
These patterns will keep your logic more separated from underlying library code.
If you prefer not to have many dependencies in your consuming code (ie you only want to pass 1 filesystem object and not an additional service object for your functionality) I'd use the decorator pattern instead of extending.
classMyFilesystemWithExtendedFunctionalityimplementsFileSystemOperator {
publicfunction__construct(private readonly FileSystemOperator$filesystem) {
}
// For each function in the FileSystemOperator interface create a forwarding function. public function publicfunctionfileExists(string$location): bool {
return$this->filesystem->fileExists($location);
}
publicfunctionmySpecialFunction() {
// Your magic here
}
}
While this is more work at first it is often cleaner to compose than to extend third party library code.
Question
I am using Flysystem and so far so good. I want to create an adapter with a "custom" method that will perform a few actions, and yes, I can do exactly the same with the available functions from Flysystem but I would like to encapsulate all of that code into a function. This is how my custom Adapter class looks like:
then I initialize Flysystem using this LocalAdapter:
but from there I cannot access
$filesystem->MyCustomFunction()
because the method is not callable:What I am missing here?
The text was updated successfully, but these errors were encountered: