SlideShare a Scribd company logo
1 of 47
Download to read offline
FILESYSTEM 
ABSTRACTION 
WITH FLYSYSTEM 
By @FrankDeJonge
WHAT IS FILESYSTEM ABSTRACTION? 
Bridging the gap between filesystems so the storage engine 
becomes an implementation detail.
FILESYSTEM 
ABSTRACTION 101 
What are filesystems? 
How do we use filesystems? 
Moving to remote filesystems. 
Abstraction saves the day.
WHAT IS A FILESYSTEM? 
LOGIC & STRUCTURE
WHAT IS A FILESYSTEM? 
Logic to structure and organize pieces of data in a storage 
system.
WHAT'S IN A FILESYSTEM? 
Files 
Directories 
Metadata
COMMON METADATA 
Location / Path 
Timestamps 
Permissions / Ownership
FILE SPECIFIC 
Mime-type 
File size 
Contents
DERIVED INFORMATION / PATHINFO(): 
Filename 
Basename 
Extension 
Dirname 
Mime-type
FILESYSTEM STRUCTURE
STRUCTURE TYPES: 
Nested or Linear 
 src/ 
 Filesystem.php 
 AdapterInterface.php 
 tests/ 
 Filesystem.php 
 AdapterInterface.php 
 src/Filesystem.php 
 src/AdapterInterface.php 
 tests/Filesystem.php 
 tests/AdapterInterface.php
NESTED FILESYSTEMS 
Are the most common 
Have directories 
which have files and more directories 
... which have more files and directories 
... which have more files and directories. 
We're really used to this structure.
LINEAR FILESYSTEM 
Are like key/value stores 
do have files, of course 
require a different approach 
don't really have directories.
DIRECTORIES ARE A MYTH
WHAT CAN WE DO WITH 
A FILESYSTEM? 
Several operations: 
write 
read 
update 
delete 
move / rename 
copy 
list 
inspect
HANDLING FILESYSTEMS WITH PHP, A WORLD 
OF HURT.
In vanilla PHP for local files: 
$result = file_put_contents($location, $contents); 
$contents = file_get_contents($location); 
unlink($location); 
$timestamp = mtime($location); 
$mimetype = mime_content_type($location); // DEPRECATED 
$mimetype = (new Finfo(FILEINFO_MIME_TYPE))->file($location);
LISTING FILES 
glob($location . '/*'); // ? 
scandir($location); // ? 
readdir(opendir($location)); // ? 
array_map('normalize_fileinfo', 
iterator_to_array(new DirectoryIterator($location))); // ? 
array_map('normalize_fileinfo', 
iterator_to_array(new RecursiveIteratorIterator( 
new RecursiveDirectoryIterator($location) 
))); // ?
Memory problems? Use streams: 
$resource = fopen($rLocation, 'r+'); 
$handle = fopen($location, 'w+'); 
while ( ! feof($resource)) { 
fwrite($handle, fread($resource, 1024), 1024); 
} 
$result = fclose($handle); 
fclose($resource);
WORKING WITH OTHER 
FILESYSTEMS. 
BECAUSE REASONS.
SOME REASONS WHY: 
We want to scale (horizontally). 
... so, we want stateless apps. 
We don't have to serve files ourselves. 
Share files across multiple application. 
We want to enable a nice experience to our users which was 
not possible otherwise.
SOME CONSEQUENCES 
We can't use built-in PHP function anymore. 
We'll have to start using API's 
We'll have to depend on third party code.
COMPARING INTEGRATIONS: WRITING FILES 
AWS/S3: 
$options = [ 
'Body' => $contents, 
'ContentType' => 'plain/text', 
'ContentLength' => mb_strlen($contents), 
]; 
$result = $awsClient->putObject($options); 
Dropbox: 
$result = $dropboxClient->uploadFileFromString( 
$location, 
$contents, 
WriteMode::add());
Rackspace: 
$result = $rackspaceClient->uploadObject($location, $contents); 
WebDAV: 
$result = $client->request('PUT', $location, $contents); 
FTP: 
$stream = tmpfile(); 
fwrite($stream, $contents); 
ftp_fput($connection, $destination, $stream, FTP_BINARY); 
fclose($stream);
DOWNSIDES 
Different API 
Support different kinds of input (string vs stream support) 
Different results for EVERY operation
CHOOSING A FILESYSTEM ... 
can create technical dept 
can create vendor lock-in 
require you to invest in them
SOLUTION? 
USE AN ABSTRACT 
FILESYSTEM.
WHAT IS AN ABSTRACT FILESYSTEM? 
Logic and structure that enable a generalized API to work with 
different filesystems.
WHAT DOES A ABSTRACT FILESYSTEM DO? 
It bridges the gap between different filesystem 
implementations/packages and normalizes responses.
INTRODUCING: 
FLYSYSTEM 
MANY FILESYSTEMS, ONE API
FLYSYSTEM IS: 
A filesystem abstraction layer 
Like a DBAL for files. 
Like illuminate/filesystem on steroids.
PACKED WITH SUPERPOWERS: 
Well tested code 
Easy API 
Cachable metadata 
Easy stream handling
FIRST LOOK AT FLYSYSTEM 
use LeagueFlysystemAdapterLocal; 
$adapter = new Local(__DIR__.'/path/to/dir'); 
$fs = new Filesystem($adapter); 
BASIC FS OPERATIONS: 
$fs->write($location, $contents); 
$fs->read($location); 
$fs->update($location, $contents); 
$fs->listContents($location, $recursive); 
$fs->delete($location); 
$fs->getTimestamp($location);
USING AWS S3 
use LeagueFlysystemAdapterAwsS3; 
$adapter = new AwsS3($client, 'bucket-name'); 
$fs = new Filesystem($adapter); 
BASIC FS OPERATIONS: 
$fs->write($location, $contents); 
$fs->read($location); 
$fs->update($location, $contents); 
$fs->listContents($location, $recursive); 
$fs->delete($location); 
$fs->getTimestamp($location);
USING FTP 
use LeagueFlysystemAdapterFtp; 
$adapter = new Ftp($ftpSettings); 
$fs = new Filesystem($adapter); 
BASIC FS OPERATIONS: 
$fs->write($location, $contents); 
$fs->read($location); 
$fs->update($location, $contents); 
$fs->listContents($location, $recursive); 
$fs->delete($location); 
$fs->getTimestamp($location);
MANY FILESYSTEMS 
ONE API
FLYSYSTEM AND STREAMS: 
$resource = $dropbox->readStream($location); 
$awsS3->writeStream($destination, $resource);
FLYSYSTEM IS A GATEWAY DRUG FOR... 
... DRY code 
... centralized problem domain handling 
... easily testable FS interactions 
... reduced technical dept 
... lower development costs 
... having less to learn.
TESTING FILESYSTEM INTERACTIONS 
USING FLYSYSTEM 
Becomes easier. 
Becomes more reliable. 
Might even become fun again.
DEPENDING ON FLYSYSTEM 
class Author 
{ 
public function __construct(FilesystemInterface $filesystem) 
{ 
$this->filesystem = $filesystem; 
} 
public function getBio() 
{ 
return $this->filesystem->read($this->getBioLocation()); 
} 
}
TESTING THE DEPENDANT CODE 
public function testGetBio() 
{ 
$expected = 'Some Bio'; 
$fsMock = Mockery::mock('LeagueFlysystemFilesystemInterface'); 
$fsMock->shouldReceive('read') 
->with('james-doe-bio.txt') 
->andReturn($expected); 
$autor = new Author($fsMock); 
$this->assertEquals($expected, $author->getBio()); 
} 
BONUS: this makes tests run super fast because you don't have 
to wait for blocking filesystem operations (slow).
IMPROVED STABILITY OF DEPENDANT CODE 
Class should have one reason to change 
... changing filesystems isn't one of them. 
Most code shouldn't have to care where files are stored. 
Flysystem enables classes to be unaware of this.
OPENING UP TO NEW POSSIBILITIES 
Using Dropbox for uploads 
Ease development by deferring filesystem choice. 
Painless pivots during application lifecycle. 
Nice to build a package on.
FLYSYSTEM INTEGRATIONS 
Laravel Integration: 
https://github.com/GrahamCampbell/Laravel-Flysystem 
Symfony Bundle: 
https://github.com/1up-lab/OneupFlysystemBundle 
Backup Manager: 
https://github.com/heybigname/backup-manager 
Cartalyst Media: 
http://cartalyst.com
CRAZY FLYSYSTEM ADAPTERS 
NOTE: some might be urban myths, I haven't seen them all. 
Flickr 
Youtube 
Reddit
THE LEAGUE OF EXTRAORDINARY PACKAGES 
HTTP://GITHUB.COM/THEPHPLEAGUE/FLYSYSTEM
THANKS FOR LISTENING! 
Find me on twitter @FrankDeJonge

More Related Content

What's hot

Padanan huruf kecil dengan huruf besar
Padanan huruf kecil dengan huruf besarPadanan huruf kecil dengan huruf besar
Padanan huruf kecil dengan huruf besar
Princess Honey
 
Perumpamaan
PerumpamaanPerumpamaan
Perumpamaan
guru
 

What's hot (20)

Ayat Pasif
Ayat PasifAyat Pasif
Ayat Pasif
 
CSS For Backend Developers
CSS For Backend DevelopersCSS For Backend Developers
CSS For Backend Developers
 
Padanan huruf kecil dengan huruf besar
Padanan huruf kecil dengan huruf besarPadanan huruf kecil dengan huruf besar
Padanan huruf kecil dengan huruf besar
 
Kata Bantu
Kata BantuKata Bantu
Kata Bantu
 
Teknik penyoalan: Berkelah
Teknik penyoalan: Berkelah Teknik penyoalan: Berkelah
Teknik penyoalan: Berkelah
 
Perumpamaan
PerumpamaanPerumpamaan
Perumpamaan
 
SASS - CSS with Superpower
SASS - CSS with SuperpowerSASS - CSS with Superpower
SASS - CSS with Superpower
 
Bentuk pantun
Bentuk pantunBentuk pantun
Bentuk pantun
 
Latihan Pengayaan Bahasa Malaysia Tahun 1
Latihan Pengayaan Bahasa Malaysia Tahun 1Latihan Pengayaan Bahasa Malaysia Tahun 1
Latihan Pengayaan Bahasa Malaysia Tahun 1
 
Sightly - Part 2
Sightly - Part 2Sightly - Part 2
Sightly - Part 2
 
contoh rph kssr bm tahun 5
contoh rph kssr bm tahun 5contoh rph kssr bm tahun 5
contoh rph kssr bm tahun 5
 
Panduan menjawap bm upsr bm k2
Panduan menjawap bm upsr bm k2Panduan menjawap bm upsr bm k2
Panduan menjawap bm upsr bm k2
 
Cara2 murid cemerlang
Cara2 murid cemerlangCara2 murid cemerlang
Cara2 murid cemerlang
 
Kata Kerja Transitif
Kata Kerja TransitifKata Kerja Transitif
Kata Kerja Transitif
 
Sajak sang gembala kuda
Sajak sang gembala kudaSajak sang gembala kuda
Sajak sang gembala kuda
 
ayat majmuk
ayat majmukayat majmuk
ayat majmuk
 
Bersama memartabatkan Bahasa Melayu
Bersama memartabatkan Bahasa MelayuBersama memartabatkan Bahasa Melayu
Bersama memartabatkan Bahasa Melayu
 
vaakiyam
vaakiyamvaakiyam
vaakiyam
 
Koleksikarangan(murid galus)
Koleksikarangan(murid galus)Koleksikarangan(murid galus)
Koleksikarangan(murid galus)
 
Ciri-ciri peribahasa
Ciri-ciri peribahasaCiri-ciri peribahasa
Ciri-ciri peribahasa
 

Similar to Filesystem Abstraction with Flysystem

On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
Positive Hack Days
 
Building File Systems with FUSE
Building File Systems with FUSEBuilding File Systems with FUSE
Building File Systems with FUSE
elliando dias
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSter
itsmesrl
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
Sebastian Marek
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
Fabien Potencier
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
Alexei Gorobets
 

Similar to Filesystem Abstraction with Flysystem (20)

Filesystems Lisbon 2018
Filesystems Lisbon 2018Filesystems Lisbon 2018
Filesystems Lisbon 2018
 
On secure application of PHP wrappers
On secure application  of PHP wrappersOn secure application  of PHP wrappers
On secure application of PHP wrappers
 
Building File Systems with FUSE
Building File Systems with FUSEBuilding File Systems with FUSE
Building File Systems with FUSE
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSter
 
Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023Filesystem Management with Flysystem - php[tek] 2023
Filesystem Management with Flysystem - php[tek] 2023
 
Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023Filesystem Management with Flysystem at PHP UK 2023
Filesystem Management with Flysystem at PHP UK 2023
 
Writing and using php streams and sockets
Writing and using php streams and socketsWriting and using php streams and sockets
Writing and using php streams and sockets
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
 
vfsStream - a better approach for file system dependent tests
vfsStream - a better approach for file system dependent testsvfsStream - a better approach for file system dependent tests
vfsStream - a better approach for file system dependent tests
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
 
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo EditionLithium: The Framework for People Who Hate Frameworks, Tokyo Edition
Lithium: The Framework for People Who Hate Frameworks, Tokyo Edition
 
Zend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching loggingZend framework 03 - singleton factory data mapper caching logging
Zend framework 03 - singleton factory data mapper caching logging
 
eZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedeZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisited
 
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry PiGrâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
Grâce aux tags Varnish, j'ai switché ma prod sur Raspberry Pi
 
Training on php by cyber security infotech (csi)
Training on  php by cyber security infotech (csi)Training on  php by cyber security infotech (csi)
Training on php by cyber security infotech (csi)
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 
Dependency injection in Drupal 8
Dependency injection in Drupal 8Dependency injection in Drupal 8
Dependency injection in Drupal 8
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Filesystem Abstraction with Flysystem

  • 1. FILESYSTEM ABSTRACTION WITH FLYSYSTEM By @FrankDeJonge
  • 2. WHAT IS FILESYSTEM ABSTRACTION? Bridging the gap between filesystems so the storage engine becomes an implementation detail.
  • 3. FILESYSTEM ABSTRACTION 101 What are filesystems? How do we use filesystems? Moving to remote filesystems. Abstraction saves the day.
  • 4. WHAT IS A FILESYSTEM? LOGIC & STRUCTURE
  • 5. WHAT IS A FILESYSTEM? Logic to structure and organize pieces of data in a storage system.
  • 6. WHAT'S IN A FILESYSTEM? Files Directories Metadata
  • 7. COMMON METADATA Location / Path Timestamps Permissions / Ownership
  • 8. FILE SPECIFIC Mime-type File size Contents
  • 9. DERIVED INFORMATION / PATHINFO(): Filename Basename Extension Dirname Mime-type
  • 11. STRUCTURE TYPES: Nested or Linear  src/  Filesystem.php  AdapterInterface.php  tests/  Filesystem.php  AdapterInterface.php  src/Filesystem.php  src/AdapterInterface.php  tests/Filesystem.php  tests/AdapterInterface.php
  • 12. NESTED FILESYSTEMS Are the most common Have directories which have files and more directories ... which have more files and directories ... which have more files and directories. We're really used to this structure.
  • 13. LINEAR FILESYSTEM Are like key/value stores do have files, of course require a different approach don't really have directories.
  • 15. WHAT CAN WE DO WITH A FILESYSTEM? Several operations: write read update delete move / rename copy list inspect
  • 16. HANDLING FILESYSTEMS WITH PHP, A WORLD OF HURT.
  • 17. In vanilla PHP for local files: $result = file_put_contents($location, $contents); $contents = file_get_contents($location); unlink($location); $timestamp = mtime($location); $mimetype = mime_content_type($location); // DEPRECATED $mimetype = (new Finfo(FILEINFO_MIME_TYPE))->file($location);
  • 18. LISTING FILES glob($location . '/*'); // ? scandir($location); // ? readdir(opendir($location)); // ? array_map('normalize_fileinfo', iterator_to_array(new DirectoryIterator($location))); // ? array_map('normalize_fileinfo', iterator_to_array(new RecursiveIteratorIterator( new RecursiveDirectoryIterator($location) ))); // ?
  • 19. Memory problems? Use streams: $resource = fopen($rLocation, 'r+'); $handle = fopen($location, 'w+'); while ( ! feof($resource)) { fwrite($handle, fread($resource, 1024), 1024); } $result = fclose($handle); fclose($resource);
  • 20. WORKING WITH OTHER FILESYSTEMS. BECAUSE REASONS.
  • 21. SOME REASONS WHY: We want to scale (horizontally). ... so, we want stateless apps. We don't have to serve files ourselves. Share files across multiple application. We want to enable a nice experience to our users which was not possible otherwise.
  • 22. SOME CONSEQUENCES We can't use built-in PHP function anymore. We'll have to start using API's We'll have to depend on third party code.
  • 23. COMPARING INTEGRATIONS: WRITING FILES AWS/S3: $options = [ 'Body' => $contents, 'ContentType' => 'plain/text', 'ContentLength' => mb_strlen($contents), ]; $result = $awsClient->putObject($options); Dropbox: $result = $dropboxClient->uploadFileFromString( $location, $contents, WriteMode::add());
  • 24. Rackspace: $result = $rackspaceClient->uploadObject($location, $contents); WebDAV: $result = $client->request('PUT', $location, $contents); FTP: $stream = tmpfile(); fwrite($stream, $contents); ftp_fput($connection, $destination, $stream, FTP_BINARY); fclose($stream);
  • 25. DOWNSIDES Different API Support different kinds of input (string vs stream support) Different results for EVERY operation
  • 26. CHOOSING A FILESYSTEM ... can create technical dept can create vendor lock-in require you to invest in them
  • 27. SOLUTION? USE AN ABSTRACT FILESYSTEM.
  • 28. WHAT IS AN ABSTRACT FILESYSTEM? Logic and structure that enable a generalized API to work with different filesystems.
  • 29. WHAT DOES A ABSTRACT FILESYSTEM DO? It bridges the gap between different filesystem implementations/packages and normalizes responses.
  • 30. INTRODUCING: FLYSYSTEM MANY FILESYSTEMS, ONE API
  • 31. FLYSYSTEM IS: A filesystem abstraction layer Like a DBAL for files. Like illuminate/filesystem on steroids.
  • 32. PACKED WITH SUPERPOWERS: Well tested code Easy API Cachable metadata Easy stream handling
  • 33. FIRST LOOK AT FLYSYSTEM use LeagueFlysystemAdapterLocal; $adapter = new Local(__DIR__.'/path/to/dir'); $fs = new Filesystem($adapter); BASIC FS OPERATIONS: $fs->write($location, $contents); $fs->read($location); $fs->update($location, $contents); $fs->listContents($location, $recursive); $fs->delete($location); $fs->getTimestamp($location);
  • 34. USING AWS S3 use LeagueFlysystemAdapterAwsS3; $adapter = new AwsS3($client, 'bucket-name'); $fs = new Filesystem($adapter); BASIC FS OPERATIONS: $fs->write($location, $contents); $fs->read($location); $fs->update($location, $contents); $fs->listContents($location, $recursive); $fs->delete($location); $fs->getTimestamp($location);
  • 35. USING FTP use LeagueFlysystemAdapterFtp; $adapter = new Ftp($ftpSettings); $fs = new Filesystem($adapter); BASIC FS OPERATIONS: $fs->write($location, $contents); $fs->read($location); $fs->update($location, $contents); $fs->listContents($location, $recursive); $fs->delete($location); $fs->getTimestamp($location);
  • 37. FLYSYSTEM AND STREAMS: $resource = $dropbox->readStream($location); $awsS3->writeStream($destination, $resource);
  • 38. FLYSYSTEM IS A GATEWAY DRUG FOR... ... DRY code ... centralized problem domain handling ... easily testable FS interactions ... reduced technical dept ... lower development costs ... having less to learn.
  • 39. TESTING FILESYSTEM INTERACTIONS USING FLYSYSTEM Becomes easier. Becomes more reliable. Might even become fun again.
  • 40. DEPENDING ON FLYSYSTEM class Author { public function __construct(FilesystemInterface $filesystem) { $this->filesystem = $filesystem; } public function getBio() { return $this->filesystem->read($this->getBioLocation()); } }
  • 41. TESTING THE DEPENDANT CODE public function testGetBio() { $expected = 'Some Bio'; $fsMock = Mockery::mock('LeagueFlysystemFilesystemInterface'); $fsMock->shouldReceive('read') ->with('james-doe-bio.txt') ->andReturn($expected); $autor = new Author($fsMock); $this->assertEquals($expected, $author->getBio()); } BONUS: this makes tests run super fast because you don't have to wait for blocking filesystem operations (slow).
  • 42. IMPROVED STABILITY OF DEPENDANT CODE Class should have one reason to change ... changing filesystems isn't one of them. Most code shouldn't have to care where files are stored. Flysystem enables classes to be unaware of this.
  • 43. OPENING UP TO NEW POSSIBILITIES Using Dropbox for uploads Ease development by deferring filesystem choice. Painless pivots during application lifecycle. Nice to build a package on.
  • 44. FLYSYSTEM INTEGRATIONS Laravel Integration: https://github.com/GrahamCampbell/Laravel-Flysystem Symfony Bundle: https://github.com/1up-lab/OneupFlysystemBundle Backup Manager: https://github.com/heybigname/backup-manager Cartalyst Media: http://cartalyst.com
  • 45. CRAZY FLYSYSTEM ADAPTERS NOTE: some might be urban myths, I haven't seen them all. Flickr Youtube Reddit
  • 46. THE LEAGUE OF EXTRAORDINARY PACKAGES HTTP://GITHUB.COM/THEPHPLEAGUE/FLYSYSTEM
  • 47. THANKS FOR LISTENING! Find me on twitter @FrankDeJonge