Skip to content

Commit

Permalink
Merge pull request #217 from K-Phoen/dev-naming
Browse files Browse the repository at this point in the history
Give access to the property mapping object to the namers
  • Loading branch information
K-Phoen committed Apr 7, 2014
2 parents 4df9c3f + 324e978 commit a7f193a
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 43 deletions.
15 changes: 12 additions & 3 deletions Mapping/PropertyMapping.php
Expand Up @@ -222,16 +222,25 @@ public function setMappingName($mappingName)
* Gets the configured upload directory.
*
* @param object|null $obj
* @param string|null $field
*
* @return string The configured upload directory.
*/
public function getUploadDir($obj = null, $field = null)
public function getUploadDir($obj = null)
{
if ($this->hasDirectoryNamer()) {
return $this->getDirectoryNamer()->directoryName($obj, $field, $this->mapping['upload_destination']);
return $this->getDirectoryNamer()->directoryName($obj, $this);
}

return $this->getUploadDestination();
}

/**
* Gets the base upload directory.
*
* @return string The configured upload directory.
*/
public function getUploadDestination()
{
return $this->mapping['upload_destination'];
}

Expand Down
12 changes: 8 additions & 4 deletions Naming/DirectoryNamerInterface.php
Expand Up @@ -2,6 +2,8 @@

namespace Vich\UploaderBundle\Naming;

use Vich\UploaderBundle\Mapping\PropertyMapping;

/**
* NamerInterface.
*
Expand All @@ -12,10 +14,12 @@ interface DirectoryNamerInterface
/**
* Creates a directory name for the file being uploaded.
*
* @param object $obj The object the upload is attached to.
* @param string $field The name of the uploadable field to generate a name for.
* @param string $uploadDir The upload directory set in config
* @param object $object The object the upload is attached to.
* @param Propertymapping $mapping The mapping to use to manipulate the given object.
*
* @note The $object parameter can be null.
*
* @return string The directory name.
*/
public function directoryName($obj, $field, $uploadDir);
public function directoryName($object, PropertyMapping $mapping);
}
9 changes: 6 additions & 3 deletions Naming/NamerInterface.php
Expand Up @@ -2,6 +2,8 @@

namespace Vich\UploaderBundle\Naming;

use Vich\UploaderBundle\Mapping\PropertyMapping;

/**
* NamerInterface.
*
Expand All @@ -12,9 +14,10 @@ interface NamerInterface
/**
* Creates a name for the file being uploaded.
*
* @param object $obj The object the upload is attached to.
* @param string $field The name of the uploadable field to generate a name for.
* @param object $object The object the upload is attached to.
* @param Propertymapping $mapping The mapping to use to manipulate the given object.
*
* @return string The file name.
*/
public function name($obj, $field);
public function name($object, PropertyMapping $mapping);
}
11 changes: 4 additions & 7 deletions Naming/OrignameNamer.php
Expand Up @@ -5,6 +5,8 @@
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

use Vich\UploaderBundle\Mapping\PropertyMapping;

/**
* OrignameNamer
*
Expand All @@ -15,14 +17,9 @@ class OrignameNamer implements NamerInterface
/**
* {@inheritDoc}
*/
public function name($obj, $field)
public function name($object, PropertyMapping $mapping)
{
$refObj = new \ReflectionObject($obj);

$refProp = $refObj->getProperty($field);
$refProp->setAccessible(true);

$file = $refProp->getValue($obj);
$file = $mapping->getFile($object);

/** @var $file UploadedFile */

Expand Down
11 changes: 4 additions & 7 deletions Naming/UniqidNamer.php
Expand Up @@ -4,6 +4,8 @@

use Symfony\Component\HttpFoundation\File\UploadedFile;

use Vich\UploaderBundle\Mapping\PropertyMapping;

/**
* UniqidNamer
*
Expand All @@ -14,14 +16,9 @@ class UniqidNamer implements NamerInterface
/**
* {@inheritDoc}
*/
public function name($obj, $field)
public function name($object, PropertyMapping $mapping)
{
$refObj = new \ReflectionObject($obj);

$refProp = $refObj->getProperty($field);
$refProp->setAccessible(true);

$file = $refProp->getValue($obj);
$file = $mapping->getFile($object);
$name = uniqid();

if ($extension = $this->getExtension($file)) {
Expand Down
10 changes: 5 additions & 5 deletions Storage/AbstractStorage.php
Expand Up @@ -53,18 +53,18 @@ public function upload($obj)
}

if ($mapping->getDeleteOnUpdate() && ($name = $mapping->getFileName($obj))) {
$dir = $mapping->getUploadDir($obj, $mapping->getFilePropertyName());
$dir = $mapping->getUploadDir($obj);

$this->doRemove($dir, $name);
}

if ($mapping->hasNamer()) {
$name = $mapping->getNamer()->name($obj, $mapping->getFilePropertyName());
$name = $mapping->getNamer()->name($obj, $mapping);
} else {
$name = $file->getClientOriginalName();
}

$dir = $mapping->getUploadDir($obj, $mapping->getFilePropertyName());
$dir = $mapping->getUploadDir($obj);

$this->doUpload($file, $dir, $name);

Expand Down Expand Up @@ -101,7 +101,7 @@ public function remove($obj)
continue;
}

$dir = $mapping->getUploadDir($obj, $mapping->getFilePropertyName());
$dir = $mapping->getUploadDir($obj);

$this->doRemove($dir, $name);
}
Expand All @@ -123,7 +123,7 @@ abstract protected function doResolvePath($dir, $name);
public function resolvePath($obj, $field, $className = null)
{
list($mapping, $filename) = $this->getFilename($obj, $field, $className);
$dir = $mapping->getUploadDir($obj, $field);
$dir = $mapping->getUploadDir($obj);

return $this->doResolvePath($dir, $filename);
}
Expand Down
2 changes: 1 addition & 1 deletion Storage/FileSystemStorage.php
Expand Up @@ -53,7 +53,7 @@ public function resolveUri($obj, $field, $className = null)
{
list($mapping, $name) = $this->getFilename($obj, $field, $className);
$uriPrefix = $mapping->getUriPrefix();
$parts = explode($uriPrefix, $this->convertWindowsDirectorySeparator($mapping->getUploadDir($obj, $field)));
$parts = explode($uriPrefix, $this->convertWindowsDirectorySeparator($mapping->getUploadDir($obj)));

return sprintf('%s/%s', $uriPrefix . array_pop($parts), $name);
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/Mapping/ProperyMappingTest.php
Expand Up @@ -75,4 +75,24 @@ public function testPropertiesAreSet()
$this->assertSame($date, $object->getFile());
$this->assertSame('joe.png', $object->getFileName());
}

public function testDirectoryNamerIsCalled()
{
$prop = new PropertyMapping('file', 'fileName');
$prop->setMapping(array(
'upload_destination' => '/tmp',
));

$namer = $this->getMock('Vich\UploaderBundle\Naming\DirectoryNamerInterface');
$namer
->expects($this->once())
->method('directoryName')
->with(null, $prop)
->will($this->returnValue('/other-dir'));

$prop->setDirectoryNamer($namer);

$this->assertEquals('/other-dir', $prop->getUploadDir());
$this->assertEquals('/tmp', $prop->getUploadDestination());
}
}
15 changes: 10 additions & 5 deletions Tests/Naming/OrignameNamerTest.php
Expand Up @@ -3,7 +3,6 @@
namespace Vich\UploaderBundle\Tests\Naming;

use Vich\UploaderBundle\Naming\OrignameNamer;
use Vich\UploaderBundle\Tests\DummyEntity;

/**
* OrignameNamerTest.
Expand All @@ -28,17 +27,23 @@ public function testNameReturnsAnUniqueName($name, $pattern)
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
->disableOriginalConstructor()
->getMock();

$file
->expects($this->any())
->method('getClientOriginalName')
->will($this->returnValue($name));

$entity = new DummyEntity;
$entity->setFile($file);
$entity = new \DateTime();

$mapping = $this->getMockBuilder('Vich\UploaderBundle\Mapping\PropertyMapping')
->disableOriginalConstructor()
->getMock();
$mapping->expects($this->once())
->method('getFile')
->with($entity)
->will($this->returnValue($file));

$namer = new OrignameNamer();

$this->assertRegExp($pattern, $namer->name($entity, 'file'));
$this->assertRegExp($pattern, $namer->name($entity, $mapping));
}
}
16 changes: 10 additions & 6 deletions Tests/Naming/UniqidNamerTest.php
Expand Up @@ -3,7 +3,6 @@
namespace Vich\UploaderBundle\Tests\Naming;

use Vich\UploaderBundle\Naming\UniqidNamer;
use Vich\UploaderBundle\Tests\DummyEntity;

/**
* UniqidNamerTest.
Expand Down Expand Up @@ -31,22 +30,27 @@ public function testNameReturnsAnUniqueName($originalName, $guessedExtension, $p
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
->disableOriginalConstructor()
->getMock();

$file
->expects($this->any())
->method('getClientOriginalName')
->will($this->returnValue($originalName));

$file
->expects($this->any())
->method('guessExtension')
->will($this->returnValue($guessedExtension));

$entity = new DummyEntity;
$entity->setFile($file);
$entity = new \DateTime();

$mapping = $this->getMockBuilder('Vich\UploaderBundle\Mapping\PropertyMapping')
->disableOriginalConstructor()
->getMock();
$mapping->expects($this->once())
->method('getFile')
->with($entity)
->will($this->returnValue($file));

$namer = new UniqidNamer();

$this->assertRegExp($pattern, $namer->name($entity, 'file'));
$this->assertRegExp($pattern, $namer->name($entity, $mapping));
}
}
4 changes: 2 additions & 2 deletions Tests/Storage/FileSystemStorageTest.php
Expand Up @@ -338,7 +338,7 @@ public function testFilenameWithDirectoriesIsUploadedToCorrectDirectory($dir, $f
$namer
->expects($this->once())
->method('name')
->with($this->object, 'file')
->with($this->object, $this->mapping)
->will($this->returnValue($filename));

$this->mapping
Expand All @@ -365,7 +365,7 @@ public function testFilenameWithDirectoriesIsUploadedToCorrectDirectory($dir, $f
$this->mapping
->expects($this->once())
->method('getUploadDir')
->with($this->object, 'file')
->with($this->object)
->will($this->returnValue($dir));

$file
Expand Down
2 changes: 2 additions & 0 deletions UPGRADE.md
Expand Up @@ -4,6 +4,8 @@ Upgrading from v0.9.0 to master
- `inject_on_load` config param defaults to false. Set it to
true if you want to keep your old behavior.

- the `NamerInterface` and `DirectoryNamerInterface` were modified.

Upgrading from v0.5.0 to 0.6.0
===============================

Expand Down

0 comments on commit a7f193a

Please sign in to comment.