Skip to content

Commit

Permalink
Merge pull request #1427 from syffer/directory-namer/configurable-pat…
Browse files Browse the repository at this point in the history
…h-from-yaml

Allow creating directory subfolders by giving the path in the config
  • Loading branch information
garak committed Jun 11, 2024
2 parents 0cf94a3 + bf359c5 commit c053523
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/namer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<service id="Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer" public="true">
<argument type="service" id="property_accessor" on-invalid="null"/>
</service>
<service id="Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer" public="true"/>
<service id="Vich\UploaderBundle\Naming\SmartUniqueNamer" public="true">
<argument type="service" id="Vich\UploaderBundle\Util\Transliterator"/>
</service>
Expand All @@ -34,6 +35,7 @@
<service id="vich_uploader.directory_namer_subdir" alias="Vich\UploaderBundle\Naming\SubdirDirectoryNamer" public="true"/>
<service id="vich_uploader.namer_directory_property" alias="Vich\UploaderBundle\Naming\PropertyDirectoryNamer" public="true"/>
<service id="vich_uploader.namer_directory_current_date_time" alias="Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer" public="true"/>
<service id="vich_uploader.namer_directory_configurable" alias="Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer" public="true"/>
<service id="vich_uploader.namer_smart_unique" alias="Vich\UploaderBundle\Naming\SmartUniqueNamer" public="true"/>
<service id="Vich\UploaderBundle\Util\Transliterator" public="false">
<argument type="service" id="slugger"/>
Expand Down
18 changes: 18 additions & 0 deletions docs/namers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
32 changes: 32 additions & 0 deletions src/Naming/ConfigurableDirectoryNamer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Vich\UploaderBundle\Naming;

use Vich\UploaderBundle\Mapping\PropertyMapping;

/**
* Directory namer which can create subfolder which path is given in the directory namer's options.
*/
class ConfigurableDirectoryNamer implements DirectoryNamerInterface, ConfigurableInterface
{
/** @var string */
private $directoryPath = '';

/**
* @param array $options Options for this namer. The following options are accepted:
* - directory_path: the path of the folders to create
*/
public function configure(array $options): void
{
if (!isset($options['directory_path'])) {
throw new \InvalidArgumentException('Option "directory_path" is missing.');
}

$this->directoryPath = $options['directory_path'];
}

public function directoryName($object, PropertyMapping $mapping): string
{
return $this->directoryPath;
}
}
32 changes: 32 additions & 0 deletions tests/Naming/ConfigurableDirectoryNamerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Vich\UploaderBundle\Tests\Naming;

use Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer;
use Vich\UploaderBundle\Tests\DummyEntity;
use Vich\UploaderBundle\Tests\TestCase;

final class ConfigurableDirectoryNamerTest extends TestCase
{
public function testNameReturnsTheRightName(): void
{
$entity = new DummyEntity();
$entity->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']);
}
}

0 comments on commit c053523

Please sign in to comment.