Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating directory subfolders by giving the path in the config #1427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}
Loading