/** * {@inheritDoc} */ public function remove($obj) { $mappings = $this->factory->fromObject($obj); foreach ($mappings as $mapping) { if ($mapping->getDeleteOnRemove()) { $name = $mapping->getFileNameProperty()->getValue($obj); if (null === $name) { continue; } @unlink(sprintf('%s/%s', $mapping->getUploadDir(), $name)); } } }
/** * {@inheritDoc} */ public function injectFiles($obj) { $mappings = $this->factory->fromObject($obj); foreach ($mappings as $mapping) { if ($mapping->getInjectOnLoad()) { $name = $mapping->getFileNameProperty()->getValue($obj); if (is_null($name)) { continue; } $path = sprintf('%s/%s', $mapping->getUploadDir(), $name); $mapping->getProperty()->setValue($obj, new File($path, false)); } } }
/** * {@inheritDoc} */ public function injectFiles($obj) { $mappings = $this->factory->fromObject($obj); foreach ($mappings as $mapping) { if ($mapping->getInjectOnLoad()) { $field = $mapping->getProperty()->getName(); try { $path = $this->storage->resolvePath($obj, $field); } catch (\InvalidArgumentException $e) { continue; } $mapping->getProperty()->setValue($obj, new File($path, false)); } } }
/** * {@inheritDoc} */ public function remove($obj) { $mappings = $this->factory->fromObject($obj); /** @var $mapping PropertyMapping */ foreach ($mappings as $mapping) { if ($mapping->getDeleteOnRemove()) { $name = $mapping->getFileNameProperty()->getValue($obj); if (null === $name) { continue; } $dir = $mapping->getUploadDir($obj, $mapping->getProperty()->getName()); $this->doRemove($dir, $name); } } }
public function testConfiguredNamerRetrievedFromContainer() { $obj = new DummyEntity(); $class = new \ReflectionClass($obj); $mappings = array('dummy_file' => array('upload_dir' => '/tmp', 'delete_on_remove' => true, 'namer' => 'my.custom.namer', 'inject_on_load' => true)); $uploadable = $this->getMockBuilder('Vich\\UploaderBundle\\Mapping\\Annotation\\Uploadable')->disableOriginalConstructor()->getMock(); $fileField = $this->getMockBuilder('Vich\\UploaderBundle\\Mapping\\Annotation\\UploadableField')->disableOriginalConstructor()->getMock(); $fileField->expects($this->any())->method('getMapping')->will($this->returnValue('dummy_file')); $fileField->expects($this->once())->method('getPropertyName')->will($this->returnValue('file')); $fileField->expects($this->once())->method('getFileNameProperty')->will($this->returnValue('fileName')); $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\NamerInterface'); $this->container->expects($this->once())->method('get')->with('my.custom.namer')->will($this->returnValue($namer)); $this->adapter->expects($this->any())->method('getReflectionClass')->will($this->returnValue($class)); $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue($uploadable)); $this->driver->expects($this->once())->method('readUploadableFields')->with($class)->will($this->returnValue(array($fileField))); $factory = new PropertyMappingFactory($this->container, $this->driver, $this->adapter, $mappings); $mappings = $factory->fromObject($obj); $this->assertEquals(1, count($mappings)); if (count($mappings) > 0) { $mapping = $mappings[0]; $this->assertEquals($namer, $mapping->getNamer()); $this->assertTrue($mapping->hasNamer()); } }
public function testConfiguredNamerRetrievedFromContainer() { $obj = new DummyEntity(); $mappings = array('dummy_file' => array('upload_destination' => 'images', 'delete_on_remove' => true, 'delete_on_update' => true, 'namer' => 'my.custom.namer', 'inject_on_load' => true, 'directory_namer' => null)); $namer = $this->getMock('Vich\\UploaderBundle\\Naming\\NamerInterface'); $this->container->expects($this->once())->method('get')->with('my.custom.namer')->will($this->returnValue($namer)); $this->metadata->expects($this->once())->method('isUploadable')->with('Vich\\UploaderBundle\\Tests\\DummyEntity')->will($this->returnValue(true)); $this->metadata->expects($this->once())->method('getUploadableFields')->with('Vich\\UploaderBundle\\Tests\\DummyEntity')->will($this->returnValue(array('file' => array('mapping' => 'dummy_file', 'propertyName' => 'file', 'fileNameProperty' => 'fileName')))); $factory = new PropertyMappingFactory($this->container, $this->metadata, $mappings); $mappings = $factory->fromObject($obj); $this->assertEquals(1, count($mappings)); $mapping = $mappings['dummy_file']; $this->assertEquals($namer, $mapping->getNamer()); $this->assertTrue($mapping->hasNamer()); }