22
33- [ Installation] ( #installation )
44- [ Basic usage] ( #basic-usage )
5- - [ Defining multiple filesystems] ( #defining-multiple-filesystems )
5+ - [ Using multiple storages to improve readability] ( #using-multiple-storages-to-improve-readability )
6+ - [ Using memory storage in tests] ( #using-memory-storage-in-tests )
67
78## Installation
89
@@ -11,7 +12,7 @@ flysystem-bundle requires PHP 7.1+ and Symfony 4.2+.
1112You can install the bundle using Symfony Flex:
1213
1314```
14- composer require thephpleague /flysystem-bundle
15+ composer require league /flysystem-bundle
1516```
1617
1718## Basic usage
@@ -23,90 +24,136 @@ use Flysystem in your application as soon as you install the bundle:
2324# config/packages/flysystem.yaml
2425
2526flysystem :
26- default_filesystem : ' default.storage'
27- filesystems :
27+ storages :
2828 default.storage :
29- adapter : ' flysystem.adapter.local'
29+ adapter : ' local'
30+ options :
31+ directory : ' %kernel.project_dir%/storage'
3032` ` `
3133
32- This configuration creates a single filesystem service, configured using the local adapter in the ` storge` directory,
33- and provides this service for autowiring using the `League\Flysystem\FilesystemInterface` interface.
34- This autowiring will target the default filesystem defined in the bundle configuration.
35-
36- This means that if you are using autowiring, you can typehint `League\Flysystem\FilesystemInterface` in any
37- of your services to get the default filesystem :
34+ This configuration defines a single storage service (` default.storage`) based on the local adapter
35+ and configured to use the `%kernel.project_dir%/storage` directory.
3836
39- ` ` ` php
40- use League\F lysystem\F ilesystemInterface;
37+ For each storage defined under `flysystem.storages`, an associated service is created using the
38+ name you provide (in this case, a service `default.storage` will be created). The bundle also
39+ creates a named alias for each of these services.
4140
42- class MyService
43- {
44- private $storage;
41+ This means you have two way of using the defined storages :
42+
43+ * either using autowiring, by typehinting against the `FilesystemInterface` and using the
44+ variable name matching one of your storages :
45+
46+ ` ` ` php
47+ use League\F lysystem\F ilesystemInterface;
4548
46- public function __construct(FilesystemInterface $storage)
49+ class MyService
4750 {
48- $this->storage = $storage;
51+ private $storage;
52+
53+ // The variable name $defaultStorage matters: it needs to be the camelized version
54+ // of the name of your storage.
55+ public function __construct(FilesystemInterface $defaultStorage)
56+ {
57+ $this->storage = $defaultStorage;
58+ }
59+
60+ // ...
4961 }
62+ ` ` `
5063
51- // ...
52- }
53- ` ` `
64+ The same goes for controllers :
65+
66+ ` ` ` php
67+ use League\F lysystem\F ilesystemInterface;
68+
69+ class MyController
70+ {
71+ // The variable name $defaultStorage matters: it needs to be the camelized version
72+ // of the name of your storage.
73+ public function index(FilesystemInterface $defaultStorage)
74+ {
75+ // ...
76+ }
77+ }
78+ ` ` `
79+
80+ * or using manual injection, by injecting the service named `default.storage` inside
81+ your services.
82+
83+
84+ # # Using multiple storages to improve readability
85+
86+ While using the default storage can be enough, it is usually recommended to create multiple
87+ storages, even if behind the scene you may rely on the same adapter.
88+
89+ The reason for this is the added readability this provides to your project code : by naming
90+ your storages using their **intents**, your will naturally increase the readability of your
91+ autowired arguments. For example :
92+
93+ ` ` ` yaml
94+ # config/packages/flysystem.yaml
5495
55- The same goes for controllers :
96+ flysystem:
97+ storages:
98+ users.storage:
99+ adapter: 'local'
100+ options:
101+ directory: '%kernel.project_dir%/storage/users'
102+
103+ projects.storage:
104+ adapter: 'local'
105+ options:
106+ directory: '%kernel.project_dir%/storage/projects'
107+ ` ` `
56108
57109` ` ` php
58110use League\F lysystem\F ilesystemInterface;
59111
60- class MyController
112+ class MyService
61113{
62- public function index(FilesystemInterface $storage)
114+ private $usersStorage;
115+ private $projectsStorage;
116+
117+ public function __construct(FilesystemInterface $usersStorage, FilesystemInterface $projectsStorage)
63118 {
64- // ...
119+ $this->usersStorage = $usersStorage;
120+ $this->projectsStorage = $projectsStorage;
65121 }
122+
123+ // ...
66124}
67125` ` `
68126
69- If you are not using autowiring, you can inject the `flysystem` service into your services
70- manually to get the default filesystem.
71127
72- # # Defining multiple filesystems
128+ # # Using memory storage in tests
73129
74- Using a single filesystem is good way to get up and running quickly, but it is often useful to
75- create multiple instances of Flysystem in order to manage different filesystems .
130+ One of the best reason to use a filesystem abstraction in your project is the ability
131+ it gives you to swap the actual implementation during tests .
76132
77- This bundle provides this ability using `named aliases` : by leveraging the variable name in addition to
78- the interface name, autowiring is able to inject the proper filesystem you want in your services .
133+ More specifically, it can be useful to swap from a persisted storage to a memory one during
134+ tests, both to ensure the state is reset between tests and to increase tests speed .
79135
80- This means that if you are using autowiring, you can create multiple filesystems in the configuration of the
81- bundle and typehint `League\Flysystem\FilesystemInterface` with a variable having the same name as your filesystem
82- name to get this specific filesystem :
136+ To achieve this, you can overwrite your storages in the test environment :
83137
84138` ` ` yaml
85139# config/packages/flysystem.yaml
86140
87141flysystem:
88- default_filesystem: 'app'
89- filesystems:
90- upload.storage:
142+ storages:
143+ users.storage:
91144 adapter: 'local'
92145 options:
93- directory: '%kernel.project_dir%/storage'
94-
95- tmp.storage:
96- adapter: 'flysystem.adapter.local'
97- options:
98- directory: '/tmp'
99- ` ` `
146+ directory: '%kernel.project_dir%/storage/users'
147+ ` ` `
100148
101- ` ` ` php
102- use League \F lysystem \F ilesystemInterface;
149+ ` ` ` yaml
150+ # config/packages/test/flysystem.yaml
103151
104- class MyController
105- {
106- public function index(FilesystemInterface $fs, FilesystemInterface $tmpStorage)
107- {
108- // $fs is referencing the default filesystem ("app") because the variable name is not a filesystem name
109- // $tmpStorage is referencing the "tmp.storage" filesystem
110- }
111- }
152+ flysystem:
153+ storages:
154+ users.storage:
155+ adapter: 'memory'
112156` ` `
157+
158+ This configuration will swap every reference to the `users.storage` service (or to the
159+ ` FilesystemInterface $usersStorage` typehint) from a local adapter to a memory one during tests.
0 commit comments