SlideShare a Scribd company logo
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

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Andres Baravalle
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practical
Nitesh Dubey
 
Module 1 introduction to Linux
Module 1 introduction to LinuxModule 1 introduction to Linux
Module 1 introduction to Linux
Tushar B Kute
 
Html
HtmlHtml
Unit ii java script and xhtml documents and dynamic documents with javascript
Unit ii java script and xhtml documents and dynamic documents with javascriptUnit ii java script and xhtml documents and dynamic documents with javascript
Unit ii java script and xhtml documents and dynamic documents with javascript
zahid7578
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
Sergi Martínez
 
Css selectors
Css selectorsCss selectors
Css selectors
Parth Trivedi
 
Xml parsing
Xml parsingXml parsing
Xml parsing
Malintha Adikari
 
Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...
Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...
Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...
Gluster.org
 
Json
JsonJson
Flask – Python
Flask – PythonFlask – Python
Flask – Python
Max Claus Nunes
 
WEKA: The Knowledge Flow Interface
WEKA: The Knowledge Flow InterfaceWEKA: The Knowledge Flow Interface
WEKA: The Knowledge Flow Interface
DataminingTools Inc
 
So You Want to Write an Exporter
So You Want to Write an ExporterSo You Want to Write an Exporter
So You Want to Write an Exporter
Brian Brazil
 
HTML5 introduction for beginners
HTML5 introduction for beginnersHTML5 introduction for beginners
HTML5 introduction for beginners
Vineeth N Krishnan
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Presentation of bootstrap
Presentation of bootstrapPresentation of bootstrap
Presentation of bootstrap
1amitgupta
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Taha Malampatti
 
Html introduction
Html introductionHtml introduction
Html introduction
RanjithaM32
 
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Django Tutorial | Django Web Development With Python | Django Training and Ce...Django Tutorial | Django Web Development With Python | Django Training and Ce...
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Edureka!
 
Introduction to css & its attributes with syntax
Introduction to css & its attributes with syntaxIntroduction to css & its attributes with syntax
Introduction to css & its attributes with syntax
priyadharshini murugan
 

What's hot (20)

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practical
 
Module 1 introduction to Linux
Module 1 introduction to LinuxModule 1 introduction to Linux
Module 1 introduction to Linux
 
Html
HtmlHtml
Html
 
Unit ii java script and xhtml documents and dynamic documents with javascript
Unit ii java script and xhtml documents and dynamic documents with javascriptUnit ii java script and xhtml documents and dynamic documents with javascript
Unit ii java script and xhtml documents and dynamic documents with javascript
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 
Css selectors
Css selectorsCss selectors
Css selectors
 
Xml parsing
Xml parsingXml parsing
Xml parsing
 
Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...
Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...
Deploying pNFS over Distributed File Storage w/ Jiffin Tony Thottan and Niels...
 
Json
JsonJson
Json
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 
WEKA: The Knowledge Flow Interface
WEKA: The Knowledge Flow InterfaceWEKA: The Knowledge Flow Interface
WEKA: The Knowledge Flow Interface
 
So You Want to Write an Exporter
So You Want to Write an ExporterSo You Want to Write an Exporter
So You Want to Write an Exporter
 
HTML5 introduction for beginners
HTML5 introduction for beginnersHTML5 introduction for beginners
HTML5 introduction for beginners
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Presentation of bootstrap
Presentation of bootstrapPresentation of bootstrap
Presentation of bootstrap
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Html introduction
Html introductionHtml introduction
Html introduction
 
Django Tutorial | Django Web Development With Python | Django Training and Ce...
Django Tutorial | Django Web Development With Python | Django Training and Ce...Django Tutorial | Django Web Development With Python | Django Training and Ce...
Django Tutorial | Django Web Development With Python | Django Training and Ce...
 
Introduction to css & its attributes with syntax
Introduction to css & its attributes with syntaxIntroduction to css & its attributes with syntax
Introduction to css & its attributes with syntax
 

Similar to Filesystem Abstraction with Flysystem

Filesystems Lisbon 2018
Filesystems Lisbon 2018Filesystems Lisbon 2018
Filesystems Lisbon 2018
Frank de Jonge
 
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
 
eZ Publish Cluster Unleashed
eZ Publish Cluster UnleashedeZ Publish Cluster Unleashed
eZ Publish Cluster Unleashed
Bertrand Dunogier
 
PHP 5.3
PHP 5.3PHP 5.3
PHP 5.3
Idaf_1er
 
Introducing FSter
Introducing FSterIntroducing FSter
Introducing FSter
itsmesrl
 
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
Mark Niebergall
 
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
Mark Niebergall
 
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
Elizabeth Smith
 
vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking vfsStream - effective filesystem mocking
vfsStream - effective filesystem mocking
Sebastian Marek
 
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
Frank Kleine
 
Dependency injection-zendcon-2010
Dependency injection-zendcon-2010Dependency injection-zendcon-2010
Dependency injection-zendcon-2010
Fabien Potencier
 
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
Nate Abele
 
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
Tricode (part of Dept)
 
eZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedeZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisited
Bertrand Dunogier
 
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
Jérémy Derussé
 
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)
Cyber Security Infotech Pvt. Ltd.
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
Fabien Potencier
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 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

Camunda Chapter NY Meetup July 2024.pptx
Camunda Chapter NY Meetup July 2024.pptxCamunda Chapter NY Meetup July 2024.pptx
Camunda Chapter NY Meetup July 2024.pptx
ZachWylie3
 
UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...
UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...
UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...
FIDO Alliance
 
UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...
UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...
UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...
FIDO Alliance
 
Semantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software DevelopmentSemantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software Development
Baishakhi Ray
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
Google Developer Group - Harare
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
alexjohnson7307
 
LeadMagnet IQ Review: Unlock the Secret to Effortless Traffic and Leads.pdf
LeadMagnet IQ Review:  Unlock the Secret to Effortless Traffic and Leads.pdfLeadMagnet IQ Review:  Unlock the Secret to Effortless Traffic and Leads.pdf
LeadMagnet IQ Review: Unlock the Secret to Effortless Traffic and Leads.pdf
SelfMade bd
 
Patch Tuesday de julio
Patch Tuesday de julioPatch Tuesday de julio
Patch Tuesday de julio
Ivanti
 
Improving Learning Content Efficiency with Reusable Learning Content
Improving Learning Content Efficiency with Reusable Learning ContentImproving Learning Content Efficiency with Reusable Learning Content
Improving Learning Content Efficiency with Reusable Learning Content
Enterprise Knowledge
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
SynapseIndia
 
Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...
Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...
Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...
shanihomely
 
Sonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdfSonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdf
SubhamMandal40
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
ldtexsolbl
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
siddu769252
 
Integrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecaseIntegrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecase
shyamraj55
 
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
alexjohnson7307
 
It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...
Zilliz
 
Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10
ankush9927
 
The Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - CoatueThe Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - Coatue
Razin Mustafiz
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
SAI KAILASH R
 

Recently uploaded (20)

Camunda Chapter NY Meetup July 2024.pptx
Camunda Chapter NY Meetup July 2024.pptxCamunda Chapter NY Meetup July 2024.pptx
Camunda Chapter NY Meetup July 2024.pptx
 
UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...
UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...
UX Webinar Series: Essentials for Adopting Passkeys as the Foundation of your...
 
UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...
UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...
UX Webinar Series: Drive Revenue and Decrease Costs with Passkeys for Consume...
 
Semantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software DevelopmentSemantic-Aware Code Model: Elevating the Future of Software Development
Semantic-Aware Code Model: Elevating the Future of Software Development
 
Google I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged SlidesGoogle I/O Extended Harare Merged Slides
Google I/O Extended Harare Merged Slides
 
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
leewayhertz.com-AI agents for healthcare Applications benefits and implementa...
 
LeadMagnet IQ Review: Unlock the Secret to Effortless Traffic and Leads.pdf
LeadMagnet IQ Review:  Unlock the Secret to Effortless Traffic and Leads.pdfLeadMagnet IQ Review:  Unlock the Secret to Effortless Traffic and Leads.pdf
LeadMagnet IQ Review: Unlock the Secret to Effortless Traffic and Leads.pdf
 
Patch Tuesday de julio
Patch Tuesday de julioPatch Tuesday de julio
Patch Tuesday de julio
 
Improving Learning Content Efficiency with Reusable Learning Content
Improving Learning Content Efficiency with Reusable Learning ContentImproving Learning Content Efficiency with Reusable Learning Content
Improving Learning Content Efficiency with Reusable Learning Content
 
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptxUse Cases & Benefits of RPA in Manufacturing in 2024.pptx
Use Cases & Benefits of RPA in Manufacturing in 2024.pptx
 
Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...
Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...
Premium Girls Call Mumbai 9920725232 Unlimited Short Providing Girls Service ...
 
Sonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdfSonkoloniya documentation - ONEprojukti.pdf
Sonkoloniya documentation - ONEprojukti.pdf
 
Types of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technologyTypes of Weaving loom machine & it's technology
Types of Weaving loom machine & it's technology
 
Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024Generative AI Reasoning Tech Talk - July 2024
Generative AI Reasoning Tech Talk - July 2024
 
Integrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecaseIntegrating Kafka with MuleSoft 4 and usecase
Integrating Kafka with MuleSoft 4 and usecase
 
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
leewayhertz.com-Generative AI tech stack Frameworks infrastructure models and...
 
It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...It's your unstructured data: How to get your GenAI app to production (and spe...
It's your unstructured data: How to get your GenAI app to production (and spe...
 
Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10Computer HARDWARE presenattion by CWD students class 10
Computer HARDWARE presenattion by CWD students class 10
 
The Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - CoatueThe Path to General-Purpose Robots - Coatue
The Path to General-Purpose Robots - Coatue
 
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and DisadvantagesBLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
BLOCKCHAIN TECHNOLOGY - Advantages and Disadvantages
 

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