Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public function getConfigTreeBuilder()
->scalarNode('storage')->defaultValue('vich_uploader.storage.file_system')->end()
->scalarNode('twig')->defaultTrue()->end()
->scalarNode('gaufrette')->defaultFalse()->end()
->scalarNode('upload_on_persist')->defaultTrue()->end()
->scalarNode('unlink_on_remove')->defaultTrue()->end()
Copy link
Collaborator

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

->scalarNode('inject_on_load')->defaultTrue()->end()
->arrayNode('mappings')
->useAttributeAsKey('id')
->prototype('array')
Expand Down
31 changes: 31 additions & 0 deletions DependencyInjection/VichUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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']) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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']) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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'));
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
$tagMap should be updated too

}

if ($config['unlink_on_remove']) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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'));
}
}
}
60 changes: 60 additions & 0 deletions EventListener/AbstractListener.php
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);
}
}
21 changes: 21 additions & 0 deletions EventListener/InjectListener.php
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);
}
}}
21 changes: 21 additions & 0 deletions EventListener/UnlinkListener.php
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);
}
}
}
36 changes: 36 additions & 0 deletions EventListener/UploadListener.php
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

/**
* 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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->adapter->recomputeChangeSet($args);
}
}
}
137 changes: 0 additions & 137 deletions EventListener/UploaderListener.php

This file was deleted.

10 changes: 8 additions & 2 deletions Resources/config/listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<parameters>
<parameter key="vich_uploader.listener.abstract.class">Vich\UploaderBundle\EventListener\AbstractListener</parameter>
<parameter key="vich_uploader.listener.inject.class">Vich\UploaderBundle\EventListener\InjectListener</parameter>
<parameter key="vich_uploader.listener.upload.class">Vich\UploaderBundle\EventListener\UploadListener</parameter>
<parameter key="vich_uploader.listener.unlink.class">Vich\UploaderBundle\EventListener\UnlinkListener</parameter>
</parameters>

<service id="vich_uploader.listener.uploader" class="Vich\UploaderBundle\EventListener\UploaderListener" public="false">
<services>
<service id="vich_uploader.listener.abstract" class="%vich_uploader.listener.abstract.class%" abstract="true">
<argument type="service" id="vich_uploader.adapter" />
<argument type="service" id="vich_uploader.annotation_driver" />
<argument type="service" id="vich_uploader.storage" />
Expand Down
Loading