Permalink
Browse files

more refactoring

  • Loading branch information...
1 parent 2b3bc30 commit 88ee3ef0c03149f94b8bf1f8d6841f13edea0548 @dustin10 dustin10 committed Jan 28, 2012
@@ -34,6 +34,7 @@ public function getConfigTreeBuilder()
->scalarNode('upload_dir')->isRequired()->end()
->scalarNode('namer')->defaultNull()->end()
->scalarNode('delete_on_remove')->defaultTrue()->end()
+ ->scalarNode('inject_on_load')->defaultTrue()->end()
->end()
->end()
->end()
@@ -4,8 +4,9 @@
use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;
-use Vich\UploaderBundle\Upload\UploaderInterface;
+use Vich\UploaderBundle\Storage\StorageInterface;
use Vich\UploaderBundle\Adapter\AdapterInterface;
+use Vich\UploaderBundle\Injector\FileInjectorInterface;
use Vich\UploaderBundle\Driver\AnnotationDriver;
/**
@@ -16,32 +17,39 @@
class UploaderListener implements EventSubscriber
{
/**
- * @var AdapterInterface $adapter
+ * @var \Vich\UploaderBundle\Adapter\AdapterInterface $adapter
*/
protected $adapter;
/**
- * @var AnnotationDriver $driver
+ * @var \Vich\UploaderBundle\Driver\AnnotationDriver $driver
*/
protected $driver;
/**
- * @var UploaderInterface $namer
+ * @var \Vich\UploaderBundle\Storage\StorageInterface $storage
*/
- protected $uploader;
+ protected $storage;
+
+ /**
+ * @var \Vich\UploaderBundle\Injector\FileInjectorInterface $injector
+ */
+ protected $injector;
/**
* Constructs a new instance of UploaderListener.
*
* @param \Vich\UploaderBundle\Adapter\AdapterInterface $adapter The adapter.
* @param \Vich\UploaderBundle\Driver\AnnotationDriver $driver The driver.
- * @param \Vich\UploaderBundle\Upload\UploaderInterface $uploader The uploader.
+ * @param \Vich\UploaderBundle\Storage\StorageInterface $storage The storage.
+ * @param \Vich\UploaderBundle\Injector\FileInjectorInterface $injector The injector.
*/
- public function __construct(AdapterInterface $adapter, AnnotationDriver $driver, UploaderInterface $uploader)
+ public function __construct(AdapterInterface $adapter, AnnotationDriver $driver, StorageInterface $storage, FileInjectorInterface $injector)
{
$this->adapter = $adapter;
$this->driver = $driver;
- $this->uploader = $uploader;
+ $this->storage = $storage;
+ $this->injector = $injector;
}
/**
@@ -54,7 +62,8 @@ public function getSubscribedEvents()
return array(
'prePersist',
'preUpdate',
- 'postRemove'
+ 'postLoad',
+ 'postRemove',
);
}
@@ -68,7 +77,7 @@ public function prePersist(EventArgs $args)
$obj = $this->adapter->getObjectFromArgs($args);
if ($this->isUploadable($obj)) {
- $this->uploader->upload($obj);
+ $this->storage->upload($obj);
}
}
@@ -82,11 +91,25 @@ public function preUpdate(EventArgs $args)
$obj = $this->adapter->getObjectFromArgs($args);
if ($this->isUploadable($obj)) {
- $this->uploader->upload($obj);
+ $this->storage->upload($obj);
$this->adapter->recomputeChangeSet($args);
}
}
+
+ /**
+ * Populates uploadable fields from filename properties
+ * if necessary.
+ *
+ * @param \Doctrine\Common\EventArgs $args
+ */
+ public function postLoad(EventArgs $args)
+ {
+ $obj = $this->adapter->getObjectFromArgs($args);
+ if ($this->isUploadable($obj)) {
+ $this->injector->injectFiles($obj);
+ }
+ }
/**
* Removes the file if necessary.
@@ -98,7 +121,7 @@ public function postRemove(EventArgs $args)
$obj = $this->adapter->getObjectFromArgs($args);
if ($this->isUploadable($obj)) {
- $this->uploader->remove($obj);
+ $this->storage->remove($obj);
}
}
@@ -0,0 +1,52 @@
+<?php
+
+namespace Vich\UploaderBundle\Injector;
+
+use Vich\UploaderBundle\Injector\FileInjectorInterface;
+use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
+
+/**
+ * FileInjector.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+class FileInjector implements FileInjectorInterface
+{
+ /**
+ * @var \Vich\UploaderBundle\Mapping\PropertyMappingFactory $factory
+ */
+ protected $factory;
+
+ /**
+ * Constructs a new instance of FileInjector.
+ *
+ * @param \Vich\UploaderBundle\Mapping\PropertyMappingFactory $factory The factory.
+ */
+ public function __construct(PropertyMappingFactory $factory)
+ {
+ $this->factory = $factory;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function injectFiles($obj)
+ {
+ $mappings = $this->factory->fromObject($obj);
+ foreach ($mappings as $mapping) {
+ if ($mapping->getInjectOnLoad()) {
+ $name = $mapping->getFileNamePropety()->getValue();
+ if (is_null($name)) {
+ continue;
+ }
+
+ $path = sprintf('%s/%s', $mapping->getUploadDir(), $name);
+
+ $mapping->getProperty()->setValue(
+ $obj,
+ new \Symfony\Component\HttpFoundation\File\File($path, false)
+ );
+ }
+ }
+ }
+}
@@ -0,0 +1,20 @@
+<?php
+
+namespace Vich\UploaderBundle\Injector;
+
+/**
+ * FileInjectorInterface.
+ *
+ * @author Dustin Dobervich <ddobervich@gmail.com>
+ */
+interface FileInjectorInterface
+{
+ /**
+ * Injects the uploadable fields of the specified object
+ * with a populated Symfony\Component\HttpFoundation\File\File
+ * instance if the field is configured for injection.
+ *
+ * @param object $obj The object.
+ */
+ function injectFiles($obj);
+}
@@ -1,6 +1,6 @@
<?php
-namespace Vich\UploaderBundle\Annotation;
+namespace Vich\UploaderBundle\Mapping\Annotation;
/**
* Uploadable.
@@ -1,6 +1,6 @@
<?php
-namespace Vich\UploaderBundle\Annotation;
+namespace Vich\UploaderBundle\Mapping\Annotation;
/**
* UploadableField.
@@ -193,4 +193,16 @@ public function getDeleteOnRemove()
{
return $this->mapping['delete_on_remove'];
}
+
+ /**
+ * Determines if the uploadable field should be injected with a
+ * Symfony\Component\HttpFoundation\File\File instance when
+ * the object is loaded from the datastore.
+ *
+ * @return bool True if the field should be injected, false otherwise.
+ */
+ public function getInjectOnLoad()
+ {
+ return $this->mapping['inject_on_load'];
+ }
}
@@ -6,7 +6,7 @@
use Vich\UploaderBundle\Driver\AnnotationDriver;
use Vich\UploaderBundle\Adapter\AdapterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
-use Vich\UploaderBundle\Annotation\UploadableField;
+use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
/**
* PropertyMappingFactory.
@@ -10,10 +10,11 @@
interface NamerInterface
{
/**
- * Creates a name for the file upload.
- *
+ * 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.
* @return string The file name.
*/
- function name($obj);
+ function name($obj, $field);
}
@@ -217,5 +217,6 @@ vich_uploader:
upload_dir: ~ # required
namer: ~ # specify a namer service id for this entity, null default
delete_on_remove: true # determines whether to delete file upon removal of entity
+ inject_on_load: true # determines whether to inject a File instance upon load
# ...
```
@@ -1,53 +1,47 @@
<?php
-namespace Vich\UploaderBundle\Upload;
+namespace Vich\UploaderBundle\Storage;
-use Vich\UploaderBundle\Upload\UploaderInterface;
+use Vich\UploaderBundle\Storage\StorageInterface;
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
+use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
- * Uploader.
- *
+ * FileSystemStorage.
+ *
* @author Dustin Dobervich <ddobervich@gmail.com>
*/
-class Uploader implements UploaderInterface
+class FileSystemStorage implements StorageInterface
{
/**
- * @var PropertyMappingFactory $container
+ * @var \Vich\UploaderBundle\Mapping\PropertyMappingFactory $factory
*/
protected $factory;
-
- /**
- * @var string $webDirName
- */
- protected $webDirName;
/**
- * Constructs a new instance of Uploader.
+ * Constructs a new instance of FileSystemStorage.
*
- * @param \Vich\UploaderBundle\Mapping\PropertyMappingFactory $factory The mapping factory.
- * @param $webDirName The name of the application's public directory.
+ * @param \Vich\UploaderBundle\Mapping\PropertyMappingFactory $factory The factory.
*/
- public function __construct(PropertyMappingFactory $factory, $webDirName)
+ public function __construct(PropertyMappingFactory $factory)
{
$this->factory = $factory;
- $this->webDirName = $webDirName;
}
-
+
/**
* {@inheritDoc}
*/
public function upload($obj)
{
$mappings = $this->factory->fromObject($obj);
- foreach($mappings as $mapping) {
+ foreach ($mappings as $mapping) {
$file = $mapping->getPropertyValue($obj);
- if (is_null($file)) {
+ if (is_null($file) || !($file instanceof UploadedFile)) {
continue;
}
if ($mapping->hasNamer()) {
- $name = $mapping->getNamer()->name($obj);
+ $name = $mapping->getNamer()->name($obj, $mapping->getProperty()->getName());
} else {
$name = $file->getClientOriginalName();
}
@@ -57,26 +51,26 @@ public function upload($obj)
$mapping->getFileNameProperty()->setValue($obj, $name);
}
}
-
+
/**
* {@inheritDoc}
*/
public function remove($obj)
{
$mappings = $this->factory->fromObject($obj);
- foreach($mappings as $mapping) {
+ foreach ($mappings as $mapping) {
if ($mapping->getDeleteOnRemove()) {
$name = $mapping->getFileNameProperty()->getValue($obj);
unlink(sprintf('%s/%s', $mapping->getUploadDir(), $name));
}
}
}
-
+
/**
* {@inheritDoc}
*/
- public function getPublicPath($obj, $field)
+ public function resolvePath($obj, $field)
{
$mapping = $this->factory->fromField($obj, $field);
if (null === $mapping) {
@@ -85,10 +79,9 @@ public function getPublicPath($obj, $field)
));
}
- $uploadDir = $mapping->getUploadDir();
- $index = strpos($uploadDir, $this->webDirName);
- $relDir = substr($uploadDir, $index + strlen($this->webDirName));
-
- return sprintf('%s/%s', $relDir, $mapping->getFilenameProperty()->getValue($obj));
+ return sprintf('%s/%s',
+ $mapping->getUploadDir(),
+ $mapping->getFileNameProperty()->getValue()
+ );
}
}
Oops, something went wrong.

0 comments on commit 88ee3ef

Please sign in to comment.