Skip to content

Commit c053523

Browse files
authored
Merge pull request #1427 from syffer/directory-namer/configurable-path-from-yaml
Allow creating directory subfolders by giving the path in the config
2 parents 0cf94a3 + bf359c5 commit c053523

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

config/namer.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<service id="Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer" public="true">
2323
<argument type="service" id="property_accessor" on-invalid="null"/>
2424
</service>
25+
<service id="Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer" public="true"/>
2526
<service id="Vich\UploaderBundle\Naming\SmartUniqueNamer" public="true">
2627
<argument type="service" id="Vich\UploaderBundle\Util\Transliterator"/>
2728
</service>
@@ -34,6 +35,7 @@
3435
<service id="vich_uploader.directory_namer_subdir" alias="Vich\UploaderBundle\Naming\SubdirDirectoryNamer" public="true"/>
3536
<service id="vich_uploader.namer_directory_property" alias="Vich\UploaderBundle\Naming\PropertyDirectoryNamer" public="true"/>
3637
<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"/>
3739
<service id="vich_uploader.namer_smart_unique" alias="Vich\UploaderBundle\Naming\SmartUniqueNamer" public="true"/>
3840
<service id="Vich\UploaderBundle\Util\Transliterator" public="false">
3941
<argument type="service" id="slugger"/>

docs/namers.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ At the moment there are several available namers:
110110
* `Vich\UploaderBundle\Naming\SubdirDirectoryNamer`
111111
* `Vich\UploaderBundle\Naming\PropertyDirectoryNamer`
112112
* `Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer`
113+
* `Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer`
113114

114115
**SubdirDirectoryNamer** creates subdirs depending on the file name, i.e. `abcdef.jpg` will be
115116
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:
181182
date_time_property: uploadTimestamp # see above example
182183
```
183184

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+
184202
If no directory namer is configured for a mapping, the bundle will simply use
185203
the `upload_destination` configuration option.
186204

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

0 commit comments

Comments
 (0)