File tree Expand file tree Collapse file tree 4 files changed +84
-0
lines changed Expand file tree Collapse file tree 4 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 22
22
<service id =" Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer" public =" true" >
23
23
<argument type =" service" id =" property_accessor" on-invalid =" null" />
24
24
</service >
25
+ <service id =" Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer" public =" true" />
25
26
<service id =" Vich\UploaderBundle\Naming\SmartUniqueNamer" public =" true" >
26
27
<argument type =" service" id =" Vich\UploaderBundle\Util\Transliterator" />
27
28
</service >
34
35
<service id =" vich_uploader.directory_namer_subdir" alias =" Vich\UploaderBundle\Naming\SubdirDirectoryNamer" public =" true" />
35
36
<service id =" vich_uploader.namer_directory_property" alias =" Vich\UploaderBundle\Naming\PropertyDirectoryNamer" public =" true" />
36
37
<service id =" vich_uploader.namer_directory_current_date_time" alias =" Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer" public =" true" />
38
+ <service id =" vich_uploader.namer_directory_configurable" alias =" Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer" public =" true" />
37
39
<service id =" vich_uploader.namer_smart_unique" alias =" Vich\UploaderBundle\Naming\SmartUniqueNamer" public =" true" />
38
40
<service id =" Vich\UploaderBundle\Util\Transliterator" public =" false" >
39
41
<argument type =" service" id =" slugger" />
Original file line number Diff line number Diff line change @@ -110,6 +110,7 @@ At the moment there are several available namers:
110
110
* `Vich\UploaderBundle\Naming\SubdirDirectoryNamer`
111
111
* `Vich\UploaderBundle\Naming\PropertyDirectoryNamer`
112
112
* `Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer`
113
+ * `Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer`
113
114
114
115
**SubdirDirectoryNamer** creates subdirs depending on the file name, i.e. `abcdef.jpg` will be
115
116
stored in a folder `ab`. It is also possible to configure how many chars use per directory name and
@@ -181,6 +182,23 @@ vich_uploader:
181
182
date_time_property: uploadTimestamp # see above example
182
183
` ` `
183
184
185
+ **ConfigurableDirectoryNamer** creates subdirs which path is given in the directory namer's options.
186
+
187
+ To use it, you just have to specify the service for the `directory_namer`
188
+ configuration option of your mapping, and **must** set the option `directory_path` :
189
+
190
+ ` ` ` yaml
191
+ vich_uploader:
192
+ # ...
193
+ mappings:
194
+ products:
195
+ upload_destination: products
196
+ directory_namer:
197
+ service: vich_uploader.namer_directory_configurable
198
+ options:
199
+ directory_path: 'folder/subfolder/subsubfolder'
200
+ ` ` `
201
+
184
202
If no directory namer is configured for a mapping, the bundle will simply use
185
203
the `upload_destination` configuration option.
186
204
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Vich \UploaderBundle \Naming ;
4
+
5
+ use Vich \UploaderBundle \Mapping \PropertyMapping ;
6
+
7
+ /**
8
+ * Directory namer which can create subfolder which path is given in the directory namer's options.
9
+ */
10
+ class ConfigurableDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface
11
+ {
12
+ /** @var string */
13
+ private $ directoryPath = '' ;
14
+
15
+ /**
16
+ * @param array $options Options for this namer. The following options are accepted:
17
+ * - directory_path: the path of the folders to create
18
+ */
19
+ public function configure (array $ options ): void
20
+ {
21
+ if (!isset ($ options ['directory_path ' ])) {
22
+ throw new \InvalidArgumentException ('Option "directory_path" is missing. ' );
23
+ }
24
+
25
+ $ this ->directoryPath = $ options ['directory_path ' ];
26
+ }
27
+
28
+ public function directoryName ($ object , PropertyMapping $ mapping ): string
29
+ {
30
+ return $ this ->directoryPath ;
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Vich \UploaderBundle \Tests \Naming ;
4
+
5
+ use Vich \UploaderBundle \Naming \ConfigurableDirectoryNamer ;
6
+ use Vich \UploaderBundle \Tests \DummyEntity ;
7
+ use Vich \UploaderBundle \Tests \TestCase ;
8
+
9
+ final class ConfigurableDirectoryNamerTest extends TestCase
10
+ {
11
+ public function testNameReturnsTheRightName (): void
12
+ {
13
+ $ entity = new DummyEntity ();
14
+ $ entity ->setFileName ('file name ' );
15
+ $ mapping = $ this ->getPropertyMappingMock ();
16
+
17
+ $ namer = new ConfigurableDirectoryNamer ();
18
+ $ namer ->configure (['directory_path ' => 'folder/subfolder/subsubfolder ' ]);
19
+
20
+ self ::assertSame ('folder/subfolder/subsubfolder ' , $ namer ->directoryName ($ entity , $ mapping ));
21
+ }
22
+
23
+ public function testConfigurationFailsIfTheDirectoryPathIsntSpecified (): void
24
+ {
25
+ $ this ->expectException (\LogicException::class);
26
+ $ this ->expectExceptionMessage ('Option "directory_path" is missing. ' );
27
+
28
+ $ namer = new ConfigurableDirectoryNamer ();
29
+
30
+ $ namer ->configure (['incorrect ' => 'options ' ]);
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments