-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow creating directory subfolders by giving the path in the config
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\Tests\Naming; | ||
|
||
use Vich\UploaderBundle\Naming\ConfigurableDirectoryNamer; | ||
use Vich\UploaderBundle\Naming\PropertyDirectoryNamer; | ||
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']); | ||
} | ||
} |