-
Notifications
You must be signed in to change notification settings - Fork 518
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
[WIP] Refactor event system #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
use Vich\UploaderBundle\DependencyInjection\Configuration; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
|
||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
|
||
/** | ||
* VichUploaderExtension. | ||
* | ||
|
@@ -84,4 +87,32 @@ public function load(array $configs, ContainerBuilder $container) | |
$container->setParameter('vich_uploader.adapter.class', $this->adapterMap[$driver]); | ||
$container->getDefinition('vich_uploader.listener.uploader')->addTag($this->tagMap[$driver]); | ||
} | ||
|
||
private function loadEventListeners(array $config, ContainerBuilder $container) | ||
{ | ||
if ($config['inject_on_load']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should get rid of this if, and check if we need to execute the logic based con configuration params at runtime |
||
$container | ||
->setDefinition('vich_uploader.listener.inject', new DefinitionDecorator('vich_uploader.event_listener.abstract')) | ||
->setClass('vich_uploader.listener.inject.class') | ||
->setPublic(false) | ||
->addTag('doctrine.event_listener', array('event' => 'postLoad')); | ||
} | ||
|
||
if ($config['upload_on_persist']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should get rid of this if, and check if we need to execute the logic based con configuration params at runtime |
||
$container | ||
->setDefinition('vich_uploader.listener.upload', new DefinitionDecorator('vich_uploader.event_listener.abstract')) | ||
->setClass('vich_uploader.listener.upload.class') | ||
->setPublic(false) | ||
->addTag('doctrine.event_listener', array('event' => 'prePersist')) | ||
->addTag('doctrine.event_listener', array('event' => 'preUpdate')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should use ->addTag($this->tagMap[$driver].... in order to use the correct tag name for ORM or Mongodb |
||
} | ||
|
||
if ($config['unlink_on_remove']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should get rid of this if, and check if we need to execute the logic based con configuration params at runtime |
||
$container | ||
->setDefinition('vich_uploader.listener.inject', new DefinitionDecorator('vich_uploader.event_listener.abstract')) | ||
->setClass('vich_uploader.listener.inject.class') | ||
->setPublic(false) | ||
->addTag('doctrine.event_listener', array('event' => 'postRemove')); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\EventListener; | ||
|
||
use Vich\UploaderBundle\Storage\StorageInterface; | ||
use Vich\UploaderBundle\Adapter\AdapterInterface; | ||
use Vich\UploaderBundle\Injector\FileInjectorInterface; | ||
use Vich\UploaderBundle\Driver\AnnotationDriver; | ||
|
||
abstract class AbstractListener | ||
{ | ||
/** | ||
* @var AdapterInterface | ||
*/ | ||
protected $adapter; | ||
|
||
/** | ||
* @var AnnotationDriver | ||
*/ | ||
protected $driver; | ||
|
||
/** | ||
* @var StorageInterface | ||
*/ | ||
protected $storage; | ||
|
||
/** | ||
* @var FileInjectorInterface | ||
*/ | ||
protected $injector; | ||
|
||
/** | ||
* Constructs a new instance of UploaderListener. | ||
* | ||
* @param AdapterInterface $adapter The adapter. | ||
* @param AnnotationDriver $driver The driver. | ||
* @param StorageInterface $storage The storage. | ||
* @param FileInjectorInterface $injector The injector. | ||
*/ | ||
public function __construct(AdapterInterface $adapter, AnnotationDriver $driver, StorageInterface $storage, FileInjectorInterface $injector) | ||
{ | ||
$this->adapter = $adapter; | ||
$this->driver = $driver; | ||
$this->storage = $storage; | ||
$this->injector = $injector; | ||
} | ||
|
||
/** | ||
* Tests if the object is Uploadable. | ||
* | ||
* @param object $obj The object. | ||
* @return boolean True if uploadable, false otherwise. | ||
*/ | ||
protected function isUploadable($obj) | ||
{ | ||
$class = $this->adapter->getReflectionClass($obj); | ||
|
||
return null !== $this->driver->readUploadable($class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\EventListener; | ||
|
||
use Doctrine\Common\EventArgs; | ||
|
||
class InjectListener extends AbstractListener | ||
{ | ||
/** | ||
* 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); | ||
} | ||
}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\EventListener; | ||
|
||
use Doctrine\Common\EventArgs; | ||
|
||
class UnlinkListener extends AbstractListener | ||
{ | ||
/** | ||
* Removes the file if necessary. | ||
* | ||
* @param EventArgs $args The event arguments. | ||
*/ | ||
public function postRemove(EventArgs $args) | ||
{ | ||
$obj = $this->adapter->getObjectFromArgs($args); | ||
if ($this->isUploadable($obj)) { | ||
$this->storage->remove($obj); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\EventListener; | ||
|
||
use Doctrine\Common\EventArgs; | ||
|
||
class UploadListener extends AbstractListener | ||
{ | ||
/** | ||
* Checks for for file to upload. | ||
* | ||
* @param \Doctrine\Common\EventArgs $args The event arguments. | ||
*/ | ||
public function prePersist(EventArgs $args) | ||
{ | ||
$obj = $this->adapter->getObjectFromArgs($args); | ||
if ($this->isUploadable($obj)) { | ||
$this->storage->upload($obj); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here this is missing merged into master after your PR |
||
} | ||
} | ||
|
||
/** | ||
* Update the file and file name if necessary. | ||
* | ||
* @param EventArgs $args The event arguments. | ||
*/ | ||
public function preUpdate(EventArgs $args) | ||
{ | ||
$obj = $this->adapter->getObjectFromArgs($args); | ||
if ($this->isUploadable($obj)) { | ||
$this->storage->upload($obj); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here this is missing merged into master after your PR |
||
$this->adapter->recomputeChangeSet($args); | ||
} | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be delete_on_remove for consistence with mapping node
also delete_on_update is missing here