From bf359c5527047576040b7a9ddf1fa4bd541d5556 Mon Sep 17 00:00:00 2001 From: Maxime PINEAU Date: Mon, 5 Feb 2024 10:18:15 +0100 Subject: [PATCH] Allow creating directory subfolders by giving the path in the config --- config/namer.xml | 2 ++ docs/namers.md | 18 +++++++++++ src/Naming/ConfigurableDirectoryNamer.php | 32 +++++++++++++++++++ .../Naming/ConfigurableDirectoryNamerTest.php | 32 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 src/Naming/ConfigurableDirectoryNamer.php create mode 100644 tests/Naming/ConfigurableDirectoryNamerTest.php diff --git a/config/namer.xml b/config/namer.xml index 899f5d6d..73a701d3 100644 --- a/config/namer.xml +++ b/config/namer.xml @@ -22,6 +22,7 @@ + @@ -34,6 +35,7 @@ + diff --git a/docs/namers.md b/docs/namers.md index 3d29eabf..768b207e 100644 --- a/docs/namers.md +++ b/docs/namers.md @@ -110,6 +110,7 @@ At the moment there are several available namers: * `Vich\UploaderBundle\Naming\SubdirDirectoryNamer` * `Vich\UploaderBundle\Naming\PropertyDirectoryNamer` * `Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer` +* `Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer` **SubdirDirectoryNamer** creates subdirs depending on the file name, i.e. `abcdef.jpg` will be 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: date_time_property: uploadTimestamp # see above example ``` +**ConfigurableDirectoryNamer** creates subdirs which path is given in the directory namer's options. + +To use it, you just have to specify the service for the `directory_namer` +configuration option of your mapping, and **must** set the option `directory_path`: + +``` yaml +vich_uploader: + # ... + mappings: + products: + upload_destination: products + directory_namer: + service: vich_uploader.namer_directory_configurable + options: + directory_path: 'folder/subfolder/subsubfolder' +``` + If no directory namer is configured for a mapping, the bundle will simply use the `upload_destination` configuration option. diff --git a/src/Naming/ConfigurableDirectoryNamer.php b/src/Naming/ConfigurableDirectoryNamer.php new file mode 100644 index 00000000..5b972111 --- /dev/null +++ b/src/Naming/ConfigurableDirectoryNamer.php @@ -0,0 +1,32 @@ +directoryPath = $options['directory_path']; + } + + public function directoryName($object, PropertyMapping $mapping): string + { + return $this->directoryPath; + } +} diff --git a/tests/Naming/ConfigurableDirectoryNamerTest.php b/tests/Naming/ConfigurableDirectoryNamerTest.php new file mode 100644 index 00000000..9ad1f9e8 --- /dev/null +++ b/tests/Naming/ConfigurableDirectoryNamerTest.php @@ -0,0 +1,32 @@ +setFileName('file name'); + $mapping = $this->getPropertyMappingMock(); + + $namer = new ConfigurableDirectoryNamer(); + $namer->configure(['directory_path' => 'folder/subfolder/subsubfolder']); + + self::assertSame('folder/subfolder/subsubfolder', $namer->directoryName($entity, $mapping)); + } + + public function testConfigurationFailsIfTheDirectoryPathIsntSpecified(): void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Option "directory_path" is missing.'); + + $namer = new ConfigurableDirectoryNamer(); + + $namer->configure(['incorrect' => 'options']); + } +}